LuaTokenStreamTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 50.72 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 70
loc 138
rs 10
c 2
b 1
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testDoubleQuotedString() 7 7 1
A testSingleQuotedString() 7 7 1
A testNestedString() 7 7 1
A testEscapedString() 7 7 1
A testOtherNestedString() 7 7 1
A testNestedNestedString() 7 7 1
A testComplexNestedString() 7 7 1
A testNumberInt() 7 7 1
A testNumberFloat() 7 7 1
A testPunctuation() 0 9 2
A testIdentifier() 7 7 1
A testKeyword() 0 9 2
A testInvalidCharacter() 0 5 1
A testUnclosedNestedString() 0 5 1
A testInvalidDoubleBracketOpenString() 0 5 1
A testInvalidDoubleBracketCloseString() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}