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/;
list("name"="Json2Html", "desription"="converts json 2 html table format")
END

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

is
        to-r(from-json($json1), field-names => <name desription>),
        to-r($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/;
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
        to-r($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/;
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
        to-r($data3, field-names => <data name id age profession>).subst(/\s/,''):g,
        $res3.subst(/\s/,''):g,
        'professionals';

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

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

done-testing;
