<?php
namespace nur\data\types;

use nur\b\ValueException;
use nur\t\TestCase;

class StringTypeTest extends TestCase {
  function testAllowedValues() {
    $t = new StringType(["allowed_values" => ["first", "second"]]);

    self::assertTrue($t->isInstance(null));
    self::assertFalse($t->isInstance(false));
    self::assertFalse($t->isInstance(true));
    self::assertTrue($t->isInstance("first"));
    self::assertTrue($t->isInstance("second"));
    self::assertFalse($t->isInstance("third"));

    self::assertNull($t->with(null));
    self::assertFalse($t->with(false));
    self::assertException(ValueException::class, [$t, "with"], true);
    self::assertSame("first", $t->with("  first  "));
    self::assertSame("second", $t->with("  second  "));
    self::assertException(ValueException::class, [$t, "with"], "  third  ");
  }

  function testTrim() {
    $t = new StringType();

    self::assertSame("hello  \n  world", $t->with("  hello  \r\n  world  "));
    self::assertSame("a\nb\nc", $t->with("\ra\rb\r\nc\r\n"));
  }
}
