|
26 | 26 | ->and(BoolStandard::fromString('TrUe')->toString())->toBe('true'); |
27 | 27 | }); |
28 | 28 |
|
| 29 | +it('parses extended true/false string aliases', function (): void { |
| 30 | + // true-like |
| 31 | + expect(BoolStandard::fromString('1')->value())->toBeTrue() |
| 32 | + ->and(BoolStandard::fromString('yes')->value())->toBeTrue() |
| 33 | + ->and(BoolStandard::fromString('on')->value())->toBeTrue() |
| 34 | + ->and(BoolStandard::fromString('Y')->value())->toBeTrue() |
| 35 | + // false-like |
| 36 | + ->and(BoolStandard::fromString('0')->value())->toBeFalse() |
| 37 | + ->and(BoolStandard::fromString('no')->value())->toBeFalse() |
| 38 | + ->and(BoolStandard::fromString('off')->value())->toBeFalse() |
| 39 | + ->and(BoolStandard::fromString('N')->value())->toBeFalse(); |
| 40 | +}); |
| 41 | + |
29 | 42 | it('throws on invalid string values', function (): void { |
30 | | - expect(fn() => BoolStandard::fromString('yes')) |
31 | | - ->toThrow(BoolTypeException::class, 'Expected string "true"\"1" or "false"\"0", got "yes"'); |
| 43 | + expect(fn() => BoolStandard::fromString('yes1')) |
| 44 | + ->toThrow(BoolTypeException::class, 'Expected string "true" or "false", got "yes1"'); |
32 | 45 | }); |
33 | 46 |
|
34 | 47 | it('parses valid integer values 1/0', function (): void { |
|
42 | 55 | expect(fn() => BoolStandard::fromInt(-1)) |
43 | 56 | ->toThrow(BoolTypeException::class, 'Expected int "1" or "0", got "-1"'); |
44 | 57 | }); |
| 58 | + |
| 59 | +it('tryFromString returns Undefined on invalid input and BoolStandard on valid', function (): void { |
| 60 | + $ok = BoolStandard::tryFromString('true'); |
| 61 | + $fail = BoolStandard::tryFromString('maybe'); |
| 62 | + |
| 63 | + expect($ok)->toBeInstanceOf(BoolStandard::class) |
| 64 | + ->and($ok->value())->toBeTrue(); |
| 65 | + |
| 66 | + // Undefined type instance indicates failure without throwing |
| 67 | + expect($fail::class)->toBe(PhpTypedValues\Undefined\UndefinedStandard::class); |
| 68 | +}); |
| 69 | + |
| 70 | +it('tryFromInt returns Undefined on invalid input and BoolStandard on valid', function (): void { |
| 71 | + $one = BoolStandard::tryFromInt(1); |
| 72 | + $zero = BoolStandard::tryFromInt(0); |
| 73 | + $bad = BoolStandard::tryFromInt(3); |
| 74 | + |
| 75 | + expect($one)->toBeInstanceOf(BoolStandard::class) |
| 76 | + ->and($one->value())->toBeTrue() |
| 77 | + ->and($zero)->toBeInstanceOf(BoolStandard::class) |
| 78 | + ->and($zero->value())->toBeFalse() |
| 79 | + ->and($bad::class)->toBe(PhpTypedValues\Undefined\UndefinedStandard::class); |
| 80 | +}); |
| 81 | + |
| 82 | +it('parses string values with surrounding whitespace', function (): void { |
| 83 | + expect(BoolStandard::fromString(' true ')->value())->toBeTrue() |
| 84 | + ->and(BoolStandard::fromString("\tFALSE \n")->value())->toBeFalse() |
| 85 | + ->and(BoolStandard::fromString(' on ')->value())->toBeTrue() |
| 86 | + ->and(BoolStandard::fromString(' off ')->value())->toBeFalse(); |
| 87 | +}); |
0 commit comments