xref: /openbsd/gnu/usr.bin/perl/cpan/Pod-Simple/t/stree.t (revision 5486feef)
1use strict;
2use warnings;
3use Test::More tests => 30;
4
5#use Pod::Simple::Debug (6);
6
7use Pod::Simple::SimpleTree;
8print "# Pod::Simple version $Pod::Simple::VERSION\n";
9
10my $hashes_dont_matter = 0;
11
12
13my $x = 'Pod::Simple::SimpleTree';
14sub x {
15 my $p = $x->new;
16 $p->merge_text(1);
17 $p->parse_string_document( shift )->root;
18}
19
20print "# a bit of meta-testing...\n";
21ok( deq( 1,     1     ));
22ok(!deq( 2,     1     ));
23
24ok( deq( undef, undef ));
25ok(!deq( undef, 1     ));
26ok(!deq( 1,     undef ));
27
28ok( deq( [ ],   [ ]    ));
29ok(!deq( [ ],   1      ));
30ok(!deq( 1,     [ ]    ));
31
32ok( deq( [1],   [1]    ));
33ok(!deq( [1],   1      ));
34ok(!deq( 1,     [1]    ));
35ok(!deq( [1],   [ ]    ));
36ok(!deq( [ ],   [1]    ));
37ok(!deq( [1],   [2]    ));
38ok(!deq( [2],   [1]    ));
39
40ok( deq( [ ],   [ ]    ));
41ok(!deq( [ ],   1      ));
42ok(!deq( 1,     [ ]    ));
43
44ok( deq( {},    {}     ));
45ok(!deq( {},    1      ));
46ok(!deq( 1,     {}     ));
47ok(!deq( {1,2}, {}     ));
48ok(!deq( {},    {1,2}  ));
49ok( deq( {1,2}, {1,2}  ));
50ok(!deq( {2,1}, {1,2}  ));
51
52
53
54
55print '# ', Pod::Simple::pretty(x( "=pod\n\nI like pie.\n" )), "\n";
56print "# Making sure we get a tree at all...\n";
57ok x( "=pod\n\nI like pie.\n" );
58
59
60print "# Some real tests...\n";
61ok( deq( x( "=pod\n\nI like pie.\n"),
62  [ "Document", {"start_line"=>1},
63    [ "Para",   {"start_line"=>3},
64      "I like pie."
65    ]
66  ]
67));
68
69$hashes_dont_matter = 1;
70
71ok( deq( x("=pod\n\nB<foo\t>\n"),
72  [ "Document", {},
73    [ "Para",   {},
74      ["B",     {},
75        "foo "
76      ]
77    ]
78  ]
79));
80
81
82ok( deq( x("=pod\n\nB<pieF<zorch>X<foo>I<pling>>\n"),
83  [ "Document", {},
84    [ "Para",   {},
85      ["B",     {},
86        "pie",
87        ['F',{}, 'zorch'],
88        ['X',{}, 'foo'  ],
89        ['I',{}, 'pling'],
90      ]
91    ]
92  ]
93));
94
95ok( deq( x("=over\n\n=item B<pieF<zorch>X<foo>I<pling>>!\n\n=back"),
96  [ "Document", {},
97    [ "over-text", {},
98      [ "item-text", {},
99        ["B",     {},
100          "pie",
101          ['F',{}, 'zorch'],
102          ['X',{}, 'foo'  ],
103          ['I',{}, 'pling'],
104        ],
105        '!'
106      ]
107    ]
108  ]
109));
110
111sub deq { # deep-equals
112  #print "# deq ", Pod::Simple::pretty($_[0], $_[1]), "\n";
113  return 1 unless defined $_[0] or defined $_[1]; # two undefs = same
114  return '' if defined $_[0] xor defined $_[1];
115  return '' if ref($_[0]) ne ref($_[1]); # unequal referentiality
116  return $_[0] eq $_[1] unless ref $_[0];
117  # So it's a ref:
118  if(UNIVERSAL::isa($_[0], 'ARRAY')) {
119    return '' unless @{$_[0]} == @{$_[1]};
120    for(my $i = 0; $i < @{$_[0]}; $i++) {
121      print("# NEQ ", Pod::Simple::pretty($_[0]),
122          "\n#  != ", Pod::Simple::pretty($_[1]), "\n"),
123       return '' unless deq($_[0][$i], $_[1][$i]); # recurse!
124    }
125    return 1;
126  } elsif(UNIVERSAL::isa($_[0], 'HASH')) {
127    return 1 if $hashes_dont_matter;
128    return '' unless keys %{$_[0]} == keys %{$_[1]};
129    foreach my $k (keys %{$_[0]}) {
130      return '' unless exists $_[1]{$k};
131      return '' unless deq($_[0]{$k}, $_[1]{$k});
132    }
133    return 1;
134  } else {
135    print "# I don't know how to deque $_[0] & $_[1]\n";
136    return 1;
137  }
138}
139