Generic test to verify if a type obey the monad laws.
public static test ( callable $assertEqual, callable $return, callable $f, callable $g, mixed $x ) | ||
$assertEqual | callable | Asserting function (Monad $m1, Monad $m2, $message) |
$return | callable | Monad "constructor" |
$f | callable | Monadic function |
$g | callable | Monadic function |
$x | mixed | Value to put into a monad |
/** * @dataProvider provideData */ public function test_if_io_monad_obeys_the_laws($f, $g, $x) { MonadLaws::test(function (IO $f, IO $g, $message) { $this->assertEquals($f->run(), $g->run(), $message); }, function ($x) { return IO::of(function () use($x) { return $x; }); }, $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_identity_monad_obeys_the_laws($f, $g, $x) { MonadLaws::test(f\curryN(3, [$this, 'assertEquals']), f\curryN(1, Identity::of), $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_writer_monad_obeys_the_laws($f, $g, $x) { MonadLaws::test(function (Writer $a, Writer $b, $message) { $this->assertEquals($a->runWriter(), $b->runWriter(), $message); }, Writer\pure, $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_maybe_monad_obeys_the_laws($return, $f, $g, $x) { MonadLaws::test(f\curryN(3, [$this, 'assertEquals']), f\curryN(1, $return), $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_reader_monad_obeys_the_laws($f, $g, $x, $env) { MonadLaws::test(function (Reader $a, Reader $b, $message) use($env) { $this->assertEquals($a->runReader($env), $b->runReader($env), $message); }, Reader\value, $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_state_monad_obeys_the_laws($f, $g, $x, $state) { MonadLaws::test(function (State $a, State $b, $message) use($state) { $this->assertEquals($a->runState($state), $b->runState($state), $message); }, State\value, $f, $g, $x); }
/** * @dataProvider provideData */ public function test_if_list_obeys_the_laws($f, $g, $x) { MonadLaws::test(f\curryN(3, [$this, 'assertEquals']), f\curryN(1, Listt::of), $f, $g, $x); }