-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Undefined values are decoded with the specified default value (#15)
* Implementation of the codec laws as phpunit assertions
- Loading branch information
1 parent
0d88c71
commit 4a12b5a
Showing
17 changed files
with
458 additions
and
27 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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Facile\PhpCodec; | ||
|
||
use Facile\PhpCodec\Internal\Primitives\UndefinedDecoder; | ||
|
||
final class Decoders | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @template U | ||
* | ||
* @param U $default | ||
* | ||
* @return Decoder<mixed, U> | ||
*/ | ||
public static function undefined($default = null): Decoder | ||
{ | ||
return new UndefinedDecoder($default); | ||
} | ||
} |
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Facile\PhpCodec\Internal; | ||
|
||
use Facile\PhpCodec\Encoder; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @implements Encoder<T, T> | ||
*/ | ||
final class IdentityEncoder implements Encoder | ||
{ | ||
public function encode($a) | ||
{ | ||
return $a; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Facile\PhpCodec\Internal\Primitives; | ||
|
||
use Facile\PhpCodec\Decoder; | ||
use function Facile\PhpCodec\Internal\standardDecode; | ||
use Facile\PhpCodec\Internal\Undefined; | ||
use Facile\PhpCodec\Validation\Context; | ||
use Facile\PhpCodec\Validation\Validation; | ||
|
||
/** | ||
* @template U | ||
* @implements Decoder<mixed, U> | ||
*/ | ||
class UndefinedDecoder implements Decoder | ||
{ | ||
/** @var U */ | ||
private $default; | ||
|
||
/** | ||
* @param U $default | ||
*/ | ||
public function __construct($default) | ||
{ | ||
$this->default = $default; | ||
} | ||
|
||
public function validate($i, Context $context): Validation | ||
{ | ||
return $i instanceof Undefined | ||
? Validation::success($this->default) | ||
: Validation::failure($i, $context); | ||
} | ||
|
||
public function decode($i): Validation | ||
{ | ||
return standardDecode($this, $i); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'undefined'; | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Examples\Facile\PhpCodec; | ||
|
||
use Facile\PhpCodec\Codecs; | ||
use Tests\Facile\PhpCodec\BaseTestCase; | ||
|
||
class DecodePartialPropertiesTest extends BaseTestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$c = Codecs::classFromArray( | ||
[ | ||
'foo' => Codecs::string(), | ||
'bar' => Codecs::union(Codecs::int(), Codecs::undefined(-1)), | ||
], | ||
function (string $foo, int $bar): DecodePartialPropertiesTest\A { | ||
return new DecodePartialPropertiesTest\A($foo, $bar); | ||
}, | ||
DecodePartialPropertiesTest\A::class | ||
); | ||
|
||
self::asserSuccessInstanceOf( | ||
DecodePartialPropertiesTest\A::class, | ||
$c->decode(['foo' => 'str']), | ||
function (DecodePartialPropertiesTest\A $a): void { | ||
self::assertSame('str', $a->getFoo()); | ||
self::assertSame(-1, $a->getBar()); | ||
} | ||
); | ||
} | ||
} | ||
|
||
namespace Examples\Facile\PhpCodec\DecodePartialPropertiesTest; | ||
|
||
class A | ||
{ | ||
/** @var string */ | ||
private $foo; | ||
/** @var int */ | ||
private $bar; | ||
|
||
public function __construct( | ||
string $foo, | ||
int $bar | ||
) { | ||
$this->foo = $foo; | ||
$this->bar = $bar; | ||
} | ||
|
||
public function getFoo(): string | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
public function getBar(): int | ||
{ | ||
return $this->bar; | ||
} | ||
} |
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Facile\PhpCodec; | ||
|
||
use Eris\Generator as g; | ||
|
||
final class GeneratorUtils | ||
{ | ||
public static function scalar(): g | ||
{ | ||
return g\oneOf( | ||
g\int(), | ||
g\float(), | ||
g\bool(), | ||
g\string() | ||
); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Facile\PhpCodec\Internal\Combinators; | ||
|
||
use Eris\Generator as g; | ||
use Eris\TestTrait; | ||
use Facile\PhpCodec\Codec; | ||
use function Facile\PhpCodec\destructureIn; | ||
use Facile\PhpCodec\Internal\Combinators\LiteralType; | ||
use Tests\Facile\PhpCodec\BaseTestCase; | ||
use Tests\Facile\PhpCodec\GeneratorUtils; | ||
|
||
class LiteralTypeTest extends BaseTestCase | ||
{ | ||
use TestTrait; | ||
|
||
public function testLaws(): void | ||
{ | ||
$this | ||
->forAll( | ||
g\bind( | ||
g\oneOf( | ||
g\int(), | ||
g\string(), | ||
g\bool() | ||
), | ||
function ($literal): g { | ||
return g\tuple( | ||
g\constant(new LiteralType($literal)), | ||
g\oneOf( | ||
GeneratorUtils::scalar(), | ||
g\constant($literal) | ||
), | ||
g\constant($literal) | ||
); | ||
} | ||
) | ||
) | ||
->then(destructureIn(function (Codec $codec, $u, $a): void { | ||
self::codecLaws($codec)($u, $a); | ||
})); | ||
} | ||
} |
Oops, something went wrong.