<?php
namespace nur\data\types;

use nur\t\TestCase;

class BoolTypeTest extends TestCase {
  function testIs_yes() {
    self::assertTrue(BoolType::is_yes(true));
    self::assertTrue(BoolType::is_yes("true"));
    self::assertTrue(BoolType::is_yes("t"));
    self::assertTrue(BoolType::is_yes("yes"));
    self::assertTrue(BoolType::is_yes("y"));
    self::assertTrue(BoolType::is_yes("vrai"));
    self::assertTrue(BoolType::is_yes("v"));
    self::assertTrue(BoolType::is_yes("oui"));
    self::assertTrue(BoolType::is_yes("o"));
    self::assertTrue(BoolType::is_yes("1"));
    self::assertTrue(BoolType::is_yes("2"));
    self::assertTrue(BoolType::is_yes("153"));

    self::assertFalse(BoolType::is_yes(null));
    self::assertFalse(BoolType::is_yes(false));
    self::assertFalse(BoolType::is_yes(""));
    self::assertFalse(BoolType::is_yes("0"));
    self::assertFalse(BoolType::is_yes("-1"));
  }

  function testIs_no() {
    self::assertTrue(BoolType::is_no(false));
    self::assertTrue(BoolType::is_no(null));
    self::assertTrue(BoolType::is_no("false"));
    self::assertTrue(BoolType::is_no("f"));
    self::assertTrue(BoolType::is_no("no"));
    self::assertTrue(BoolType::is_no("n"));
    self::assertTrue(BoolType::is_no("faux"));
    self::assertTrue(BoolType::is_no("non"));
    self::assertTrue(BoolType::is_no("0"));

    self::assertFalse(BoolType::is_no(true));
    self::assertFalse(BoolType::is_no(""));
    self::assertFalse(BoolType::is_no("1"));
    self::assertFalse(BoolType::is_no("-1"));
  }

  function testFormat() {
    $b = new BoolType();
    self::assertSame("Oui", $b->format(true));
    self::assertSame("Non", $b->format(false));
    self::assertSame("Non", $b->format(null));

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

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

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

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

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

  function testParse() {
    $b = new BoolType();

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

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

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

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

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

  function testVerifix() {
    $b = new BoolType();

    $value = true; $b->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; $b->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; $b->verifix($value, $result);
    self::assertSame(false, $value);
    self::assertSame(["valid" => true, "value" => false, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => null, "orig_desc" => null, "parsed" => false, "remains" => false], $result);

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

    $value = "0"; $b->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"; $b->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"; $b->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"; $b->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_bool() {
    self::assertTrue(BoolType::to_bool(true));
    self::assertTrue(BoolType::to_bool(1));
    self::assertTrue(BoolType::to_bool("1"));
    self::assertTrue(BoolType::to_bool("yes"));
    self::assertTrue(BoolType::to_bool("whatever"));

    self::assertFalse(BoolType::to_bool(false));
    self::assertFalse(BoolType::to_bool(0));
    self::assertFalse(BoolType::to_bool("0"));
    self::assertFalse(BoolType::to_bool("no"));

    self::assertFalse(BoolType::to_bool(null));
    self::assertFalse(BoolType::to_bool(""));
  }
}
