163 lines
6.3 KiB
PHP
163 lines
6.3 KiB
PHP
<?php
|
|
namespace nur\sery\wip\schema\_scalar;
|
|
|
|
use nulib\tests\TestCase;
|
|
use nulib\ValueException;
|
|
|
|
class ScalarValueTest extends TestCase {
|
|
function checkValue(ScalarValue $value, $dest, bool $present, bool $available, bool $valid, bool $normalized): void {
|
|
self::assertSame($dest, $value->get(), "value");
|
|
self::assertSame($present, $value->isPresent(), "present");
|
|
self::assertSame($available, $value->isAvailable(), "available");
|
|
self::assertSame($valid, $value->isValid(), "valid");
|
|
self::assertSame($normalized, $value->isNormalized(), "normalized");
|
|
}
|
|
|
|
function testRawstring() {
|
|
$schema = new ScalarSchema("rawstring");
|
|
$value = $schema->newValue();
|
|
|
|
$string = null;
|
|
$value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, true, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = null;
|
|
$value->reset($string);
|
|
});
|
|
|
|
$string = ""; $value->reset($string, null, false);
|
|
$this->checkValue($value, "", true, true, true, true);
|
|
$string = ""; $value->reset($string);
|
|
$this->checkValue($value, "", true, true, true, true);
|
|
|
|
$string = " "; $value->reset($string, null, false);
|
|
$this->checkValue($value, " ", true, true, true, true);
|
|
$string = " "; $value->reset($string);
|
|
$this->checkValue($value, " ", true, true, true, true);
|
|
|
|
$string = "value"; $value->reset($string, null, false);
|
|
$this->checkValue($value, "value", true, true, true, true);
|
|
$string = "value"; $value->reset($string);
|
|
$this->checkValue($value, "value", true, true, true, true);
|
|
|
|
$string = " value "; $value->reset($string, null, false);
|
|
$this->checkValue($value, " value ", true, true, true, true);
|
|
$string = " value "; $value->reset($string);
|
|
$this->checkValue($value, " value ", true, true, true, true);
|
|
|
|
$string = true; $value->reset($string, null, false);
|
|
$this->checkValue($value, true, true, true, true, false);
|
|
$string = true; $value->reset($string);
|
|
$this->checkValue($value, "1", true, true, true, true);
|
|
|
|
$string = false; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, false, true, true);
|
|
$string = false; $value->reset($string);
|
|
$this->checkValue($value, null, true, false, true, true);
|
|
|
|
$string = 42; $value->reset($string, null, false);
|
|
$this->checkValue($value, 42, true, true, true, false);
|
|
$string = 42; $value->reset($string);
|
|
$this->checkValue($value, "42", true, true, true, true);
|
|
|
|
$string = []; $value->reset($string, null, false);
|
|
$this->checkValue($value, [], true, true, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = null;
|
|
$value->reset($string);
|
|
});
|
|
|
|
## Tester nullable
|
|
$schema = new ScalarSchema("?rawstring");
|
|
$value = $schema->newValue();
|
|
|
|
$string = null; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, true, true, true);
|
|
$string = null; $value->reset($string, null);
|
|
$this->checkValue($value, null, true, true, true, true);
|
|
|
|
## Tester required
|
|
$schema = new ScalarSchema(["rawstring", "required" => true]);
|
|
$value = $schema->newValue();
|
|
|
|
$string = false; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, false, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = false; $value->reset($string);
|
|
});
|
|
}
|
|
|
|
function testString() {
|
|
$schema = new ScalarSchema("string");
|
|
$value = $schema->newValue();
|
|
|
|
$string = null; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, true, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = null;
|
|
$value->reset($string);
|
|
});
|
|
|
|
$string = ""; $value->reset($string, null, false);
|
|
$this->checkValue($value, "", true, true, true, true);
|
|
$string = ""; $value->reset($string);
|
|
$this->checkValue($value, "", true, true, true, true);
|
|
|
|
$string = " "; $value->reset($string, null, false);
|
|
$this->checkValue($value, " ", true, true, true, false);
|
|
$string = " "; $value->reset($string);
|
|
$this->checkValue($value, "", true, true, true, true);
|
|
|
|
$string = "value"; $value->reset($string, null, false);
|
|
$this->checkValue($value, "value", true, true, true, true);
|
|
$string = "value"; $value->reset($string);
|
|
$this->checkValue($value, "value", true, true, true, true);
|
|
|
|
$string = " value "; $value->reset($string, null, false);
|
|
$this->checkValue($value, " value ", true, true, true, false);
|
|
$string = " value "; $value->reset($string);
|
|
$this->checkValue($value, "value", true, true, true, true);
|
|
|
|
$string = true; $value->reset($string, null, false);
|
|
$this->checkValue($value, true, true, true, true, false);
|
|
$string = true; $value->reset($string);
|
|
$this->checkValue($value, "1", true, true, true, true);
|
|
|
|
$string = false; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, false, true, true);
|
|
$string = false; $value->reset($string);
|
|
$this->checkValue($value, null, true, false, true, true);
|
|
|
|
$string = 42; $value->reset($string, null, false);
|
|
$this->checkValue($value, 42, true, true, true, false);
|
|
$string = 42; $value->reset($string);
|
|
$this->checkValue($value, "42", true, true, true, true);
|
|
|
|
$string = []; $value->reset($string, null, false);
|
|
$this->checkValue($value, [], true, true, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = null;
|
|
$value->reset($string);
|
|
});
|
|
|
|
## Tester nullable
|
|
$schema = new ScalarSchema("?string");
|
|
$value = $schema->newValue();
|
|
|
|
$string = null; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, true, true, true);
|
|
$string = null; $value->reset($string, null);
|
|
$this->checkValue($value, null, true, true, true, true);
|
|
|
|
## Tester required
|
|
$schema = new ScalarSchema(["string", "required" => true]);
|
|
$value = $schema->newValue();
|
|
|
|
$string = false; $value->reset($string, null, false);
|
|
$this->checkValue($value, null, true, false, false, false);
|
|
self::assertException(ValueException::class, function() use (&$value) {
|
|
$string = false; $value->reset($string);
|
|
});
|
|
}
|
|
}
|