File: parse.t

package info (click to toggle)
libterm-shellui-perl 0.92-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 1,286; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl -Tw

use strict;

use lib 't';
use Parse;

use Test::Simple tests => 0+keys(%Parse::Tests);


use Text::Shellwords::Cursor;

my $parser = new Text::Shellwords::Cursor;
die "No parser" unless $parser;

for my $input (sort keys %Parse::Tests) {
    my($index,$cpos,$test) = $input =~ /^(\d+):(\d*):(.*)$/;
    die "No test in item $index:    $input\n" unless defined $test;

    my($toks, $tokno, $tokoff) = $parser->parse_line($test, messages=>0, cursorpos=>$cpos);
    my $result = [$toks, $tokno, $tokoff];
    ok(0 == cmp_deep($result, $Parse::Tests{$input}), "Test $index");
}


# Tries to return a valid comparison
# If the data types don't match, claims a>b. '

sub cmp_deep
{
    my($a,$b) = @_;
    my $refa = ref $a;
    my $refb = ref $b;

    return 1 if $refa ne $refb;
    if(not $refa) {
        return $a cmp $b if defined($a) && defined($b);
        return 1 if !defined($a) && defined($b);
        return -1 if defined($a) && !defined($b);
        return 0;
    } elsif($refa eq 'SCALAR') {
        return cmp_deep($$a, $$b);
    } elsif($refa eq 'ARRAY') {
        return @$a <=> @$b if @$a <=> @$b;
        for(my $i=0; $i < @$a; $i++) {
            my $res = cmp_deep($a->[$i], $b->[$i]);
            return $res if $res;
        }
    } else {
        die "Can't compare a: $a '$refa'\n";
    }

    return 0;
}