3
declare(strict_types=1);
4
namespace Upside\Std\Test\Option;
6
use PHPUnit\Framework\TestCase;
7
use function Upside\Std\From;
8
use function Upside\Std\None;
9
use function Upside\Std\Some;
11
//TODO: Добавить больше кейсов проверки например с разными типами данных
12
class OptionTest extends TestCase
14
public function test_from(): void
19
$none = From($none_data);
20
$some = From($some_data);
22
$this->assertTrue($none->is_none());
23
$this->assertFalse($none->is_some());
24
$this->assertTrue($some->is_some());
25
$this->assertFalse($some->is_none());
26
$this->assertEquals($some_data, $some->unwrap());
29
public function test_some(): void
32
$some = Some($some_data);
34
$this->assertFalse($some->is_none());
35
$this->assertTrue($some->is_some());
36
$this->assertEquals($some_data, $some->unwrap());
39
public function test_none(): void
43
$this->assertFalse($none->is_some());
44
$this->assertTrue($none->is_none());
47
public function test_is_some(): void
52
$this->assertTrue($some->is_some());
53
$this->assertFalse($none->is_some());
56
public function test_is_none(): void
61
$this->assertFalse($some->is_none());
62
$this->assertTrue($none->is_none());
65
public function test_unwrap(): void
68
$some = Some($some_data);
70
$this->assertEquals($some_data, $some->unwrap());
73
public function test_unwrap_exception(): void
75
$this->expectException(\RuntimeException::class);
76
$this->expectExceptionMessage('The option is none');
82
public function test_unwrap_or(): void
85
$some = Some($some_data);
88
$this->assertEquals($some_data, $some->unwrap_or('default'));
89
$this->assertEquals('default', $none->unwrap_or('default'));
92
public function test_unwrap_or_callback(): void
95
$callback_data = 'callback';
96
$callback = fn() => $callback_data;
98
$some = Some($some_data);
101
$this->assertEquals($some_data, $some->unwrap_or_else($callback));
102
$this->assertEquals($callback_data, $none->unwrap_or_else($callback));
105
public function test_expect(): void
109
$this->expectException(\RuntimeException::class);
110
$this->expectExceptionMessage($msg);
116
public function test_expect_or(): void
120
$this->expectException(\LogicException::class);
121
$this->expectExceptionMessage($msg);
124
$none->expect_or(new \LogicException($msg));