Completed
Pull Request — master (#124)
by Enrico
03:49
created

PlusTest::testIntToIntDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 17
rs 9.4285
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 PlusTest extends \Tests\PHPSA\TestCase
10
{
11
    /**
12
     * Data provider for Plus {int} + {int} = {int}
13
     *
14
     * @return array
15
     */
16
    public function testIntToIntDataProvider()
17
    {
18
        return array(
19
            array(-1, -1, -2),
20
            array(-1, 0, -1),
21
            array(0, -1, -1),
22
            array(-1, 2, 1),
23
            array(2, -1, 1),
24
            array(0, 0, 0),
25
            array(0, 1, 1),
26
            array(1, 0, 1),
27
            array(1, 2, 3),
28
            array(2, 1, 3),
29
            array(25, 25, 50),
30
            array(50, 50, 100),
31
        );
32
    }
33
34
    /**
35
     * Data provider for Plus {int} + {float} = {int}
36
     *
37
     * @return array
38
     */
39
    public function testIntToFloatDataProvider()
40
    {
41
        return array(
42
            array(1, -1.5, -0.5),
43
            array(1, -1.0, 0.0),
44
            array(-1, -1.0, -2.0),
45
            array(-1, -2.55, -3.55),
46
            array(1, 1.5, 2.5),
47
            array(1, 2.5, 3.5),
48
            array(1, 4.5, 5.5),
49
            array(1, 4.75, 5.75),
50
            array(25, 24.75, 49.75)
51
        );
52
    }
53
54
    /**
55
     * Tests {int} + {int} = {int}
56
     *
57
     * @dataProvider testIntToIntDataProvider
58
     */
59
    public function testPlusIntToInt($a, $b, $c)
60
    {
61
        $this->assertInternalType('int', $a);
62
        $this->assertInternalType('int', $b);
63
        $this->assertInternalType('int', $c);
64
65
        $baseExpression = new Node\Expr\BinaryOp\Plus(
66
            new Node\Scalar\LNumber($a),
67
            new Node\Scalar\LNumber($b)
68
        );
69
        $compiledExpression = $this->compileExpression($baseExpression);
70
71
        $this->assertInstanceOfCompiledExpression($compiledExpression);
72
        $this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType());
73
        $this->assertSame($c, $compiledExpression->getValue());
74
    }
75
76
    /**
77
     * Tests {int} + {float} = {float}
78
     *
79
     * @dataProvider testIntToFloatDataProvider
80
     */
81
    public function testPlusIntToFloat($a, $b, $c)
82
    {
83
        $this->assertInternalType('int', $a);
84
        $this->assertInternalType('double', $b);
85
        $this->assertInternalType('double', $c);
86
87
        $baseExpression = new Node\Expr\BinaryOp\Plus(
88
            new Node\Scalar\LNumber($a),
89
            new Node\Scalar\DNumber($b)
90
        );
91
        $compiledExpression = $this->compileExpression($baseExpression);
92
93
        $this->assertInstanceOfCompiledExpression($compiledExpression);
94
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
95
        $this->assertSame($c, $compiledExpression->getValue());
96
    }
97
98
    /**
99
     * Tests {float} + {int} = {float}
100
     *
101
     * testPlusFloatToInt($b, $a - it's special to use already defined fixtures
102
     *
103
     * @dataProvider testIntToFloatDataProvider
104
     */
105
    public function testPlusFloatToInt($b, $a, $c)
106
    {
107
        $this->assertInternalType('double', $a);
108
        $this->assertInternalType('int', $b);
109
        $this->assertInternalType('double', $c);
110
111
        $baseExpression = new Node\Expr\BinaryOp\Plus(
112
            new Node\Scalar\DNumber($a),
113
            new Node\Scalar\LNumber($b)
114
        );
115
        $compiledExpression = $this->compileExpression($baseExpression);
116
117
        $this->assertInstanceOfCompiledExpression($compiledExpression);
118
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
119
        $this->assertSame($c, $compiledExpression->getValue());
120
    }
121
122
    /**
123
     * Tests {float} + {float} = {float}
124
     *
125
     * @dataProvider testIntToFloatDataProvider
126
     */
127
    public function testPlusFloatToFloat($a, $b, $c)
128
    {
129
        $a = (float) $a;
130
131
        $this->assertInternalType('double', $a);
132
        $this->assertInternalType('double', $b);
133
        $this->assertInternalType('double', $c);
134
135
        $baseExpression = new Node\Expr\BinaryOp\Plus(
136
            /**
137
             * float casting to use already defined fixtures (float) {int} + {float} = {float}
138
             */
139
            new Node\Scalar\DNumber($a),
140
            new Node\Scalar\DNumber($b)
141
        );
142
        $compiledExpression = $this->compileExpression($baseExpression);
143
144
        $this->assertInstanceOfCompiledExpression($compiledExpression);
145
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
146
        $this->assertSame($c, $compiledExpression->getValue());
147
    }
148
149
    /**
150
     * Tests {left-expr::UNKNOWN} + {right-expr}
151
     */
152
    public function testFirstUnexpectedTypes()
153
    {
154
        $baseExpression = new Node\Expr\BinaryOp\Plus(
155
            $this->newFakeScalarExpr(),
156
            $this->newScalarExpr(1)
157
        );
158
        $compiledExpression = $this->compileExpression($baseExpression);
159
160
        $this->assertInstanceOfCompiledExpression($compiledExpression);
161
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
162
        $this->assertSame(null, $compiledExpression->getValue());
163
    }
164
165
    /**
166
     * Tests {left-expr} + {right-expr::UNKNOWN}
167
     */
168
    public function testSecondUnexpectedTypes()
169
    {
170
        $baseExpression = new Node\Expr\BinaryOp\Plus(
171
            $this->newScalarExpr(1),
172
            $this->newFakeScalarExpr()
173
        );
174
        $compiledExpression = $this->compileExpression($baseExpression);
175
176
        $this->assertInstanceOfCompiledExpression($compiledExpression);
177
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
178
        $this->assertSame(null, $compiledExpression->getValue());
179
    }
180
}
181