Passed
Push — master ( b8c5dd...52423a )
by Koen
12:09
created

LuaTokenStreamTest::testUnclosedNestedString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 5
c 1
b 1
f 0
rs 9.4285
nc 1
cc 1
eloc 3
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 testOtherNestedString() {
43
        $obj = new TokenStream(new InputStream('[=[one [[two]] one]=]'));
44
45
        $token = $obj->next();
46
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
47
        $this->assertEquals('one [[two]] one', $token->getValue());
48
    }
49
50 View Code Duplication
    public function testNestedNestedString() {
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 testComplexNestedString() {
59
        $obj = new TokenStream(new InputStream('[===[one [ [==[ one]===]'));
60
61
        $token = $obj->next();
62
        $this->assertEquals(Token::TYPE_STRING, $token->getType());
63
        $this->assertEquals('one [ [==[ one', $token->getValue());
64
    }
65
66 View Code Duplication
    public function testNumberInt() {
67
        $obj = new TokenStream(new InputStream("1337"));
68
69
        $token = $obj->next();
70
        $this->assertEquals(Token::TYPE_NUMBER, $token->getType());
71
        $this->assertEquals(1337, $token->getValue());
72
    }
73
74 View Code Duplication
    public function testNumberFloat() {
75
        $obj = new TokenStream(new InputStream("13.37"));
76
77
        $token = $obj->next();
78
        $this->assertEquals(Token::TYPE_NUMBER, $token->getType());
79
        $this->assertEquals(13.37, $token->getValue());
80
    }
81
82
    public function testPunctuation() {
83
        foreach ([',', '{', '}', '=', '[', ']'] as $punc) {
84
            $obj = new TokenStream(new InputStream($punc));
85
86
            $token = $obj->next();
87
            $this->assertEquals(Token::TYPE_PUNCTUATION, $token->getType());
88
            $this->assertEquals($punc, $token->getValue());
89
        }
90
    }
91
92 View Code Duplication
    public function testIdentifier() {
93
        $obj = new TokenStream(new InputStream("foo"));
94
95
        $token = $obj->next();
96
        $this->assertEquals(Token::TYPE_IDENTIFIER, $token->getType());
97
        $this->assertEquals('foo', $token->getValue());
98
    }
99
100
    public function testKeyword() {
101
        foreach (Lua::$luaKeywords as $keyword) {
102
            $obj = new TokenStream(new InputStream($keyword));
103
104
            $token = $obj->next();
105
            $this->assertEquals(Token::TYPE_KEYWORD, $token->getType());
106
            $this->assertEquals($keyword, $token->getValue());
107
        }
108
    }
109
110
    /**
111
     * @expectedException \Vlaswinkel\Lua\ParseException
112
     * @expectedExceptionMessage Cannot handle character: * (ord: 42)
113
     */
114
    public function testInvalidCharacter() {
115
        $obj = new TokenStream(new InputStream("*"));
116
117
        $obj->next();
118
    }
119
120
    /**
121
     * @expectedException \Vlaswinkel\Lua\ParseException
122
     */
123
    public function testUnclosedNestedString() {
124
        $obj = new TokenStream(new InputStream("[=[ test ]]"));
125
126
        $obj->next();
127
    }
128
}