/
inspector_spec.rb
178 lines (162 loc) · 6.25 KB
/
inspector_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
require 'spec_helper'
describe Looksee::Inspector do
include TemporaryClasses
describe "#inspect" do
before do
Looksee.stub(:default_lookup_path_options).and_return({})
Looksee.stub(:styles).and_return(Hash.new{'%s'})
@object = Object.new
temporary_module :M
temporary_class(:C) { include M }
Looksee.adapter.ancestors[@object] = [C, M]
end
describe "output width" do
before do
Looksee.adapter.public_methods[C] = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj']
Looksee.adapter.public_methods[M] = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh']
@lookup_path = Looksee::LookupPath.new(@object)
end
it "should columnize output to the given width, if any" do
inspector = Looksee::Inspector.new(@lookup_path, :visibilities => [:public], :width => 20)
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| aaa ccc eee ggg
| bbb ddd fff hhh
|C
| aa cc ee gg ii
| bb dd ff hh jj
EOS
end
it "should columnize output to the current terminal width, if detectable" do
original_columns = ENV['COLUMNS']
ENV['COLUMNS'] = '20'
begin
inspector = Looksee::Inspector.new(@lookup_path, :visibilities => [:public])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| aaa ccc eee ggg
| bbb ddd fff hhh
|C
| aa cc ee gg ii
| bb dd ff hh jj
EOS
ensure
ENV['COLUMNS'] = original_columns
end
end
it "should columnize output to the configured default otherwise" do
Looksee.stub(:default_width).and_return(20)
inspector = Looksee::Inspector.new(@lookup_path, :visibilities => [:public])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| aaa ccc eee ggg
| bbb ddd fff hhh
|C
| aa cc ee gg ii
| bb dd ff hh jj
EOS
end
end
it "should not show any blank lines if a module has no methods" do
Looksee.adapter.public_methods[M] = [:public1, :public2]
lookup_path = Looksee::LookupPath.new(@object)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| public1 public2
|C
EOS
end
it "should show singleton classes as class names in brackets" do
Looksee.adapter.ancestors[C] = [C.singleton_class]
Looksee.adapter.public_methods[C.singleton_class] = [:public1, :public2]
lookup_path = Looksee::LookupPath.new(C)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public])
inspector.inspect.should == <<-EOS.demargin.chomp
|[C]
| public1 public2
EOS
end
it "should handle singleton classes of singleton classes correctly" do
Looksee.adapter.ancestors[C.singleton_class] = [C.singleton_class.singleton_class]
Looksee.adapter.public_methods[C.singleton_class.singleton_class] = [:public1, :public2]
lookup_path = Looksee::LookupPath.new(C.singleton_class)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public])
inspector.inspect.should == <<-EOS.demargin.chomp
|[[C]]
| public1 public2
EOS
end
it "should only show methods of the selected visibilities" do
temporary_class :E
Looksee.adapter.set_methods(E, [:public], [:protected], [:private], [:undefined])
Looksee.adapter.ancestors[@object] = [E]
lookup_path = Looksee::LookupPath.new(@object)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:protected])
inspector.inspect.should == <<-EOS.demargin.chomp
|E
| protected
EOS
end
it "should show overridden methods if selected" do
Looksee.adapter.set_methods(C, [:public], [:protected], [:private], [:undefined])
Looksee.adapter.set_methods(M, [:public], [:protected], [:private], [:undefined])
lookup_path = Looksee::LookupPath.new(@object)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public, :overridden])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| public
|C
| public
EOS
end
it "should not show overridden methods if not selected" do
Looksee.adapter.set_methods(C, [:public], [:protected], [:private], [:undefined])
Looksee.adapter.set_methods(M, [:public], [:protected], [:private], [:undefined])
lookup_path = Looksee::LookupPath.new(@object)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public, :nooverridden])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
|C
| public
EOS
end
it "should only show methods that match the given filters, if any are given" do
Looksee.adapter.public_methods[C] = [:ab, :ax, :ba, :xa]
Looksee.adapter.public_methods[M] = [:ab, :ax, :ba, :xa]
lookup_path = Looksee::LookupPath.new(@object)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public, :overridden], :filters => [/^a/, 'b'])
inspector.inspect.should == <<-EOS.demargin.chomp
|M
| ab ax ba
|C
| ab ax ba
EOS
end
end
describe ".styles" do
before do
styles = {
:module => "`%s'",
:public => "{%s}",
:protected => "[%s]",
:private => "<%s>",
:undefined => "~%s~",
:overridden => "(%s)",
}
Looksee.stub(:styles).and_return(styles)
end
it "should delimit each word with the configured delimiters" do
temporary_class :C
c = C.new
Looksee.adapter.ancestors[c] = [C]
Looksee.adapter.set_methods(C, [:public], [:protected], [:private], [:undefined])
lookup_path = Looksee::LookupPath.new(c)
inspector = Looksee::Inspector.new(lookup_path, :visibilities => [:public, :protected, :private, :undefined, :overridden])
inspector.inspect.should == <<-EOS.demargin.chomp
|\`C\'
| <private> [protected] {public} ~undefined~
EOS
end
end
end