<?php
namespace nur\data\types;

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

class IntTypeTest extends TestCase {
  function testFormat() {
    $i = new IntType();
    self::assertSame("", $i->format(null));
    self::assertSame("", $i->format(false));
    self::assertSame("0", $i->format(0));
    self::assertSame("1", $i->format(1));
    self::assertSame("155", $i->format(155));
    self::assertSame("-302", $i->format(-302));
  }

  function testParse() {
    $i = new IntType();

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

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

    $input = "0";
    self::assertSame("0", $i->parse($input));
    self::assertSame("", $input);

    $input = "1";
    self::assertSame("1", $i->parse($input));
    self::assertSame("", $input);
    
    $input = "155";
    self::assertSame("155", $i->parse($input));
    self::assertSame("", $input);

    $input = "155BAD";
    self::assertSame("155", $i->parse($input));
    self::assertSame("BAD", $input);

    $input = "-302";
    self::assertSame("-302", $i->parse($input));
    self::assertSame("", $input);

    $input = "-302BAD";
    self::assertSame("-302", $i->parse($input));
    self::assertSame("BAD", $input);
    
    $input = "  257  ";
    self::assertSame(false, $i->parse($input));
    self::assertSame("  257  ", $input);

    $input = "  257  BAD  ";
    self::assertSame(false, $i->parse($input));
    self::assertSame("  257  BAD  ", $input);
  }

  function testVerifix() {
    $i = new IntType();

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

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

    $value = ""; $i->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 = "whatever"; $i->verifix($value, $result);
    $error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
    self::assertSame("whatever", $value);
    self::assertSame(["valid" => false, "value" => "whatever", "value_desc" => null,
      "error_code" => "invalid", "error" => $error, "exception" => null,
      "orig" => "whatever", "orig_desc" => null, "parsed" => '', "remains" => "whatever"], $result);

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

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

    $value = "155BAD"; $i->verifix($value, $result);
    $error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
    self::assertSame("155BAD", $value);
    self::assertSame(["valid" => false, "value" => "155BAD", "value_desc" => null,
      "error_code" => "invalid", "error" => $error, "exception" => null,
      "orig" => "155BAD", "orig_desc" => null, "parsed" => "155", "remains" => "BAD"], $result);
    
    $value = "-302"; $i->verifix($value, $result);
    self::assertSame(-302, $value);
    self::assertSame(["valid" => true, "value" => -302, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "-302", "orig_desc" => null, "parsed" => "-302", "remains" => ""], $result);

    $value = "-302BAD"; $i->verifix($value, $result);
    $error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
    self::assertSame("-302BAD", $value);
    self::assertSame(["valid" => false, "value" => "-302BAD", "value_desc" => null,
      "error_code" => "invalid", "error" => $error, "exception" => null,
      "orig" => "-302BAD", "orig_desc" => null, "parsed" => "-302", "remains" => "BAD"], $result);
    
    $value = "  257  "; $i->verifix($value, $result);
    self::assertSame(257, $value);
    self::assertSame(["valid" => true, "value" => 257, "value_desc" => null,
      "error_code" => null, "error" => false, "exception" => null,
      "orig" => "  257  ", "orig_desc" => null, "parsed" => "257", "remains" => ""], $result);

    $value = "  257  BAD  "; $i->verifix($value, $result);
    $error = str_replace("{value_desc}", var_export($value, true), IntType::MESSAGES["invalid"]);
    self::assertSame("  257  BAD  ", $value);
    self::assertSame(["valid" => false, "value" => "  257  BAD  ", "value_desc" => null,
      "error_code" => "invalid", "error" => $error, "exception" => null,
      "orig" => "  257  BAD  ", "orig_desc" => null, "parsed" => "257", "remains" => "  BAD"], $result);
  }

  function testTo_zint() {
    self::assertFalse(IntType::to_zint(false));
    self::assertNull(IntType::to_zint(null));
    self::assertNull(IntType::to_zint(""));

    self::assertSame(0, IntType::to_zint(0));
    self::assertSame(0, IntType::to_zint("0"));
    self::assertSame(0, IntType::to_zint("00"));

    self::assertSame(1, IntType::to_int(true));
    self::assertSame(1, IntType::to_zint(1));
    self::assertSame(1, IntType::to_zint("1"));

    self::assertSame(155, IntType::to_zint(155));
    self::assertSame(155, IntType::to_zint("155"));

    self::assertSame(-302, IntType::to_zint(-302));
    self::assertSame(-302, IntType::to_zint("-302"));

    self::assertSame(257, IntType::to_zint(257));
    self::assertSame(257, IntType::to_zint("  257  "));

    self::assertSame(1, IntType::to_zint(1.234));

    self::assertException(ValueException::class, [IntType::class, "to_zint"], "whatever");
    self::assertException(ValueException::class, [IntType::class, "to_zint"], "1.234");
  }

  function testTo_int() {
    self::assertSame(0, IntType::to_int(false));
    self::assertSame(0, IntType::to_int(null));
    self::assertSame(0, IntType::to_int(""));

    self::assertSame(0, IntType::to_int(0));
    self::assertSame(0, IntType::to_int("0"));
    self::assertSame(0, IntType::to_int("00"));

    self::assertSame(1, IntType::to_int(true));
    self::assertSame(1, IntType::to_int(1));
    self::assertSame(1, IntType::to_int("1"));

    self::assertSame(155, IntType::to_int(155));
    self::assertSame(155, IntType::to_int("155"));

    self::assertSame(-302, IntType::to_int(-302));
    self::assertSame(-302, IntType::to_int("-302"));

    self::assertSame(257, IntType::to_int(257));
    self::assertSame(257, IntType::to_int("  257  "));

    self::assertSame(1, IntType::to_int(1.234));

    self::assertException(ValueException::class, [IntType::class, "to_int"], "whatever");
    self::assertException(ValueException::class, [IntType::class, "to_int"], "1.234");
  }
}
