-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add checks in the Either constructor
- add static method to instantiate the Either class - more tests
- Loading branch information
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
use MightFail\Either; | ||
|
||
test('class must exist', function () { | ||
expect(class_exists(Either::class))->toBeTrue(); | ||
}); | ||
|
||
test('should extend the IteratorIterator class', function () { | ||
$either = new Either('foo'); | ||
|
||
expect($either)->toBeInstanceOf(IteratorIterator::class); | ||
}); | ||
|
||
test('should implement the ArrayAccess interface', function () { | ||
$either = new Either('foo'); | ||
|
||
expect($either)->toBeInstanceOf(ArrayAccess::class); | ||
}); | ||
|
||
test('should have a result property', function () { | ||
$either = new Either('foo'); | ||
|
||
expect($either->result)->toBe('foo'); | ||
}); | ||
|
||
test('should have an error property', function () { | ||
$either = new Either(null, new Exception('foo')); | ||
|
||
expect($either->error)->toBeInstanceOf(Exception::class); | ||
}); | ||
|
||
test('should throw an error if result is an exception', function () { | ||
expect(fn () => new Either(new Exception('foo')))->toThrow(InvalidArgumentException::class, 'Result cannot be a Throwable.'); | ||
}); | ||
|
||
test('should throw an error if both result and error are null', function () { | ||
expect(fn () => new Either(null, null))->toThrow(InvalidArgumentException::class, 'Either result or error must be set.'); | ||
}); | ||
|
||
test('should have the correct methods', function () { | ||
expect(method_exists(Either::class, 'getIterator'))->toBeTrue() | ||
->and(method_exists(Either::class, 'offsetSet'))->toBeTrue() | ||
->and(method_exists(Either::class, 'offsetExists'))->toBeTrue() | ||
->and(method_exists(Either::class, 'offsetUnset'))->toBeTrue() | ||
->and(method_exists(Either::class, 'offsetGet'))->toBeTrue() | ||
->and(method_exists(Either::class, 'create'))->toBeTrue(); | ||
}); | ||
|
||
test('can be instantiated with new', function () { | ||
$either = new Either('foo'); | ||
|
||
expect($either)->toBeInstanceOf(Either::class) | ||
->and($either->result)->toBe('foo') | ||
->and($either->error)->toBeNull(); | ||
|
||
[$error, $result] = $either; | ||
|
||
expect($error)->toBeNull() | ||
->and($result)->toBe('foo'); | ||
|
||
$either = new Either(null, new Exception('foo')); | ||
|
||
expect($either)->toBeInstanceOf(Either::class) | ||
->and($either->result)->toBeNull() | ||
->and($either->error)->toBeInstanceOf(Exception::class); | ||
|
||
[$error, $result] = $either; | ||
|
||
expect($error)->toBeInstanceOf(Exception::class) | ||
->and($result)->toBeNull(); | ||
}); | ||
|
||
test('can be instantiated with from', function () { | ||
$either = Either::create('foo'); | ||
|
||
expect($either)->toBeInstanceOf(Either::class) | ||
->and($either->result)->toBe('foo') | ||
->and($either->error)->toBeNull(); | ||
|
||
[$error, $result] = $either; | ||
|
||
expect($error)->toBeNull() | ||
->and($result)->toBe('foo'); | ||
|
||
$either = Either::create(null, new Exception('foo')); | ||
|
||
expect($either)->toBeInstanceOf(Either::class) | ||
->and($either->result)->toBeNull() | ||
->and($either->error)->toBeInstanceOf(Exception::class); | ||
|
||
[$error, $result] = $either; | ||
|
||
expect($error)->toBeInstanceOf(Exception::class) | ||
->and($result)->toBeNull(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use MightFail\Either; | ||
use MightFail\Fail; | ||
use MightFail\Might; | ||
|
||
test('class must exist and have a static from method', function () { | ||
expect(class_exists(Fail::class))->toBeTrue() | ||
->and(method_exists(Fail::class, 'from'))->toBeTrue(); | ||
}); | ||
|
||
test('creates a Fail instance', function () { | ||
$might = Fail::from(new Exception('foo')); | ||
|
||
expect($might)->toBeInstanceOf(Fail::class); | ||
}); | ||
|
||
test('extends from the Either class', function () { | ||
$might = Fail::from(new Exception('foo')); | ||
|
||
expect($might)->toBeInstanceOf(Either::class) | ||
->and($might)->toBeInstanceOf(Fail::class) | ||
->and($might)->not->toBeInstanceOf(Might::class); | ||
}); | ||
|
||
test('has Either properties', function () { | ||
$might = Fail::from(new Exception('foo')); | ||
|
||
expect($might->result)->toBeNull() | ||
->and($might->error)->toBeInstanceOf(Exception::class); | ||
|
||
[$error, $result] = $might; | ||
|
||
expect($error)->toBeInstanceOf(Exception::class) | ||
->and($result)->toBeNull(); | ||
}); | ||
|
||
test('can instantiate with new', function () { | ||
$might = new Fail(new Exception('foo')); | ||
|
||
expect($might)->toBeInstanceOf(Fail::class) | ||
->and($might->result)->toBeNull() | ||
->and($might->error)->toBeInstanceOf(Exception::class); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use MightFail\Either; | ||
use MightFail\Fail; | ||
use MightFail\Might; | ||
|
||
test('class must exist and have a static from method', function () { | ||
expect(class_exists(Might::class))->toBeTrue() | ||
->and(method_exists(Might::class, 'from'))->toBeTrue(); | ||
}); | ||
|
||
test('creates a Might instance', function () { | ||
$might = Might::from('foo'); | ||
|
||
expect($might)->toBeInstanceOf(Might::class); | ||
}); | ||
|
||
test('extends from the Either class', function () { | ||
$might = Might::from('foo'); | ||
|
||
expect($might)->toBeInstanceOf(Either::class) | ||
->and($might)->toBeInstanceOf(Might::class) | ||
->and($might)->not->toBeInstanceOf(Fail::class); | ||
}); | ||
|
||
test('has Either properties', function () { | ||
$might = Might::from('foo'); | ||
|
||
expect($might->result)->toBe('foo') | ||
->and($might->error)->toBeNull(); | ||
|
||
[$error, $result] = $might; | ||
|
||
expect($error)->toBeNull() | ||
->and($result)->toBe('foo'); | ||
}); | ||
|
||
test('can instantiate with new', function () { | ||
$might = new Might('foo'); | ||
|
||
expect($might)->toBeInstanceOf(Might::class) | ||
->and($might->result)->toBe('foo') | ||
->and($might->error)->toBeNull(); | ||
}); |