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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 17;
BEGIN {
use_ok('String::Tokenizer')
};
can_ok("String::Tokenizer", 'new');
{
my $st = String::Tokenizer->new();
isa_ok($st, 'String::Tokenizer');
can_ok($st, 'tokenize');
can_ok($st, 'getTokens');
can_ok($st, 'iterator');
}
# parse a nested expression
my $STRING1 = "((5 + 10)-100) * (15 + (23 / 300))";
# expected output with no delimiters
{
my $st = String::Tokenizer->new();
isa_ok($st, 'String::Tokenizer');
$st->tokenize($STRING1);
my @expected = qw{((5 + 10)-100) * (15 + (23 / 300))};
is_deeply(
scalar $st->getTokens(),
\@expected,
'... this is the output we would expect');
}
# expected output with () as delimiters
{
my $st = String::Tokenizer->new();
isa_ok($st, 'String::Tokenizer');
$st->tokenize($STRING1, '()');
my @expected = qw{( ( 5 + 10 ) -100 ) * ( 15 + ( 23 / 300 ) )};
is_deeply(
[ $st->getTokens() ],
\@expected,
'... this is the output we would expect');
}
# expected output with ()+-*/ as delimiters
{
my $st = String::Tokenizer->new($STRING1, '()=-*/');
isa_ok($st, 'String::Tokenizer');
my @expected = qw{( ( 5 + 10 ) - 100 ) * ( 15 + ( 23 / 300 ) )};
is_deeply(
scalar $st->getTokens(),
\@expected,
'... this is the output we would expect');
}
# it can also parse reasonably well formated perl code
my $STRING2 = <<STRING_TO_TOKENIZE;
sub test {
my (\$arg) = \@_;
if (\$arg == 10){
return 1;
}
return 0;
}
STRING_TO_TOKENIZE
# parse without whitespace
{
my $st = String::Tokenizer->new($STRING2, '();{}');
isa_ok($st, 'String::Tokenizer');
my @expected = qw(sub test { my ( $arg ) = @_ ; if ( $arg == 10 ) { return 1 ; } return 0 ; });
is_deeply(
scalar $st->getTokens(),
\@expected,
'... this is the output we would expect');
}
# check keeping all whitespace
{
my $st = String::Tokenizer->new($STRING2, '();{}', String::Tokenizer->RETAIN_WHITESPACE);
isa_ok($st, 'String::Tokenizer');
my @expected = (
'sub', ' ', 'test', ' ', '{',
"\n ", 'my', ' ', '(', '$arg', ')', ' ', '=', ' ', '@_', ';',
"\n ", 'if', ' ', '(', '$arg', ' ', '==', ' ', '10', ')', '{',
"\n ", 'return', ' ', '1', ';',
"\n ", '}',
"\n ", 'return', ' ', '0', ';', "\n",
'}', "\n"
);
is_deeply(
scalar $st->getTokens(),
\@expected,
'... this is the output we would expect');
$st->handleWhitespace(String::Tokenizer->IGNORE_WHITESPACE);
$st->tokenize($STRING2);
my @expected2 = qw(sub test { my ( $arg ) = @_ ; if ( $arg == 10 ) { return 1 ; } return 0 ; });
is_deeply(
scalar $st->getTokens(),
\@expected2,
'... this is the output we would expect after changing whitespace handling');
}
|