use v6.d;

use lib '.';
use lib './lib';

use Data::Translators;
use JSON::Fast;
use Test;

#===========================================================
## 1
#===========================================================
my $json1 = q:to/END/;
{
	"name": "Json2Html",
	"desription": "converts json 2 html table format"
}
END

my $res1 = q:to/END/;
<table border="1">
	<tr>
		<th>name</th>
		<td>Json2Html</td>
	</tr>
	<tr>
		<th>desription</th>
		<td>converts json 2 html table format</td>
	</tr>
</table>
END

is
        to-html($json1, field-names => <name desription>).subst(/\s/, ''):g,
        $res1.subst(/\s/, ''):g,
        'basic';

is
        to-html(from-json($json1), field-names => <name desription>),
        to-html($json1, field-names => <name desription>),
        'basic eqv';

#===========================================================
## 2
#===========================================================
my $ds2 = (${:id("304"), :passengerAge("60"), :passengerClass("1st"), :passengerSex("male"), :passengerSurvival("died")},
           ${:id("594"), :passengerAge("30"), :passengerClass("2nd"), :passengerSex("female"), :passengerSurvival("survived")},
           ${:id("1163"), :passengerAge("-1"), :passengerClass("3rd"), :passengerSex("male"), :passengerSurvival("survived")},
           ${:id("1038"), :passengerAge("-1"), :passengerClass("3rd"), :passengerSex("female"), :passengerSurvival("survived")});

my $res2 = q:to/END/;
<table border="1"><thead><tr><th>id</th><th>passengerClass</th><th>passengerSex</th><th>passengerAge</th><th>passengerSurvival</th></tr></thead><tbody><tr><td>304</td><td>1st</td><td>male</td><td>60</td><td>died</td></tr><tr><td>594</td><td>2nd</td><td>female</td><td>30</td><td>survived</td></tr><tr><td>1163</td><td>3rd</td><td>male</td><td>-1</td><td>survived</td></tr><tr><td>1038</td><td>3rd</td><td>female</td><td>-1</td><td>survived</td></tr></tbody></table>
END

is
        to-html($ds2, field-names => <id passengerClass passengerSex passengerAge passengerSurvival>),
        $res2.trim,
        'dataset';

#===========================================================
## 3
#===========================================================
my $data3 = q:to/END/;
{
  "data": [
    {
      "id": 1,
      "name": "Alice",
      "age": 25,
      "profession": "Engineer"
    },
    {
      "id": 2,
      "name": "Bob",
      "age": 30,
      "profession": "Doctor"
    },
    {
      "id": 3,
      "name": "Charlie",
      "age": 28,
      "profession": "Artist"
    },
    {
      "id": 4,
      "name": "Diana",
      "age": 32,
      "profession": "Teacher"
    }
  ]
}
END

my $res3 = q:to/END/;
<table border="1"><tr><th>data</th><td><table border="1"><thead><tr><th>id</th><th>profession</th><th>age</th><th>name</th></tr></thead><tbody><tr><td>1</td><td>Engineer</td><td>25</td><td>Alice</td></tr><tr><td>2</td><td>Doctor</td><td>30</td><td>Bob</td></tr><tr><td>3</td><td>Artist</td><td>28</td><td>Charlie</td></tr><tr><td>4</td><td>Teacher</td><td>32</td><td>Diana</td></tr></tbody></table></td></tr></table>
END

is
        to-html($data3, field-names => <data id profession age name>).subst(/\s/,''):g,
        $res3.subst(/\s/,''):g,
        'professionals';


#===========================================================
## 4
#===========================================================

ok to-html([  Pair.new(Whatever, 'Line1'), 'B' => 'Line2']);

done-testing;
