Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 3 | class Parser_test extends CI_TestCase { |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 4 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 5 | public function set_up() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 6 | { |
Andrey Andreev | 10e7a32 | 2014-02-20 16:42:16 +0200 | [diff] [blame] | 7 | $this->parser = new CI_Parser(); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 8 | $this->ci_instance_var('parser', $this->parser); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 9 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 10 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 11 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 12 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 13 | public function test_set_delimiters() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 14 | { |
| 15 | // Make sure default delimiters are there |
| 16 | $this->assertEquals('{', $this->parser->l_delim); |
| 17 | $this->assertEquals('}', $this->parser->r_delim); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 18 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 19 | // Change them to square brackets |
| 20 | $this->parser->set_delimiters('[', ']'); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 21 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 22 | // Make sure they changed |
| 23 | $this->assertEquals('[', $this->parser->l_delim); |
| 24 | $this->assertEquals(']', $this->parser->r_delim); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 25 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 26 | // Reset them |
| 27 | $this->parser->set_delimiters(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 28 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 29 | // Make sure default delimiters are there |
| 30 | $this->assertEquals('{', $this->parser->l_delim); |
| 31 | $this->assertEquals('}', $this->parser->r_delim); |
| 32 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 33 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 34 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 35 | |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 36 | public function test_parse_string() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 37 | { |
| 38 | $data = array( |
| 39 | 'title' => 'Page Title', |
| 40 | 'body' => 'Lorem ipsum dolor sit amet.' |
| 41 | ); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 42 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 43 | $template = "{title}\n{body}"; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 44 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 45 | $result = implode("\n", $data); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 46 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 47 | $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); |
| 48 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 49 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 50 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 51 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 52 | public function test_parse() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 53 | { |
| 54 | $this->_parse_no_template(); |
| 55 | $this->_parse_var_pair(); |
| 56 | $this->_mismatched_var_pair(); |
| 57 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 58 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 59 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 60 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 61 | private function _parse_no_template() |
| 62 | { |
| 63 | $this->assertFalse($this->parser->parse_string('', '', TRUE)); |
| 64 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 65 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 66 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 67 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 68 | private function _parse_var_pair() |
| 69 | { |
| 70 | $data = array( |
| 71 | 'title' => 'Super Heroes', |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 72 | 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 73 | ); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 74 | |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 75 | $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}"; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 76 | |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 77 | $this->assertEquals("Super Heroes\nyes\nno\nsecond: yes no", $this->parser->parse_string($template, $data, TRUE)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 78 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 79 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 80 | // -------------------------------------------------------------------- |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 81 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 82 | private function _mismatched_var_pair() |
| 83 | { |
| 84 | $data = array( |
| 85 | 'title' => 'Super Heroes', |
Andrey Andreev | a9938a0 | 2014-01-17 14:55:56 +0200 | [diff] [blame] | 86 | 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 87 | ); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 88 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 89 | $template = "{title}\n{powers}{invisibility}\n{flying}"; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 90 | $result = "Super Heroes\n{powers}{invisibility}\n{flying}"; |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 91 | |
| 92 | $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 95 | } |