michel-randexp 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,273 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Randexp::Reducer do
4
+ describe ".reduce" do
5
+ it "should expect a sexp, and return a string" do
6
+ Randexp::Reducer.reduce([:literal, 'a']).should be_instance_of(String)
7
+ end
8
+
9
+ it "should be an alias for :[]" do
10
+ Randexp::Reducer[[:literal, 'a']].should == Randexp::Reducer.reduce([:literal, 'a'])
11
+ end
12
+ end
13
+
14
+ describe ".quantify" do
15
+ it "should call reduce with the sexp and quantity arguments as the :quantify sexp's head and tail" do
16
+ Randexp::Reducer.should_receive(:reduce).with([:literal, 'a'], :*)
17
+ Randexp::Reducer.quantify([[:literal, 'a'], :*], :'?') # :'?' is ignored
18
+ end
19
+ end
20
+
21
+ describe ".random" do
22
+ it "should call :char with the quantity argument if the sexp's value is :w" do
23
+ Randexp::Reducer.should_receive(:char).with(:*)
24
+ Randexp::Reducer.random([:w], :*)
25
+ end
26
+
27
+ it "should call :digit with the quantity argument if the sexp's value is :d" do
28
+ Randexp::Reducer.should_receive(:digit).with(:*)
29
+ Randexp::Reducer.random([:d], :*)
30
+ end
31
+
32
+ it "should call :whitespace with the quantity argument if the sexp's value is :w" do
33
+ Randexp::Reducer.should_receive(:whitespace).with(:*)
34
+ Randexp::Reducer.random([:s], :*)
35
+ end
36
+
37
+
38
+ it "should call :any with the quantity argument if the sexp's value is :\".\"" do
39
+ Randexp::Reducer.should_receive(:any).with(:".")
40
+ Randexp::Reducer.random([:"."], :".")
41
+ end
42
+
43
+ it "should call :randgen with the quantity argument if the sexp's value for all other cases" do
44
+ Randexp::Reducer.should_receive(:randgen).with(:alpha_numeric, :*)
45
+ Randexp::Reducer.random([:alpha_numeric], :*)
46
+ end
47
+ end
48
+
49
+ describe ".literal" do
50
+ it "should raise an exception if the quantity argument is :+ or :'+?'" do
51
+ lambda { Randexp::Reducer.literal(['a'], :+) }.should raise_error("Sorry, \"a+\" is too vague, try setting a range: \"a{1,3}\"")
52
+ lambda { Randexp::Reducer.literal(['b'], :'+?') }.should raise_error("Sorry, \"b+\" is too vague, try setting a range: \"b{1,3}\"")
53
+ end
54
+
55
+ it "should raise an exception if the quantity argument is :* or :'*?'" do
56
+ lambda { Randexp::Reducer.literal(['a'], :*) }.should raise_error("Sorry, \"a*\" is too vague, try setting a range: \"a{0,3}\"")
57
+ lambda { Randexp::Reducer.literal(['b'], :'*?') }.should raise_error("Sorry, \"b*\" is too vague, try setting a range: \"b{0,3}\"")
58
+ end
59
+ end
60
+
61
+ describe ".intersection" do
62
+ it "should raise an exception if the quantity arguement is :+ or :'+?'" do
63
+ lambda { Randexp::Reducer.intersection([[:literal, 'a'], [:literal, 'b']], :+) }.should raise_error("Sorry, \"((...)|(...))+\" is too vague, try setting a range: \"((...)|(...)){1, 3}\"")
64
+ lambda { Randexp::Reducer.intersection([[:literal, 'b'], [:literal, 'a']], :'+?') }.should raise_error("Sorry, \"((...)|(...))+\" is too vague, try setting a range: \"((...)|(...)){1, 3}\"")
65
+ end
66
+
67
+ it "should raise an exception if the quantity argument is :* or :'*?'" do
68
+ lambda { Randexp::Reducer.intersection([[:literal, 'a'], [:literal, 'b']], :*) }.should raise_error("Sorry, \"((...)|(...))*\" is too vague, try setting a range: \"((...)|(...)){0, 3}\"")
69
+ lambda { Randexp::Reducer.intersection([[:literal, 'b'], [:literal, 'a']], :'*?') }.should raise_error("Sorry, \"((...)|(...))*\" is too vague, try setting a range: \"((...)|(...)){0, 3}\"")
70
+ end
71
+ end
72
+
73
+ describe ".union" do
74
+ it "should raise an exception if the quantity arguement is :+ or :'+?'" do
75
+ lambda { Randexp::Reducer.union([[:literal, 'a'], [:literal, 'b']], :+) }.should raise_error("Sorry, \"(...)+\" is too vague, try setting a range: \"(...){1, 3}\"")
76
+ lambda { Randexp::Reducer.union([[:literal, 'b'], [:literal, 'a']], :'+?') }.should raise_error("Sorry, \"(...)+\" is too vague, try setting a range: \"(...){1, 3}\"")
77
+ end
78
+
79
+ it "should raise an exception if the quantity argument is :* or :'*?'" do
80
+ lambda { Randexp::Reducer.union([[:literal, 'a'], [:literal, 'b']], :*) }.should raise_error("Sorry, \"(...)*\" is too vague, try setting a range: \"(...){0, 3}\"")
81
+ lambda { Randexp::Reducer.union([[:literal, 'b'], [:literal, 'a']], :'*?') }.should raise_error("Sorry, \"(...)*\" is too vague, try setting a range: \"(...){0, 3}\"")
82
+ end
83
+ end
84
+
85
+ describe ".char" do
86
+ it "should call Randgen.char if the quantity argument is :'?'" do
87
+ Randgen.should_receive(:char)
88
+ Randexp::Reducer.char(:'?')
89
+ end
90
+
91
+ it "should call Randgen.char if the quantity argument is 1" do
92
+ Randgen.should_receive(:char)
93
+ Randexp::Reducer.char(1)
94
+ end
95
+
96
+ it "should call Randgen.char if the quantity argument is nil" do
97
+ Randgen.should_receive(:char)
98
+ Randexp::Reducer.char(nil)
99
+ end
100
+
101
+ it "should call Randgen.word if the quantity argument is :+" do
102
+ Randgen.should_receive(:word)
103
+ Randexp::Reducer.char(:+)
104
+ end
105
+
106
+ it "should call Randgen.word if the quantity argument is :'+?'" do
107
+ Randgen.should_receive(:word)
108
+ Randexp::Reducer.char(:'+?')
109
+ end
110
+
111
+ it "should call Randgen.word with the :length option if the quantity argument is an Integer" do
112
+ Randgen.should_receive(:word).with(:length => 5)
113
+ Randexp::Reducer.char(5)
114
+ end
115
+
116
+ it "should call Randgen.word with the :length option if the quantity argument is a Range" do
117
+ range = 1..10
118
+ range.should_receive(:pick).and_return 7
119
+ Randgen.should_receive(:word).with(:length => 7)
120
+ Randexp::Reducer.char(range)
121
+ end
122
+ end
123
+
124
+
125
+ describe ".any" do
126
+ it "should call Randgen.any if the quantity argument is :'?'" do
127
+ Randgen.should_receive(:any)
128
+ Randexp::Reducer.any(:'?')
129
+ end
130
+
131
+ it "should call Randgen.any if the quantity argument is 1" do
132
+ Randgen.should_receive(:any)
133
+ Randexp::Reducer.any(1)
134
+ end
135
+
136
+ it "should call Randgen.any if the quantity argument is nil" do
137
+ Randgen.should_receive(:any)
138
+ Randexp::Reducer.any(nil)
139
+ end
140
+
141
+ it "should call Randgen.any if the quantity argument is :+" do
142
+ Randgen.should_receive(:any)
143
+ Randexp::Reducer.any(:+)
144
+ end
145
+
146
+ it "should call Randgen.any if the quantity argument is :'+?'" do
147
+ Randgen.should_receive(:any)
148
+ Randexp::Reducer.any(:'+?')
149
+ end
150
+
151
+ it "should call Randgen.any with the :length option if the quantity argument is an Integer" do
152
+ Randgen.should_receive(:any).with(:length => 5)
153
+ Randexp::Reducer.any(5)
154
+ end
155
+
156
+ it "should call Randgen.any with the :length option if the quantity argument is a Range" do
157
+ range = 1..10
158
+ range.should_receive(:pick).and_return 7
159
+ Randgen.should_receive(:any).with(:length => 7)
160
+ Randexp::Reducer.any(range)
161
+ end
162
+ end
163
+
164
+
165
+
166
+
167
+ describe ".whitespace" do
168
+ it "should call Randgen.whitespace if the quantity is :'?'" do
169
+ Randgen.should_receive(:whitespace)
170
+ Randexp::Reducer.whitespace(:'?')
171
+ end
172
+
173
+ it "should call Randgen.whitespace if the quantity is nil" do
174
+ Randgen.should_receive(:whitespace)
175
+ Randexp::Reducer.whitespace(nil)
176
+ end
177
+
178
+ it "should call Randgen.whitespace quantity times if the quantity is an Integer" do
179
+ Randgen.should_receive(:whitespace).exactly(5).times
180
+ Randexp::Reducer.whitespace(5)
181
+ end
182
+
183
+ it "should call Randgen.whitespace quantity times if the quantity is a Range" do
184
+ range = 1..10
185
+ range.should_receive(:pick).and_return 7
186
+ Randgen.should_receive(:whitespace).exactly(7).times
187
+ Randexp::Reducer.whitespace(range)
188
+ end
189
+
190
+ it "should raise an exception if the quantity arguement is :+ or :'+?'" do
191
+ lambda { Randexp::Reducer.whitespace(:+) }.should raise_error("Sorry, \"\\s+\" is too vague, try setting a range: \"\\s{1, 5}\"")
192
+ lambda { Randexp::Reducer.whitespace(:'+?') }.should raise_error("Sorry, \"\\s+\" is too vague, try setting a range: \"\\s{1, 5}\"")
193
+ end
194
+
195
+ it "should raise an exception if the quantity argument is :* or :'*?'" do
196
+ lambda { Randexp::Reducer.whitespace(:*) }.should raise_error("Sorry, \"\\s*\" is too vague, try setting a range: \"\\s{0, 5}\"")
197
+ lambda { Randexp::Reducer.whitespace(:'*?') }.should raise_error("Sorry, \"\\s*\" is too vague, try setting a range: \"\\s{0, 5}\"")
198
+ end
199
+ end
200
+
201
+ describe ".digit" do
202
+ it "should call Randgen.digit if the quantity is :'?'" do
203
+ Randgen.should_receive(:digit)
204
+ Randexp::Reducer.digit(:'?')
205
+ end
206
+
207
+ it "should call Randgen.digit if the quantity is nil" do
208
+ Randgen.should_receive(:digit)
209
+ Randexp::Reducer.digit(nil)
210
+ end
211
+
212
+ it "should call Randgen.digit quantity times if the quantity is an Integer" do
213
+ Randgen.should_receive(:digit).exactly(5).times
214
+ Randexp::Reducer.digit(5)
215
+ end
216
+
217
+ it "should call Randgen.digit quantity times if the quantity is a Range" do
218
+ range = 1..10
219
+ range.should_receive(:pick).and_return 7
220
+ Randgen.should_receive(:digit).exactly(7).times
221
+ Randexp::Reducer.digit(range)
222
+ end
223
+
224
+ it "should raise an exception if the quantity arguement is :+ or :'+?'" do
225
+ lambda { Randexp::Reducer.digit(:+) }.should raise_error("Sorry, \"\\d+\" is too vague, try setting a range: \"\\d{1, 5}\"")
226
+ lambda { Randexp::Reducer.digit(:'+?') }.should raise_error("Sorry, \"\\d+\" is too vague, try setting a range: \"\\d{1, 5}\"")
227
+ end
228
+
229
+ it "should raise an exception if the quantity argument is :* or :'*?'" do
230
+ lambda { Randexp::Reducer.digit(:*) }.should raise_error("Sorry, \"\\d*\" is too vague, try setting a range: \"\\d{0, 5}\"")
231
+ lambda { Randexp::Reducer.digit(:'*?') }.should raise_error("Sorry, \"\\d*\" is too vague, try setting a range: \"\\d{0, 5}\"")
232
+ end
233
+ end
234
+
235
+ describe ".randgen" do
236
+ it "should send Randgen the method name argument with a :length => 1 option if the quantity is :'?'" do
237
+ Randgen.should_receive(:send).with(:foo, :length => 1)
238
+ Randexp::Reducer.randgen([:foo], :'?')
239
+ end
240
+
241
+ it "should send Randgen the method name argument if the quantity is nil" do
242
+ Randgen.should_receive(:send).with(:bar)
243
+ Randexp::Reducer.randgen([:bar], nil)
244
+ end
245
+
246
+ it "should send Rangen the method name argument if the quantity is 1" do
247
+ Randgen.should_receive(:send).with(:baz)
248
+ Randexp::Reducer.randgen([:baz], 1)
249
+ end
250
+
251
+ it "should send Randgen the method name argument if the quantity is :+" do
252
+ Randgen.should_receive(:send).with(:foo)
253
+ Randexp::Reducer.randgen([:foo], :+)
254
+ end
255
+
256
+ it "should send Randgen the method name argument if the quantity is :*" do
257
+ Randgen.should_receive(:send).with(:bar)
258
+ Randexp::Reducer.randgen([:bar], :*)
259
+ end
260
+
261
+ it "should send Randgen the method name argument with a length option if the quantity is a Range" do
262
+ range = 1..10
263
+ range.should_receive(:pick).and_return 7
264
+ Randgen.should_receive(:send).with(:baz, :length => 7)
265
+ Randexp::Reducer.randgen([:baz], range)
266
+ end
267
+
268
+ it "should send Randgen the method name argument with a length option if the quantity is a Integer" do
269
+ Randgen.should_receive(:send).with(:baz, :length => 7)
270
+ Randexp::Reducer.randgen([:baz], 7)
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,164 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Randexp do
4
+ describe "#initialize" do
5
+ it "should set the sexp attribute" do
6
+ Randexp.new("abcd").sexp.should_not be_nil
7
+ end
8
+ end
9
+
10
+ describe ".reduce" do
11
+ it "should not change the original sexp in any way" do
12
+ @randexp = Randexp.new("def")
13
+ @sexp = @randexp.sexp
14
+
15
+ @randexp.reduce
16
+
17
+ @sexp.should == @randexp.sexp
18
+ end
19
+ end
20
+ end
21
+
22
+ describe Randexp do
23
+ describe "#parse" do
24
+
25
+ describe '("\\w")' do
26
+ it "should be a random sexp" do
27
+ Randexp::Parser.parse("\\w").first.should == :random
28
+ end
29
+
30
+ it "should hold a word symbol" do
31
+ Randexp::Parser.parse("\\w").last.should == :w
32
+ end
33
+ end
34
+
35
+ describe '("\\s")' do
36
+ it "should be a literal sexp" do
37
+ Randexp::Parser.parse("\\s").first.should == :random
38
+ end
39
+
40
+ it "should hold a whitespace symbol " do
41
+ Randexp::Parser.parse("\\s").last.should == :s
42
+ end
43
+ end
44
+
45
+ describe '("\\d")' do
46
+ it "should be a literal sexp" do
47
+ Randexp::Parser.parse("\\d").first.should == :random
48
+ end
49
+
50
+ it "should hold a digit character " do
51
+ Randexp::Parser.parse("\\d").last.should == :d
52
+ end
53
+ end
54
+
55
+ describe '("\\c")' do
56
+ it "should be a literal sexp" do
57
+ Randexp::Parser.parse("\\c").first.should == :random
58
+ end
59
+
60
+ it "should hold a digit character " do
61
+ Randexp::Parser.parse("\\c").last.should == :c
62
+ end
63
+ end
64
+
65
+ describe '("(\\w)")' do
66
+ it "should be a random sexp" do
67
+ Randexp::Parser.parse("(\\w)").first.should == :random
68
+ Randexp::Parser.parse("(\\w)").last.should == :w
69
+ end
70
+ end
71
+
72
+ describe '("(\\w)(\\d)")' do
73
+ it "should be a union between random sexp's" do
74
+ Randexp::Parser.parse("(\\w)(\\d)").first.should == :union
75
+ Randexp::Parser.parse("(\\w)(\\d)")[1].first.should == :random
76
+ Randexp::Parser.parse("(\\w)(\\d)")[2].first.should == :random
77
+ end
78
+ end
79
+
80
+ describe '("(\\w)(\\s)(\\d)")' do
81
+ xit "should be a union between 3 sexp's" do
82
+ Randexp::Parser.parse("(\\w)(\\s)(\\d)").first.should == :union
83
+ Randexp::Parser.parse("(\\w)(\\s)(\\d)").size.should == 4
84
+ end
85
+ end
86
+
87
+ describe '("\\w*")' do
88
+ it "should be a quantify sexp and hold a random sexp" do
89
+ Randexp::Parser.parse("\\w*").first.should == :quantify
90
+ Randexp::Parser.parse("\\w*")[1].first.should == :random
91
+ Randexp::Parser.parse("\\w*")[2].should == :*
92
+ end
93
+ end
94
+
95
+ it "should blah" do
96
+ Randexp::Parser.parse("(\\w)|(\\d)").should == [:intersection, [:random, :w], [:random, :d]]
97
+ end
98
+
99
+ describe '("[:sentence:]")' do
100
+ it "should be a random sexp" do
101
+ Randexp::Parser.parse("[:sentence:]").first.should == :random
102
+ Randexp::Parser.parse("[:sentence:]").last.should == :sentence
103
+ end
104
+ end
105
+
106
+ describe '(.) any' do
107
+ it "should hold any character" do
108
+ Randexp::Parser.parse(".").last.should == :"."
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#reduce" do
114
+ it "should return a character" do
115
+ Randexp.new("\\w").reduce.should =~ /\w/
116
+ end
117
+
118
+ it "should return a word" do
119
+ Randexp.new("\\w+").reduce.should =~ /\w+/
120
+ end
121
+
122
+ it "should return a word or an empty string" do
123
+ Randexp.new("\\w*").reduce.should =~ /\w*/
124
+ end
125
+
126
+ it "should return a word with 4 to 5 characters" do
127
+ Randexp.new("\\w{4,5}").reduce.should =~ /\w{4,5}/
128
+ end
129
+
130
+ it "should return a digit" do
131
+ Randexp.new("\\d").reduce.should =~ /\d/
132
+ end
133
+
134
+ it "should return a 2 to 10 digit number" do
135
+ Randexp.new("\\d{2,10}").reduce.should =~ /\d{2,10}/
136
+ end
137
+
138
+ it "should return a digit or empty string" do
139
+ Randexp.new("\\d?").reduce.should =~ /\d?/
140
+ end
141
+
142
+ it "should return a digit or a character" do
143
+ Randexp.new("\\d|\\w").reduce.should =~ /\w|\d/
144
+ end
145
+
146
+ xit "should return a word or a 3 digit number" do
147
+ Randexp.new("\\d{3}|\\w+").reduce.should =~ /\w+|d{3}/
148
+ end
149
+
150
+ it "should return a word or number" do
151
+ Randexp.new("\\w+|\\d{3}").reduce.should =~ /\w+|d{3}/
152
+ end
153
+
154
+ it "should return a sentence" do
155
+ Randexp.new("[:sentence:]").reduce.should =~ /(\w+\s)*\w+/
156
+ end
157
+
158
+ it "should handle a telephone number" do
159
+ 100.times do
160
+ Randexp.new("(\\d{3}-)?\\d{3}-\\d{4}").reduce.should =~ /(\d{3}-)?\d{3}-\d{4}/
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,216 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Randgen do
4
+ describe ".bool" do
5
+ it "should return 'true' or 'false'" do
6
+ 100.times do
7
+ ['true', 'false'].should include(Randgen.bool)
8
+ end
9
+ end
10
+ end
11
+
12
+ describe ".lchar" do
13
+ it "should return 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', or 'z'" do
14
+ 100.times do
15
+ ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'].should include(Randgen.lchar)
16
+ end
17
+ end
18
+ end
19
+
20
+ describe ".uchar" do
21
+ it "should return 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', or 'Z'" do
22
+ 100.times do
23
+ ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'].should include(Randgen.uchar)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ".char" do
29
+ it "should return 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', or 'z'" do
30
+ 100.times do
31
+ ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'].should include(Randgen.char)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe ".whitespace" do
37
+ it "should return '\\t', '\\n', '\\r', or '\\f'" do
38
+ 100.times do
39
+ ["\t", "\n", "\r", "\f"].should include(Randgen.whitespace)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe ".digit" do
45
+ it "should return '0', '1', '2', '3', '4', '5', '6', '7', '8', or '9'" do
46
+ 100.times do
47
+ ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].should include(Randgen.digit)
48
+ end
49
+ end
50
+ end
51
+
52
+ describe ".alpha_numeric" do
53
+ it "should return 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', or '9'" do
54
+ 100.times do
55
+ ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].should include(Randgen.alpha_numeric)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe ".word" do
61
+ it "should handle generating long strings" do
62
+ Randgen.word(:length => 64).length.should == 64
63
+ end
64
+ it "should pick an entry from Dictionary" do
65
+ 10.times do
66
+ Randexp::Dictionary.words.should include(Randgen.word)
67
+ end
68
+ end
69
+
70
+ it "should pick a word with a length if the length option is supplied" do
71
+ 10.times do
72
+ length = (3..10).pick
73
+ Randgen.word(:length => length).length.should == length
74
+ end
75
+ end
76
+
77
+ it "should not return a string that is not a word" do
78
+ strings = %w[foo's bars]
79
+ Randexp::Dictionary.should_receive(:words).at_least(1).and_return strings
80
+
81
+ 100.times do
82
+ Randgen.word.should_not == "foo's"
83
+ end
84
+ end
85
+ end
86
+
87
+ describe ".first_name" do
88
+ it "should pick a word from the female names list if the gender option is female" do
89
+ 100.times do
90
+ female_name = Randgen.first_name(:gender => :female)
91
+ RealName.female_first_names.should include(female_name)
92
+ end
93
+ end
94
+
95
+ it "should pick a word from the male names list if the gender option is male" do
96
+ 100.times do
97
+ male_name = Randgen.first_name(:gender => :male)
98
+ RealName.male_first_names.should include(male_name)
99
+ end
100
+ end
101
+
102
+ it "should pick a word from the male names list with the same length in the options" do
103
+ 100.times do
104
+ length = (3..10).pick
105
+ male_name = Randgen.first_name(:length => length)
106
+ (RealName.female_first_names + RealName.male_first_names).should include(male_name)
107
+ end
108
+ end
109
+ end
110
+
111
+ describe ".last_name" do
112
+ it "should pick a word from the last names list with the same length in the options" do
113
+ 100.times do
114
+ length = (3..10).pick
115
+ last_name = Randgen.last_name(:length => length)
116
+ RealName.surnames.should include(last_name)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe ".name" do
122
+ it "should be two words long" do
123
+ 100.times do
124
+ Randgen.name.should =~ /\w+ \w+/
125
+ end
126
+ end
127
+ end
128
+
129
+ describe ".sentence" do
130
+ it "should be capitalized" do
131
+ 10.times do
132
+ Randgen.sentence.should =~ /^[A-Z]/
133
+ end
134
+ end
135
+ end
136
+
137
+ describe ".paragraph" do
138
+ it "should end in a period" do
139
+ 10.times do
140
+ Randgen.paragraph.should =~ /\.$/
141
+ end
142
+ end
143
+ end
144
+
145
+ describe ".phone_number" do
146
+ it "should match /(\\d{3}-)?\\d{3}-\\d{4}/ when no length is given" do
147
+ 100.times do
148
+ Randgen.phone_number =~ /(\d{3}-)?\d{3}-\d{4}/
149
+ end
150
+ end
151
+
152
+ it "should match /\\d{3}-\\d{4}/ when the length is 7" do
153
+ 100.times do
154
+ Randgen.phone_number(:length => 7) =~ /\d{3}-\d{4}/
155
+ end
156
+ end
157
+
158
+ it "should match /\\d{3}-\\d{3}-\\d{4}/ when the length is 10" do
159
+ 100.times do
160
+ Randgen.phone_number(:length => 10) =~ /\d{3}-\d{3}-\d{4}/
161
+ end
162
+ end
163
+ end
164
+
165
+ it "should generate a first name" do
166
+ 100.times do
167
+ Randgen.first_name.should =~ /\w/
168
+ end
169
+ end
170
+
171
+ it "should generate a male first name" do
172
+ male_list = RealName.male_first_names
173
+ 100.times do
174
+ Randgen.first_name(:gender => :male).should =~ /\w/
175
+ male_list.include?(Randgen.first_name(:gender => :male)).should be_true
176
+ end
177
+ end
178
+
179
+ it "should generate a female first name" do
180
+ female_list = RealName.female_first_names
181
+ 100.times do
182
+ Randgen.first_name(:gender => :female).should =~ /\w/
183
+ female_list.include?(Randgen.first_name(:gender => :female)).should be_true
184
+ end
185
+ end
186
+
187
+ it "should generate a last name" do
188
+ 100.times do
189
+ Randgen.last_name.should =~ /\w/
190
+ end
191
+ end
192
+
193
+ it "should generate a real name" do
194
+ 100.times do
195
+ Randgen.name.should =~ /\w{2}/
196
+ end
197
+ end
198
+
199
+ it "should generate a real male name" do
200
+ male_list = RealName.male_first_names
201
+ 100.times do
202
+ name = Randgen.name(:gender => :male)
203
+ name.should =~ /\w{2}/
204
+ male_list.include?(name.split(' ').first).should be_true
205
+ end
206
+ end
207
+
208
+ it "should generate a real female name" do
209
+ female_list = RealName.female_first_names
210
+ 100.times do
211
+ name = Randgen.name(:gender => :female)
212
+ name.should =~ /\w{2}/
213
+ female_list.include?(name.split(' ').first).should be_true
214
+ end
215
+ end
216
+ end