File: 20_String_Tokenizer_Iterator_test.t

package info (click to toggle)
libstring-tokenizer-perl 0.06-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 419; makefile: 2
file content (180 lines) | stat: -rw-r--r-- 5,565 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 100;

BEGIN { 
    use_ok('String::Tokenizer') 
};

# first check that our inner class 
# cannot be called from outside
eval {
    String::Tokenizer::Iterator->new();
};
like($@, qr/Insufficient Access Priviledges/, '... this should have died');

# it can also parse reasonably well formated perl code  
my $STRING = <<STRING_TO_TOKENIZE;
sub test {
    my (\$arg) = \@_;
	if (\$arg == 10){
		return 1;
	}
	return 0;
}

STRING_TO_TOKENIZE

my @expected = qw(sub test { my ( $arg ) = @_ ; if ( $arg == 10 ) { return 1 ; } return 0 ; });

my $st = String::Tokenizer->new($STRING, '();{}');
isa_ok($st, "String::Tokenizer");  

can_ok("String::Tokenizer::Iterator", 'new');

my $i = $st->iterator();
isa_ok($i, "String::Tokenizer::Iterator");

can_ok($i, 'reset');
can_ok($i, 'hasNextToken');
can_ok($i, 'hasPrevToken');
can_ok($i, 'nextToken');
can_ok($i, 'prevToken');
can_ok($i, 'currentToken');
can_ok($i, 'lookAheadToken');
can_ok($i, 'skipToken');
can_ok($i, 'skipTokens');
can_ok($i, 'skipTokensUntil');
can_ok($i, 'collectTokensUntil');

my @iterator_output;
push @iterator_output => $i->nextToken() while $i->hasNextToken();

ok(!defined($i->nextToken()), '... this is undefined');
ok(!defined($i->lookAheadToken()), '... this is undefined');

is_deeply(
    \@iterator_output,
    \@expected,
    '... this is the output we would expect'); 
  
my @reverse_iterator_output;
push @reverse_iterator_output => $i->prevToken() while $i->hasPrevToken();  

ok(!defined($i->prevToken()), '... this is undefined');
ok(!defined($i->lookAheadToken()), '... this is undefined');
  
is_deeply(
    \@reverse_iterator_output,
    [ reverse @expected ],
    '... this is the output we would expect'); 

my $look_ahead;
while ($i->hasNextToken()) {  
    my $next = $i->nextToken();
    my $current = $i->currentToken();
    is($look_ahead, $next, '... our look ahead matches out next') if defined $look_ahead;
    is($current, $next, '... our current matches out next');
    $look_ahead = $i->lookAheadToken();  
}

$i->reset();
            
my @expected5 = qw({ ( ) @_ if $arg 10 { 1 } 0 });              
  
my @skip_output;
$i->skipTokens(2); 
while ($i->hasNextToken()) {
    push @skip_output => $i->nextToken();
    $i->skipToken();    
}

is_deeply(
        \@skip_output,
        \@expected5,
        '... this is the output we would expect');  
  
# test the skipTokensUntil and collectTokensUntil function with a double quoted string

{
    my $st = String::Tokenizer->new('this "is a good way" to "test a double quoted" string' , '"');
    isa_ok($st, "String::Tokenizer");  
        
    my $i = $st->iterator();
    
    isa_ok($i, "String::Tokenizer::Iterator");  

    is($i->nextToken(), 'this', '... got the right start token');
    ok($i->skipTokensUntil('"'), '... this will successfully skip');
    is($i->nextToken(), 'is', '... got the right token next expected');
    ok($i->skipTokensUntil('"'), '... this will successfully skip');
    is($i->nextToken(), 'to', '... got the right token next expected');
    is($i->nextToken(), '"', '... got the right token next expected');    
    is_deeply(
        [ $i->collectTokensUntil('"') ],
        [ qw/test a double quoted/ ],
        '... got the collection we expected');
    is($i->nextToken(), 'string', '... got the right token next expected');      
}

{
    my $st = String::Tokenizer->new('this "is a good way" to "test a double quoted" string' , '"');
    isa_ok($st, "String::Tokenizer");  
        
    my $i = $st->iterator();
    
    isa_ok($i, "String::Tokenizer::Iterator");  

    is($i->nextToken(), 'this', '... got the right start token');
    ok(!$i->skipTokensUntil('?'), '... this will not successfully match and so not skip');   
    is_deeply(
        [ $i->collectTokensUntil('"') ],
        [ ],
        '... got the collection (or lack thereof) we expected');
    is_deeply(
        [ $i->collectTokensUntil('"') ],
        [ qw/is a good way/ ],
        '... got the collection (or lack thereof) we expected');
    is($i->nextToken(), 'to', '... got the right token next expected'); 
    is_deeply(
        [ $i->collectTokensUntil('not found') ],
        [ ],
        '... got the collection (or lack thereof) we expected');    
    is($i->nextToken(), '"', '... got the right token next expected');                          
}

{
    my $st = String::Tokenizer->new(
                    'this is "a     good way" to "test a   double quoted  " string' , 
                    '"', 
                    String::Tokenizer->RETAIN_WHITESPACE
                    );
    isa_ok($st, "String::Tokenizer");  
        
    my $i = $st->iterator();
    
    isa_ok($i, "String::Tokenizer::Iterator");  

    is($i->nextToken(), 'this', '... got the right start token');
    $i->skipTokenIfWhitespace();
    is($i->lookAheadToken(), 'is', '... got the right start token');
    $i->skipTokenIfWhitespace();
    is($i->nextToken(), 'is', '... got the right start token');    
    $i->skipTokenIfWhitespace();
    is($i->nextToken(), '"', '... got the right start token');
    
    is_deeply(
        [ $i->collectTokensUntil('"') ],
        [ "a", "     ", "good", " ", "way" ],
        '... got the collection (or lack thereof) we expected');
        
    is($i->lookAheadToken(), ' ', '... our next token is whitespace');
    $i->skipTokenIfWhitespace();    
    is($i->nextToken(), 'to', '... got the right token next expected'); 
    
    # this is enough for now, we dont need to test the rest of the string
}