| 1 |
# -*- coding:utf-8; -*- |
| 2 |
use Test::More tests => 22; |
| 3 |
use Tk; |
| 4 |
use Tk::widgets qw(Text); |
| 5 |
use utf8; |
| 6 |
|
| 7 |
my $mw = MainWindow->new; |
| 8 |
my $tw = $mw->Text; |
| 9 |
my $all = <<'TEXT'; |
| 10 |
|
| 11 |
This is the text we are matching. |
| 12 |
$42 |
| 13 |
£42 |
| 14 |
|
| 15 |
TEXT |
| 16 |
|
| 17 |
$tw->insert(end => $all); |
| 18 |
is($tw->get('1.0','end -1 char'),$all,"Contents as expected"); |
| 19 |
|
| 20 |
my $eposn; |
| 21 |
my $ecoun; |
| 22 |
|
| 23 |
$eposn = $tw->search(-count => \$ecount, -exact => 'matching','1.0'); |
| 24 |
is($eposn,'2.24',"Correct -exact postion"); |
| 25 |
is($ecount,8,"Correct -exact length"); |
| 26 |
|
| 27 |
my $rposn; |
| 28 |
my $rcount; |
| 29 |
|
| 30 |
$rposn = $tw->search(-count => \$rcount, -regexp => 'matching','1.0'); |
| 31 |
is($rposn,'2.24',"Correct -regexp postion"); |
| 32 |
is($rcount,8,"Correct -regexp length"); |
| 33 |
|
| 34 |
$rposn = $tw->search(-count => \$rcount, -regexp => 'tHiS','1.0'); |
| 35 |
is($rposn,undef,"Correct non-match"); |
| 36 |
|
| 37 |
$rposn = $tw->search(-count => \$rcount, -nocase => -regexp => 'tHiS','1.0'); |
| 38 |
is($rposn,'2.0',"Correct -regexp -nocase"); |
| 39 |
|
| 40 |
$eposn = $tw->search(-count => \$rcount, -nocase => -exact => 'tHiS','1.0'); |
| 41 |
is($eposn,'2.0',"Correct -exact -nocase"); |
| 42 |
|
| 43 |
$eposn = $tw->search(-count => \$ecount, -nocase => -exact => '£42','1.0'); |
| 44 |
is($eposn,'4.5',"Correct -exact high-bit posn"); |
| 45 |
is($ecount,3,"Correct -exact high-bit len"); |
| 46 |
|
| 47 |
TODO: { |
| 48 |
local $TODO = "perl regexp bug pre perl5.8.1" if $] < 5.008001; |
| 49 |
$rcount = 0; |
| 50 |
$rposn = $tw->search(-count => \$rcount, -nocase => -regexp => '£42','1.0'); |
| 51 |
is($rposn,'4.5',"Correct -regexp high-bit posn"); |
| 52 |
is($rcount,3,"Correct -regexp high-bit len"); |
| 53 |
}; |
| 54 |
|
| 55 |
my $qposn; |
| 56 |
my $qcount; |
| 57 |
|
| 58 |
$qposn = $tw->search(-count => \$qcount, -regexp => qr/matching/,'1.0'); |
| 59 |
is($qposn,'2.24',"Correct -regexp qr// postion"); |
| 60 |
is($qcount,8,"Correct -regexp qr// length"); |
| 61 |
|
| 62 |
SKIP: { |
| 63 |
skip "Perl too old", 8 unless $] >= 5.008001; |
| 64 |
my $tword = qr/\bt\w+|\D\d+/i; |
| 65 |
my $start = '1.0'; |
| 66 |
my $i = 0; |
| 67 |
my @word = $all =~ /$tword/g; |
| 68 |
while ($qposn = $tw->search(-count => \$qcount, -regexp => $tword,$start,'end')) |
| 69 |
{ |
| 70 |
$start = $tw->index("$qposn +$qcount chars"); |
| 71 |
my $s = $tw->get($qposn,$start); |
| 72 |
print "# '$s'\n"; |
| 73 |
is($s,$word[$i++],"Right word"); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
$rposn = $tw->search(-count => \$rcount, -regexp => '£\d+','1.0'); |
| 78 |
$i = $tw->get("$rposn+1 chars","$rposn + $rcount chars"); |
| 79 |
is($rposn,'4.5',"Correct -regexp postion"); |
| 80 |
is($rcount,3,"Correct -regexp length"); |
| 81 |
is($i,'42',"UTF-8 Skip correct"); |
| 82 |
|
| 83 |
} |
| 84 |
|