|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Tests; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Kadet\Highlighter\Parser\CloseRule; |
|
20
|
|
|
use Kadet\Highlighter\Parser\OpenRule; |
|
21
|
|
|
use Kadet\Highlighter\Parser\Rule; |
|
22
|
|
|
use Kadet\Highlighter\Parser\Token; |
|
23
|
|
|
|
|
24
|
|
|
class TokenTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
public function testCreation() |
|
27
|
|
|
{ |
|
28
|
|
|
$rule = new Rule(); |
|
29
|
|
|
$token = new Token(['test.name', 'pos' => 10, 'index' => 10, 'rule' => $rule]); |
|
30
|
|
|
|
|
31
|
|
|
$this->assertEquals($token->name, 'test.name', 'Token name is invalid'); |
|
32
|
|
|
$this->assertEquals($token->pos, 10, 'Position is invalid'); |
|
33
|
|
|
$this->assertEquals($token->index, 10, 'Index is invalid'); |
|
34
|
|
|
$this->assertEquals($token->getRule(), $rule, 'Rule is invalid'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testCreationWithStart() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$start = new Token(['test.name', 'pos' => 5]); |
|
40
|
|
|
$token = new Token(['test.name', 'pos' => 10, 'start' => $start]); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals($token->getStart(), $start, 'Token is not pointing to start.'); |
|
43
|
|
|
$this->assertEquals($start->getEnd(), $token, 'Start is not pointing to token.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
View Code Duplication |
public function testCreationWithEnd() |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$end = new Token(['test.name', 'pos' => 15]); |
|
49
|
|
|
$token = new Token(['test.name', 'pos' => 10, 'end' => $end]); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals($token->getEnd(), $end, 'Token is not pointing to end.'); |
|
52
|
|
|
$this->assertEquals($end->getStart(), $token, 'End is not pointing to token.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testCreationWithLength() |
|
56
|
|
|
{ |
|
57
|
|
|
$token = new Token(['test.name', 'pos' => 15, 'length' => 10]); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($token->getEnd()->pos, 25, 'Token is not pointing to end.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testLength() |
|
63
|
|
|
{ |
|
64
|
|
|
$token = new Token(['test.name', 'pos' => 15, 'length' => 10]); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals($token->getLength(), 10, 'Length is invalid'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testIsStart() |
|
70
|
|
|
{ |
|
71
|
|
|
$token = new Token(['test.name', 'pos' => 10]); |
|
72
|
|
|
$close = new Token(['test.name', 'pos' => 10, 'rule' => new CloseRule()]); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertTrue($token->isStart()); |
|
75
|
|
|
$this->assertFalse($close->isStart()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testIsEnd() |
|
79
|
|
|
{ |
|
80
|
|
|
$token = new Token(['test.name', 'pos' => 15]); |
|
81
|
|
|
new Token(['test.name', 'pos' => 10, 'end' => $token]); |
|
82
|
|
|
|
|
83
|
|
|
$close = new Token(['test.name', 'pos' => 10, 'rule' => new OpenRule()]); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue($token->isEnd()); |
|
86
|
|
|
$this->assertFalse($close->isEnd()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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.