1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LuaParserTest.php |
4
|
|
|
* |
5
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
6
|
|
|
* @since 21/12/2015 17:30 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Vlaswinkel\Lua\Tests; |
10
|
|
|
|
11
|
|
|
use Vlaswinkel\Lua\AST\NilASTNode; |
12
|
|
|
use Vlaswinkel\Lua\AST\NumberASTNode; |
13
|
|
|
use Vlaswinkel\Lua\AST\StringASTNode; |
14
|
|
|
use Vlaswinkel\Lua\AST\TableASTNode; |
15
|
|
|
use Vlaswinkel\Lua\LuaInputStream; |
16
|
|
|
use Vlaswinkel\Lua\LuaParser; |
17
|
|
|
use Vlaswinkel\Lua\LuaTokenStream; |
18
|
|
|
|
19
|
|
|
class LuaParserTest extends \PHPUnit_Framework_TestCase { |
20
|
|
View Code Duplication |
public function testString() { |
21
|
|
|
$parser = new LuaParser(new LuaTokenStream(new LuaInputStream('"foo"'))); |
22
|
|
|
|
23
|
|
|
$node = $parser->parse(); |
24
|
|
|
|
25
|
|
|
$this->assertEquals(StringASTNode::NAME, $node->getName()); |
26
|
|
|
$this->assertInstanceOf(StringASTNode::class, $node); |
27
|
|
|
$this->assertEquals("foo", $node->getValue()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
public function testNumber() { |
31
|
|
|
$parser = new LuaParser(new LuaTokenStream(new LuaInputStream('1337'))); |
32
|
|
|
|
33
|
|
|
$node = $parser->parse(); |
34
|
|
|
|
35
|
|
|
$this->assertEquals(NumberASTNode::NAME, $node->getName()); |
36
|
|
|
$this->assertInstanceOf(NumberASTNode::class, $node); |
37
|
|
|
$this->assertEquals(1337, $node->getValue()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function testNil() { |
41
|
|
|
$parser = new LuaParser(new LuaTokenStream(new LuaInputStream('nil'))); |
42
|
|
|
|
43
|
|
|
$node = $parser->parse(); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(NilASTNode::NAME, $node->getName()); |
46
|
|
|
$this->assertInstanceOf(NilASTNode::class, $node); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function testTableKey() { |
50
|
|
|
$parser = new LuaParser(new LuaTokenStream(new LuaInputStream('["test"]'))); |
51
|
|
|
|
52
|
|
|
$node = $parser->parse(); |
53
|
|
|
|
54
|
|
|
$this->assertEquals(StringASTNode::NAME, $node->getName()); |
55
|
|
|
$this->assertInstanceOf(StringASTNode::class, $node); |
56
|
|
|
$this->assertEquals("test", $node->getValue()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSimpleTable() { |
60
|
|
|
$parser = new LuaParser( |
61
|
|
|
new LuaTokenStream( |
62
|
|
|
new LuaInputStream( |
63
|
|
|
'{ |
64
|
|
|
foo = "bar" |
65
|
|
|
}' |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$node = $parser->parse(); |
71
|
|
|
|
72
|
|
|
$this->assertEquals(TableASTNode::NAME, $node->getName()); |
73
|
|
|
$this->assertInstanceOf(TableASTNode::class, $node); |
74
|
|
|
|
75
|
|
|
$this->assertCount(1, $node->getEntries()); |
76
|
|
|
$entry = $node->getEntries()[0]; |
77
|
|
|
|
78
|
|
|
$this->assertTrue($entry->hasKey()); |
79
|
|
|
$this->assertEquals(StringASTNode::NAME, $entry->getKey()->getName()); |
80
|
|
|
$this->assertInstanceOf(StringASTNode::class, $entry->getKey()); |
81
|
|
|
$this->assertEquals("foo", $entry->getKey()->getValue()); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(StringASTNode::NAME, $entry->getValue()->getName()); |
84
|
|
|
$this->assertInstanceOf(StringASTNode::class, $entry->getValue()); |
85
|
|
|
$this->assertEquals("bar", $entry->getValue()->getValue()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testNestedTable() { |
89
|
|
|
$parser = new LuaParser( |
90
|
|
|
new LuaTokenStream( |
91
|
|
|
new LuaInputStream( |
92
|
|
|
'{ |
93
|
|
|
foo = { |
94
|
|
|
["test"] = { |
95
|
|
|
1337, |
96
|
|
|
"bar" |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
}' |
100
|
|
|
) |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$node = $parser->parse(); |
105
|
|
|
|
106
|
|
|
$this->assertEquals(TableASTNode::NAME, $node->getName()); |
107
|
|
|
$this->assertInstanceOf(TableASTNode::class, $node); |
108
|
|
|
|
109
|
|
|
$this->assertCount(1, $node->getEntries()); |
110
|
|
|
$entry = $node->getEntries()[0]; |
111
|
|
|
|
112
|
|
|
$this->assertTrue($entry->hasKey()); |
113
|
|
|
$this->assertEquals(StringASTNode::NAME, $entry->getKey()->getName()); |
114
|
|
|
$this->assertInstanceOf(StringASTNode::class, $entry->getKey()); |
115
|
|
|
$this->assertEquals("foo", $entry->getKey()->getValue()); |
116
|
|
|
|
117
|
|
|
$this->assertEquals(TableASTNode::NAME, $entry->getValue()->getName()); |
118
|
|
|
$this->assertInstanceOf(TableASTNode::class, $entry->getValue()); |
119
|
|
|
$this->assertCount(1, $entry->getValue()->getEntries()); |
120
|
|
|
|
121
|
|
|
$nestedEntry = $entry->getValue()->getEntries()[0]; |
122
|
|
|
|
123
|
|
|
$this->assertTrue($nestedEntry->hasKey()); |
124
|
|
|
$this->assertEquals(StringASTNode::NAME, $nestedEntry->getKey()->getName()); |
125
|
|
|
$this->assertInstanceOf(StringASTNode::class, $nestedEntry->getKey()); |
126
|
|
|
$this->assertEquals("test", $nestedEntry->getKey()->getValue()); |
127
|
|
|
|
128
|
|
|
$this->assertEquals(TableASTNode::NAME, $nestedEntry->getValue()->getName()); |
129
|
|
|
$this->assertInstanceOf(TableASTNode::class, $nestedEntry->getValue()); |
130
|
|
|
$this->assertCount(2, $nestedEntry->getValue()->getEntries()); |
131
|
|
|
|
132
|
|
|
$nestedNestedEntry1 = $nestedEntry->getValue()->getEntries()[0]; |
133
|
|
|
|
134
|
|
|
$this->assertFalse($nestedNestedEntry1->hasKey()); |
135
|
|
|
|
136
|
|
|
$this->assertEquals(NumberASTNode::NAME, $nestedNestedEntry1->getValue()->getName()); |
137
|
|
|
$this->assertInstanceOf(NumberASTNode::class, $nestedNestedEntry1->getValue()); |
138
|
|
|
$this->assertEquals(1337, $nestedNestedEntry1->getValue()->getValue()); |
139
|
|
|
|
140
|
|
|
$nestedNestedEntry2 = $nestedEntry->getValue()->getEntries()[1]; |
141
|
|
|
|
142
|
|
|
$this->assertFalse($nestedNestedEntry2->hasKey()); |
143
|
|
|
|
144
|
|
|
$this->assertEquals(StringASTNode::NAME, $nestedNestedEntry2->getValue()->getName()); |
145
|
|
|
$this->assertInstanceOf(StringASTNode::class, $nestedNestedEntry2->getValue()); |
146
|
|
|
$this->assertEquals("bar", $nestedNestedEntry2->getValue()->getValue()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @expectedException \Vlaswinkel\Lua\LuaParseException |
151
|
|
|
*/ |
152
|
|
|
public function testInvalid() { |
153
|
|
|
$parser = new LuaParser(new LuaTokenStream(new LuaInputStream('{ test[bar }'))); |
154
|
|
|
|
155
|
|
|
$parser->parse(); |
156
|
|
|
} |
157
|
|
|
} |