### ----------------------------------------------------
### -- Graph
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: xt/20-graphviz-dot-format.rakutest
### ----------------------------------------------------
use v6.d;
use Test;

use lib <. lib>;
use Graph;


## 1
subtest {
    my @edges = '1' => '2', '2' => '1', '1' => '3', '3' => '2';
    my $res = Graph.new(@edges, :directed).dot(:mixed);

    my $dot = q:to/END/;
digraph {

bgcolor="none";
node [style=filled, fixedsize=true, shape=circle, color="Black", fillcolor="SteelBlue", penwidth=1, fontsize=12, fontcolor="White", labelloc=c, width=0.3, height=0.3];
edge [color="SteelBlue", penwidth=2, fontsize=10, fontcolor="Ivory", arrowsize=1];
"1"; "2"; "3";
"1" -> "3" [dir=forward]
"1" -> "2" [dir=none]
"3" -> "2" [dir=forward]
}
END

    is $res.indices('->').elems, 3, 'Expected number of edge specs';
    is $res.indices('[dir=forward]').elems, 2, 'Expected number of directed edge specs';
    is $res.indices('[dir=none]').elems, 1, 'Expected number of un-directed edge specs';

}, 'Directed graph as mixed';

## 2
subtest {
    my @edges = '1' => '2', '2' => '1', '1' => '3', '3' => '2';
    my %edge-labels = '1' => { '2' => 'here' };
    my $res = Graph.new(@edges, :directed).dot(:%edge-labels, :mixed);

    my $dot = q:to/END/;
digraph {

bgcolor="none";
node [style=filled, fixedsize=true, shape=circle, color="Black", fillcolor="SteelBlue", penwidth=1, fontsize=12, fontcolor="White", labelloc=c, width=0.3, height=0.3];
edge [color="SteelBlue", penwidth=2, fontsize=10, fontcolor="Ivory", arrowsize=1];
"1"; "2"; "3";
"2" -> "1" [label="here", dir=none]
"1" -> "3" [label="", dir=forward]
"3" -> "2" [label="", dir=forward]
}
END

    is $res.indices('->').elems, 3, 'Expected number of edge specs';
    is $res.indices('dir=forward]').elems, 2, 'Expected number of directed edge specs';
    is $res.indices('dir=none]').elems, 1, 'Expected number of un-directed edge specs';
    is $res.indices('label=').elems, 3, 'Expected number of edge labels';
    is $res.indices('label="here"').elems, 1, 'Expected number of "here" edge labels';
}, 'Labeled directed graph as mixed';


## 3
subtest {
    my @edges =
            { :from("Anti-tank weapons"), :label("defend"), :to("Armor") },
            { :from("Armor"), :label("attack"), :to("Infantry and Artillery") },
            { :from("Air force"), :label("attack"), :to("Armor") },
            { :from("Air force"), :label("attack"), :to("Infantry and Artillery") },
            { :from("Missiles"), :label("defend"), :to("Air force") },
            { :from("Infantry and Artillery"), :label("attack"), :to("Missiles") },
            { :from("Infantry and Artillery"), :label("attack"), :to("Anti-tank weapons") };

    my $g = Graph.new(@edges):d;
    my %highlight = 'Red' => Graph.new( @edges.grep(*<label> eq 'defend'), :directed).edges;

    my $processed = $g.process-highlight-spec(%highlight, :directed);

    isa-ok $processed, Map:D, 'expected return type';
    is $processed.keys, <Red>, 'expected keys of the result';
    is $processed<Red>.keys.sort, <edges vertexes>, 'expected keys of the result';
    is $processed<Red><edges> ~~ List:D | Array:D | Seq:D, True, 'expected edges value';
    is ($processed<Red><edges>.all ~~ List:D | Array:D | Seq:D), True, 'expected edges shape 1';
    is $processed<Red><edges>».elems.min == 2 && $processed<Red><edges>».elems.max == 2, True, 'expected edges shape 1';

}, 'Highlight spec processings';

## 4
subtest {
    my @edges = '1' => '2', '2' => '1', '1' => '3', '3' => '2';
    my %edge-labels = '1' => { '2' => 'here' };
    my %highlight = Red => ('1' => '2',);
    my $res = Graph.new(@edges, :directed).dot(:%highlight);

    my $dot = q:to/END/;
digraph {
bgcolor="none";
node [style=filled, fixedsize=true, shape=circle, color="Black", fillcolor="SteelBlue", penwidth=1, fontsize=12, fontcolor="White", labelloc=c, width=0.3, height=0.3];
edge [color="SteelBlue", penwidth=2, fontsize=10, fontcolor="Ivory", arrowsize=1];
"1"; "2"; "3";
"2" -> "1"
"1" -> "3"
"3" -> "2"
edge [color="Red", penwidth=2, arrowsize=1];
"1" -> "2"
}
END

    is $res.indices('->').elems, 4, 'Expected number of edge specs';
    is $res.indices('[dir=forward]').elems, 0, 'Expected number of directed edge specs';
    is $res.indices('[dir=none]').elems, 0, 'Expected number of un-directed edge specs';
    is $res.indices('color="Red"').elems, 1, 'Expected number of Red color specs';

}, 'Directed graph with highlights';

done-testing;


