File: 04where.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 (81 lines) | stat: -rw-r--r-- 1,647 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
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

my @positions;
my @wheres;

my $diemsg;
my $warnmsg;

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

   sub parse
   {
      my $self = shift;

      main::is( $self->pos,
         $positions[0],
         '->pos before parsing' );
      main::is( [ $self->where ],
         $wheres[0],
         '->where before parsing' );

      $self->expect( "hello" );
      main::is( $self->pos,
         $positions[1],
         '->pos during parsing' );
      main::is( [ $self->where ],
         $wheres[1],
         '->where during parsing' );

      $self->expect( qr/world/ );
      main::is( $self->pos,
         $positions[2],
         '->pos after parsing' );
      main::is( [ $self->where ],
         $wheres[2],
         '->where after parsing' );

      $self->die( $diemsg ) if $diemsg;
      $self->warn( $warnmsg ) if $warnmsg;

      return 1;
   }
}

my $parser = TestParser->new;

@positions = ( 0, 5, 11 );
@wheres = (
   [ 1, 0, "hello world" ],
   [ 1, 5, "hello world" ],
   [ 1, 11, "hello world" ], );
$parser->from_string( "hello world" );

@positions = ( 0, 5, 11 );
@wheres = (
   [ 1, 0, "hello" ],
   [ 1, 5, "hello" ],
   [ 2, 5, "world" ], );
$parser->from_string( "hello\nworld" );

{
   $diemsg = "stop here";
   like( dies { $parser->from_string( "hello\nworld" ) },
      qr/^stop here on line 2 at:\nworld\n/, 'Exception from ->die failure' );
   undef $diemsg;
}

{
   $warnmsg = "note here";
   is( warnings { $parser->from_string( "hello\nworld" ) },
      [ match(qr/^note here on line 2 at:\nworld\n/) ],
      'Warning from ->warn' );
}

done_testing;