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
|
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2::TestHelper tests => 24;
use FindBin;
use lib "$FindBin::Bin";
use my_helper;
use Test::Builder;
use Glib qw(TRUE FALSE);
use Gtk2::SourceView2;
exit tests();
sub tests {
test_search();
return 0;
}
sub test_search {
my $buffer = get_buffer();
my @results;
my $start = $buffer->get_start_iter;
# Forward search from the start
@results = Gtk2::SourceView2::Iter->forward_search($start, "Atreus", [ 'text-only' ]);
is_text_at(
[ @results ],
{
start_line => 4,
start_offset => 43,
end_line => 4,
end_offset => 49,
},
"Forward search"
);
# Forward search from the start for a non existing word
@results = Gtk2::SourceView2::Iter->forward_search($start, "Homer", [ 'text-only' ]);
is_text_at(
[ @results ],
undef,
"Forward search not found"
);
# Multi line
@results = Gtk2::SourceView2::Iter->forward_search(
$start, "a\nprey to Dogs", [ 'text-only', 'case-insensitive' ]
);
is_text_at(
[ @results ],
{
start_line => 2,
start_offset => 61,
end_line => 3,
end_offset => 12,
},
"Forward search multi-line"
);
my $end = $buffer->get_end_iter;
# Look backwards for a word that's not there
@results = Gtk2::SourceView2::Iter->backward_search($end, "Homer", [ 'text-only' ]);
is_text_at(
[ @results ],
undef,
"Backward search not found"
);
# Look backwards for a word
@results = Gtk2::SourceView2::Iter->backward_search($end, "Achilles", [ ]);
is_text_at(
[ @results ],
{
start_line => 5,
start_offset => 10,
end_line => 5,
end_offset => 18,
},
"Backward search for Achilles"
);
# Look forwards for the same word as before
@results = Gtk2::SourceView2::Iter->forward_search($start, "Achilles", [ ]);
is_text_at(
[ @results ],
{
start_line => 0,
start_offset => 30,
end_line => 0,
end_offset => 38,
},
"Forward search for Achilles"
);
}
sub is_text_at {
my ($iters, $positions, $message) = @_;
my ($start, $end) = @{ $iters };
my $tester = Test::Builder->new();
if ($start && $positions) {
$tester->is_num($start->get_line, $positions->{start_line}, "$message (start line)");
$tester->is_num($start->get_line_offset, $positions->{start_offset}, "$message (start offset)");
}
else {
$tester->ok(! defined $start, "Start line for undef");
$tester->ok(! defined $positions, "Start offset for undef");
}
if ($end && $positions) {
$tester->is_num($end->get_line, $positions->{end_line}, "$message (end line)");
$tester->is_num($end->get_line_offset, $positions->{end_offset}, "$message (end offset)");
}
else {
$tester->ok(! defined $end, "$message (end line for undef)");
$tester->ok(! defined $positions, "$message (end offset for undef)");
}
}
sub get_buffer {
my $buffer = Gtk2::SourceView2::Buffer->new(undef);
$buffer->set_text(<<'__ILLIAD__');
Sing, O goddess, the anger of Achilles son of Peleus, that
brought countless ills upon the Achaeans. Many a brave soul did
it send hurrying down to Hades, and many a hero did it yield a
prey to dogs and vultures, for so were the counsels of Jove
fulfilled from the day on which the son of Atreus, king of men,
and great Achilles, first fell out with one another.
__ILLIAD__
return $buffer;
}
|