summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Parser_test.php
blob: 6e5c192dd6c9c25268baed22441c1c6ac182b770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

class Parser_test extends CI_TestCase {

	public function set_up()
	{
		$this->parser = new Mock_Libraries_Parser();
		$this->ci_instance_var('parser', $this->parser);
	}

	// --------------------------------------------------------------------

	public function test_set_delimiters()
	{
		// Make sure default delimiters are there
		$this->assertEquals('{', $this->parser->l_delim);
		$this->assertEquals('}', $this->parser->r_delim);

		// Change them to square brackets
		$this->parser->set_delimiters('[', ']');

		// Make sure they changed
		$this->assertEquals('[', $this->parser->l_delim);
		$this->assertEquals(']', $this->parser->r_delim);

		// Reset them
		$this->parser->set_delimiters();

		// Make sure default delimiters are there
		$this->assertEquals('{', $this->parser->l_delim);
		$this->assertEquals('}', $this->parser->r_delim);
	}

	// --------------------------------------------------------------------

	public function test_parse_simple_string()
	{
		$data = array(
			'title' => 'Page Title',
			'body' => 'Lorem ipsum dolor sit amet.'
		);

		$template = "{title}\n{body}";

		$result = implode("\n", $data);

		$this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
	}

	// --------------------------------------------------------------------

	public function test_parse()
	{
		$this->_parse_no_template();
		$this->_parse_var_pair();
		$this->_mismatched_var_pair();
	}

	// --------------------------------------------------------------------

	private function _parse_no_template()
	{
		$this->assertFalse($this->parser->parse_string('', '', TRUE));
	}

	// --------------------------------------------------------------------

	private function _parse_var_pair()
	{
		$data = array(
			'title'		=> 'Super Heroes',
			'powers'	=> array(
					array(
						'invisibility'	=> 'yes',
						'flying'		=> 'no'),
			)
		);

		$template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}";

		$this->assertEquals("Super Heroes\nyes\nno\nsecond: yes no", $this->parser->parse_string($template, $data, TRUE));
	}

	// --------------------------------------------------------------------

	private function _mismatched_var_pair()
	{
		$data = array(
			'title'		=> 'Super Heroes',
			'powers'	=> array(
					array(
						'invisibility'	=> 'yes',
						'flying'		=> 'no'),
			)
		);

		$template = "{title}\n{powers}{invisibility}\n{flying}";
		$result = "Super Heroes\n{powers}{invisibility}\n{flying}";

		$this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
	}

}