1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* LuaTokenStreamTest.php |
4
|
|
|
* |
5
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
6
|
|
|
* @since 21/12/2015 17:19 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Vlaswinkel\Lua\Tests; |
10
|
|
|
|
11
|
|
|
use Vlaswinkel\Lua\Lua; |
12
|
|
|
use Vlaswinkel\Lua\LuaInputStream; |
13
|
|
|
use Vlaswinkel\Lua\LuaToken; |
14
|
|
|
use Vlaswinkel\Lua\LuaTokenStream; |
15
|
|
|
|
16
|
|
|
class LuaTokenStreamTest extends \PHPUnit_Framework_TestCase { |
17
|
|
View Code Duplication |
public function testDoubleQuotedString() { |
|
|
|
|
18
|
|
|
$obj = new LuaTokenStream(new LuaInputStream('"foo"')); |
19
|
|
|
|
20
|
|
|
$token = $obj->next(); |
21
|
|
|
$this->assertEquals(LuaToken::TYPE_STRING, $token->getType()); |
22
|
|
|
$this->assertEquals("foo", $token->getValue()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function testSingleQuotedString() { |
|
|
|
|
26
|
|
|
$obj = new LuaTokenStream(new LuaInputStream("'foo'")); |
27
|
|
|
|
28
|
|
|
$token = $obj->next(); |
29
|
|
|
$this->assertEquals(LuaToken::TYPE_STRING, $token->getType()); |
30
|
|
|
$this->assertEquals("foo", $token->getValue()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testNumberInt() { |
|
|
|
|
34
|
|
|
$obj = new LuaTokenStream(new LuaInputStream("1337")); |
35
|
|
|
|
36
|
|
|
$token = $obj->next(); |
37
|
|
|
$this->assertEquals(LuaToken::TYPE_NUMBER, $token->getType()); |
38
|
|
|
$this->assertEquals(1337, $token->getValue()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testNumberFloat() { |
|
|
|
|
42
|
|
|
$obj = new LuaTokenStream(new LuaInputStream("13.37")); |
43
|
|
|
|
44
|
|
|
$token = $obj->next(); |
45
|
|
|
$this->assertEquals(LuaToken::TYPE_NUMBER, $token->getType()); |
46
|
|
|
$this->assertEquals(13.37, $token->getValue()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testPunctuation() { |
50
|
|
|
foreach ([',', '{', '}', '=', '[', ']'] as $punc) { |
51
|
|
|
$obj = new LuaTokenStream(new LuaInputStream($punc)); |
52
|
|
|
|
53
|
|
|
$token = $obj->next(); |
54
|
|
|
$this->assertEquals(LuaToken::TYPE_PUNCTUATION, $token->getType()); |
55
|
|
|
$this->assertEquals($punc, $token->getValue()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testIdentifier() { |
|
|
|
|
60
|
|
|
$obj = new LuaTokenStream(new LuaInputStream("foo")); |
61
|
|
|
|
62
|
|
|
$token = $obj->next(); |
63
|
|
|
$this->assertEquals(LuaToken::TYPE_IDENTIFIER, $token->getType()); |
64
|
|
|
$this->assertEquals('foo', $token->getValue()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testKeyword() { |
68
|
|
|
foreach (Lua::$luaKeywords as $keyword) { |
69
|
|
|
$obj = new LuaTokenStream(new LuaInputStream($keyword)); |
70
|
|
|
|
71
|
|
|
$token = $obj->next(); |
72
|
|
|
$this->assertEquals(LuaToken::TYPE_KEYWORD, $token->getType()); |
73
|
|
|
$this->assertEquals($keyword, $token->getValue()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \Vlaswinkel\Lua\LuaParseException |
79
|
|
|
* @expectedExceptionMessage Cannot handle character: * (ord: 42) |
80
|
|
|
*/ |
81
|
|
|
public function testInvalidCharacter() { |
82
|
|
|
$obj = new LuaTokenStream(new LuaInputStream("*")); |
83
|
|
|
|
84
|
|
|
$obj->next(); |
85
|
|
|
} |
86
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.