blob: 394c22692f58039ea8e3e7dcd43d9b2441ba1697 [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 {
Andrey Andreevc1862882012-06-09 23:16:58 +03004
Eric Barnes68286a42011-04-21 22:00:33 -04005 public function set_up()
Pascal Kriete69c97a72011-04-20 21:44:54 -04006 {
dchill427ecc5cd2012-10-12 16:25:51 -04007 $this->parser = new Mock_Libraries_Parser();
8 $this->ci_instance_var('parser', $this->parser);
Pascal Kriete69c97a72011-04-20 21:44:54 -04009 }
Andrey Andreevc1862882012-06-09 23:16:58 +030010
Pascal Kriete69c97a72011-04-20 21:44:54 -040011 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030012
Eric Barnes68286a42011-04-21 22:00:33 -040013 public function test_set_delimiters()
Pascal Kriete69c97a72011-04-20 21:44:54 -040014 {
15 // Make sure default delimiters are there
16 $this->assertEquals('{', $this->parser->l_delim);
17 $this->assertEquals('}', $this->parser->r_delim);
Andrey Andreevc1862882012-06-09 23:16:58 +030018
Pascal Kriete69c97a72011-04-20 21:44:54 -040019 // Change them to square brackets
20 $this->parser->set_delimiters('[', ']');
Andrey Andreevc1862882012-06-09 23:16:58 +030021
Pascal Kriete69c97a72011-04-20 21:44:54 -040022 // Make sure they changed
23 $this->assertEquals('[', $this->parser->l_delim);
24 $this->assertEquals(']', $this->parser->r_delim);
Andrey Andreevc1862882012-06-09 23:16:58 +030025
Pascal Kriete69c97a72011-04-20 21:44:54 -040026 // Reset them
27 $this->parser->set_delimiters();
Andrey Andreevc1862882012-06-09 23:16:58 +030028
Pascal Kriete69c97a72011-04-20 21:44:54 -040029 // Make sure default delimiters are there
30 $this->assertEquals('{', $this->parser->l_delim);
31 $this->assertEquals('}', $this->parser->r_delim);
32 }
Andrey Andreevc1862882012-06-09 23:16:58 +030033
Pascal Kriete69c97a72011-04-20 21:44:54 -040034 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030035
Eric Barnes68286a42011-04-21 22:00:33 -040036 public function test_parse_simple_string()
Pascal Kriete69c97a72011-04-20 21:44:54 -040037 {
38 $data = array(
39 'title' => 'Page Title',
40 'body' => 'Lorem ipsum dolor sit amet.'
41 );
Andrey Andreevc1862882012-06-09 23:16:58 +030042
Pascal Kriete69c97a72011-04-20 21:44:54 -040043 $template = "{title}\n{body}";
Andrey Andreevc1862882012-06-09 23:16:58 +030044
Pascal Kriete69c97a72011-04-20 21:44:54 -040045 $result = implode("\n", $data);
Andrey Andreevc1862882012-06-09 23:16:58 +030046
Pascal Kriete69c97a72011-04-20 21:44:54 -040047 $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
48 }
Andrey Andreevc1862882012-06-09 23:16:58 +030049
Pascal Kriete69c97a72011-04-20 21:44:54 -040050 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030051
Eric Barnes68286a42011-04-21 22:00:33 -040052 public function test_parse()
Pascal Kriete69c97a72011-04-20 21:44:54 -040053 {
54 $this->_parse_no_template();
55 $this->_parse_var_pair();
56 $this->_mismatched_var_pair();
57 }
Andrey Andreevc1862882012-06-09 23:16:58 +030058
Pascal Kriete69c97a72011-04-20 21:44:54 -040059 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030060
Pascal Kriete69c97a72011-04-20 21:44:54 -040061 private function _parse_no_template()
62 {
63 $this->assertFalse($this->parser->parse_string('', '', TRUE));
64 }
Andrey Andreevc1862882012-06-09 23:16:58 +030065
Pascal Kriete69c97a72011-04-20 21:44:54 -040066 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030067
Pascal Kriete69c97a72011-04-20 21:44:54 -040068 private function _parse_var_pair()
69 {
70 $data = array(
71 'title' => 'Super Heroes',
72 'powers' => array(
73 array(
74 'invisibility' => 'yes',
75 'flying' => 'no'),
76 )
77 );
Andrey Andreevc1862882012-06-09 23:16:58 +030078
Pascal Kriete69c97a72011-04-20 21:44:54 -040079 $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}";
Andrey Andreevc1862882012-06-09 23:16:58 +030080
81 $this->assertEquals("Super Heroes\nyes\nno", $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -040082 }
Andrey Andreevc1862882012-06-09 23:16:58 +030083
Pascal Kriete69c97a72011-04-20 21:44:54 -040084 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030085
Pascal Kriete69c97a72011-04-20 21:44:54 -040086 private function _mismatched_var_pair()
87 {
88 $data = array(
89 'title' => 'Super Heroes',
90 'powers' => array(
91 array(
92 'invisibility' => 'yes',
93 'flying' => 'no'),
94 )
95 );
Andrey Andreevc1862882012-06-09 23:16:58 +030096
Pascal Kriete69c97a72011-04-20 21:44:54 -040097 $template = "{title}\n{powers}{invisibility}\n{flying}";
Pascal Kriete69c97a72011-04-20 21:44:54 -040098 $result = "Super Heroes\n{powers}{invisibility}\n{flying}";
Andrey Andreevc1862882012-06-09 23:16:58 +030099
100 $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -0400101 }
102
Pascal Kriete69c97a72011-04-20 21:44:54 -0400103}