-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtests.rb
193 lines (172 loc) · 3.78 KB
/
tests.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
require 'minitest'
require 'minitest/autorun'
require './lib/spicy-proton'
class Tests < Minitest::Test
def test_single_word
types.each do |type|
corpus { |c| assert_generated(c, type) }
end
end
def test_pair
corpus { |c|
pair = c.pair
assert_string(pair)
assert(pair.length >= 3)
assert(pair.index('-') > 0)
}
end
def test_pair_separator
corpus { |c|
pair = c.pair(':')
assert_string(pair)
assert(pair.length >= 3)
assert(pair.index(':') > 0)
}
end
def test_format
corpus { |c|
fmt = c.format('%a1%n2%b3%v4')
assert_string(fmt)
assert(fmt.length >= 12)
assert(fmt.index('1') > 0)
assert(fmt.index('2') > fmt.index('1'))
assert(fmt.index('3') > fmt.index('2'))
assert(fmt.index('4') > fmt.index('3'))
assert_nil(fmt.index('%a'))
assert_nil(fmt.index('%n'))
assert_nil(fmt.index('%b'))
assert_nil(fmt.index('%v'))
fmt = c.format('%%')
assert_equal('%', fmt)
fmt = c.format('%z')
assert_equal('%z', fmt)
}
end
def test_min
types.each do |type|
corpus { |c|
assert_generated(c, type, min: 8)
}
end
end
def test_max
types.each do |type|
corpus { |c|
assert_generated(c, type, max: 4)
}
end
end
def test_min_max
types.each do |type|
corpus { |c|
assert_generated(c, type, min: 3, max: 4)
}
end
end
def test_invert_min_max
types.each do |type|
corpus { |c|
assert_error(ArgumentError) do
c.send(type, min: 2, max: 1)
end
}
end
end
def test_bad_min
types.each do |type|
corpus { |c|
word = c.send(type, min: 1000)
assert_nil(word)
}
end
end
def test_bad_max
types.each do |type|
corpus { |c|
word = c.send(type, max: 0)
assert_nil(word)
}
end
end
def test_large_range
types.each do |type|
corpus { |c|
word = c.send(type, min: 0, max: 10000)
assert_string(word)
}
end
end
def test_equal_min_max
types.each do |type|
corpus { |c|
word = c.send(type, min: 6, max: 6)
assert_string(word)
assert_equal(6, word.length)
}
end
end
def test_length
types.each do |type|
corpus { |c|
word = c.send(type, length: 4)
assert_string(word)
assert_equal(4, word.length)
}
end
end
def test_length_min_max
types.each do |type|
corpus { |c|
assert_error(ArgumentError) do
c.send(type, length: 5, min: 4)
end
assert_error(ArgumentError) do
c.send(type, length: 5, max: 7)
end
}
end
end
def test_word_lists
c = Spicy::Proton.new
refute(c.adjectives.empty?)
refute(c.nouns.empty?)
refute(c.adverbs.empty?)
refute(c.verbs.empty?)
refute(c.adjectives.any?(&:empty?))
refute(c.nouns.any?(&:empty?))
refute(c.adverbs.any?(&:empty?))
refute(c.verbs.any?(&:empty?))
end
private
def corpus(&block)
block.call(Spicy::Proton)
block.call(Spicy::Proton.new)
end
def types
[:adjective, :noun, :adverb, :verb]
end
def assert_error(type, &block)
error = nil
begin
block.call
rescue => e
error = e
ensure
refute_equal(nil, error)
assert_equal(type, error.class)
end
end
def assert_generated(source, type, opts = {})
min, max = opts[:min], opts[:max]
string = source.send(type, min: min, max: max)
assert(string.length >= min) if min
assert(string.length <= max) if max
assert_string(string)
end
def assert_string(string)
refute_nil(string)
assert(string.is_a?(String))
assert(string.length > 0)
string
end
end