Passed
Push — master ( 1a0a87...a70f7d )
by Koen
05:46 queued 03:06
created

LuaParserTest::testComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 3
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
    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
    /**
181
     * @expectedException \Vlaswinkel\Lua\ParseException
182
     */
183
    public function testInvalid() {
184
        $parser = new Parser(new TokenStream(new InputStream('{ test[bar }')));
185
186
        $parser->parse();
187
    }
188
189
    public function testComments() {
190
        $parser = new Parser(new TokenStream(new InputStream('{
191
        -- comment
192
    foo = {
193
        test = 123
194
    }
195
}')));
196
197
        $parser->parse();
198
    }
199
200
    public function testInlineComments() {
201
        $parser = new Parser(new TokenStream(new InputStream('{
202
    foo = {
203
        test = 123 -- comment
204
    }
205
}')));
206
207
        $parser->parse();
208
    }
209
}