<?php
namespace nur\data\types;

use nur\t\TestCase;

class TriboolTypeTest extends TestCase {
  function testFormat() {
    $t = new TriboolType();
    self::assertSame("Oui", $t->format(true));
    self::assertSame("Non", $t->format(false));
    self::assertSame("", $t->format(null));

    $t = new TriboolType(["format" => "on"]);
    self::assertSame("O", $t->format(true));
    self::assertSame("N", $t->format(false));
    self::assertSame("N", $t->format(null));

    $t = new TriboolType(["format" => "onn"]);
    self::assertSame("O", $t->format(true));
    self::assertSame("N", $t->format(false));
    self::assertSame("", $t->format(null));

    $t = new TriboolType(["format" => "ouinon"]);
    self::assertSame("Oui", $t->format(true));
    self::assertSame("Non", $t->format(false));
    self::assertSame("Non", $t->format(null));

    $t = new TriboolType(["format" => "ouinonnull"]);
    self::assertSame("Oui", $t->format(true));
    self::assertSame("Non", $t->format(false));
    self::assertSame("", $t->format(null));

    $t = new TriboolType(["format" => "xn"]);
    self::assertSame("X", $t->format(true));
    self::assertSame("", $t->format(false));
    self::assertSame("", $t->format(null));
  }

  function testParse() {
    $t = new TriboolType();

    $input = "";
    self::assertSame(false, $t->parse($input));
    self::assertSame("", $input);

    $input = "whatever";
    self::assertSame(false, $t->parse($input));
    self::assertSame("whatever", $input);

    $input = "true";
    self::assertSame("true", $t->parse($input));
    self::assertSame("", $input);

    $input = "false";
    self::assertSame("false", $t->parse($input));
    self::assertSame("", $input);

    $input = "yoyo";
    self::assertSame("y", $t->parse($input));
    self::assertSame("oyo", $input);
  }

  function testVerifix() {
    $t = new TriboolType();

    $value = true; $t->verifix($value, $result);
    self::assertSame(true, $value);
    self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => true, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = false; $t->verifix($value, $result);
    self::assertSame(false, $value);
    self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => false, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = null; $t->verifix($value, $result);
    self::assertSame(null, $value);
    self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = ""; $t->verifix($value, $result);
    self::assertSame(null, $value);
    self::assertSame(["valid" => true, "value" => null, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "", "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = "0"; $t->verifix($value, $result);
    self::assertSame(false, $value);
    self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "0", "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = "no"; $t->verifix($value, $result);
    self::assertSame(false, $value);
    self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "no", "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = "yes"; $t->verifix($value, $result);
    self::assertSame(true, $value);
    self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "yes", "orig_desc" => null, "parsed" => false, "remains" => false], $result);

    $value = "whatever"; $t->verifix($value, $result);
    self::assertSame(true, $value);
    self::assertSame(["valid" => true, "value" => true, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "whatever", "orig_desc" => null, "parsed" => false, "remains" => false], $result);
  }

  function testTo_tribool() {
    self::assertTrue(TriboolType::to_tribool(true));
    self::assertTrue(TriboolType::to_tribool(1));
    self::assertTrue(TriboolType::to_tribool("1"));
    self::assertTrue(TriboolType::to_tribool("yes"));
    self::assertTrue(TriboolType::to_tribool("whatever"));

    self::assertFalse(TriboolType::to_tribool(false));
    self::assertFalse(TriboolType::to_tribool(0));
    self::assertFalse(TriboolType::to_tribool("0"));
    self::assertFalse(TriboolType::to_tribool("no"));

    self::assertNull(TriboolType::to_tribool(null));
    self::assertFalse(TriboolType::to_tribool(""));
  }
}
