~ubuntu-branches/ubuntu/quantal/padre/quantal

« back to all changes in this revision

Viewing changes to t/19_search.t

  • Committer: Package Import Robot
  • Author(s): Dominique Dumont
  • Date: 2012-05-12 12:40:00 UTC
  • mfrom: (1.3.5)
  • Revision ID: package-import@ubuntu.com-20120512124000-lpfuc4ny1eo3ecvx
Tags: 0.96+dfsg1-1
* new upstream version
* control: 
   * updated dependencies versions
   - removed  libformat-human-bytes-perl
   * cleanup unneeded version dependencies 
   - removed provides on padre-plugin-api-2. The only remaining packages
     depending on this meta package are already incompatible with new 
     padre  (and broken upstream)
   * no need to depend on libtest-warn-perl 0.24, 0.23 is enough
* patches:
   - removed hack-missing-context-menu (obsolete)
   + Skip upstream test that broke with new DBI. See patch header 
     for more details
   + fix spellings reported by lintian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
use Test::More;
 
6
 
 
7
BEGIN {
 
8
        unless ( $ENV{DISPLAY} or $^O eq 'MSWin32' ) {
 
9
                plan skip_all => 'Needs DISPLAY';
 
10
                exit 0;
 
11
        }
 
12
        plan tests => 27;
 
13
}
 
14
 
 
15
use Test::NoWarnings;
 
16
use t::lib::Padre;
 
17
use Padre::Search ();
 
18
 
 
19
 
 
20
 
 
21
 
 
22
 
 
23
######################################################################
 
24
# Basic tests for the core matches method
 
25
 
 
26
SCOPE: {
 
27
        my ( $start, $end, @matches ) = Padre::Search->matches(
 
28
                text  => "abc",
 
29
                regex => qr/x/,
 
30
                from  => 0,
 
31
                to    => 0,
 
32
        );
 
33
        is_deeply( \@matches, [], 'no match' );
 
34
}
 
35
 
 
36
SCOPE: {
 
37
        my (@matches) = Padre::Search->matches(
 
38
                text  => "abc",
 
39
                regex => qr/(b)/,
 
40
                from  => 0,
 
41
                to    => 0,
 
42
        );
 
43
        is_deeply( \@matches, [ 1, 2, [ 1, 2 ] ], 'one match' );
 
44
}
 
45
 
 
46
SCOPE: {
 
47
        my (@matches) = Padre::Search->matches(
 
48
                text  => "abcbxb",
 
49
                regex => qr/(b)/,
 
50
                from  => 0,
 
51
                to    => 0,
 
52
        );
 
53
        is_deeply( \@matches, [ 1, 2, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches' );
 
54
}
 
55
 
 
56
SCOPE: {
 
57
        my (@matches) = Padre::Search->matches(
 
58
                text  => "abcbxb",
 
59
                regex => qr/(b)/,
 
60
                from  => 1,
 
61
                to    => 2,
 
62
        );
 
63
        is_deeply( \@matches, [ 3, 4, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches' );
 
64
}
 
65
 
 
66
SCOPE: {
 
67
        my (@matches) = Padre::Search->matches(
 
68
                text  => "abcbxb",
 
69
                regex => qr/(b)/,
 
70
                from  => 3,
 
71
                to    => 4,
 
72
        );
 
73
        is_deeply( \@matches, [ 5, 6, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches' );
 
74
}
 
75
 
 
76
SCOPE: {
 
77
        my (@matches) = Padre::Search->matches(
 
78
                text  => "abcbxb",
 
79
                regex => qr/(b)/,
 
80
                from  => 5,
 
81
                to    => 6,
 
82
        );
 
83
        is_deeply( \@matches, [ 1, 2, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches, wrapping' );
 
84
}
 
85
 
 
86
SCOPE: {
 
87
        my (@matches) = Padre::Search->matches(
 
88
                text      => "abcbxb",
 
89
                regex     => qr/(b)/,
 
90
                from      => 5,
 
91
                to        => 6,
 
92
                backwards => 1,
 
93
        );
 
94
        is_deeply( \@matches, [ 3, 4, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches backwards' );
 
95
}
 
96
 
 
97
SCOPE: {
 
98
        my (@matches) = Padre::Search->matches(
 
99
                text      => "abcbxb",
 
100
                regex     => qr/(b)/,
 
101
                from      => 1,
 
102
                to        => 2,
 
103
                backwards => 1,
 
104
        );
 
105
        is_deeply( \@matches, [ 5, 6, [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ], 'three matches backwards wrapping' );
 
106
}
 
107
 
 
108
SCOPE: {
 
109
        my (@matches) = Padre::Search->matches(
 
110
                text  => "abcbxb",
 
111
                regex => qr/(b(.))/,
 
112
                from  => 1,
 
113
                to    => 2,
 
114
        );
 
115
        is_deeply( \@matches, [ 3, 5, [ 1, 3 ], [ 3, 5 ] ], '2 matches' );
 
116
}
 
117
 
 
118
SCOPE: {
 
119
        my (@matches) = Padre::Search->matches(
 
120
                text      => "abcbxb",
 
121
                regex     => qr/(b(.?))/,
 
122
                from      => 1,
 
123
                to        => 2,
 
124
                backwards => 1,
 
125
        );
 
126
        is_deeply( \@matches, [ 5, 6, [ 1, 3 ], [ 3, 5 ], [ 5, 6 ] ], 'three matches bw, wrap' );
 
127
}
 
128
 
 
129
SCOPE: {
 
130
        my $str = qq( perl ("שלום"); perl );
 
131
        my (@matches) = Padre::Search->matches(
 
132
                text  => $str,
 
133
                regex => qr/(perl)/,
 
134
                from  => 0,
 
135
                to    => 0,
 
136
        );
 
137
 
 
138
        # TODO are these really correct numbers?
 
139
        is_deeply( \@matches, [ 1, 5, [ 1, 5 ], [ 28, 32 ] ], 'two matches with unicode' );
 
140
        is( substr( $str, 1, 4 ), 'perl' );
 
141
}
 
142
 
 
143
SCOPE: {
 
144
        my $str = 'müssen';
 
145
        my (@matches) = Padre::Search->matches(
 
146
                text  => $str,
 
147
                regex => qr/(üss)/,
 
148
                from  => 0,
 
149
                to    => 0,
 
150
        );
 
151
        is_deeply( \@matches, [ 1, 7, [ 1, 7 ] ], 'one match with unicode regex' );
 
152
        is( substr( $str, 1, 4 ), 'üss' );
 
153
}
 
154
 
 
155
 
 
156
 
 
157
 
 
158
 
 
159
######################################################################
 
160
# Searching within a selection
 
161
 
 
162
my $text = <<'END_TEXT';
 
163
Roses are red,
 
164
Violets are blue,
 
165
All your base are belong to us.
 
166
END_TEXT
 
167
 
 
168
SCOPE: {
 
169
        my $search = new_ok( 'Padre::Search', [ find_term => 'are' ] );
 
170
        my ( $first_char, $last_char, @all ) = $search->matches(
 
171
                text  => $text,
 
172
                regex => qr/are/,
 
173
                from  => 0,
 
174
                to    => length($text),
 
175
        );
 
176
 
 
177
        ok( $first_char, 'calling matches with proper parameters should work' );
 
178
        is( $first_char, 6, 'found first entry at position 6' );
 
179
        is( $last_char,  9, 'found first entry ending at position 9' );
 
180
        is( substr( $text, $first_char, $last_char - $first_char ),
 
181
                'are',
 
182
                'position is correct',
 
183
        );
 
184
 
 
185
        is_deeply(
 
186
                \@all,
 
187
                [   [ 6,  9 ],
 
188
                        [ 23, 26 ],
 
189
                        [ 47, 50 ],
 
190
                ],
 
191
                'matches returns a correct structure',
 
192
        );
 
193
}
 
194
 
 
195
SCOPE: {
 
196
        my $search    = new_ok( 'Padre::Search', [ find_term => 'are' ] );
 
197
        my $sel_begin = 5;
 
198
        my $sel_end   = 30;
 
199
        my ( $first_char, $last_char, @all ) = $search->matches(
 
200
                text  => substr( $text, $sel_begin, $sel_end - $sel_begin ),
 
201
                regex => qr/are/,
 
202
                from  => 0,
 
203
                to    => $sel_end - $sel_begin,
 
204
        );
 
205
        ok( $first_char, 'calling matches with proper parameters should work' );
 
206
        is( $first_char, 1, 'found relative entry at position 1' );
 
207
        is( $last_char,  4, 'found relative entry ending at position 4' );
 
208
        is( substr( $text, $first_char + $sel_begin, $last_char - $first_char ),
 
209
                'are',
 
210
                'relative position is correct',
 
211
        );
 
212
 
 
213
        is_deeply(
 
214
                \@all,
 
215
                [   [ 1,  4 ],
 
216
                        [ 18, 21 ],
 
217
                ],
 
218
                'matches returns a correct relative structure (within selection)',
 
219
        );
 
220
}