Skip to content

Commit be5d6da

Browse files
committed
Simplify comparison of Int64
1 parent 69ed0a8 commit be5d6da

File tree

2 files changed

+91
-43
lines changed

2 files changed

+91
-43
lines changed

tests/Comparator/Int64Comparator.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,21 @@
66
use SebastianBergmann\Comparator\Comparator;
77
use SebastianBergmann\Comparator\ComparisonFailure;
88

9-
use function is_int;
109
use function is_numeric;
11-
use function is_string;
1210
use function sprintf;
1311

14-
use const PHP_INT_SIZE;
15-
1612
class Int64Comparator extends Comparator
1713
{
1814
public function accepts($expected, $actual)
1915
{
20-
// Only compare if either value is an Int64
16+
// Only compare if either value is an Int64 and the other value is numeric
2117
return ($expected instanceof Int64 && $this->isComparable($actual))
2218
|| ($actual instanceof Int64 && $this->isComparable($expected));
2319
}
2420

2521
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
2622
{
27-
if (PHP_INT_SIZE == 8) {
28-
// On 64-bit systems, compare integers directly
29-
$expectedValue = (int) $expected;
30-
$actualValue = (int) $actual;
31-
} else {
32-
// On 32-bit systems, compare integers as strings
33-
$expectedValue = (string) $expected;
34-
$actualValue = (string) $actual;
35-
}
36-
37-
if ($expectedValue === $actualValue) {
23+
if ($expected == $actual) {
3824
return;
3925
}
4026

@@ -54,8 +40,6 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f
5440

5541
private function isComparable($value): bool
5642
{
57-
return $value instanceof Int64 // Int64 instances
58-
|| is_int($value) // Integer values
59-
|| (is_string($value) && is_numeric($value)); // Numeric strings (is_numeric accepts floats)
43+
return $value instanceof Int64 || is_numeric($value);
6044
}
6145
}

tests/Comparator/Int64ComparatorTest.php

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,67 @@ public static function provideAcceptsValues(): Generator
2929
'actualValue' => 123,
3030
];
3131

32-
yield 'Expects Int64, Actual string' => [
32+
yield 'Expects Int64, Actual int string' => [
3333
'expectedResult' => true,
3434
'expectedValue' => new Int64(123),
3535
'actualValue' => '123',
3636
];
3737

3838
yield 'Expects Int64, Actual float' => [
39-
'expectedResult' => false,
39+
'expectedResult' => true,
4040
'expectedValue' => new Int64(123),
4141
'actualValue' => 123.0,
4242
];
4343

44+
yield 'Expects Int64, Actual float string' => [
45+
'expectedResult' => true,
46+
'expectedValue' => new Int64(123),
47+
'actualValue' => '123.0',
48+
];
49+
50+
yield 'Expects Int64, Actual non-numeric string' => [
51+
'expectedResult' => false,
52+
'expectedValue' => new Int64(123),
53+
'actualValue' => 'foo',
54+
];
55+
4456
yield 'Expects int, Actual Int64' => [
4557
'expectedResult' => true,
4658
'expectedValue' => 123,
4759
'actualValue' => new Int64(123),
4860
];
4961

50-
yield 'Expects string, Actual Int64' => [
62+
yield 'Expects int string, Actual Int64' => [
5163
'expectedResult' => true,
5264
'expectedValue' => '123',
5365
'actualValue' => new Int64(123),
5466
];
5567

5668
yield 'Expects float, Actual Int64' => [
57-
'expectedResult' => false,
69+
'expectedResult' => true,
5870
'expectedValue' => 123.0,
5971
'actualValue' => new Int64(123),
6072
];
6173

74+
yield 'Expects float string, Actual Int64' => [
75+
'expectedResult' => true,
76+
'expectedValue' => '123.0',
77+
'actualValue' => new Int64(123),
78+
];
79+
80+
yield 'Expects non-numeric string, Actual Int64' => [
81+
'expectedResult' => false,
82+
'expectedValue' => 'foo',
83+
'actualValue' => new Int64(123),
84+
];
85+
6286
yield 'Expects float, Actual Float' => [
6387
'expectedResult' => false,
6488
'expectedValue' => 123.0,
6589
'actualValue' => 123.0,
6690
];
6791

68-
yield 'Expects string, Actual string' => [
92+
yield 'Expects numeric string, Actual numeric string' => [
6993
'expectedResult' => false,
7094
'expectedValue' => '123',
7195
'actualValue' => '123',
@@ -84,28 +108,48 @@ public function testMatchingAssertions($expected, $actual): void
84108
public static function provideMatchingAssertions(): Generator
85109
{
86110
yield 'Expected Int64, Actual Int64' => [
87-
'expected' => new Int64(123),
88-
'actual' => new Int64(123),
111+
'expected' => new Int64(8589934592),
112+
'actual' => new Int64(8589934592),
89113
];
90114

91115
yield 'Expected Int64, Actual int' => [
92-
'expected' => new Int64(123),
93-
'actual' => 123,
116+
'expected' => new Int64(8589934592),
117+
'actual' => 8589934592,
118+
];
119+
120+
yield 'Expected Int64, Actual int string' => [
121+
'expected' => new Int64(8589934592),
122+
'actual' => '8589934592',
123+
];
124+
125+
yield 'Expected Int64, Actual float' => [
126+
'expected' => new Int64(8589934592),
127+
'actual' => 8589934592.0,
94128
];
95129

96-
yield 'Expected Int64, Actual string' => [
97-
'expected' => new Int64(123),
98-
'actual' => '123',
130+
yield 'Expected Int64, Actual float string' => [
131+
'expected' => new Int64(8589934592),
132+
'actual' => '8589934592.0',
99133
];
100134

101135
yield 'Expected int, Actual Int64' => [
102-
'expected' => 123,
103-
'actual' => new Int64(123),
136+
'expected' => 8589934592,
137+
'actual' => new Int64(8589934592),
104138
];
105139

106-
yield 'Expected string, Actual Int64' => [
107-
'expected' => '123',
108-
'actual' => new Int64(123),
140+
yield 'Expected int string, Actual Int64' => [
141+
'expected' => '8589934592',
142+
'actual' => new Int64(8589934592),
143+
];
144+
145+
yield 'Expected float, Actual Int64' => [
146+
'expected' => 8589934592.0,
147+
'actual' => new Int64(8589934592),
148+
];
149+
150+
yield 'Expected float string, Actual Int64' => [
151+
'expected' => '8589934592.0',
152+
'actual' => new Int64(8589934592),
109153
];
110154
}
111155

@@ -120,27 +164,47 @@ public function testFailingAssertions($expected, $actual): void
120164
public static function provideFailingValues(): Generator
121165
{
122166
yield 'Expected Int64, Actual Int64' => [
123-
'expected' => new Int64(123),
167+
'expected' => new Int64(8589934592),
124168
'actual' => new Int64(456),
125169
];
126170

127171
yield 'Expected Int64, Actual int' => [
128-
'expected' => new Int64(123),
172+
'expected' => new Int64(8589934592),
129173
'actual' => 456,
130174
];
131175

132-
yield 'Expected Int64, Actual string' => [
133-
'expected' => new Int64(123),
176+
yield 'Expected Int64, Actual int string' => [
177+
'expected' => new Int64(8589934592),
134178
'actual' => '456',
135179
];
136180

181+
yield 'Expected Int64, Actual float' => [
182+
'expected' => new Int64(8589934592),
183+
'actual' => 8589934592.1,
184+
];
185+
186+
yield 'Expected Int64, Actual float string' => [
187+
'expected' => new Int64(8589934592),
188+
'actual' => '8589934592.1',
189+
];
190+
137191
yield 'Expected int, Actual Int64' => [
138-
'expected' => 123,
192+
'expected' => 8589934592,
193+
'actual' => new Int64(456),
194+
];
195+
196+
yield 'Expected int string, Actual Int64' => [
197+
'expected' => '8589934592',
198+
'actual' => new Int64(456),
199+
];
200+
201+
yield 'Expected float, Actual Int64' => [
202+
'expected' => 8589934592.1,
139203
'actual' => new Int64(456),
140204
];
141205

142-
yield 'Expected string, Actual Int64' => [
143-
'expected' => '123',
206+
yield 'Expected float string, Actual Int64' => [
207+
'expected' => '8589934592.1',
144208
'actual' => new Int64(456),
145209
];
146210
}

0 commit comments

Comments
 (0)