ParserTest::testSingleUnit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Vlaswinkel\UnitConverter\Parser\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Vlaswinkel\UnitConverter\Parser\AST\BinaryOperatorASTNode;
7
use Vlaswinkel\UnitConverter\Parser\AST\DigitASTNode;
8
use Vlaswinkel\UnitConverter\Parser\AST\UnitASTNode;
9
use Vlaswinkel\UnitConverter\Parser\InputStream;
10
use Vlaswinkel\UnitConverter\Parser\Lexer;
11
use Vlaswinkel\UnitConverter\Parser\Parser;
12
13
/**
14
 * Class ParserTest
15
 *
16
 * @author Koen Vlaswinkel <[email protected]>
17
 * @package Vlaswinkel\UnitConverter\Tests
18
 */
19
class ParserTest extends TestCase {
20
    public function testSingleUnit() {
21
        $parser = $this->createParser('kg');
22
23
        $node = $parser->parse();
24
        $this->assertEquals(new UnitASTNode('kg'), $node);
25
    }
26
27 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...
28
        $parser = $this->createParser('N * m');
29
30
        $node = $parser->parse();
31
        $this->assertEquals(
32
            new BinaryOperatorASTNode(
33
                '*',
34
                new UnitASTNode('N'),
35
                new UnitASTNode('m')
36
            ),
37
            $node
38
        );
39
    }
40
41 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...
42
        $parser = $this->createParser('m/s');
43
44
        $node = $parser->parse();
45
        $this->assertEquals(
46
            new BinaryOperatorASTNode(
47
                '/',
48
                new UnitASTNode('m'),
49
                new UnitASTNode('s')
50
            ),
51
            $node
52
        );
53
    }
54
55 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...
56
        $parser = $this->createParser('m^2');
57
58
        $node = $parser->parse();
59
        $this->assertEquals(
60
            new BinaryOperatorASTNode(
61
                '^',
62
                new UnitASTNode('m'),
63
                new DigitASTNode('2')
64
            ),
65
            $node
66
        );
67
    }
68
69
    public function testExponentialUnits() {
70
        $parser = $this->createParser('m^2/s');
71
72
        $node = $parser->parse();
73
74
        $this->assertEquals(
75
            new BinaryOperatorASTNode(
76
                '/',
77
                new BinaryOperatorASTNode(
78
                    '^',
79
                    new UnitASTNode('m'),
80
                    new DigitASTNode('2')
81
                ),
82
                new UnitASTNode('s')
83
            ),
84
            $node
85
        );
86
    }
87
88
    public function testOhm() {
89
        $parser = $this->createParser('kg*m^2*s^-3*A^-2');
90
91
        $node = $parser->parse();
92
        $this->assertEquals(
93
            new BinaryOperatorASTNode(
94
                '*',
95
                new BinaryOperatorASTNode(
96
                    '*',
97
                    new BinaryOperatorASTNode(
98
                        '*',
99
                        new UnitASTNode('kg'),
100
                        new BinaryOperatorASTNode(
101
                            '^',
102
                            new UnitASTNode('m'),
103
                            new DigitASTNode('2')
104
                        )
105
                    ),
106
                    new BinaryOperatorASTNode(
107
                        '^',
108
                        new UnitASTNode('s'),
109
                        new DigitASTNode('-3')
110
                    )
111
                ),
112
                new BinaryOperatorASTNode(
113
                    '^',
114
                    new UnitASTNode('A'),
115
                    new DigitASTNode('-2')
116
                )
117
            ),
118
            $node
119
        );
120
    }
121
122
    private function createParser(string $expression): Parser {
123
        return new Parser(new Lexer(new InputStream($expression)));
124
    }
125
}