1#!perl -w
2use strict;
3use Test::More tests => 44;
4
5BEGIN { use_ok('Text::Glob', qw( glob_to_regex match_glob ) ) }
6
7my $regex = glob_to_regex( 'foo' );
8is( ref $regex, 'Regexp', "glob_to_regex hands back a regex" );
9ok( 'foo'    =~ $regex, "matched foo" );
10ok( 'foobar' !~ $regex, "didn't match foobar" );
11
12ok(  match_glob( 'foo', 'foo'      ), "absolute string" );
13ok( !match_glob( 'foo', 'foobar'   ) );
14
15ok(  match_glob( 'foo.*', 'foo.'     ), "* wildcard" );
16ok(  match_glob( 'foo.*', 'foo.bar'  ) );
17ok( !match_glob( 'foo.*', 'gfoo.bar' ) );
18
19ok(  match_glob( 'foo.?p', 'foo.cp' ), "? wildcard" );
20ok( !match_glob( 'foo.?p', 'foo.cd' ) );
21
22ok(  match_glob( 'foo.{c,h}', 'foo.h' ), ".{alternation,or,something}" );
23ok(  match_glob( 'foo.{c,h}', 'foo.c' ) );
24ok( !match_glob( 'foo.{c,h}', 'foo.o' ) );
25
26ok(  match_glob( 'foo.\\{c,h}\\*', 'foo.{c,h}*' ), '\escaping' );
27ok( !match_glob( 'foo.\\{c,h}\\*', 'foo.\\c' ) );
28
29ok(  match_glob( 'foo.(bar)', 'foo.(bar)'), "escape ()" );
30
31ok( !match_glob( '*.foo',  '.file.foo' ), "strict . rule fail" );
32ok(  match_glob( '.*.foo', '.file.foo' ), "strict . rule match" );
33{
34local $Text::Glob::strict_leading_dot;
35ok(  match_glob( '*.foo', '.file.foo' ), "relaxed . rule" );
36}
37
38ok( !match_glob( '*.fo?',   'foo/file.fob' ), "strict wildcard / fail" );
39ok(  match_glob( '*/*.fo?', 'foo/file.fob' ), "strict wildcard / match" );
40{
41local $Text::Glob::strict_wildcard_slash;
42ok(  match_glob( '*.fo?', 'foo/file.fob' ), "relaxed wildcard /" );
43}
44
45
46ok( !match_glob( 'foo/*.foo', 'foo/.foo' ), "more strict wildcard / fail" );
47ok(  match_glob( 'foo/.f*',   'foo/.foo' ), "more strict wildcard / match" );
48{
49local $Text::Glob::strict_wildcard_slash;
50ok(  match_glob( '*.foo', 'foo/.foo' ), "relaxed wildcard /" );
51}
52
53ok(  match_glob( 'f+.foo', 'f+.foo' ), "properly escape +" );
54ok( !match_glob( 'f+.foo', 'ffff.foo' ) );
55
56ok(  match_glob( "foo\nbar", "foo\nbar" ), "handle embedded \\n" );
57ok( !match_glob( "foo\nbar", "foobar" ) );
58
59ok(  match_glob( 'test[abc]', 'testa' ), "[abc]" );
60ok(  match_glob( 'test[abc]', 'testb' ) );
61ok(  match_glob( 'test[abc]', 'testc' ) );
62ok( !match_glob( 'test[abc]', 'testd' ) );
63
64ok(  match_glob( 'foo$bar.*', 'foo$bar.c'), "escaping \$" );
65
66ok(  match_glob( 'foo^bar.*', 'foo^bar.c'), "escaping ^" );
67
68ok(  match_glob( 'foo|bar.*', 'foo|bar.c'), "escaping |" );
69
70
71ok(  match_glob( '{foo,{bar,baz}}', 'foo'), "{foo,{bar,baz}}" );
72ok(  match_glob( '{foo,{bar,baz}}', 'bar') );
73ok(  match_glob( '{foo,{bar,baz}}', 'baz') );
74ok( !match_glob( '{foo,{bar,baz}}', 'foz') );
75
76ok(  match_glob( 'foo@bar', 'foo@bar'), '@ character');
77ok(  match_glob( 'foo$bar', 'foo$bar'), '$ character');
78ok(  match_glob( 'foo%bar', 'foo%bar'), '% character');
79