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,191 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "#{'*' * 80}\nRegression Specs:" do
|
4
|
+
it "/abcd/ => 'abcd'" do
|
5
|
+
100.times do
|
6
|
+
/abcd/.gen.should == 'abcd'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "/(abcd)|(defg)/ => ['abcd', 'defg']" do
|
11
|
+
100.times do
|
12
|
+
['abcd', 'defg'].should include(/(abcd)|(defg)/.gen)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "/(abcd)|(defg)|(hijk)/ => ['abcd', 'defg', 'hijk']" do
|
17
|
+
100.times do
|
18
|
+
['abcd', 'defg', 'hijk'].should include(/(abcd)|(defg)|(hijk)/.gen)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "/((abcd)|(defg))|(hijk)/ => ['abcd', 'defg', 'hijk']" do
|
23
|
+
100.times do
|
24
|
+
['abcd', 'defg', 'hijk'].should include(/((abcd)|(defg))|(hijk)/.gen)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "/(abcd)|((defg)|(hijk))/ => ['abcd', 'defg', 'hijk']" do
|
29
|
+
100.times do
|
30
|
+
['abcd', 'defg', 'hijk'].should include(/(abcd)|((defg)|(hijk))/.gen)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "/(abc)def(ghi)/ => 'abcdefghi'" do
|
35
|
+
100.times do
|
36
|
+
/(abc)def(ghi)/.gen.should == 'abcdefghi'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "/(((abc)))/ => 'abc'" do
|
41
|
+
100.times do
|
42
|
+
/(((abc)))/.gen.should == 'abc'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "/ab(c(def))/ => 'abcdef'" do
|
47
|
+
100.times do
|
48
|
+
/ab(c(def))/.gen.should == 'abcdef'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "/(\\w+)/ => /\\w+/" do
|
53
|
+
100.times do
|
54
|
+
/(\w+)/.gen.should =~ /\w+/
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "/\\w+ \\w+/ => /\\w+\\s\\w+/" do
|
59
|
+
100.times do
|
60
|
+
/\w+ \w+/.gen.should =~ /\w+\s\w+/
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "/\\w*/ => /(\\w+)|/" do
|
65
|
+
100.times do
|
66
|
+
/\w*/.gen.should =~ /(\w+)|/
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "/\\w{2,5}/ => /\\w{2,5}/" do
|
71
|
+
100.times do
|
72
|
+
/\w{2,5}/.gen.should =~ /\w{2,5}/
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "/\\w{1}/ => /\\w/" do
|
77
|
+
100.times do
|
78
|
+
/\w{1}/.gen.should =~ /\w/
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "/\\w{4}/ => /\\w{4}/" do
|
83
|
+
100.times do
|
84
|
+
/\w{4}/.gen.should =~ /\w{4}/
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "/[:word:]/ => /\\w+/" do
|
89
|
+
100.times do
|
90
|
+
/[:word:]/.gen.should =~ /\w+/
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it "/[:bool:]/ => /true|false/" do
|
95
|
+
/[:bool:]/.gen.should =~ /true|false/
|
96
|
+
end
|
97
|
+
|
98
|
+
it "/[:sentence:]/ => /(\w+ )*/" do
|
99
|
+
100.times do
|
100
|
+
/[:sentence:]/.gen.should =~ /(\w+ )*/
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it "/[:paragraph:]/ => /( (\w+ )*\.)*/" do
|
105
|
+
100.times do
|
106
|
+
/[:paragraph:]/.gen.should =~ /(\w+ )*/
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it "/(abc|def){1,2}/ => ['abc', 'def', 'abcabc', 'abcdef', 'defabc', 'defdef']" do
|
111
|
+
100.times do
|
112
|
+
['abc', 'def', 'abcabc', 'abcdef', 'defabc', 'defdef'].should include(/(abc|def){1,2}/.gen)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it "/abc(def)?hij/ => /abc(def)?hij/" do
|
117
|
+
100.times do
|
118
|
+
/abc(def)?hij/.gen.should =~ /abc(def)?hij/
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it "/ab(c(def))?h/ => /ab(c(def))?h/" do
|
123
|
+
100.times do
|
124
|
+
/ab(c(def))?h/.gen.should =~ /ab(c(def))?h/
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
it "/abc?/ => ['ab', 'abc']" do
|
129
|
+
100.times do
|
130
|
+
['ab', 'abc'].should include(/abc?/.gen)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it "/(\\d{3}-)?\\d{3}-\\d{4}/ => /(\\d{3}-)?\\d{3}-\\d{4}/" do
|
135
|
+
100.times do
|
136
|
+
/(\d{3}-)?\d{3}-\d{4}/.gen.should =~ /(\d{3}-)?\d{3}-\d{4}/
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "/[:phone_number:]/ => /(\\d{3}-)?\\d{3}-\\d{4}/" do
|
141
|
+
100.times do
|
142
|
+
/[:phone_number:]/.gen.should =~ /(\d{3}-)?\d{3}-\d{4}/
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
it "/[:phone_number:]{7}/ => /\\d{3}-\\d{4}/" do
|
147
|
+
100.times do
|
148
|
+
/[:phone_number:]{7}/.gen.should =~ /\d{3}-\d{4}/
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it "/[:phone_number:]{10}/ => /\\d{3}-\\d{3}-\\d{4}/" do
|
153
|
+
100.times do
|
154
|
+
/[:phone_number:]{10}/.gen.should =~ /\d{3}-\d{3}-\d{4}/
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
it "/\\w+@\\w+\\.(com|org|net)/ => /\\w+@\\w+\\.(com|org|net)/.gen" do
|
159
|
+
100.times do
|
160
|
+
/\w+@\w+\.(com|org|net)/.gen.should =~ /\w+@\w+\.(com|org|net)/
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
it "/\\$\\d{2,3}\\.\\d{2}/ => /\\$\\d{2,3}\\.\\d{2}/" do
|
165
|
+
100.times do
|
166
|
+
/\$\d{2,3}\.\d{2}/.gen.should =~ /\$\d{2,3}\.\d{2}/
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
it "/[:first_name:]/ => /\\w+/" do
|
171
|
+
100.times do
|
172
|
+
/[:first_name:]/.gen.should =~ /\w+/
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
it "/[:last_name:]/ => /\\w+/" do
|
177
|
+
/[:last_name:]/.gen.should =~ /\w+/
|
178
|
+
end
|
179
|
+
|
180
|
+
it "/[:name:]/ => /\\w+ \\w+/" do
|
181
|
+
/[:name:]/.gen.should =~ /\w+ \w+/
|
182
|
+
end
|
183
|
+
|
184
|
+
it "/[:last_name:]{5,10}/ => /\\w{5,10}/" do
|
185
|
+
/[:last_name:]{5,10}/.gen.should =~ /\w{5,10}/
|
186
|
+
end
|
187
|
+
|
188
|
+
it "/[:first_name:]{5,10}/ => /\\w{5,10}/" do
|
189
|
+
/[:first_name:]{5,10}/.gen.should =~ /\w{5,10}/
|
190
|
+
end
|
191
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
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_rand_value an entry from Dictionary" do
|
65
|
+
10.times do
|
66
|
+
Randomexp::Dictionary.words.should include(Randgen.word)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should pick_rand_value a word with a length if the length option is supplied" do
|
71
|
+
10.times do
|
72
|
+
length = (3..10).pick_rand_value
|
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
|
+
Randomexp::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_rand_value 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_rand_value 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_rand_value a word from the male names list with the same length in the options" do
|
103
|
+
100.times do
|
104
|
+
length = (3..10).pick_rand_value
|
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
|
+
|
110
|
+
it "returns nil if there is no name that matches the length option" do
|
111
|
+
Randgen.first_name(:length => 100).should be_nil
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe ".last_name" do
|
116
|
+
it "should pick_rand_value a word from the last names list with the same length in the options" do
|
117
|
+
100.times do
|
118
|
+
length = (3..10).pick_rand_value
|
119
|
+
last_name = Randgen.last_name(:length => length)
|
120
|
+
RealName.surnames.should include(last_name)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "returns nil if there is no name that matches the length option" do
|
125
|
+
Randgen.last_name(:length => 100).should be_nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe ".name" do
|
130
|
+
it "should be two words long" do
|
131
|
+
100.times do
|
132
|
+
Randgen.name.should =~ /\w+ \w+/
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe ".email" do
|
138
|
+
it "should pick_rand_value a local-part from the dictionnary" do
|
139
|
+
100.times do
|
140
|
+
Randomexp::Dictionary.words.should include(Randgen.email[/^(\w+)@/, 1])
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should use the domain option paramter" do
|
145
|
+
100.times do
|
146
|
+
Randgen.email(:domain => 'foobar.com')[/@((?:\w|\.)+)$/, 1].should == 'foobar.com'
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should pick_rand_value a subdomain of example.org by default" do
|
151
|
+
100.times do
|
152
|
+
Randomexp::Dictionary.words.should include(Randgen.email[/@(\w+)\.example\.org$/, 1])
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
describe ".sentence" do
|
158
|
+
it "should be capitalized" do
|
159
|
+
10.times do
|
160
|
+
Randgen.sentence.should =~ /^[A-Z]/
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe ".paragraph" do
|
166
|
+
it "should end in a period" do
|
167
|
+
10.times do
|
168
|
+
Randgen.paragraph.should =~ /\.$/
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
describe ".phone_number" do
|
174
|
+
it "should match /(\\d{3}-)?\\d{3}-\\d{4}/ when no length is given" do
|
175
|
+
100.times do
|
176
|
+
Randgen.phone_number =~ /(\d{3}-)?\d{3}-\d{4}/
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should match /\\d{3}-\\d{4}/ when the length is 7" do
|
181
|
+
100.times do
|
182
|
+
Randgen.phone_number(:length => 7) =~ /\d{3}-\d{4}/
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should match /\\d{3}-\\d{3}-\\d{4}/ when the length is 10" do
|
187
|
+
100.times do
|
188
|
+
Randgen.phone_number(:length => 10) =~ /\d{3}-\d{3}-\d{4}/
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should generate a first name" do
|
194
|
+
100.times do
|
195
|
+
Randgen.first_name.should =~ /\w/
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should generate a male first name" do
|
200
|
+
male_list = RealName.male_first_names
|
201
|
+
100.times do
|
202
|
+
Randgen.first_name(:gender => :male).should =~ /\w/
|
203
|
+
male_list.include?(Randgen.first_name(:gender => :male)).should be_true
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should generate a female first name" do
|
208
|
+
female_list = RealName.female_first_names
|
209
|
+
100.times do
|
210
|
+
Randgen.first_name(:gender => :female).should =~ /\w/
|
211
|
+
female_list.include?(Randgen.first_name(:gender => :female)).should be_true
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should generate a last name" do
|
216
|
+
100.times do
|
217
|
+
Randgen.last_name.should =~ /\w/
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should generate a real name" do
|
222
|
+
100.times do
|
223
|
+
Randgen.name.should =~ /\w{2}/
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should generate a real male name" do
|
228
|
+
male_list = RealName.male_first_names
|
229
|
+
100.times do
|
230
|
+
name = Randgen.name(:gender => :male)
|
231
|
+
name.should =~ /\w{2}/
|
232
|
+
male_list.include?(name.split(' ').first).should be_true
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should generate a real female name" do
|
237
|
+
female_list = RealName.female_first_names
|
238
|
+
100.times do
|
239
|
+
name = Randgen.name(:gender => :female)
|
240
|
+
name.should =~ /\w{2}/
|
241
|
+
female_list.include?(name.split(' ').first).should be_true
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Randomexp::Parser do
|
4
|
+
describe ".parse" do
|
5
|
+
it "should return a sexp for a non-empty string" do
|
6
|
+
Randomexp::Parser.parse("abc").should be_instance_of(Array)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return nil for an empty string" do
|
10
|
+
Randomexp::Parser.parse("").should be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should alias :[] to :parse" do
|
14
|
+
Randomexp::Parser[""].should be_nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".quantify" do
|
19
|
+
it "should return a :quantify sexp" do
|
20
|
+
Randomexp::Parser.quantify([:literal, 'a'], :*)[0].should == :quantify
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should push the quantify symbol on the end of the sexp" do
|
24
|
+
Randomexp::Parser.quantify([:literal, 'a'], :*).last.should == :*
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should push the argument sexp to the first entry of the :quantify sexp" do
|
28
|
+
Randomexp::Parser.quantify([:literal, 'a'], :*)[1].should == [:literal, 'a']
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".union" do
|
33
|
+
it "should return the union of the right-hand side if the left-hand side is nil" do
|
34
|
+
Randomexp::Parser.union(nil, [:literal, 'a']).should == Randomexp::Parser.union([:literal, 'a'])
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return the left-hand side if the right hand side is not present" do
|
38
|
+
Randomexp::Parser.union([:literal, 'a']).should == [:literal, 'a']
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should append the right-hand side(s) to the left-hand side if the left-hand side is a union sexp" do
|
42
|
+
Randomexp::Parser.union([:union, [:literal, 'a'], [:literal, 'b']], [:literal, 'c']).should == [:union, [:literal, 'a'], [:literal, 'b'], [:literal, 'c']]
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return a :union sexp between the left-hand and right-hand sexp's" do
|
46
|
+
Randomexp::Parser.union([:literal, 'a'], [:literal, 'b']).should == [:union, [:literal, 'a'], [:literal, 'b']]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe ".intersection" do
|
51
|
+
it "should prepend the left-hand side onto the right-hand side :intersection sexp if the right-hand side is an :intersection sexp" do
|
52
|
+
Randomexp::Parser.intersection([:literal, 'a'], [:intersection, [:literal, 'b'], [:literal, 'c']]).should == [:intersection, [:literal, 'a'], [:literal, 'b'], [:literal, 'c']]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should create an :intersection sexp between the left-hand and right-hand sexp's" do
|
56
|
+
Randomexp::Parser.intersection([:literal, 'a'], [:literal, 'b']).should == [:intersection, [:literal, 'a'], [:literal, 'b']]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe ".random" do
|
61
|
+
it "should return a :random sexp" do
|
62
|
+
Randomexp::Parser.random('w').should be_instance_of(Array)
|
63
|
+
Randomexp::Parser.random('w').first.should == :random
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should convert the char parameter to a symbol" do
|
67
|
+
Randomexp::Parser.random('w').last.should == :w
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe ".literal" do
|
72
|
+
it "should return a literal sexp" do
|
73
|
+
Randomexp::Parser.literal('a').should be_instance_of(Array)
|
74
|
+
Randomexp::Parser.literal('a').first.should == :literal
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|