/
enumerable_exhibit_spec.rb
229 lines (194 loc) · 5.51 KB
/
enumerable_exhibit_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
require_relative '../spec_helper_lite'
require_relative '../../lib/display_case'
describe DisplayCase::EnumerableExhibit do
describe "class" do
subject { DisplayCase::EnumerableExhibit }
it "is applicable to Enumerables" do
assert subject.applicable_to?([])
assert subject.applicable_to?({})
end
it "is inapplicable to singular objects" do
refute subject.applicable_to?(Object.new)
end
it "is itself enumerable" do
assert (subject < Enumerable)
end
end
subject { DisplayCase::EnumerableExhibit.new(model, context) }
let(:model) { ["e1", "e2", "e3"] }
let(:context) { Object.new }
before do
# #exhibit is part of the superclass interface, not this class'
# interface, so it is fair game for stubbing
stub(subject).exhibit {|model|
@last_exhibited = model
"exhibit(#{model})"
}
end
describe "#each" do
it "exhibits each element" do
results = []
subject.each do |e| results << e end
results.must_equal(["exhibit(e1)", "exhibit(e2)", "exhibit(e3)"])
end
it "returns enumerator" do
subject.each.must_be_instance_of(Enumerator)
end
it "chains with_index" do
results = []
subject.each.with_index do |*e| results << e end
results.must_equal([
["exhibit(e1)", 0],
["exhibit(e2)", 1],
["exhibit(e3)", 2]])
end
end
describe "#each_with_index" do
it "exhibits each element" do
results = []
subject.each_with_index do |*e| results << e end
results.must_equal([
["exhibit(e1)", 0],
["exhibit(e2)", 1],
["exhibit(e3)", 2]])
end
end
describe "#to_enum" do
it "returns an exhibited enumerator" do
subject.to_enum
@last_exhibited.to_a.must_equal(model)
end
end
describe "#to_ary" do
it "returns an array of itself" do
assert(subject.to_ary == subject.to_a)
end
end
describe "#to_json" do
it "returns #as_json from elements in its collection" do
subject.to_json.must_equal('["exhibit(e1)","exhibit(e2)","exhibit(e3)"]')
with_custom_as_json = subject.map{|m| m.define_singleton_method(:as_json){|opts={}| 0 }; m }
with_custom_as_json.to_json.must_equal("[0,0,0]")
end
end
describe "#grep" do
it "exhibits the result set" do
subject.grep(/[12]/).must_equal('exhibit(["e1", "e2"])')
end
end
describe "#select" do
it "exhibits each result" do
subject.select{|e| /[23]/ === e}.must_equal('exhibit(["e2", "e3"])')
end
end
describe "#detect" do
it "exhibits the result" do
subject.detect{|e| /[23]/ === e}.
must_equal("exhibit(e2)")
end
end
describe "#[]" do
it "exhibits the result" do
subject[1].must_equal("exhibit(e2)")
end
end
describe "#fetch" do
it "exhibits the result" do
subject.fetch(1).must_equal("exhibit(e2)")
end
end
describe "#first" do
it "exhibits the result" do
subject.first.must_equal("exhibit(e1)")
end
end
describe "#last" do
it "exhibits the result" do
subject.last.must_equal("exhibit(e3)")
end
end
describe "#pop" do
it "exhibits the result" do
subject.pop.must_equal("exhibit(e3)")
end
end
describe "#min" do
it "exhibits the result" do
subject.min.must_equal("exhibit(e1)")
end
end
describe "#max" do
it "exhibits the result" do
subject.max.must_equal("exhibit(e3)")
end
end
describe "#minmax" do
it "exhibits the result" do
subject.minmax.must_equal(["exhibit(e1)", "exhibit(e3)"])
end
end
describe "#sort" do
it "exhibits the result" do
subject.sort{|x,y| y <=> x}.must_equal('exhibit(["e3", "e2", "e1"])')
end
end
describe "#sort_by" do
it "exhibits the result" do
subject.sort_by(&:to_s).must_equal('exhibit(["e1", "e2", "e3"])')
end
it "returns an enumerator without arguments" do
subject.sort_by.must_match(/Enumerator/)
end
end
describe "#reverse" do
it "exhibits the result" do
subject.reverse.must_equal('exhibit(["e3", "e2", "e1"])')
end
end
describe "#slice" do
it "exhibits the result" do
subject.slice(1,2).must_equal('exhibit(["e2", "e3"])')
end
end
describe "#values_at" do
it "exhibits the result" do
subject.values_at(0,2).must_equal('exhibit(["e1", "e3"])')
end
end
describe "#reject" do
it "exhibits the result" do
subject.reject{|e| e =~ /2/}.must_equal('exhibit(["e1", "e3"])')
end
it "returns an enumerator without arguments" do
subject.reject.must_match(/Enumerator/)
end
end
describe "#partition" do
it "exhibits the result" do
subject.partition{|e| e < "e2"}.
must_equal(['exhibit(["e1"])', 'exhibit(["e2", "e3"])'])
end
end
describe "#group_by" do
it "exhibits the result" do
subject.group_by{|e| e == "e2"}.
must_equal({ true => 'exhibit(["e2"])',
false => 'exhibit(["e1", "e3"])'})
end
end
describe "#render" do
let(:template) { Object.new }
before do
stub(subject).exhibit{|object| object}
model.each do |e|
mock(e).render(template, {a: 1}){ "R(#{e})" }
end
end
it "concatenates the rendered elements" do
subject.render(template, {a: 1}).must_equal("R(e1)R(e2)R(e3)")
end
it "produces a safe string" do
assert(subject.render(template, {a: 1}).html_safe?)
end
end
end