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

testInvalidDoubleBracketCloseString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Vlaswinkel\Lua\Tests;
4
5
use Vlaswinkel\Lua\InputStream;
6
use Vlaswinkel\Lua\Lua;
7
use Vlaswinkel\Lua\ParseException;
8
use Vlaswinkel\Lua\Token;
9
use Vlaswinkel\Lua\TokenStream;
10
11
/**
12
 * Class LuaTokenStreamTest
13
 *
14
 * @author  Koen Vlaswinkel <[email protected]>
15
 * @package Vlaswinkel\Lua\Tests
16
 */
17
class LuaTokenStreamTest extends \PHPUnit_Framework_TestCase {
18 View Code Duplication
    public function testDoubleQuotedString() {
19
        $obj = new TokenStream(new InputStream('"foo"'));
20
21
        $token = $obj->next();
22
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
23
        $this->assertEquals("foo", $token->getValue());
24
    }
25
26 View Code Duplication
    public function testSingleQuotedString() {
27
        $obj = new TokenStream(new InputStream("'foo'"));
28
29
        $token = $obj->next();
30
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
31
        $this->assertEquals("foo", $token->getValue());
32
    }
33
34 View Code Duplication
    public function testNestedString() {
35
        $obj = new TokenStream(new InputStream("[=[ Like this ]=]"));
36
37
        $token = $obj->next();
38
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
39
        $this->assertEquals(' Like this ', $token->getValue());
40
    }
41
42 View Code Duplication
    public function testEscapedString() {
43
        $obj = new TokenStream(new InputStream('" test \n\r\t\v\\\\\""'));
44
45
        $token = $obj->next();
46
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
47
        $this->assertEquals(" test \n\r\t\v\\\"", $token->getValue());
48
    }
49
50 View Code Duplication
    public function testOtherNestedString() {
51
        $obj = new TokenStream(new InputStream('[=[one [[two]] one]=]'));
52
53
        $token = $obj->next();
54
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
55
        $this->assertEquals('one [[two]] one', $token->getValue());
56
    }
57
58 View Code Duplication
    public function testNestedNestedString() {
59
        $obj = new TokenStream(new InputStream('[=[one [==[two]==] one]=]'));
60
61
        $token = $obj->next();
62
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
63
        $this->assertEquals('one [==[two]==] one', $token->getValue());
64
    }
65
66 View Code Duplication
    public function testComplexNestedString() {
67
        $obj = new TokenStream(new InputStream('[===[one [ [==[ one]===]'));
68
69
        $token = $obj->next();
70
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
71
        $this->assertEquals('one [ [==[ one', $token->getValue());
72
    }
73
74 View Code Duplication
    public function testNumberInt() {
75
        $obj = new TokenStream(new InputStream("1337"));
76
77
        $token = $obj->next();
78
        $this->assertEquals(Token::TYPE_NUMBER, $token->getType());
79
        $this->assertEquals(1337, $token->getValue());
80
    }
81
82 View Code Duplication
    public function testNumberFloat() {
83
        $obj = new TokenStream(new InputStream("13.37"));
84
85
        $token = $obj->next();
86
        $this->assertEquals(Token::TYPE_NUMBER, $token->getType());
87
        $this->assertEquals(13.37, $token->getValue());
88
    }
89
90
    public function testPunctuation() {
91
        foreach ([',', '{', '}', '=', '[', ']'] as $punc) {
92
            $obj = new TokenStream(new InputStream($punc));
93
94
            $token = $obj->next();
95
            $this->assertEquals(Token::TYPE_PUNCTUATION, $token->getType());
96
            $this->assertEquals($punc, $token->getValue());
97
        }
98
    }
99
100 View Code Duplication
    public function testIdentifier() {
101
        $obj = new TokenStream(new InputStream("foo"));
102
103
        $token = $obj->next();
104
        $this->assertEquals(Token::TYPE_IDENTIFIER, $token->getType());
105
        $this->assertEquals('foo', $token->getValue());
106
    }
107
108
    public function testKeyword() {
109
        foreach (Lua::$luaKeywords as $keyword) {
110
            $obj = new TokenStream(new InputStream($keyword));
111
112
            $token = $obj->next();
113
            $this->assertEquals(Token::TYPE_KEYWORD, $token->getType());
114
            $this->assertEquals($keyword, $token->getValue());
115
        }
116
    }
117
118
    /**
119
     * @expectedException \Vlaswinkel\Lua\ParseException
120
     * @expectedExceptionMessage Cannot handle character: * (ord: 42)
121
     */
122
    public function testInvalidCharacter() {
123
        $obj = new TokenStream(new InputStream("*"));
124
125
        $obj->next();
126
    }
127
128
    /**
129
     * @expectedException \Vlaswinkel\Lua\ParseException
130
     */
131
    public function testUnclosedNestedString() {
132
        $obj = new TokenStream(new InputStream("[=[ test ]]"));
133
134
        $obj->next();
135
    }
136
137
    /**
138
     * @expectedException \Vlaswinkel\Lua\ParseException
139
     */
140
    public function testInvalidDoubleBracketOpenString() {
141
        $obj = new TokenStream(new InputStream('[=== test ]===]'));
142
143
        $obj->next();
144
    }
145
146
    /**
147
     * @expectedException \Vlaswinkel\Lua\ParseException
148
     */
149
    public function testInvalidDoubleBracketCloseString() {
150
        $obj = new TokenStream(new InputStream('[==[ test ]== '));
151
152
        $obj->next();
153
    }
154
}