nur-ture/tests/wip/schema/_scalar/ScalarValueTest.php

252 lines
9.5 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 = "text"; $value->reset($string, null, false);
$this->checkValue($value, "text", true, true, true, true);
$string = "text"; $value->reset($string);
$this->checkValue($value, "text", true, true, true, true);
$string = " text "; $value->reset($string, null, false);
$this->checkValue($value, " text ", true, true, true, true);
$string = " text "; $value->reset($string);
$this->checkValue($value, " text ", 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 = 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 = 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 = "text"; $value->reset($string, null, false);
$this->checkValue($value, "text", true, true, true, true);
$string = "text"; $value->reset($string);
$this->checkValue($value, "text", true, true, true, true);
$string = " text "; $value->reset($string, null, false);
$this->checkValue($value, " text ", true, true, true, false);
$string = " text "; $value->reset($string);
$this->checkValue($value, "text", 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 = 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 = 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);
});
}
function testInt() {
$schema = new ScalarSchema("int");
$value = $schema->newValue();
$int = null; $value->reset($int, null, false);
$this->checkValue($value, null, true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = null;
$value->reset($int);
});
$int = 42; $value->reset($int, null, false);
$this->checkValue($value, 42, true, true, true, true);
$int = 42; $value->reset($int);
$this->checkValue($value, 42, true, true, true, true);
$int = "42"; $value->reset($int, null, false);
$this->checkValue($value, "42", true, true, true, false);
$int = "42"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, true);
$int = "42.5"; $value->reset($int, null, false);
$this->checkValue($value, "42.5", true, true, true, false);
$int = "42.5"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, true);
$int = "42,5"; $value->reset($int, null, false);
$this->checkValue($value, "42,5", true, true, true, false);
$int = "42,5"; $value->reset($int);
$this->checkValue($value, 42, true, true, true, true);
$int = ""; $value->reset($int, null, false);
$this->checkValue($value, "", true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = "";
$value->reset($int);
});
$int = " "; $value->reset($int, null, false);
$this->checkValue($value, " ", true, true, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$int = " ";
$value->reset($int);
});
$int = "text"; $value->reset($int, null, false);
$this->checkValue($value, "text", true, true, false, true);
self::assertException(ValueException::class, function() use (&$value) {
$int = "text";
$value->reset($int);
});
$int = false; $value->reset($int, null, false);
$this->checkValue($value, null, true, false, true, true);
$int = false; $value->reset($int);
$this->checkValue($value, null, true, false, true, true);
$int = true; $value->reset($int, null, false);
$this->checkValue($value, true, true, true, true, false);
$int = true; $value->reset($int);
$this->checkValue($value, 1, true, true, true, true);
$int = []; $value->reset($int, 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("?int");
$value = $schema->newValue();
$int = null; $value->reset($int, null, false);
$this->checkValue($value, null, true, true, true, true);
$int = null; $value->reset($int, null);
$this->checkValue($value, null, true, true, true, true);
## Tester required
$schema = new ScalarSchema(["int", "required" => true]);
$value = $schema->newValue();
$int = false; $value->reset($int, null, false);
$this->checkValue($value, null, true, false, false, false);
self::assertException(ValueException::class, function() use (&$value) {
$string = false; $value->reset($string);
});
}
}