Passed
Push — master ( 52423a...23b91d )
by Koen
03:18
created

LuaParserTest::testInvalidKeyword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Vlaswinkel\Lua\Tests;
4
5
use Vlaswinkel\Lua\AST\NilASTNode;
6
use Vlaswinkel\Lua\AST\NumberASTNode;
7
use Vlaswinkel\Lua\AST\StringASTNode;
8
use Vlaswinkel\Lua\AST\TableASTNode;
9
use Vlaswinkel\Lua\InputStream;
10
use Vlaswinkel\Lua\Parser;
11
use Vlaswinkel\Lua\TokenStream;
12
13
/**
14
 * Class LuaParserTest
15
 *
16
 * @author  Koen Vlaswinkel <[email protected]>
17
 * @package Vlaswinkel\Lua\Tests
18
 */
19
class LuaParserTest extends \PHPUnit_Framework_TestCase {
20 View Code Duplication
    public function testString() {
21
        $parser = new Parser(new TokenStream(new InputStream('"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 testStringWithSpaces() {
31
        $parser = new Parser(new TokenStream(new InputStream('"foo bar."')));
32
33
        $node = $parser->parse();
34
35
        $this->assertEquals(StringASTNode::NAME, $node->getName());
36
        $this->assertInstanceOf(StringASTNode::class, $node);
37
        $this->assertEquals("foo bar.", $node->getValue());
38
    }
39
40 View Code Duplication
    public function testAlternateString() {
41
        $parser = new Parser(new TokenStream(new InputStream('[[foo]]')));
42
43
        $node = $parser->parse();
44
45
        $this->assertEquals(StringASTNode::NAME, $node->getName());
46
        $this->assertInstanceOf(StringASTNode::class, $node);
47
        $this->assertEquals("foo", $node->getValue());
48
    }
49
50
    // https://github.com/koesie10/LuaSerializer/issues/1
51 View Code Duplication
    public function testAlternateStringWithSpaces() {
52
        $parser = new Parser(new TokenStream(new InputStream('[[foo bar.]]')));
53
54
        $node = $parser->parse();
55
56
        $this->assertEquals(StringASTNode::NAME, $node->getName());
57
        $this->assertInstanceOf(StringASTNode::class, $node);
58
        $this->assertEquals("foo bar.", $node->getValue());
59
    }
60
61 View Code Duplication
    public function testNumber() {
62
        $parser = new Parser(new TokenStream(new InputStream('1337')));
63
64
        $node = $parser->parse();
65
66
        $this->assertEquals(NumberASTNode::NAME, $node->getName());
67
        $this->assertInstanceOf(NumberASTNode::class, $node);
68
        $this->assertEquals(1337, $node->getValue());
69
    }
70
71 View Code Duplication
    public function testNil() {
72
        $parser = new Parser(new TokenStream(new InputStream('nil')));
73
74
        $node = $parser->parse();
75
76
        $this->assertEquals(NilASTNode::NAME, $node->getName());
77
        $this->assertInstanceOf(NilASTNode::class, $node);
78
    }
79
80 View Code Duplication
    public function testTableKey() {
81
        $parser = new Parser(new TokenStream(new InputStream('["test"]')));
82
83
        $node = $parser->parse();
84
85
        $this->assertEquals(StringASTNode::NAME, $node->getName());
86
        $this->assertInstanceOf(StringASTNode::class, $node);
87
        $this->assertEquals("test", $node->getValue());
88
    }
89
90 View Code Duplication
    public function testSimpleTable() {
91
        $parser = new Parser(
92
            new TokenStream(
93
                new InputStream(
94
                    '{
95
            foo = "bar"
96
        }'
97
                )
98
            )
99
        );
100
101
        $node = $parser->parse();
102
103
        $this->assertEquals(TableASTNode::NAME, $node->getName());
104
        $this->assertInstanceOf(TableASTNode::class, $node);
105
106
        $this->assertCount(1, $node->getEntries());
107
        $entry = $node->getEntries()[0];
108
109
        $this->assertTrue($entry->hasKey());
110
        $this->assertEquals(StringASTNode::NAME, $entry->getKey()->getName());
111
        $this->assertInstanceOf(StringASTNode::class, $entry->getKey());
112
        $this->assertEquals("foo", $entry->getKey()->getValue());
113
114
        $this->assertEquals(StringASTNode::NAME, $entry->getValue()->getName());
115
        $this->assertInstanceOf(StringASTNode::class, $entry->getValue());
116
        $this->assertEquals("bar", $entry->getValue()->getValue());
117
    }
118
119
    public function testNestedTable() {
120
        $parser = new Parser(
121
            new TokenStream(
122
                new InputStream(
123
                    '{
124
            foo = {
125
                ["test"] = {
126
                    1337,
127
                    "bar"
128
                }
129
            }
130
        }'
131
                )
132
            )
133
        );
134
135
        $node = $parser->parse();
136
137
        $this->assertEquals(TableASTNode::NAME, $node->getName());
138
        $this->assertInstanceOf(TableASTNode::class, $node);
139
140
        $this->assertCount(1, $node->getEntries());
141
        $entry = $node->getEntries()[0];
142
143
        $this->assertTrue($entry->hasKey());
144
        $this->assertEquals(StringASTNode::NAME, $entry->getKey()->getName());
145
        $this->assertInstanceOf(StringASTNode::class, $entry->getKey());
146
        $this->assertEquals("foo", $entry->getKey()->getValue());
147
148
        $this->assertEquals(TableASTNode::NAME, $entry->getValue()->getName());
149
        $this->assertInstanceOf(TableASTNode::class, $entry->getValue());
150
        $this->assertCount(1, $entry->getValue()->getEntries());
151
152
        $nestedEntry = $entry->getValue()->getEntries()[0];
153
154
        $this->assertTrue($nestedEntry->hasKey());
155
        $this->assertEquals(StringASTNode::NAME, $nestedEntry->getKey()->getName());
156
        $this->assertInstanceOf(StringASTNode::class, $nestedEntry->getKey());
157
        $this->assertEquals("test", $nestedEntry->getKey()->getValue());
158
159
        $this->assertEquals(TableASTNode::NAME, $nestedEntry->getValue()->getName());
160
        $this->assertInstanceOf(TableASTNode::class, $nestedEntry->getValue());
161
        $this->assertCount(2, $nestedEntry->getValue()->getEntries());
162
163
        $nestedNestedEntry1 = $nestedEntry->getValue()->getEntries()[0];
164
165
        $this->assertFalse($nestedNestedEntry1->hasKey());
166
167
        $this->assertEquals(NumberASTNode::NAME, $nestedNestedEntry1->getValue()->getName());
168
        $this->assertInstanceOf(NumberASTNode::class, $nestedNestedEntry1->getValue());
169
        $this->assertEquals(1337, $nestedNestedEntry1->getValue()->getValue());
170
171
        $nestedNestedEntry2 = $nestedEntry->getValue()->getEntries()[1];
172
173
        $this->assertFalse($nestedNestedEntry2->hasKey());
174
175
        $this->assertEquals(StringASTNode::NAME, $nestedNestedEntry2->getValue()->getName());
176
        $this->assertInstanceOf(StringASTNode::class, $nestedNestedEntry2->getValue());
177
        $this->assertEquals("bar", $nestedNestedEntry2->getValue()->getValue());
178
    }
179
180 View Code Duplication
    public function testTableWithNestedAlternateStrings() {
181
        $parser = new Parser(
182
            new TokenStream(
183
                new InputStream(
184
                    '{
185
            foo = [[bar]]
186
        }'
187
                )
188
            )
189
        );
190
191
        $node = $parser->parse();
192
193
        $this->assertEquals(TableASTNode::NAME, $node->getName());
194
        $this->assertInstanceOf(TableASTNode::class, $node);
195
196
        $this->assertCount(1, $node->getEntries());
197
        $entry = $node->getEntries()[0];
198
199
        $this->assertTrue($entry->hasKey());
200
        $this->assertEquals(StringASTNode::NAME, $entry->getKey()->getName());
201
        $this->assertInstanceOf(StringASTNode::class, $entry->getKey());
202
        $this->assertEquals("foo", $entry->getKey()->getValue());
203
204
        $this->assertEquals(StringASTNode::NAME, $entry->getValue()->getName());
205
        $this->assertInstanceOf(StringASTNode::class, $entry->getValue());
206
        $this->assertEquals("bar", $entry->getValue()->getValue());
207
    }
208
209
    /**
210
     * @expectedException \Vlaswinkel\Lua\ParseException
211
     */
212
    public function testInvalid() {
213
        $parser = new Parser(new TokenStream(new InputStream('{ test[bar }')));
214
215
        $parser->parse();
216
    }
217
218
    /**
219
     * @expectedException \Vlaswinkel\Lua\ParseException
220
     */
221
    public function testInvalidKeyword() {
222
        $parser  = new Parser(new TokenStream(new InputStream('function')));
223
224
        $node = $parser->parse();
225
        $this->assertEquals('test', $node->getName());
226
    }
227
228
    public function testComments() {
229
        $parser = new Parser(new TokenStream(new InputStream('{
230
        -- comment
231
    foo = {
232
        test = 123
233
    }
234
}')));
235
236
        $parser->parse();
237
    }
238
239
    public function testInlineComments() {
240
        $parser = new Parser(new TokenStream(new InputStream('{
241
    foo = {
242
        test = 123 -- comment
243
    }
244
}')));
245
246
        $parser->parse();
247
    }
248
249
    public function testAdvancedTable() {
250
        $parser = new Parser(new TokenStream(new InputStream(file_get_contents(__DIR__ . '/advanced-test.lua'))));
251
252
        $parser->parse();
253
    }
254
}