/
test_show_doc.rb
333 lines (283 loc) · 9.08 KB
/
test_show_doc.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
require 'helper'
if !mri18_and_no_real_source_location?
describe "show-doc" do
before do
@o = Object.new
end
it 'should output a method\'s documentation' do
pry_eval("show-doc sample_method").should =~ /sample doc/
end
it 'should output a method\'s documentation with line numbers' do
pry_eval("show-doc sample_method -l").should =~ /\d: sample doc/
end
it 'should output a method\'s documentation with line numbers (base one)' do
pry_eval("show-doc sample_method -b").should =~ /1: sample doc/
end
it 'should output a method\'s documentation if inside method without needing to use method name' do
# sample comment
def @o.sample
pry_eval(binding, 'show-doc').should =~ /sample comment/
end
@o.sample
end
it "should be able to find super methods" do
c = Class.new{
# classy initialize!
def initialize(*args); end
}
d = Class.new(c){
# grungy initialize??
def initialize(*args, &block); end
}
o = d.new
# instancey initialize!
def o.initialize; end
t = pry_tester(binding)
t.eval("show-doc o.initialize").should =~ /instancey initialize/
t.eval("show-doc --super o.initialize").should =~ /grungy initialize/
t.eval("show-doc o.initialize -ss").should =~ /classy initialize/
begin
require 'pry-doc'
t.eval("show-doc --super o.initialize -ss").should ==
t.eval("show-doc Object#initialize")
rescue LoadError
end
end
describe "rdoc highlighting" do
it "should syntax highlight code in rdoc" do
c = Class.new{
# This can initialize your class:
#
# a = c.new :foo
#
# @param foo
def initialize(foo); end
}
begin
t = pry_tester(binding)
t.eval("show-doc c#initialize").should =~ /c.new :foo/
Pry.config.color = true
# I don't want the test to rely on which colour codes are there, just to
# assert that "something" is being colourized.
t.eval("show-doc c#initialize").should.not =~ /c.new :foo/
ensure
Pry.config.color = false
end
end
it "should syntax highlight `code` in rdoc" do
c = Class.new{
# After initializing your class with `c.new(:foo)`, go have fun!
#
# @param foo
def initialize(foo); end
}
begin
t = pry_tester(binding)
t.eval("show-doc c#initialize").should =~ /c.new\(:foo\)/
Pry.config.color = true
# I don't want the test to rely on which colour codes are there, just to
# assert that "something" is being colourized.
t.eval("show-doc c#initialize").should.not =~ /c.new\(:foo\)/
ensure
Pry.config.color = false
end
end
it "should not syntax highlight `` inside code" do
c = Class.new{
# Convert aligned output (from many shell commands) into nested arrays:
#
# a = decolumnize `ls -l $HOME`
#
# @param output
def decolumnize(output); end
}
begin
t = pry_tester(binding)
Pry.config.color = true
t.eval("show-doc c#decolumnize").should =~ /ls -l \$HOME/
t.eval("show-doc c#decolumnize").should.not =~ /`ls -l \$HOME`/
ensure
Pry.config.color = false
end
end
end
describe "on sourcable objects" do
it "should show documentation for object" do
# this is a documentation
hello = proc { puts 'hello world!' }
mock_pry(binding, "show-doc hello").should =~ /this is a documentation/
end
end
describe "on modules" do
before do
# god this is boring1
class ShowSourceTestClass
def alpha
end
end
# god this is boring2
module ShowSourceTestModule
def alpha
end
end
# god this is boring3
ShowSourceTestClassWeirdSyntax = Class.new do
def beta
end
end
# god this is boring4
ShowSourceTestModuleWeirdSyntax = Module.new do
def beta
end
end
end
after do
Object.remove_const :ShowSourceTestClass
Object.remove_const :ShowSourceTestClassWeirdSyntax
Object.remove_const :ShowSourceTestModule
Object.remove_const :ShowSourceTestModuleWeirdSyntax
end
describe "basic functionality, should show docs for top-level module definitions" do
it 'should show docs for a class' do
pry_eval("show-doc ShowSourceTestClass").should =~
/god this is boring1/
end
it 'should show docs for a module' do
pry_eval("show-doc ShowSourceTestModule").should =~
/god this is boring2/
end
it 'should show docs for a class when Const = Class.new syntax is used' do
pry_eval("show-doc ShowSourceTestClassWeirdSyntax").should =~
/god this is boring3/
end
it 'should show docs for a module when Const = Module.new syntax is used' do
pry_eval("show-doc ShowSourceTestModuleWeirdSyntax").should =~
/god this is boring4/
end
end
if !Pry::Helpers::BaseHelpers.mri_18?
describe "in REPL" do
it 'should find class defined in repl' do
t = pry_tester
t.eval <<-RUBY
# hello tobina
class TobinaMyDog
def woof
end
end
RUBY
t.eval('show-doc TobinaMyDog').should =~ /hello tobina/
Object.remove_const :TobinaMyDog
end
end
end
it 'should lookup module name with respect to current context' do
constant_scope(:AlphaClass, :BetaClass) do
# top-level beta
class BetaClass
def alpha
end
end
class AlphaClass
# nested beta
class BetaClass
def beta
end
end
end
pry_eval(AlphaClass, "show-doc BetaClass").should =~ /nested beta/
end
end
it 'should look up nested modules' do
constant_scope(:AlphaClass) do
class AlphaClass
# nested beta
class BetaClass
def beta
end
end
end
pry_eval("show-doc AlphaClass::BetaClass").should =~
/nested beta/
end
end
describe "show-doc -a" do
it 'should show the docs for all monkeypatches defined in different files' do
# local monkeypatch
class TestClassForShowSource
def beta
end
end
result = pry_eval("show-doc TestClassForShowSource -a")
result.should =~ /used by/
result.should =~ /local monkeypatch/
end
end
describe "when no class/module arg is given" do
before do
module TestHost
# hello there froggy
module M
def d; end
def e; end
end
end
end
after do
Object.remove_const(:TestHost)
end
it 'should return doc for current module' do
pry_eval(TestHost::M, "show-doc").should =~ /hello there froggy/
end
end
describe "should skip over broken modules" do
before do
module TestHost
# hello
module M
binding.eval("def a; end", "dummy.rb", 1)
binding.eval("def b; end", "dummy.rb", 2)
binding.eval("def c; end", "dummy.rb", 3)
end
# goodbye
module M
def d; end
def e; end
end
end
end
after do
Object.remove_const(:TestHost)
end
it 'should return doc for first valid module' do
result = pry_eval("show-doc TestHost::M")
result.should =~ /goodbye/
result.should.not =~ /hello/
end
end
end
describe "on commands" do
# mostly copied & modified from test_help.rb
before do
@oldset = Pry.config.commands
@set = Pry.config.commands = Pry::CommandSet.new do
import Pry::Commands
end
end
after do
Pry.config.commands = @oldset
end
it 'should display help for a specific command' do
pry_eval('show-doc ls').should =~ /Usage: ls/
end
it 'should display help for a regex command with a "listing"' do
@set.command /bar(.*)/, "Test listing", :listing => "foo" do; end
pry_eval('show-doc foo').should =~ /Test listing/
end
it 'should display help for a command with a spaces in its name' do
@set.command "command with spaces", "description of a command with spaces" do; end
pry_eval('show-doc "command with spaces"').should =~ /description of a command with spaces/
end
end
end
end