|
4 | 4 |
|
5 | 5 | use PhpTypedValues\Exception\IntegerTypeException; |
6 | 6 | use PhpTypedValues\Integer\MariaDb\IntTiny; |
| 7 | +use PhpTypedValues\Undefined\Alias\Undefined; |
7 | 8 |
|
8 | 9 | it('accepts values within signed tinyint range and preserves value', function (): void { |
9 | 10 | $a = new IntTiny(-128); |
|
38 | 39 | expect(fn() => IntTiny::fromString('12.3')) |
39 | 40 | ->toThrow(IntegerTypeException::class, 'String "12.3" has no valid strict integer value'); |
40 | 41 | }); |
| 42 | + |
| 43 | +it('IntTiny::tryFromString returns value within -128..127', function (): void { |
| 44 | + $vMin = IntTiny::tryFromString('-128'); |
| 45 | + $v0 = IntTiny::tryFromString('0'); |
| 46 | + $vMax = IntTiny::tryFromString('127'); |
| 47 | + |
| 48 | + expect($vMin) |
| 49 | + ->toBeInstanceOf(IntTiny::class) |
| 50 | + ->and($vMin->value())->toBe(-128) |
| 51 | + ->and($v0) |
| 52 | + ->toBeInstanceOf(IntTiny::class) |
| 53 | + ->and($v0->value())->toBe(0) |
| 54 | + ->and($vMax) |
| 55 | + ->toBeInstanceOf(IntTiny::class) |
| 56 | + ->and($vMax->value())->toBe(127); |
| 57 | +}); |
| 58 | + |
| 59 | +it('IntTiny::tryFromString returns Undefined outside range and for non-integer strings', function (): void { |
| 60 | + expect(IntTiny::tryFromString('128')) |
| 61 | + ->toBeInstanceOf(Undefined::class) |
| 62 | + ->and(IntTiny::tryFromString('5.0')) |
| 63 | + ->toBeInstanceOf(Undefined::class); |
| 64 | +}); |
| 65 | + |
| 66 | +it('IntTiny::tryFromInt returns value within range and Undefined otherwise', function (): void { |
| 67 | + $ok = IntTiny::tryFromInt(-5); |
| 68 | + $bad = IntTiny::tryFromInt(200); |
| 69 | + |
| 70 | + expect($ok) |
| 71 | + ->toBeInstanceOf(IntTiny::class) |
| 72 | + ->and($ok->value())->toBe(-5) |
| 73 | + ->and($bad) |
| 74 | + ->toBeInstanceOf(Undefined::class); |
| 75 | +}); |
0 commit comments