1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#!/usr/bin/perl -w
use strict;
use lib '../lib', 'lib';
use GraphViz;
use Test::More tests => 30;
# make a nice simple graph and check how output is handled.
my $g = GraphViz->new();
$g->add_node(label => 'London');
{
# Check filehandle
my $fh = do { local *FH; *FH; }; # doubled to avoid warnings
open $fh, ">as_foo.1"
or die "Cannot write to as_foo.1: $!";
$g->as_dot($fh);
close $fh;
my @result = read_file('as_foo.1');
check_result(@result);
}
{
# Check filehandle #2
local *OUT;
open OUT, ">as_foo.2"
or die "Cannot write to as_foo.2: $!";
$g->as_dot(\*OUT);
close OUT;
my @result = read_file('as_foo.2');
check_result(@result);
}
{
# Check filename
$g->as_dot('as_foo.3');
my @result = read_file('as_foo.3');
check_result(@result);
}
{
# Check scalar ref
my $result;
$g->as_dot(\$result);
check_result(split /\n/, $result);
}
{
# Check returned
my $result = $g->as_dot();
check_result(split /\n/, $result);
}
{
# Check coderef
my $result;
$g->as_dot(sub { $result .= shift });
check_result(split /\n/, $result);
}
unlink 'as_foo.1';
unlink 'as_foo.2';
unlink 'as_foo.3';
sub read_file {
my $filename = shift;
local *FILE;
open FILE, "<$filename"
or die "Cannot read $filename: $!";
return (<FILE>);
}
sub check_result {
my @result = @_;
my $expect = <<'EOF';
Expected something like:
digraph test {
node [ label = "\N" ];
graph [bb= "0,0,66,38"];
node1 [label=London, pos="33,19", width="0.89", height="0.50"];
}
EOF
# have to use regexes cause the output includes numbers that may
# change each time
like($result[0], qr/^digraph test {/);
like($result[1], qr/^\s+graph \[ratio=fill\];/);
like($result[2], qr/^\s*node\s*\[\s*label\s*=\s*"\\N"\s*\];\s*/);
like($result[3], qr/^\s*graph\s*\[bb=.*/);
like($result[4], qr/^\s*node1\s*\[label=London.*\];/);
}
|