use v6.d;

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

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

#===========================================================
## 1-2
#===========================================================
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
        data-translation($json1, field-names => <name desription>).subst(/\s/, ''):g,
        $res1.subst(/\s/, ''):g,
        'basic';

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

#===========================================================
## 3
#===========================================================
my $ds3 = (${: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 $res3 = 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
        data-translation($ds3, target => 'HTML', field-names => <id passengerClass passengerSex passengerAge passengerSurvival>),
        $res3.trim,
        'dataset HTML';

#===========================================================
## 4
#===========================================================
my $ds4 = (${: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 $res4 = q:to/END/;
data.frame(`id` = c("304", "594", "1163", "1038"),
`passengerClass` = c("1st", "2nd", "3rd", "3rd"),
`passengerSex` = c("male", "female", "male", "female"),
`passengerAge` = c("60", "30", "-1", "-1"),
`passengerSurvival` = c("died", "survived", "survived", "survived"))
END

is
        data-translation($ds4, target => 'R', field-names => <id passengerClass passengerSex passengerAge passengerSurvival>),
        $res4.trim,
        'dataset R';

#===========================================================
## 5
#===========================================================
my $data5 = 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 $res5 = q:to/END/;
list("data"=data.frame(`name` = c("Alice", "Bob", "Charlie", "Diana"),
`id` = c(1, 2, 3, 4),
`age` = c(25, 30, 28, 32),
`profession` = c("Engineer", "Doctor", "Artist", "Teacher")))
END

is
        data-translation($data5, target => 'R', field-names => <data name id age profession>).subst(/\s/,''):g,
        $res5.subst(/\s/,''):g,
        'professionals';

done-testing;