LexerTest::testDividedUnits()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Vlaswinkel\UnitConverter\Parser\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Vlaswinkel\UnitConverter\Parser\InputStream;
7
use Vlaswinkel\UnitConverter\Parser\Lexer;
8
use Vlaswinkel\UnitConverter\Parser\Operator;
9
use Vlaswinkel\UnitConverter\Parser\Token;
10
11
/**
12
 * Class LexerTest
13
 *
14
 * @author Koen Vlaswinkel <[email protected]>
15
 * @package Vlaswinkel\UnitConverter\Tests
16
 */
17
class LexerTest extends TestCase {
18
    public function testDigit() {
19
        $lexer = $this->createLexer('2');
20
21
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '2'), $lexer->next());
22
        $this->assertTrue($lexer->eof());
23
    }
24
25
    public function testSingleUnit() {
26
        $lexer = $this->createLexer('kg');
27
28
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'kg'), $lexer->next());
29
        $this->assertTrue($lexer->eof());
30
    }
31
32
    public function testSingleUnitWithWhitespace() {
33
        $lexer = $this->createLexer(" m \t\n");
34
35
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
36
        $this->assertTrue($lexer->eof());
37
    }
38
39 View Code Duplication
    public function testMultipliedUnits() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
40
        $lexer = $this->createLexer('N * m');
41
42
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'N'), $lexer->next());
43
        $this->assertEquals(new Operator('*', 1), $lexer->next());
44
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
45
        $this->assertTrue($lexer->eof());
46
    }
47
48 View Code Duplication
    public function testDividedUnits() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
49
        $lexer = $this->createLexer('m/s');
50
51
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
52
        $this->assertEquals(new Operator('/', 1), $lexer->next());
53
        $this->assertEquals(new Token(Token::TYPE_UNIT, 's'), $lexer->next());
54
        $this->assertTrue($lexer->eof());
55
    }
56
57 View Code Duplication
    public function testExponentialUnit() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
58
        $lexer = $this->createLexer('m^2');
59
60
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
61
        $this->assertEquals(new Operator('^', 2), $lexer->next());
62
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '2'), $lexer->next());
63
        $this->assertTrue($lexer->eof());
64
    }
65
66
    public function testExponentialUnits() {
67
        $lexer = $this->createLexer('m^2/s');
68
69
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
70
        $this->assertEquals(new Operator('^', 2), $lexer->next());
71
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '2'), $lexer->next());
72
        $this->assertEquals(new Operator('/', 1), $lexer->next());
73
        $this->assertEquals(new Token(Token::TYPE_UNIT, 's'), $lexer->next());
74
        $this->assertTrue($lexer->eof());
75
    }
76
77
    public function testOhm() {
78
        $lexer = $this->createLexer('kg*m^2*s^-3*A^-2');
79
80
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'kg'), $lexer->next());
81
        $this->assertEquals(new Operator('*', 1), $lexer->next());
82
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'm'), $lexer->next());
83
        $this->assertEquals(new Operator('^', 2), $lexer->next());
84
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '2'), $lexer->next());
85
        $this->assertEquals(new Operator('*', 1), $lexer->next());
86
        $this->assertEquals(new Token(Token::TYPE_UNIT, 's'), $lexer->next());
87
        $this->assertEquals(new Operator('^', 2), $lexer->next());
88
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '-3'), $lexer->next());
89
        $this->assertEquals(new Operator('*', 1), $lexer->next());
90
        $this->assertEquals(new Token(Token::TYPE_UNIT, 'A'), $lexer->next());
91
        $this->assertEquals(new Operator('^', 2), $lexer->next());
92
        $this->assertEquals(new Token(Token::TYPE_DIGIT, '-2'), $lexer->next());
93
        $this->assertTrue($lexer->eof());
94
    }
95
96
    private function createLexer(string $expression): Lexer {
97
        return new Lexer(new InputStream($expression));
98
    }
99
}