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