| 1 | package ICANN::RST::Graph; | ||
| 2 | # ABSTRACT: an object representing a graph of an RST test plan. | ||
| 3 | use Encode; | ||
| 4 | use base qw(GraphViz2); | ||
| 5 | use utf8; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my ($package, @cases) = @_; | ||
| 10 | |||
| 11 | my $self = bless($package->SUPER::new( | ||
| 12 | 'global' => { | ||
| 13 | 'directed' => 1, | ||
| 14 | }, | ||
| 15 | 'graph' => { | ||
| 16 | 'layout' => 'dot', | ||
| 17 | 'rankdir' => 'LR', | ||
| 18 | } | ||
| 19 | ), $package); | ||
| 20 | |||
| 21 | $self->add_cases(@cases); | ||
| 22 | |||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub add_cases { | ||
| 27 | my ($self, @cases) = @_; | ||
| 28 | |||
| 29 | foreach my $case (@cases) { | ||
| 30 | $self->add_case($case); | ||
| 31 | } | ||
| 32 | |||
| 33 | for (my $i = 0 ; $i < scalar(@cases) ; $i++) { | ||
| 34 | my $case = $cases[$i]; | ||
| 35 | |||
| 36 | my @deps = $case->dependencies; | ||
| 37 | |||
| 38 | if (scalar(@deps) < 1 && $i > 0) { | ||
| 39 | $self->add_case_edge($cases[$i-1], $case); | ||
| 40 | |||
| 41 | } else { | ||
| 42 | foreach my $dep (@deps) { | ||
| 43 | $self->add_case_edge($dep, $case); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | sub add_case_edge { | ||
| 50 | my ($self, $from, $to) = @_; | ||
| 51 | $self->add_edge( | ||
| 52 | 'from' => $from->id, | ||
| 53 | 'to' => $to->id, | ||
| 54 | ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub add_case { | ||
| 58 | my ($self, $case) = @_; | ||
| 59 | |||
| 60 | $self->add_node( | ||
| 61 | 'name' => $case->id, | ||
| 62 | 'href' => sprintf('#Test-Case-%s', $case->id), | ||
| 63 | 'tooltip' => $self->entity_encode($case->summary), | ||
| 64 | 'shape' => 'box', | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub entity_encode { | ||
| 69 | my ($self, $str) = @_; | ||
| 70 | |||
| 71 | return join('', map { sprintf('&#%u;', ord($_)) } split(//, encode("UTF-8", $str))); | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; | ||
| 75 | |||
| 76 | __END__ | ||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =encoding UTF-8 | ||
| 81 | |||
| 82 | =head1 NAME | ||
| 83 | |||
| 84 | ICANN::RST::Graph - an object representing a graph of an RST test plan. | ||
| 85 | |||
| 86 | =head1 VERSION | ||
| 87 | |||
| 88 | version 0.03 | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Gavin Brown <gavin.brown@icann.org> | ||
| 93 | |||
| 94 | =head1 COPYRIGHT AND LICENSE | ||
| 95 | |||
| 96 | This software is copyright (c) 2025 by Internet Corporation for Assigned Names and Number (ICANN). | ||
| 97 | |||
| 98 | This is free software; you can redistribute it and/or modify it under | ||
| 99 | the same terms as the Perl 5 programming language system itself. | ||
| 100 | |||
| 101 | =cut |