/
glob_spec.rb
120 lines (99 loc) · 2.76 KB
/
glob_spec.rb
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
here = File.expand_path(File.dirname(__FILE__))
require "#{here}/spec_helper.rb"
require "rerun/glob"
module Rerun
describe Glob do
describe "#to_regexp" do
it "makes a regexp" do
Glob.new("foo*").to_regexp.should == /#{Glob::START_OF_FILENAME}foo.*#{Glob::END_OF_STRING}/
end
end
describe "#to_regexp_string" do
{
"x" => "x",
"*" => ".*",
"foo*" => "foo.*",
"*foo" => ".*foo",
"*foo*" => ".*foo.*",
"?" => ".",
"." => "\\.",
"{foo,bar,baz}" => "(foo|bar|baz)",
"{.txt,.md}" => '(\.txt|\.md)',
# pass through slash-escapes verbatim
"\\x" => "\\x",
"\\." => "\\.",
"\\*" => "\\*",
"\\\\" => "\\\\",
"**/*.txt" => "([^/]+/)*.*\\.txt",
}.each_pair do |glob_string, regexp_string|
specify glob_string do
Glob.new(glob_string).to_regexp_string.should ==
Glob::START_OF_FILENAME + regexp_string + Glob::END_OF_STRING
end
end
end
describe "specifically" do
{
"*.txt" => {
:hits=> [
"foo.txt",
"foo/bar.txt",
"/foo/bar.txt",
"bar.baz.txt",
"/foo/bar.baz.txt",
],
:misses => [
"foo.txt.html",
"tmp/foo.txt.html",
"/tmp/foo.txt.html",
#"tmp/.foo.txt",
]
},
"tmp/foo.*" => {
:hits => [
"tmp/foo.txt",
],
:misses => [
"stmp/foo.txt",
"tmp/foofoo.txt",
]
}
}.each_pair do |glob, paths|
paths[:hits].each do |path|
specify "#{glob} matches #{path}" do
Glob.new(glob).to_regexp.should =~ path
end
end
paths[:misses].each do |path|
specify "#{glob} doesn't match #{path}" do
Glob.new(glob).to_regexp.should_not =~ path
end
end
end
end
describe "#smoosh" do
def check_smoosh string, array
glob = Glob.new("")
glob.smoosh(string.split('')).should == array
end
it "ignores non-stars" do
check_smoosh "", []
check_smoosh "abc", ["a", "b", "c"]
end
it "passes solitary stars" do
check_smoosh "*", ["*"]
check_smoosh "a*b", ["a", "*", "b"]
end
it "smooshes two stars in a row into a single '**' string" do
check_smoosh "**", ["**"]
check_smoosh "a**b", ["a", "**", "b"]
check_smoosh "**b", ["**", "b"]
check_smoosh "a**", ["a", "**"]
end
it "treats **/ like **" do
check_smoosh "**/", ["**"]
check_smoosh "a**/b", ["a", "**", "b"]
end
end
end
end