File: stree.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (138 lines) | stat: -rw-r--r-- 3,083 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use strict;
use warnings;
use Test::More tests => 30;

#use Pod::Simple::Debug (6);

use Pod::Simple::SimpleTree;
print "# Pod::Simple version $Pod::Simple::VERSION\n";

my $hashes_dont_matter = 0;


my $x = 'Pod::Simple::SimpleTree';
sub x {
 my $p = $x->new;
 $p->merge_text(1);
 $p->parse_string_document( shift )->root;
}

print "# a bit of meta-testing...\n";
ok( deq( 1,     1     ));
ok(!deq( 2,     1     ));

ok( deq( undef, undef ));
ok(!deq( undef, 1     ));
ok(!deq( 1,     undef ));

ok( deq( [ ],   [ ]    ));
ok(!deq( [ ],   1      ));
ok(!deq( 1,     [ ]    ));

ok( deq( [1],   [1]    ));
ok(!deq( [1],   1      ));
ok(!deq( 1,     [1]    ));
ok(!deq( [1],   [ ]    ));
ok(!deq( [ ],   [1]    ));
ok(!deq( [1],   [2]    ));
ok(!deq( [2],   [1]    ));

ok( deq( [ ],   [ ]    ));
ok(!deq( [ ],   1      ));
ok(!deq( 1,     [ ]    ));

ok( deq( {},    {}     ));
ok(!deq( {},    1      ));
ok(!deq( 1,     {}     ));
ok(!deq( {1,2}, {}     ));
ok(!deq( {},    {1,2}  ));
ok( deq( {1,2}, {1,2}  ));
ok(!deq( {2,1}, {1,2}  ));




print '# ', Pod::Simple::pretty(x( "=pod\n\nI like pie.\n" )), "\n";
print "# Making sure we get a tree at all...\n";
ok x( "=pod\n\nI like pie.\n" );


print "# Some real tests...\n";
ok( deq( x( "=pod\n\nI like pie.\n"),
  [ "Document", {"start_line"=>1},
    [ "Para",   {"start_line"=>3},
      "I like pie."
    ]
  ]
));

$hashes_dont_matter = 1;

ok( deq( x("=pod\n\nB<foo\t>\n"),
  [ "Document", {},
    [ "Para",   {},
      ["B",     {},
        "foo "
      ]
    ]
  ]
));


ok( deq( x("=pod\n\nB<pieF<zorch>X<foo>I<pling>>\n"),
  [ "Document", {},
    [ "Para",   {},
      ["B",     {},
        "pie",
        ['F',{}, 'zorch'],
        ['X',{}, 'foo'  ],
        ['I',{}, 'pling'],
      ]
    ]
  ]
));

ok( deq( x("=over\n\n=item B<pieF<zorch>X<foo>I<pling>>!\n\n=back"),
  [ "Document", {},
    [ "over-text", {},
      [ "item-text", {},
        ["B",     {},
          "pie",
          ['F',{}, 'zorch'],
          ['X',{}, 'foo'  ],
          ['I',{}, 'pling'],
        ],
        '!'
      ]
    ]
  ]
));

sub deq { # deep-equals
  #print "# deq ", Pod::Simple::pretty($_[0], $_[1]), "\n";
  return 1 unless defined $_[0] or defined $_[1]; # two undefs = same
  return '' if defined $_[0] xor defined $_[1];
  return '' if ref($_[0]) ne ref($_[1]); # unequal referentiality
  return $_[0] eq $_[1] unless ref $_[0];
  # So it's a ref:
  if(UNIVERSAL::isa($_[0], 'ARRAY')) {
    return '' unless @{$_[0]} == @{$_[1]};
    for(my $i = 0; $i < @{$_[0]}; $i++) {
      print("# NEQ ", Pod::Simple::pretty($_[0]),
          "\n#  != ", Pod::Simple::pretty($_[1]), "\n"),
       return '' unless deq($_[0][$i], $_[1][$i]); # recurse!
    }
    return 1;
  } elsif(UNIVERSAL::isa($_[0], 'HASH')) {
    return 1 if $hashes_dont_matter;
    return '' unless keys %{$_[0]} == keys %{$_[1]};
    foreach my $k (keys %{$_[0]}) {
      return '' unless exists $_[1]{$k};
      return '' unless deq($_[0]{$k}, $_[1]{$k});
    }
    return 1;
  } else {
    print "# I don't know how to deque $_[0] & $_[1]\n";
    return 1;
  }
}