/
random_word_spec.rb
201 lines (156 loc) · 5.46 KB
/
random_word_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
require File.expand_path("spec_helper", File.dirname(__FILE__))
describe RandomWord, "enumerator" do
subject {RandomWord.enumerator(["aaa", "bbb", "ccc"])}
it "can get you the next word in its list" do
expect(subject.next).to be_one_of(["aaa", "bbb", "ccc"])
end
it "raises error when it runs out of words" do
3.times{subject.next}
expect{subject.next}.to raise_error(StopIteration)
end
it "make sure each word is only returned once" do
already_received = []
3.times do
expect(new_word = subject.next).not_to be_one_of(already_received)
already_received << new_word
end
end
end
describe RandomWord do
after(:all) do
RandomWord.instance_eval{ @nouns, @adjs = nil, nil } # reset rspec effects
end
it "can return a random noun enumerator" do
expect(RandomWord.nouns).to respond_to(:next)
end
it "can return a random adj enumerator" do
expect(RandomWord.adjs).to respond_to(:next)
end
it "can return a random phrase enumerator" do
expect(RandomWord.phrases.next).to be_a(String)
end
end
describe RandomWord, "#exclude" do
let(:word_list) { ["aaa","ccc","c", "cab", "abc", "ace", "dad"] }
[
{:name => "normal words", :exclude => "ccc", :expected => Set.new(["aaa","c", "cab", "abc", "ace", "dad"])},
{:name => "regex", :exclude => /c/, :expected => Set.new(["aaa", "dad"])},
{:name => "list", :exclude => [/c/,/d/], :expected => Set.new(["aaa"])},
].each do |rec|
it "will not return an excluded #{rec[:name]}" do
subject = RandomWord.enumerator(word_list, rec[:exclude])
received_words = []
loop do
received_words << subject.next
end rescue StopIteration
expect(Set.new(received_words)).to eq(rec[:expected])
end
end
end
describe "RandomWord#nouns", "with exclusions" do
subject{ RandomWord.nouns }
before(:each) do
expect(RandomWord).to receive(:load_word_list).and_return(["aaa","bbb", "ccc"])
end
after(:each) do
RandomWord.exclude_list.clear
RandomWord.instance_eval{ @nouns, @adjs = nil, nil } # reset rspec effects
end
it "will not return an excluded word" do
RandomWord.exclude_list << "ccc"
received_words = []
loop do
received_words << subject.next
end
expect(received_words).not_to include "ccc"
expect(received_words).to include "aaa"
expect(received_words).to include "bbb"
end
end
shared_examples 'allows constraints on word length' do |method|
context 'when constraining' do
let(:word_list) { %w(aa bbb cccc ddddd) }
before(:each) do
expect(RandomWord).to receive(:load_word_list).and_return(word_list)
end
after(:each) do
RandomWord.instance_eval{ @nouns, @adjs = nil, nil }
end
let(:next_words) do
[].tap do |next_words|
loop do
begin
next_words << RandomWord.send(method, length_constraints).next
rescue StopIteration
# We've tried all the words in the short test list we're using.
break
end
end
end
end
context 'by maximum length' do
let(:length_constraints) { {not_longer_than: 2} }
it 'excludes the correct words' do
expect(next_words).to match_array %w(aa)
end
end
context 'by minimum length' do
let(:length_constraints) { {not_shorter_than: 4} }
it 'excludes the correct words' do
expect(next_words).to match_array %w(cccc ddddd)
end
end
context 'by both minimum and maximum length' do
let(:length_constraints) { {not_shorter_than: 3, not_longer_than: 4} }
it 'excludes the correct words' do
expect(next_words).to match_array %w(bbb cccc)
end
end
context 'by a perverse minimum length' do
let(:length_constraints) { {not_shorter_than: -1234} }
it 'includes all words' do
expect(next_words).to match_array word_list
end
end
context 'by a perverse maximum length' do
let(:length_constraints) { {not_longer_than: -34234} }
it 'excludes all words' do
expect(next_words).to be_empty
end
end
context 'and all words are within the constraints' do
let(:length_constraints) { {not_shorter_than: 2, not_longer_than: 5} }
it 'includes all words' do
expect(next_words).to match_array word_list
end
end
end
end
shared_examples 'changing constraints in subsequent calls' do |method|
context 'when changing constraints in subsequent calls' do
let(:word_list) { %w(defenestrate as can jubilant orangutan hat) }
before(:each) do
expect(RandomWord).to receive(:load_word_list).and_return(word_list)
end
after(:each) do
RandomWord.instance_eval{ @nouns, @adjs = nil, nil }
end
it 'applies the new constraints' do
short_words = %w(as can hat)
long_words = %w(defenestrate jubilant orangutan)
3.times { expect(short_words).to include RandomWord.send(method, not_longer_than: 3).next }
3.times { expect(long_words).to include RandomWord.send(method, not_longer_than: 150).next }
expect { RandomWord.send(method).next }.to raise_exception StopIteration
end
end
end
describe RandomWord do
context '#nouns' do
include_examples 'allows constraints on word length', :nouns
include_examples 'changing constraints in subsequent calls', :nouns
end
context '#adjs' do
include_examples 'allows constraints on word length', :adjs
include_examples 'changing constraints in subsequent calls', :adjs
end
end