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