blob: c3d88fa8501515ffd785cda1daebcbc386747a0c [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 {
Pascal Kriete69c97a72011-04-20 21:44:54 -04004
Eric Barnes68286a42011-04-21 22:00:33 -04005 public function set_up()
Pascal Kriete69c97a72011-04-20 21:44:54 -04006 {
Greg Akera7f9b252011-04-21 14:54:59 -05007 $obj = new StdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->parser = new Mock_Libraries_Parser();
Greg Akera7f9b252011-04-21 14:54:59 -05009
10 $this->ci_instance($obj);
11
12 $this->parser = $obj->parser;
Pascal Kriete69c97a72011-04-20 21:44:54 -040013 }
14 // --------------------------------------------------------------------
15
Eric Barnes68286a42011-04-21 22:00:33 -040016 public function test_set_delimiters()
Pascal Kriete69c97a72011-04-20 21:44:54 -040017 {
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 Barnes68286a42011-04-21 22:00:33 -040039 public function test_parse_simple_string()
Pascal Kriete69c97a72011-04-20 21:44:54 -040040 {
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 Akera7f9b252011-04-21 14:54:59 -050054
Eric Barnes68286a42011-04-21 22:00:33 -040055 public function test_parse()
Pascal Kriete69c97a72011-04-20 21:44:54 -040056 {
57 $this->_parse_no_template();
58 $this->_parse_var_pair();
59 $this->_mismatched_var_pair();
60 }
Greg Akera7f9b252011-04-21 14:54:59 -050061
Pascal Kriete69c97a72011-04-20 21:44:54 -040062 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050063
Pascal Kriete69c97a72011-04-20 21:44:54 -040064 private function _parse_no_template()
65 {
66 $this->assertFalse($this->parser->parse_string('', '', TRUE));
67 }
Greg Akera7f9b252011-04-21 14:54:59 -050068
Pascal Kriete69c97a72011-04-20 21:44:54 -040069 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050070
Pascal Kriete69c97a72011-04-20 21:44:54 -040071 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 Akera7f9b252011-04-21 14:54:59 -050088
Pascal Kriete69c97a72011-04-20 21:44:54 -040089 // --------------------------------------------------------------------
Greg Akera7f9b252011-04-21 14:54:59 -050090
Pascal Kriete69c97a72011-04-20 21:44:54 -040091 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 Kriete69c97a72011-04-20 21:44:54 -0400110}