Completed
Pull Request — master (#178)
by Cláudio
03:52 queued 21s
created

MinusTest::testIntToIntDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\PHPSA\Compiler\Expression\Operators\Arithmetical;
4
5
use PhpParser\Node;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Compiler\Expression;
8
9
class MinusTest extends \Tests\PHPSA\TestCase
10
{
11
    /**
12
     * Data provider for Minus {int} - {int} = {int}
13
     *
14
     * @return array
15
     */
16
    public function intToIntDataProvider()
17
    {
18
        return array(
19
            array(-1, -1, 0),
20
            array(-1, 0, -1),
21
            array(0, -1, 1),
22
            array(-1, 2, -3),
23
            array(2, -1, 3),
24
            array(0, 0, 0),
25
            array(0, 1, -1),
26
            array(1, 0, 1),
27
            array(1, 2, -1),
28
            array(2, 1, 1),
29
            array(25, 25, 0),
30
            array(50, 25, 25),
31
            array(50, -25, 75),
32
            array(50, 50, 0),
33
            array(50, -50, 100),
34
            array(-50, -50, 0),
35
        );
36
    }
37
38
    /**
39
     * Tests {int} - {int} = {int}
40
     *
41
     * @dataProvider intToIntDataProvider
42
     */
43
    public function testMinusIntFromInt($a, $b, $c)
44
    {
45
        $this->assertInternalType('int', $a);
46
        $this->assertInternalType('int', $b);
47
        $this->assertInternalType('int', $c);
48
49
        $baseExpression = new Node\Expr\BinaryOp\Minus(
50
            new Node\Scalar\LNumber($a),
51
            new Node\Scalar\LNumber($b)
52
        );
53
        $compiledExpression = $this->compileExpression($baseExpression);
54
55
        $this->assertInstanceOfCompiledExpression($compiledExpression);
56
        $this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType());
57
        $this->assertSame($c, $compiledExpression->getValue());
58
    }
59
60
    /**
61
     * Data provider for Minus {int} - {double} = {double} and {double} - {double} = {double}
62
     *
63
     * @return array
64
     */
65
    public function resultDoubleDataProvider()
66
    {
67
        return array(
68
            array(-1, -1.0, 0.0),
69
            array(-1.0, -1.0, 0.0),
70
            array(3, 1.5, 1.5),
71
            array(4.0, 2.0, 2.0),
72
            array(2, 3.5, -1.5),
73
            array(2.5, 3.5, -1.0),
74
        );
75
    }
76
77
    /**
78
     * Tests {int} - {double} = {double} and {double} - {double} = {double}
79
     *
80
     * @dataProvider resultDoubleDataProvider
81
     */
82
    public function testMinusResultDouble($a, $b, $c)
83
    {
84
        $baseExpression = new Node\Expr\BinaryOp\Minus(
85
            new Node\Scalar\DNumber($a),
86
            new Node\Scalar\DNumber($b)
87
        );
88
        $compiledExpression = $this->compileExpression($baseExpression);
89
90
        $this->assertInstanceOfCompiledExpression($compiledExpression);
91
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
92
        $this->assertSame($c, $compiledExpression->getValue());
93
    }
94
95
    /**
96
     * Tests {left-expr::UNKNOWN} - {right-expr}
97
     */
98
    public function testFirstUnexpectedTypes()
99
    {
100
        $baseExpression = new Node\Expr\BinaryOp\Minus(
101
            $this->newFakeScalarExpr(),
102
            $this->newScalarExpr(1)
103
        );
104
        $compiledExpression = $this->compileExpression($baseExpression);
105
106
        $this->assertInstanceOfCompiledExpression($compiledExpression);
107
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
108
        $this->assertSame(null, $compiledExpression->getValue());
109
    }
110
111
    /**
112
     * Tests {left-expr} - {right-expr::UNKNOWN}
113
     */
114
    public function testSecondUnexpectedTypes()
115
    {
116
        $baseExpression = new Node\Expr\BinaryOp\Minus(
117
            $this->newScalarExpr(1),
118
            $this->newFakeScalarExpr()
119
        );
120
        $compiledExpression = $this->compileExpression($baseExpression);
121
122
        $this->assertInstanceOfCompiledExpression($compiledExpression);
123
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
124
        $this->assertSame(null, $compiledExpression->getValue());
125
    }
126
}
127