randomexp 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CHANGELOG +20 -0
- data/Gemfile +7 -0
- data/LICENSE +20 -0
- data/README +82 -0
- data/Rakefile +76 -0
- data/TODO +4 -0
- data/lib/randomexp/core_ext/array.rb +5 -0
- data/lib/randomexp/core_ext/integer.rb +5 -0
- data/lib/randomexp/core_ext/range.rb +9 -0
- data/lib/randomexp/core_ext/regexp.rb +7 -0
- data/lib/randomexp/core_ext.rb +6 -0
- data/lib/randomexp/dictionary.rb +32 -0
- data/lib/randomexp/parser.rb +97 -0
- data/lib/randomexp/randgen.rb +78 -0
- data/lib/randomexp/reducer.rb +102 -0
- data/lib/randomexp/version.rb +3 -0
- data/lib/randomexp/wordlists/female_names.rb +25 -0
- data/lib/randomexp/wordlists/male_names.rb +25 -0
- data/lib/randomexp/wordlists/real_name.rb +35 -0
- data/lib/randomexp.rb +22 -0
- data/randomexp.gemspec +25 -0
- data/spec/fixtures/osx_dictionary +235886 -0
- data/spec/regression/regexp_spec.rb +191 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/unit/core_ext/regexp_spec.rb +9 -0
- data/spec/unit/randgen_spec.rb +244 -0
- data/spec/unit/randomexp/parser_spec.rb +77 -0
- data/spec/unit/randomexp/reducer_spec.rb +224 -0
- data/spec/unit/randomexp_spec.rb +158 -0
- data/wordlists/female_names +4275 -0
- data/wordlists/male_names +1219 -0
- data/wordlists/surnames +475 -0
- metadata +86 -0
@@ -0,0 +1,224 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Randomexp::Reducer do
|
4
|
+
describe ".reduce" do
|
5
|
+
it "should expect a sexp, and return a string" do
|
6
|
+
Randomexp::Reducer.reduce([:literal, 'a']).should be_instance_of(String)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be an alias for :[]" do
|
10
|
+
Randomexp::Reducer[[:literal, 'a']].should == Randomexp::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
|
+
Randomexp::Reducer.should_receive(:reduce).with([:literal, 'a'], :*)
|
17
|
+
Randomexp::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
|
+
Randomexp::Reducer.should_receive(:char).with(:*)
|
24
|
+
Randomexp::Reducer.random([:w], :*)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should call :digit with the quantity argument if the sexp's value is :d" do
|
28
|
+
Randomexp::Reducer.should_receive(:digit).with(:*)
|
29
|
+
Randomexp::Reducer.random([:d], :*)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should call :whitespace with the quantity argument if the sexp's value is :w" do
|
33
|
+
Randomexp::Reducer.should_receive(:whitespace).with(:*)
|
34
|
+
Randomexp::Reducer.random([:s], :*)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should call :randgen with the quantity argument if the sexp's value for all other cases" do
|
38
|
+
Randomexp::Reducer.should_receive(:randgen).with(:alpha_numeric, :*)
|
39
|
+
Randomexp::Reducer.random([:alpha_numeric], :*)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".literal" do
|
44
|
+
it "should raise an exception if the quantity argument is :+ or :'+?'" do
|
45
|
+
lambda { Randomexp::Reducer.literal(['a'], :+) }.should raise_error("Sorry, \"a+\" is too vague, try setting a range: \"a{1,3}\"")
|
46
|
+
lambda { Randomexp::Reducer.literal(['b'], :'+?') }.should raise_error("Sorry, \"b+\" is too vague, try setting a range: \"b{1,3}\"")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should raise an exception if the quantity argument is :* or :'*?'" do
|
50
|
+
lambda { Randomexp::Reducer.literal(['a'], :*) }.should raise_error("Sorry, \"a*\" is too vague, try setting a range: \"a{0,3}\"")
|
51
|
+
lambda { Randomexp::Reducer.literal(['b'], :'*?') }.should raise_error("Sorry, \"b*\" is too vague, try setting a range: \"b{0,3}\"")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe ".intersection" do
|
56
|
+
it "should raise an exception if the quantity arguement is :+ or :'+?'" do
|
57
|
+
lambda { Randomexp::Reducer.intersection([[:literal, 'a'], [:literal, 'b']], :+) }.should raise_error("Sorry, \"((...)|(...))+\" is too vague, try setting a range: \"((...)|(...)){1, 3}\"")
|
58
|
+
lambda { Randomexp::Reducer.intersection([[:literal, 'b'], [:literal, 'a']], :'+?') }.should raise_error("Sorry, \"((...)|(...))+\" is too vague, try setting a range: \"((...)|(...)){1, 3}\"")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should raise an exception if the quantity argument is :* or :'*?'" do
|
62
|
+
lambda { Randomexp::Reducer.intersection([[:literal, 'a'], [:literal, 'b']], :*) }.should raise_error("Sorry, \"((...)|(...))*\" is too vague, try setting a range: \"((...)|(...)){0, 3}\"")
|
63
|
+
lambda { Randomexp::Reducer.intersection([[:literal, 'b'], [:literal, 'a']], :'*?') }.should raise_error("Sorry, \"((...)|(...))*\" is too vague, try setting a range: \"((...)|(...)){0, 3}\"")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe ".union" do
|
68
|
+
it "should raise an exception if the quantity arguement is :+ or :'+?'" do
|
69
|
+
lambda { Randomexp::Reducer.union([[:literal, 'a'], [:literal, 'b']], :+) }.should raise_error("Sorry, \"(...)+\" is too vague, try setting a range: \"(...){1, 3}\"")
|
70
|
+
lambda { Randomexp::Reducer.union([[:literal, 'b'], [:literal, 'a']], :'+?') }.should raise_error("Sorry, \"(...)+\" is too vague, try setting a range: \"(...){1, 3}\"")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should raise an exception if the quantity argument is :* or :'*?'" do
|
74
|
+
lambda { Randomexp::Reducer.union([[:literal, 'a'], [:literal, 'b']], :*) }.should raise_error("Sorry, \"(...)*\" is too vague, try setting a range: \"(...){0, 3}\"")
|
75
|
+
lambda { Randomexp::Reducer.union([[:literal, 'b'], [:literal, 'a']], :'*?') }.should raise_error("Sorry, \"(...)*\" is too vague, try setting a range: \"(...){0, 3}\"")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe ".char" do
|
80
|
+
it "should call Randgen.char if the quantity argument is :'?'" do
|
81
|
+
Randgen.should_receive(:char)
|
82
|
+
Randomexp::Reducer.char(:'?')
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should call Randgen.char if the quantity argument is 1" do
|
86
|
+
Randgen.should_receive(:char)
|
87
|
+
Randomexp::Reducer.char(1)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should call Randgen.char if the quantity argument is nil" do
|
91
|
+
Randgen.should_receive(:char)
|
92
|
+
Randomexp::Reducer.char(nil)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should call Randgen.word if the quantity argument is :+" do
|
96
|
+
Randgen.should_receive(:word)
|
97
|
+
Randomexp::Reducer.char(:+)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should call Randgen.word if the quantity argument is :'+?'" do
|
101
|
+
Randgen.should_receive(:word)
|
102
|
+
Randomexp::Reducer.char(:'+?')
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should call Randgen.word with the :length option if the quantity argument is an Integer" do
|
106
|
+
Randgen.should_receive(:word).with(:length => 5)
|
107
|
+
Randomexp::Reducer.char(5)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should call Randgen.word with the :length option if the quantity argument is a Range" do
|
111
|
+
range = 1..10
|
112
|
+
range.should_receive(:pick_rand_value).and_return 7
|
113
|
+
Randgen.should_receive(:word).with(:length => 7)
|
114
|
+
Randomexp::Reducer.char(range)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe ".whitespace" do
|
119
|
+
it "should call Randgen.whitespace if the quantity is :'?'" do
|
120
|
+
Randgen.should_receive(:whitespace)
|
121
|
+
Randomexp::Reducer.whitespace(:'?')
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should call Randgen.whitespace if the quantity is nil" do
|
125
|
+
Randgen.should_receive(:whitespace)
|
126
|
+
Randomexp::Reducer.whitespace(nil)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should call Randgen.whitespace quantity times if the quantity is an Integer" do
|
130
|
+
Randgen.should_receive(:whitespace).exactly(5).times
|
131
|
+
Randomexp::Reducer.whitespace(5)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should call Randgen.whitespace quantity times if the quantity is a Range" do
|
135
|
+
range = 1..10
|
136
|
+
range.should_receive(:pick_rand_value).and_return 7
|
137
|
+
Randgen.should_receive(:whitespace).exactly(7).times
|
138
|
+
Randomexp::Reducer.whitespace(range)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should raise an exception if the quantity arguement is :+ or :'+?'" do
|
142
|
+
lambda { Randomexp::Reducer.whitespace(:+) }.should raise_error("Sorry, \"\\s+\" is too vague, try setting a range: \"\\s{1, 5}\"")
|
143
|
+
lambda { Randomexp::Reducer.whitespace(:'+?') }.should raise_error("Sorry, \"\\s+\" is too vague, try setting a range: \"\\s{1, 5}\"")
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should raise an exception if the quantity argument is :* or :'*?'" do
|
147
|
+
lambda { Randomexp::Reducer.whitespace(:*) }.should raise_error("Sorry, \"\\s*\" is too vague, try setting a range: \"\\s{0, 5}\"")
|
148
|
+
lambda { Randomexp::Reducer.whitespace(:'*?') }.should raise_error("Sorry, \"\\s*\" is too vague, try setting a range: \"\\s{0, 5}\"")
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe ".digit" do
|
153
|
+
it "should call Randgen.digit if the quantity is :'?'" do
|
154
|
+
Randgen.should_receive(:digit)
|
155
|
+
Randomexp::Reducer.digit(:'?')
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should call Randgen.digit if the quantity is nil" do
|
159
|
+
Randgen.should_receive(:digit)
|
160
|
+
Randomexp::Reducer.digit(nil)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should call Randgen.digit quantity times if the quantity is an Integer" do
|
164
|
+
Randgen.should_receive(:digit).exactly(5).times
|
165
|
+
Randomexp::Reducer.digit(5)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should call Randgen.digit quantity times if the quantity is a Range" do
|
169
|
+
range = 1..10
|
170
|
+
range.should_receive(:pick_rand_value).and_return 7
|
171
|
+
Randgen.should_receive(:digit).exactly(7).times
|
172
|
+
Randomexp::Reducer.digit(range)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should raise an exception if the quantity arguement is :+ or :'+?'" do
|
176
|
+
lambda { Randomexp::Reducer.digit(:+) }.should raise_error("Sorry, \"\\d+\" is too vague, try setting a range: \"\\d{1, 5}\"")
|
177
|
+
lambda { Randomexp::Reducer.digit(:'+?') }.should raise_error("Sorry, \"\\d+\" is too vague, try setting a range: \"\\d{1, 5}\"")
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should raise an exception if the quantity argument is :* or :'*?'" do
|
181
|
+
lambda { Randomexp::Reducer.digit(:*) }.should raise_error("Sorry, \"\\d*\" is too vague, try setting a range: \"\\d{0, 5}\"")
|
182
|
+
lambda { Randomexp::Reducer.digit(:'*?') }.should raise_error("Sorry, \"\\d*\" is too vague, try setting a range: \"\\d{0, 5}\"")
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe ".randgen" do
|
187
|
+
it "should send Randgen the method name argument with a :length => 1 option if the quantity is :'?'" do
|
188
|
+
Randgen.should_receive(:send).with(:foo, :length => 1)
|
189
|
+
Randomexp::Reducer.randgen([:foo], :'?')
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should send Randgen the method name argument if the quantity is nil" do
|
193
|
+
Randgen.should_receive(:send).with(:bar)
|
194
|
+
Randomexp::Reducer.randgen([:bar], nil)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should send Rangen the method name argument if the quantity is 1" do
|
198
|
+
Randgen.should_receive(:send).with(:baz)
|
199
|
+
Randomexp::Reducer.randgen([:baz], 1)
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should send Randgen the method name argument if the quantity is :+" do
|
203
|
+
Randgen.should_receive(:send).with(:foo)
|
204
|
+
Randomexp::Reducer.randgen([:foo], :+)
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should send Randgen the method name argument if the quantity is :*" do
|
208
|
+
Randgen.should_receive(:send).with(:bar)
|
209
|
+
Randomexp::Reducer.randgen([:bar], :*)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should send Randgen the method name argument with a length option if the quantity is a Range" do
|
213
|
+
range = 1..10
|
214
|
+
range.should_receive(:pick_rand_value).and_return 7
|
215
|
+
Randgen.should_receive(:send).with(:baz, :length => 7)
|
216
|
+
Randomexp::Reducer.randgen([:baz], range)
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should send Randgen the method name argument with a length option if the quantity is a Integer" do
|
220
|
+
Randgen.should_receive(:send).with(:baz, :length => 7)
|
221
|
+
Randomexp::Reducer.randgen([:baz], 7)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Randomexp do
|
4
|
+
describe "#initialize" do
|
5
|
+
it "should set the sexp attribute" do
|
6
|
+
Randomexp.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
|
+
@randomexp = Randomexp.new("def")
|
13
|
+
@sexp = @randomexp.sexp
|
14
|
+
|
15
|
+
@randomexp.reduce
|
16
|
+
|
17
|
+
@sexp.should == @randomexp.sexp
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Randomexp do
|
23
|
+
describe "#parse" do
|
24
|
+
|
25
|
+
describe '("\\w")' do
|
26
|
+
it "should be a random sexp" do
|
27
|
+
Randomexp::Parser.parse("\\w").first.should == :random
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should hold a word symbol" do
|
31
|
+
Randomexp::Parser.parse("\\w").last.should == :w
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '("\\s")' do
|
36
|
+
it "should be a literal sexp" do
|
37
|
+
Randomexp::Parser.parse("\\s").first.should == :random
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should hold a whitespace symbol " do
|
41
|
+
Randomexp::Parser.parse("\\s").last.should == :s
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '("\\d")' do
|
46
|
+
it "should be a literal sexp" do
|
47
|
+
Randomexp::Parser.parse("\\d").first.should == :random
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should hold a digit character " do
|
51
|
+
Randomexp::Parser.parse("\\d").last.should == :d
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '("\\c")' do
|
56
|
+
it "should be a literal sexp" do
|
57
|
+
Randomexp::Parser.parse("\\c").first.should == :random
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should hold a digit character " do
|
61
|
+
Randomexp::Parser.parse("\\c").last.should == :c
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '("(\\w)")' do
|
66
|
+
it "should be a random sexp" do
|
67
|
+
Randomexp::Parser.parse("(\\w)").first.should == :random
|
68
|
+
Randomexp::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
|
+
Randomexp::Parser.parse("(\\w)(\\d)").first.should == :union
|
75
|
+
Randomexp::Parser.parse("(\\w)(\\d)")[1].first.should == :random
|
76
|
+
Randomexp::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
|
+
Randomexp::Parser.parse("(\\w)(\\s)(\\d)").first.should == :union
|
83
|
+
Randomexp::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
|
+
Randomexp::Parser.parse("\\w*").first.should == :quantify
|
90
|
+
Randomexp::Parser.parse("\\w*")[1].first.should == :random
|
91
|
+
Randomexp::Parser.parse("\\w*")[2].should == :*
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should blah" do
|
96
|
+
Randomexp::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
|
+
Randomexp::Parser.parse("[:sentence:]").first.should == :random
|
102
|
+
Randomexp::Parser.parse("[:sentence:]").last.should == :sentence
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "#reduce" do
|
108
|
+
it "should return a character" do
|
109
|
+
Randomexp.new("\\w").reduce.should =~ /\w/
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should return a word" do
|
113
|
+
Randomexp.new("\\w+").reduce.should =~ /\w+/
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should return a word or an empty string" do
|
117
|
+
Randomexp.new("\\w*").reduce.should =~ /\w*/
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should return a word with 4 to 5 characters" do
|
121
|
+
Randomexp.new("\\w{4,5}").reduce.should =~ /\w{4,5}/
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should return a digit" do
|
125
|
+
Randomexp.new("\\d").reduce.should =~ /\d/
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should return a 2 to 10 digit number" do
|
129
|
+
Randomexp.new("\\d{2,10}").reduce.should =~ /\d{2,10}/
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should return a digit or empty string" do
|
133
|
+
Randomexp.new("\\d?").reduce.should =~ /\d?/
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should return a digit or a character" do
|
137
|
+
Randomexp.new("\\d|\\w").reduce.should =~ /\w|\d/
|
138
|
+
end
|
139
|
+
|
140
|
+
xit "should return a word or a 3 digit number" do
|
141
|
+
Randomexp.new("\\d{3}|\\w+").reduce.should =~ /\w+|d{3}/
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should return a word or number" do
|
145
|
+
Randomexp.new("\\w+|\\d{3}").reduce.should =~ /\w+|d{3}/
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should return a sentence" do
|
149
|
+
Randomexp.new("[:sentence:]").reduce.should =~ /(\w+\s)*\w+/
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should handle a telephone number" do
|
153
|
+
100.times do
|
154
|
+
Randomexp.new("(\\d{3}-)?\\d{3}-\\d{4}").reduce.should =~ /(\d{3}-)?\d{3}-\d{4}/
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|