blob: b4580a4b12443cc700db8b1fc051549f6db3bcbc [file] [log] [blame]
Pascal Kriete69c97a72011-04-20 21:44:54 -04001<?php
2
Greg Akera7f9b252011-04-21 14:54:59 -05003require BASEPATH.'libraries/Parser.php';
Pascal Kriete69c97a72011-04-20 21:44:54 -04004
Greg Akera7f9b252011-04-21 14:54:59 -05005class Parser_test extends CI_TestCase
6{
Pascal Kriete69c97a72011-04-20 21:44:54 -04007
Eric Barnes68286a42011-04-21 22:00:33 -04008 public function set_up()
Pascal Kriete69c97a72011-04-20 21:44:54 -04009 {
Greg Akera7f9b252011-04-21 14:54:59 -050010 $obj = new StdClass;
11 $obj->parser = new CI_Parser();
12
13 $this->ci_instance($obj);
14
15 $this->parser = $obj->parser;
Pascal Kriete69c97a72011-04-20 21:44:54 -040016 }
17 // --------------------------------------------------------------------
18
Eric Barnes68286a42011-04-21 22:00:33 -040019 public function test_set_delimiters()
Pascal Kriete69c97a72011-04-20 21:44:54 -040020 {
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 Barnes68286a42011-04-21 22:00:33 -040042 public function test_parse_simple_string()
Pascal Kriete69c97a72011-04-20 21:44:54 -040043 {
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 Akera7f9b252011-04-21 14:54:59 -050057
Eric Barnes68286a42011-04-21 22:00:33 -040058 public function test_parse()
Pascal Kriete69c97a72011-04-20 21:44:54 -040059 {
60 $this->_parse_no_template();
61 $this->_parse_var_pair();
62 $this->_mismatched_var_pair();
63 }
Greg Akera7f9b252011-04-21 14:54:59 -050064
Pascal Kriete69c97a72011-04-20 21:44:54 -040065 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050066
Pascal Kriete69c97a72011-04-20 21:44:54 -040067 private function _parse_no_template()
68 {
69 $this->assertFalse($this->parser->parse_string('', '', TRUE));
70 }
Greg Akera7f9b252011-04-21 14:54:59 -050071
Pascal Kriete69c97a72011-04-20 21:44:54 -040072 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050073
Pascal Kriete69c97a72011-04-20 21:44:54 -040074 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 Akera7f9b252011-04-21 14:54:59 -050091
Pascal Kriete69c97a72011-04-20 21:44:54 -040092 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050093
Pascal Kriete69c97a72011-04-20 21:44:54 -040094 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 Kriete69c97a72011-04-20 21:44:54 -0400113}