-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathexpander_spec.rb
318 lines (254 loc) · 8.12 KB
/
expander_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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
$:.unshift "spec/"
require './spec/spec_helper'
require "xiki/core/launcher"
require "xiki/core/xik"
Dir["./lib/xiki/handlers/*_handler.rb"].each{|o|
require o.sub("./lib/", "")
}
%w"path code tree menu command_suggester pre_pattern pattern file_tree bookmarks".each {|o| require "xiki/core/#{o}"}
require 'xiki/core/expander'
require 'xiki/core/pattern'
require 'xiki/core/control_tab'
# describe Expander, "#expand" do
describe Expander, "#extract_ancestors" do
it "pulls out one path" do
args = "a/=b/", {}
Expander.extract_ancestors *args
args.should == ["b/", {:ancestors=>["a/"]}]
end
it "works when no nesting" do
args = "a/b/", {}
Expander.extract_ancestors *args
args.should == ["a/b/", {}]
end
it "ignores quoted path" do
args = "a/| a/=b/", {}
Expander.extract_ancestors *args
args.should == ["b/", {:ancestors=>["a/| a/"]}]
end
end
describe Expander, "#expand_file_path" do
it "expands dot" do
Expander.expand_file_path("../a").should =~ %r"\w+/a$"
Expander.expand_file_path("./a").should =~ %r"\w+/a$"
end
it "expands home dir" do
Expander.expand_file_path("~/a").should =~ %r"\w+/a$"
end
it "expands bookmarks" do
stub(Bookmarks).[]("%d") {"/tmp/dir/"}
Expander.expand_file_path("%d/a//b").should == "/tmp/dir/a//b"
Expander.expand_file_path("%d").should == "/tmp/dir"
Expander.expand_file_path("%d/").should == "/tmp/dir/"
end
it "doesn't remove double slashes for home and current dir" do
Expander.expand_file_path("./a//b").should =~ %r"a//b"
Expander.expand_file_path("~/a//b").should =~ %r"a//b"
end
it "doesn't remove double slashes for bookmarks" do
stub(Bookmarks).[]("%links") {"/tmp/file.txt"}
Expander.expand_file_path("%links//").should == "/tmp/file.txt//"
end
end
describe Expander, "#parse" do
it "handles pure dir paths" do
Expander.parse("/tmp/a/b/").should ==
{:file_path=>"/tmp/a/b/"}
end
it "handles file paths" do
Expander.parse("/tmp/a/b").should ==
{:file_path=>"/tmp/a/b"}
end
it "handles file path with spaces" do
Expander.parse("/tmp/a/b c").should ==
{:file_path=>"/tmp/a/b c"}
end
it "just passes through when input already a hash" do
Expander.parse(:foo=>"bar").should ==
{:foo=>"bar"}
end
it "handles menufied paths" do
Expander.parse("/tmp/a//").should ==
{:menufied=>"/tmp/a", :path=>"/tmp/a//"}
end
it "handles menufied path with items" do
Expander.parse("/tmp/a//b/").should ==
{:menufied=>"/tmp/a", :items=>["b"], :path=>"/tmp/a//b/"}
end
it "handles filesystem root menufied path" do
Expander.parse("//").should ==
{:menufied=>"/", :path=>"//"}
end
it "handles name that looks kind of menufied" do
Expander.parse("a/http://notmenufied.com/").should ==
{:name=>"a", :items=>["http:", "", "notmenufied.com"],
:path=>"a/http://notmenufied.com/"
}
end
it "handles names" do
Expander.parse("a").should ==
{:name=>"a", :path => "a"}
end
it "handles name when space" do
Expander.parse("a b").should ==
{:name=>"a_b", :path => "a b"}
end
it "handles name with items" do
Expander.parse("a/b/c/").should ==
{:name=>"a", :items=>["b", "c"], :path => "a/b/c/"}
end
it "handles name with quoted items" do
Expander.parse("a/| hi").should ==
{:name=>"a", :items=>["| hi"], :path => "a/| hi"}
end
it "handles name with quoted slash" do
Expander.parse("a/| foo/yau").should ==
{:name=>"a", :items=>["| foo", "yau"], :path => "a/| foo/yau"}
end
it "handles patterns" do
Expander.parse("select * from users").should ==
{:path=>"select * from users"}
end
it "handles pattern that looks kind of like a file path" do
Expander.parse("/user@site.com/a/").should ==
{:path=>"/user@site.com/a/"}
end
it "handles name with list of items" do
Expander.parse("a", ["b", "c"]).should ==
{:name=>"a", :items=>["b", "c"], :path => "a/b/c"}
end
it "handles symbol with list of items" do
Expander.parse(:a, ["b", "c"]).should ==
{:name=>"a", :items=>["b", "c"]}
end
it "merges path with item and list of items" do
Expander.parse("a/b", ["c", "d"]).should ==
{:name=>"a", :items=>["b", "c", "d"], :path=>"a/b/c/d"}
end
it "handles ancestors in string" do
Expander.parse("z/=a/").should ==
{:name=>"a", :ancestors=>["z/"], :path => "a/"}
end
it "handles ancestors with path in string" do
Expander.parse("x/y/=a/b/").should ==
{:name=>"a", :items=>["b"], :ancestors=>["x/y/"], :path => "a/b/"}
end
it "pulls out ancestors when first arg is array" do
Expander.parse(["x/y/", "m/n/", "a/b/"]).should ==
{:name=>"a", :items=>["b"], :ancestors=>["x/y/", "m/n/"],
:path => "a/b/"
}
end
it "doesn't create ancestors when first arg is array of size 1" do
Expander.parse(["a/b/"]).should ==
{:name=>"a", :items=>["b"], :path => "a/b/"}
end
it "recognizes escaped slashes" do
Expander.parse("echo/a;/b").should == {
:items => ["a/b"],
:name => "echo",
:path => "echo/a;/b"
}
end
it "pulls out extension" do
Expander.parse("echo.txt").should == {
:name=>"echo",
:extension=>".txt",
:path=>"echo.txt"
}
end
it "pulls out path and extension" do
Expander.parse("echo.txt/a/b").should == {
:items=>["a", "b"],
:name=>"echo",
:extension=>".txt",
:path=>"echo.txt/a/b"
}
end
it "pulls out period-only extension" do
Expander.parse("echo.").should == {
:name=>"echo",
:extension=>".",
:path=>"echo."
}
end
it "moves task items into :task" do
options = Expander.parse("hi/* delete")
options.should == {
:task=>"delete",
:name=>"hi",
:path=>"hi",
}
end
# Also test these...
# options = Expander.parse("select * from/* delete")
# options = Expander.parse("/tmp/* delete")
it "moves pattern task items into :task" do
options = Expander.parse("select * from hi/* delete")
options.should == {
:task=>"delete",
:path=>"select * from hi"
}
end
it "moves file task items into :task" do
options = Expander.parse("/tmp/* delete")
options.should == {
:task=>"delete",
:file_path=>"/tmp"
}
end
end
describe Expander, "#expand_literal_command method" do
before(:each) do
stub_menu_path_dirs # Has to be before each for some reason
end
it "expands command text with path" do
txt = Expander.expand_literal_command "a/\n b", :path=>"a"
txt.should == 'b'
end
it "expands when embedded code" do
txt = Expander.expand_literal_command "a/\n ! 1 + 1", :path=>"a"
txt.should == '2'
end
end
describe Expander, "#expand method" do
before(:each) do
stub_menu_path_dirs # Has to be before each for some reason
end
it "expands menu when no path" do
Expander.def(:echo) { |path| path.inspect }
Expander.expand("echo").should == '[]'
end
it "expands menu when path" do
Expander.def(:echo) { |path| path.inspect }
Expander.expand("echo/a").should == '["a"]'
end
it "takes a path list as 2nd arg" do
Expander.def(:echo) { |path| path.inspect }
Expander.expand("echo", ["a", "b"]).should == '["a", "b"]'
end
it "expands menu in XIKI_PATH" do
Expander.expand("dd").should == "+ a/\n+ b/\n+ cccc/\n+ craig/\n+ keith/\n"
end
it "expands plain file path" do
Expander.expand("#{Xiki.dir}spec/fixtures/menu/dr/").should == "+ a.rb\n+ b.rb\n"
end
it "expands menufied path" do
Expander.expand("#{Xiki.dir}spec/fixtures/menu/dr//").should == "+ a/\n+ b/\n"
end
# it "expands when literal text of command passed" do
# Expander.expand(["a"], :command_text=>"a/\n b").should == "b"
# end
# it "expands when literal text of command with path" do
# Expander.expand(["a/b"], :command_text=>"a/\n b/\n c").should == "c"
# end
end
describe Expander, ".extract_task_items" do
before(:each) do
stub_menu_path_dirs # Has to be before each for some reason
end
it "extracts when normal" do
# Expander.def(:echo) { |path| path.inspect }
# Expander.expand("echo").should == '[]'
end
end