randexp 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/Gemfile +7 -0
- data/README.textile +176 -0
- data/lib/randexp.rb +1 -0
- data/lib/randexp/reducer.rb +1 -1
- data/lib/randexp/version.rb +3 -0
- data/randexp.gemspec +25 -0
- data/spec/regression/regexp_spec.rb +8 -2
- data/spec/unit/core_ext/regexp_spec.rb +1 -1
- data/spec/unit/randexp/parser_spec.rb +1 -1
- data/spec/unit/randexp/reducer_spec.rb +1 -1
- data/spec/unit/randexp_spec.rb +1 -1
- data/spec/unit/randgen_spec.rb +1 -1
- metadata +25 -13
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
h1. randexp
|
2
|
+
|
3
|
+
bq. by Ben Burkert
|
4
|
+
|
5
|
+
h2. DESCRIPTION:
|
6
|
+
|
7
|
+
randexp makes it easy to generate random string from most regular expressions.
|
8
|
+
|
9
|
+
h2. REQUIREMENTS:
|
10
|
+
|
11
|
+
* none!
|
12
|
+
|
13
|
+
h2. INSTALL:
|
14
|
+
|
15
|
+
$ sudo gem install randexp
|
16
|
+
|
17
|
+
h2. USAGE:
|
18
|
+
|
19
|
+
randexp adds the #generate (or #gen, for short) method to the Regexp class,
|
20
|
+
which generates a 'random' string that will match your regular expression.
|
21
|
+
<pre>
|
22
|
+
<code>
|
23
|
+
>> /abc|def/.gen
|
24
|
+
# => "def"
|
25
|
+
</code>
|
26
|
+
</pre>
|
27
|
+
|
28
|
+
h2. Valid Regexp's
|
29
|
+
|
30
|
+
randexp can only generate matching string from simple regular expression.
|
31
|
+
Except for a few circumstances, wildcards are generally not allowed in the
|
32
|
+
regular expression. is pretty domain specific, so trying to guess when to
|
33
|
+
terminate a random pattern would produce unhelpful data:
|
34
|
+
|
35
|
+
<pre>
|
36
|
+
<code>
|
37
|
+
>> /Aa{3}h*!/.gen
|
38
|
+
# => RuntimeError: Sorry, "h*" is too vague, try setting a range: "h{0,3}"
|
39
|
+
>> /Aa{3}h{3,15}!/.gen
|
40
|
+
=> "Aaaahhhhh!"
|
41
|
+
|
42
|
+
>> /(never gonna (give you up|let you down), )*/.gen
|
43
|
+
=> RuntimeError: Sorry, "(...)*" is too vague, try setting a range: "(...){0, 3}"
|
44
|
+
>> /(never gonna (give you up|let you down), ){3,5}/.gen
|
45
|
+
=> "never gonna give you up, never gonna let you down, never gonna give you up, never gonna give you up, "
|
46
|
+
</code>
|
47
|
+
</pre>
|
48
|
+
|
49
|
+
The exception being word characters (\w), which generate a random word from the Dictionary class.
|
50
|
+
|
51
|
+
<pre>
|
52
|
+
<code>
|
53
|
+
>> /\w+/.gen
|
54
|
+
=> "groveling"
|
55
|
+
</code>
|
56
|
+
</pre>
|
57
|
+
|
58
|
+
h3. Primitives & Complex matches
|
59
|
+
|
60
|
+
The single character matchers supported are words(\w), whitespace(\s), and digits(\d).
|
61
|
+
|
62
|
+
<pre>
|
63
|
+
<code>
|
64
|
+
>> /\d{50}/.gen
|
65
|
+
=> "50315410741096763188525493528315906035878741451037"
|
66
|
+
</code>
|
67
|
+
</pre>
|
68
|
+
|
69
|
+
When a multiplicity constraint is placed on a word character, a word with the valid length is generated.
|
70
|
+
|
71
|
+
<pre>
|
72
|
+
<code>
|
73
|
+
>> /\w{10}/.gen # a word with 10 letters
|
74
|
+
=> "Chaucerism"
|
75
|
+
|
76
|
+
>> /\w{5,15}/.gen
|
77
|
+
=> "cabalistic"
|
78
|
+
</code>
|
79
|
+
</pre>
|
80
|
+
|
81
|
+
Complex matchers use the [:...:] syntax within the regular expression.
|
82
|
+
|
83
|
+
<pre>
|
84
|
+
<code>
|
85
|
+
>> /[:sentence:]/.gen
|
86
|
+
=> "Nonhearer demetricize toppiece filicic possessedness rhodizite zoomagnetism earwigginess steady"
|
87
|
+
</code>
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
Complex matchers can also be added by extending the Randgen class.
|
91
|
+
|
92
|
+
<pre>
|
93
|
+
<code>
|
94
|
+
class Randgen
|
95
|
+
def self.serial_number(options = {})
|
96
|
+
/XX\d{4}-\w-\d{5}/.gen
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
>> /[:serial_number:]/.gen
|
101
|
+
=> "XX3770-M-33114"
|
102
|
+
</code>
|
103
|
+
</pre>
|
104
|
+
|
105
|
+
h3. Dictionary
|
106
|
+
|
107
|
+
The Dictionary loads the local users' words file, allowing randomly generated words to be chosen from
|
108
|
+
thousands of entries to the words file. Words are mapped by their length to allow words to be randomly
|
109
|
+
chosen based on size.
|
110
|
+
|
111
|
+
h3. Real Names
|
112
|
+
|
113
|
+
One can easily generate real names based on a wordlist.
|
114
|
+
|
115
|
+
Random first name (male or female):
|
116
|
+
<pre>
|
117
|
+
<code>
|
118
|
+
>> Randgen.first_name
|
119
|
+
=> "Malika"
|
120
|
+
</code>
|
121
|
+
</pre>
|
122
|
+
|
123
|
+
Random first name, with a specific gender:
|
124
|
+
<pre>
|
125
|
+
<code>
|
126
|
+
>> Randgen.first_name(:gender => :male)
|
127
|
+
=> "Rick"
|
128
|
+
</code>
|
129
|
+
</pre>
|
130
|
+
|
131
|
+
Random first name, with a specific gender & length:
|
132
|
+
<pre>
|
133
|
+
<code>
|
134
|
+
>> Randgen.first_name(:gender => :male, :length => 10)
|
135
|
+
=> "Kristopher"
|
136
|
+
</code>
|
137
|
+
</pre>
|
138
|
+
|
139
|
+
Random last name:
|
140
|
+
<pre>
|
141
|
+
<code>
|
142
|
+
>> Randgen.last_name
|
143
|
+
=> "Klocko"
|
144
|
+
</code>
|
145
|
+
</pre>
|
146
|
+
|
147
|
+
Random name:
|
148
|
+
<pre>
|
149
|
+
<code>
|
150
|
+
>> Randgen.name
|
151
|
+
=> "Rosendo Raynor"
|
152
|
+
</code>
|
153
|
+
</pre>
|
154
|
+
|
155
|
+
alternative:
|
156
|
+
<pre>
|
157
|
+
<code>
|
158
|
+
>> /[:name:]/.gen
|
159
|
+
=> "Marc Adams"
|
160
|
+
</code>
|
161
|
+
</pre>
|
162
|
+
|
163
|
+
Random name, with a specific gender:
|
164
|
+
<pre>
|
165
|
+
<code>
|
166
|
+
>> Randgen.name(:gender => :female)
|
167
|
+
=> "Tatiana Brakus"
|
168
|
+
</code>
|
169
|
+
</pre>
|
170
|
+
|
171
|
+
|
172
|
+
h4. Contributors
|
173
|
+
|
174
|
+
* Matt Aimonetti (mattetti)
|
175
|
+
* Gerrit Kaiser (gerrit)
|
176
|
+
* Mike Vincent (shame)
|
data/lib/randexp.rb
CHANGED
data/lib/randexp/reducer.rb
CHANGED
@@ -23,7 +23,7 @@ class Randexp
|
|
23
23
|
|
24
24
|
def self.literal(cell, quantity = nil)
|
25
25
|
case quantity
|
26
|
-
when :'?' then ([''] + cell).pick
|
26
|
+
when :'?' then ([''] + cell).pick
|
27
27
|
when :+, :'+?' then raise "Sorry, \"#{cell * ''}+\" is too vague, try setting a range: \"#{cell * ''}{1,3}\""
|
28
28
|
when :*, :'*?' then raise "Sorry, \"#{cell * ''}*\" is too vague, try setting a range: \"#{cell * ''}{0,3}\""
|
29
29
|
when Range then quantity.pick.of { cell * '' } * ''
|
data/randexp.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "randexp/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "randexp"
|
7
|
+
s.version = Randexp::VERSION
|
8
|
+
s.authors = ["Ben Burkert"]
|
9
|
+
s.email = ["ben@benburkert.com"]
|
10
|
+
s.homepage = "http://github.com/benburkert/randexp"
|
11
|
+
s.summary = %q{Library for generating random strings.}
|
12
|
+
s.description = %q{Library for generating random strings from regular expressions.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "randexp"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.extra_rdoc_files = ["README", "LICENSE", "TODO"]
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
# specify any dependencies here; for example:
|
23
|
+
# s.add_development_dependency "rspec"
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
25
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require File.
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
2
|
|
3
3
|
describe "#{'*' * 80}\nRegression Specs:" do
|
4
4
|
it "/abcd/ => 'abcd'" do
|
@@ -125,6 +125,12 @@ describe "#{'*' * 80}\nRegression Specs:" do
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
128
|
+
it "/abc?/ => ['ab', 'abc']" do
|
129
|
+
100.times do
|
130
|
+
['ab', 'abc'].should include(/abc?/.gen)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
128
134
|
it "/(\\d{3}-)?\\d{3}-\\d{4}/ => /(\\d{3}-)?\\d{3}-\\d{4}/" do
|
129
135
|
100.times do
|
130
136
|
/(\d{3}-)?\d{3}-\d{4}/.gen.should =~ /(\d{3}-)?\d{3}-\d{4}/
|
@@ -160,7 +166,7 @@ describe "#{'*' * 80}\nRegression Specs:" do
|
|
160
166
|
/\$\d{2,3}\.\d{2}/.gen.should =~ /\$\d{2,3}\.\d{2}/
|
161
167
|
end
|
162
168
|
end
|
163
|
-
|
169
|
+
|
164
170
|
it "/[:first_name:]/ => /\\w+/" do
|
165
171
|
100.times do
|
166
172
|
/[:first_name:]/.gen.should =~ /\w+/
|
data/spec/unit/randexp_spec.rb
CHANGED
data/spec/unit/randgen_spec.rb
CHANGED
metadata
CHANGED
@@ -1,26 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: randexp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 7
|
10
|
+
version: 0.1.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ben Burkert
|
14
|
-
autorequire:
|
14
|
+
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-02-29 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description: Library for generating random strings.
|
23
|
-
email:
|
22
|
+
description: Library for generating random strings from regular expressions.
|
23
|
+
email:
|
24
|
+
- ben@benburkert.com
|
24
25
|
executables: []
|
25
26
|
|
26
27
|
extensions: []
|
@@ -30,24 +31,29 @@ extra_rdoc_files:
|
|
30
31
|
- LICENSE
|
31
32
|
- TODO
|
32
33
|
files:
|
34
|
+
- .gitignore
|
35
|
+
- CHANGELOG
|
36
|
+
- Gemfile
|
33
37
|
- LICENSE
|
34
38
|
- README
|
39
|
+
- README.textile
|
35
40
|
- Rakefile
|
36
41
|
- TODO
|
37
|
-
-
|
42
|
+
- lib/randexp.rb
|
43
|
+
- lib/randexp/core_ext.rb
|
38
44
|
- lib/randexp/core_ext/array.rb
|
39
45
|
- lib/randexp/core_ext/integer.rb
|
40
46
|
- lib/randexp/core_ext/range.rb
|
41
47
|
- lib/randexp/core_ext/regexp.rb
|
42
|
-
- lib/randexp/core_ext.rb
|
43
48
|
- lib/randexp/dictionary.rb
|
44
49
|
- lib/randexp/parser.rb
|
45
50
|
- lib/randexp/randgen.rb
|
46
51
|
- lib/randexp/reducer.rb
|
52
|
+
- lib/randexp/version.rb
|
47
53
|
- lib/randexp/wordlists/female_names.rb
|
48
54
|
- lib/randexp/wordlists/male_names.rb
|
49
55
|
- lib/randexp/wordlists/real_name.rb
|
50
|
-
-
|
56
|
+
- randexp.gemspec
|
51
57
|
- spec/regression/regexp_spec.rb
|
52
58
|
- spec/spec_helper.rb
|
53
59
|
- spec/unit/core_ext/regexp_spec.rb
|
@@ -87,10 +93,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
93
|
version: "0"
|
88
94
|
requirements: []
|
89
95
|
|
90
|
-
rubyforge_project:
|
96
|
+
rubyforge_project: randexp
|
91
97
|
rubygems_version: 1.6.2
|
92
98
|
signing_key:
|
93
99
|
specification_version: 3
|
94
100
|
summary: Library for generating random strings.
|
95
|
-
test_files:
|
96
|
-
|
101
|
+
test_files:
|
102
|
+
- spec/regression/regexp_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/unit/core_ext/regexp_spec.rb
|
105
|
+
- spec/unit/randexp/parser_spec.rb
|
106
|
+
- spec/unit/randexp/reducer_spec.rb
|
107
|
+
- spec/unit/randexp_spec.rb
|
108
|
+
- spec/unit/randgen_spec.rb
|