blob: 2a2348793ca94283d5dd10f82fa16b29969c00a7 [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 {
Andrey Andreev10e7a322014-02-20 16:42:16 +02007 $this->parser = new CI_Parser();
dchill427ecc5cd2012-10-12 16:25:51 -04008 $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
Andrey Andreeva9938a02014-01-17 14:55:56 +020036 public function test_parse_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',
Andrey Andreeva9938a02014-01-17 14:55:56 +020072 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no'))
Pascal Kriete69c97a72011-04-20 21:44:54 -040073 );
Andrey Andreevc1862882012-06-09 23:16:58 +030074
Andrey Andreeva9c7d182014-01-06 14:38:00 +020075 $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}";
Andrey Andreevc1862882012-06-09 23:16:58 +030076
Andrey Andreeva9c7d182014-01-06 14:38:00 +020077 $this->assertEquals("Super Heroes\nyes\nno\nsecond: yes no", $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -040078 }
Andrey Andreevc1862882012-06-09 23:16:58 +030079
Pascal Kriete69c97a72011-04-20 21:44:54 -040080 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030081
Pascal Kriete69c97a72011-04-20 21:44:54 -040082 private function _mismatched_var_pair()
83 {
84 $data = array(
85 'title' => 'Super Heroes',
Andrey Andreeva9938a02014-01-17 14:55:56 +020086 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no'))
Pascal Kriete69c97a72011-04-20 21:44:54 -040087 );
Andrey Andreevc1862882012-06-09 23:16:58 +030088
Pascal Kriete69c97a72011-04-20 21:44:54 -040089 $template = "{title}\n{powers}{invisibility}\n{flying}";
Pascal Kriete69c97a72011-04-20 21:44:54 -040090 $result = "Super Heroes\n{powers}{invisibility}\n{flying}";
Andrey Andreevc1862882012-06-09 23:16:58 +030091
92 $this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
Pascal Kriete69c97a72011-04-20 21:44:54 -040093 }
94
Pascal Kriete69c97a72011-04-20 21:44:54 -040095}