blob: b68f44a33036b652a32d631a26d541867080a78d [file] [log] [blame]
Pascal Kriete69c97a72011-04-20 21:44:54 -04001<?php
2
Taufan Adityaac5373a2012-03-28 16:03:38 +07003class Parser_test extends CI_TestCase {
Andrey Andreevc1862882012-06-09 23:16:58 +03004
Eric Barnes68286a42011-04-21 22:00:33 -04005 public function set_up()
Pascal Kriete69c97a72011-04-20 21:44:54 -04006 {
Andrey Andreevc1862882012-06-09 23:16:58 +03007 $obj = new stdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->parser = new Mock_Libraries_Parser();
Andrey Andreevc1862882012-06-09 23:16:58 +03009
Greg Akera7f9b252011-04-21 14:54:59 -050010 $this->ci_instance($obj);
Andrey Andreevc1862882012-06-09 23:16:58 +030011
Greg Akera7f9b252011-04-21 14:54:59 -050012 $this->parser = $obj->parser;
Pascal Kriete69c97a72011-04-20 21:44:54 -040013 }
Andrey Andreevc1862882012-06-09 23:16:58 +030014
Pascal Kriete69c97a72011-04-20 21:44:54 -040015 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030016
Eric Barnes68286a42011-04-21 22:00:33 -040017 public function test_set_delimiters()
Pascal Kriete69c97a72011-04-20 21:44:54 -040018 {
19 // Make sure default delimiters are there
20 $this->assertEquals('{', $this->parser->l_delim);
21 $this->assertEquals('}', $this->parser->r_delim);
Andrey Andreevc1862882012-06-09 23:16:58 +030022
Pascal Kriete69c97a72011-04-20 21:44:54 -040023 // Change them to square brackets
24 $this->parser->set_delimiters('[', ']');
Andrey Andreevc1862882012-06-09 23:16:58 +030025
Pascal Kriete69c97a72011-04-20 21:44:54 -040026 // Make sure they changed
27 $this->assertEquals('[', $this->parser->l_delim);
28 $this->assertEquals(']', $this->parser->r_delim);
Andrey Andreevc1862882012-06-09 23:16:58 +030029
Pascal Kriete69c97a72011-04-20 21:44:54 -040030 // Reset them
31 $this->parser->set_delimiters();
Andrey Andreevc1862882012-06-09 23:16:58 +030032
Pascal Kriete69c97a72011-04-20 21:44:54 -040033 // Make sure default delimiters are there
34 $this->assertEquals('{', $this->parser->l_delim);
35 $this->assertEquals('}', $this->parser->r_delim);
36 }
Andrey Andreevc1862882012-06-09 23:16:58 +030037
Pascal Kriete69c97a72011-04-20 21:44:54 -040038 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030039
Eric Barnes68286a42011-04-21 22:00:33 -040040 public function test_parse_simple_string()
Pascal Kriete69c97a72011-04-20 21:44:54 -040041 {
42 $data = array(
43 'title' => 'Page Title',
44 'body' => 'Lorem ipsum dolor sit amet.'
45 );
Andrey Andreevc1862882012-06-09 23:16:58 +030046
Pascal Kriete69c97a72011-04-20 21:44:54 -040047 $template = "{title}\n{body}";
Andrey Andreevc1862882012-06-09 23:16:58 +030048
Pascal Kriete69c97a72011-04-20 21:44:54 -040049 $result = implode("\n", $data);
Andrey Andreevc1862882012-06-09 23:16:58 +030050
Pascal Kriete69c97a72011-04-20 21:44:54 -040051 $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
52 }
Andrey Andreevc1862882012-06-09 23:16:58 +030053
Pascal Kriete69c97a72011-04-20 21:44:54 -040054 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030055
Eric Barnes68286a42011-04-21 22:00:33 -040056 public function test_parse()
Pascal Kriete69c97a72011-04-20 21:44:54 -040057 {
58 $this->_parse_no_template();
59 $this->_parse_var_pair();
60 $this->_mismatched_var_pair();
61 }
Andrey Andreevc1862882012-06-09 23:16:58 +030062
Pascal Kriete69c97a72011-04-20 21:44:54 -040063 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030064
Pascal Kriete69c97a72011-04-20 21:44:54 -040065 private function _parse_no_template()
66 {
67 $this->assertFalse($this->parser->parse_string('', '', TRUE));
68 }
Andrey Andreevc1862882012-06-09 23:16:58 +030069
Pascal Kriete69c97a72011-04-20 21:44:54 -040070 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030071
Pascal Kriete69c97a72011-04-20 21:44:54 -040072 private function _parse_var_pair()
73 {
74 $data = array(
75 'title' => 'Super Heroes',
76 'powers' => array(
77 array(
78 'invisibility' => 'yes',
79 'flying' => 'no'),
80 )
81 );
Andrey Andreevc1862882012-06-09 23:16:58 +030082
Pascal Kriete69c97a72011-04-20 21:44:54 -040083 $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}";
Andrey Andreevc1862882012-06-09 23:16:58 +030084
85 $this->assertEquals("Super Heroes\nyes\nno", $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -040086 }
Andrey Andreevc1862882012-06-09 23:16:58 +030087
Pascal Kriete69c97a72011-04-20 21:44:54 -040088 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030089
Pascal Kriete69c97a72011-04-20 21:44:54 -040090 private function _mismatched_var_pair()
91 {
92 $data = array(
93 'title' => 'Super Heroes',
94 'powers' => array(
95 array(
96 'invisibility' => 'yes',
97 'flying' => 'no'),
98 )
99 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300100
Pascal Kriete69c97a72011-04-20 21:44:54 -0400101 $template = "{title}\n{powers}{invisibility}\n{flying}";
Pascal Kriete69c97a72011-04-20 21:44:54 -0400102 $result = "Super Heroes\n{powers}{invisibility}\n{flying}";
Andrey Andreevc1862882012-06-09 23:16:58 +0300103
104 $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400105 }
106
Pascal Kriete69c97a72011-04-20 21:44:54 -0400107}