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 { |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [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 | { |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 7 | $obj = new StdClass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 8 | $obj->parser = new Mock_Libraries_Parser(); |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 9 | |
| 10 | $this->ci_instance($obj); |
| 11 | |
| 12 | $this->parser = $obj->parser; |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 13 | } |
| 14 | // -------------------------------------------------------------------- |
| 15 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 16 | public function test_set_delimiters() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 17 | { |
| 18 | // Make sure default delimiters are there |
| 19 | $this->assertEquals('{', $this->parser->l_delim); |
| 20 | $this->assertEquals('}', $this->parser->r_delim); |
| 21 | |
| 22 | // Change them to square brackets |
| 23 | $this->parser->set_delimiters('[', ']'); |
| 24 | |
| 25 | // Make sure they changed |
| 26 | $this->assertEquals('[', $this->parser->l_delim); |
| 27 | $this->assertEquals(']', $this->parser->r_delim); |
| 28 | |
| 29 | // Reset them |
| 30 | $this->parser->set_delimiters(); |
| 31 | |
| 32 | // Make sure default delimiters are there |
| 33 | $this->assertEquals('{', $this->parser->l_delim); |
| 34 | $this->assertEquals('}', $this->parser->r_delim); |
| 35 | } |
| 36 | |
| 37 | // -------------------------------------------------------------------- |
| 38 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 39 | public function test_parse_simple_string() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 40 | { |
| 41 | $data = array( |
| 42 | 'title' => 'Page Title', |
| 43 | 'body' => 'Lorem ipsum dolor sit amet.' |
| 44 | ); |
| 45 | |
| 46 | $template = "{title}\n{body}"; |
| 47 | |
| 48 | $result = implode("\n", $data); |
| 49 | |
| 50 | $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); |
| 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 54 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 55 | public function test_parse() |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 56 | { |
| 57 | $this->_parse_no_template(); |
| 58 | $this->_parse_var_pair(); |
| 59 | $this->_mismatched_var_pair(); |
| 60 | } |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 61 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 62 | // -------------------------------------------------------------------- |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 63 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 64 | private function _parse_no_template() |
| 65 | { |
| 66 | $this->assertFalse($this->parser->parse_string('', '', TRUE)); |
| 67 | } |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 68 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 69 | // -------------------------------------------------------------------- |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 70 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 71 | private function _parse_var_pair() |
| 72 | { |
| 73 | $data = array( |
| 74 | 'title' => 'Super Heroes', |
| 75 | 'powers' => array( |
| 76 | array( |
| 77 | 'invisibility' => 'yes', |
| 78 | 'flying' => 'no'), |
| 79 | ) |
| 80 | ); |
| 81 | |
| 82 | $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}"; |
| 83 | |
| 84 | $result = "Super Heroes\nyes\nno"; |
| 85 | |
| 86 | $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); |
| 87 | } |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 88 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 89 | // -------------------------------------------------------------------- |
Greg Aker | a7f9b25 | 2011-04-21 14:54:59 -0500 | [diff] [blame] | 90 | |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 91 | private function _mismatched_var_pair() |
| 92 | { |
| 93 | $data = array( |
| 94 | 'title' => 'Super Heroes', |
| 95 | 'powers' => array( |
| 96 | array( |
| 97 | 'invisibility' => 'yes', |
| 98 | 'flying' => 'no'), |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | $template = "{title}\n{powers}{invisibility}\n{flying}"; |
| 103 | |
| 104 | $result = "Super Heroes\n{powers}{invisibility}\n{flying}"; |
| 105 | |
| 106 | $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE)); |
| 107 | } |
| 108 | |
| 109 | // -------------------------------------------------------------------- |
Pascal Kriete | 69c97a7 | 2011-04-20 21:44:54 -0400 | [diff] [blame] | 110 | } |