File: 30commit.t

package info (click to toggle)
libparser-mgc-perl 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 536 kB
  • sloc: perl: 1,881; makefile: 2; sh: 1
file content (70 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download
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
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

package TestParser {
   use base qw( Parser::MGC );

   sub parse
   {
      my $self = shift;

      $self->any_of(
         sub { $self->token_int },
         sub {
            $self->scope_of( "(",
               sub {
                  $self->commit;
                  $self->token_string;
               },
               ")" );
         }
      );
   }
}

package IntStringPairsParser {
   use base qw( Parser::MGC );

   sub parse
   {
      my $self = shift;

      $self->sequence_of( sub {
         my $int = $self->token_int;
         $self->commit;

         my $str = $self->token_string;

         [ $int, $str ];
      } );
   }
}

my $parser = TestParser->new;

is( $parser->from_string( "123" ), 123, '"123"' );
is( $parser->from_string( '("hi")' ), "hi", '("hi")' );

is( dies { $parser->from_string( "(456)" ) },
   qq[Expected string delimiter on line 1 at:\n].
   qq[(456)\n].
   qq[ ^\n],
   'Exception from "(456)" failure' );

$parser = IntStringPairsParser->new;

is( $parser->from_string( "1 'one' 2 'two'" ),
   [ [ 1, "one" ], [ 2, "two" ] ],
   "1 'one' 2 'two'" );

is( dies { $parser->from_string( "1 'one' 2" ) },
   qq[Expected string on line 1 at:\n].
   qq[1 'one' 2\n].
   qq[         ^\n],
   'Exception from 1 \'one\' 2 failure' );

done_testing;