<?php
namespace nur\mapper\fsv;

use nur\t\TestCase;

class FsvSchemaTest extends TestCase {
  const FSV_SCHEMA = [
    "nom" => [10, "string"],
    "prenom" => [10, "string"],
    "date_nai" => [8, "date"],
    "exact" => [6, "number", 2],
    "approx" => [6, "number"],
  ];

  const LINE1 = "Clain     Jephté    29061975001500000153";
  const ROW1 = [
    "nom" => "Clain",
    "prenom" => "Jephté",
    "date_nai" => "29/06/1975",
    "exact" => 15.0,
    "approx" => 153,
  ];

  const LINE2a = "          Francoise 29051959";
  const LINE2b = "          Francoise 29051959            ";
  const ROW2 = [
    "nom" => "",
    "prenom" => "Francoise",
    "date_nai" => "29/05/1959",
    "exact" => false,
    "approx" => false,
  ];

  private static function latin1(string $line): string {
    return iconv("utf-8", "latin1", $line);
  }
  private static function utf8(string $line): string {
    return iconv("latin1", "utf-8", $line);
  }

  function testParse() {
    $schema = new FsvSchema(self::FSV_SCHEMA);
    self::assertSame(self::ROW1
      , $schema->parseRow(self::latin1(self::LINE1)));
    self::assertSame(self::ROW2
      , $schema->parseRow(self::latin1(self::LINE2a)));
    self::assertSame(self::ROW2
      , $schema->parseRow(self::latin1(self::LINE2b)));
  }

  function testFormat() {
    $schema = new FsvSchema(self::FSV_SCHEMA);
    self::assertSame(self::LINE1
      , self::utf8($schema->formatLine(self::ROW1)));
    self::assertSame(self::LINE2b
      , self::utf8($schema->formatLine(self::ROW2)));
  }
}