File: 20maybe.t

package info (click to toggle)
libparser-mgc-perl 0.16-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 368 kB
  • sloc: perl: 1,676; makefile: 6; sh: 4
file content (46 lines) | stat: -rw-r--r-- 819 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

my $die;

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

sub parse
{
   my $self = shift;

   $self->maybe( sub {
      die $die if $die;
      $self->token_ident;
   } ) ||
      $self->token_int;
}

package TestParser2;
use base qw( Parser::MGC );

sub parse
{
   my $self = shift;
   $self->maybe( 'token_ident' ) || $self->token_int;
}

package main;

my $parser = TestParser->new;

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

$die = "Now have to fail\n";
ok( !eval { $parser->from_string( "456" ) }, '"456" with $die fails' );
is( $@, "Now have to fail\n", 'Exception from failure' );

is( TestParser2->new->from_string( "hello" ), "hello", '"hello" as method name' );

done_testing;