jjulian-random_data 1.5.0

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.
@@ -0,0 +1,3 @@
1
+ module RandomData #:nodoc:
2
+ VERSION = '1.5.0' #:nodoc:
3
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/random_data.rb'}"
9
+ puts "Loading random_data gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
data/script/txt2html ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ load File.dirname(__FILE__) + "/../Rakefile"
4
+ require 'rubyforge'
5
+ require 'redcloth'
6
+ require 'syntax/convertors/html'
7
+ require 'erb'
8
+
9
+ download = "http://rubyforge.org/projects/#{$hoe.rubyforge_name}"
10
+ version = $hoe.version
11
+
12
+ def rubyforge_project_id
13
+ RubyForge.new.configure.autoconfig["group_ids"][$hoe.rubyforge_name]
14
+ end
15
+
16
+ class Fixnum
17
+ def ordinal
18
+ # teens
19
+ return 'th' if (10..19).include?(self % 100)
20
+ # others
21
+ case self % 10
22
+ when 1: return 'st'
23
+ when 2: return 'nd'
24
+ when 3: return 'rd'
25
+ else return 'th'
26
+ end
27
+ end
28
+ end
29
+
30
+ class Time
31
+ def pretty
32
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
33
+ end
34
+ end
35
+
36
+ def convert_syntax(syntax, source)
37
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
38
+ end
39
+
40
+ if ARGV.length >= 1
41
+ src, template = ARGV
42
+ template ||= File.join(File.dirname(__FILE__), '/../website/template.html.erb')
43
+ else
44
+ puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
45
+ exit!
46
+ end
47
+
48
+ template = ERB.new(File.open(template).read)
49
+
50
+ title = nil
51
+ body = nil
52
+ File.open(src) do |fsrc|
53
+ title_text = fsrc.readline
54
+ body_text_template = fsrc.read
55
+ body_text = ERB.new(body_text_template).result(binding)
56
+ syntax_items = []
57
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
58
+ ident = syntax_items.length
59
+ element, syntax, source = $1, $2, $3
60
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
61
+ "syntax-temp-#{ident}"
62
+ }
63
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
64
+ body = RedCloth.new(body_text).to_html
65
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
66
+ end
67
+ stat = File.stat(src)
68
+ created = stat.ctime
69
+ modified = stat.mtime
70
+
71
+ $stdout << template.result(binding)
@@ -0,0 +1,41 @@
1
+ PROLOGUE
2
+
3
+ Enter CHORUS
4
+
5
+ CHORUS.
6
+ O for a Muse of fire, that would ascend
7
+ The brightest heaven of invention,
8
+ A kingdom for a stage, princes to act,
9
+ And monarchs to behold the swelling scene!
10
+ Then should the warlike Harry, like himself,
11
+ Assume the port of Mars; and at his heels,
12
+ Leash'd in like hounds, should famine, sword, and fire,
13
+ Crouch for employment. But pardon, gentles all,
14
+ The flat unraised spirits that hath dar'd
15
+ On this unworthy scaffold to bring forth
16
+ So great an object. Can this cockpit hold
17
+ The vasty fields of France? Or may we cram
18
+ Within this wooden O the very casques
19
+ That did affright the air at Agincourt?
20
+ O, pardon! since a crooked figure may
21
+ Attest in little place a million;
22
+ And let us, ciphers to this great accompt,
23
+ On your imaginary forces work.
24
+ Suppose within the girdle of these walls
25
+ Are now confin'd two mighty monarchies,
26
+ Whose high upreared and abutting fronts
27
+ The perilous narrow ocean parts asunder.
28
+ Piece out our imperfections with your thoughts:
29
+ Into a thousand parts divide one man,
30
+ And make imaginary puissance;
31
+ Think, when we talk of horses, that you see them
32
+ Printing their proud hoofs i' th' receiving earth;
33
+ For 'tis your thoughts that now must deck our kings,
34
+ Carry them here and there, jumping o'er times,
35
+ Turning th' accomplishment of many years
36
+ Into an hour-glass; for the which supply,
37
+ Admit me Chorus to this history;
38
+ Who prologue-like, your humble patience pray
39
+ Gently to hear, kindly to judge, our play.
40
+
41
+
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require File.dirname(__FILE__) + '/../lib/random_data'
@@ -0,0 +1,277 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestRandomData < Test::Unit::TestCase
4
+
5
+ def setup
6
+ # to make random numbers deterministic for testing purposes
7
+ srand(100)
8
+ end
9
+
10
+ def test_should_return_random_phone_number
11
+ assert_equal "620-892-9039", Random.phone
12
+ end
13
+
14
+ def test_should_return_random_international_phone
15
+ assert_equal "011-9-34-9039", Random.international_phone
16
+ end
17
+
18
+ def test_should_return_random_email
19
+ assert_equal "ijones@webmail.com", Random.email
20
+ end
21
+
22
+ def test_should_return_random_date
23
+ Date.expects(:today).returns(Date.civil(2007,9,15))
24
+ assert_equal "2007-09-13", Random.date.to_s
25
+ end
26
+
27
+ def test_should_return_random_date_with_range
28
+ Date.expects(:today).returns(Date.civil(2007,9,15))
29
+ assert_equal "2007-10-29", Random.date(1500).to_s
30
+ end
31
+
32
+ def test_should_return_random_address_line_1
33
+ assert_equal "38408 Highland Blvd", Random.address_line_1
34
+ end
35
+
36
+ def test_should_return_random_address_line_2
37
+ assert_equal "Lot 792", Random.address_line_2
38
+ end
39
+
40
+ def test_should_return_random_zipcode
41
+ assert_equal "38408", Random.zipcode
42
+ end
43
+
44
+ def test_should_return_random_uk_post_code
45
+ assert_equal "BM8 24DB", Random.uk_post_code
46
+ end
47
+
48
+ def test_should_return_random_state
49
+ assert_equal "DE", Random.state
50
+ end
51
+
52
+ def test_should_return_random_state_full
53
+ assert_equal "Delaware", Random.state_full
54
+ end
55
+
56
+ def test_should_return_random_country
57
+ assert_equal "Armenia", Random.country
58
+ end
59
+
60
+ def test_should_return_random_city
61
+ assert_equal "Georgetown", Random.city
62
+ end
63
+
64
+ def test_should_return_random_firstname
65
+ assert_equal "Donald", Random.firstname
66
+ end
67
+
68
+ def test_should_return_random_firstnamemale
69
+ assert_equal "Donald", Random.firstname_male
70
+ end
71
+
72
+ def test_should_return_random_firstnamefemale
73
+ assert_equal "Charlotte", Random.firstname_female
74
+ end
75
+
76
+ def test_should_return_random_initial
77
+ assert_equal "I", Random.initial
78
+ end
79
+
80
+ def test_should_return_random_lastname
81
+ assert_equal "Clarke", Random.lastname
82
+ end
83
+
84
+ def test_should_return_random_alphanumeric
85
+ assert_equal "8O3dNFmAUqYr2YEY", Random.alphanumeric
86
+ end
87
+
88
+ def test_should_return_random_alphanumeric_with_range
89
+ assert_equal "8O3dNFm", Random.alphanumeric(7)
90
+ end
91
+
92
+ def test_should_return_random_paragraphs
93
+ assert_equal 2, Random.paragraphs(2).scan(/\n\n/).size
94
+ end
95
+
96
+ def test_should_return_random_paragraphs_with_limit
97
+ num_paragraphs = 5
98
+ assert_equal num_paragraphs, Random.paragraphs(num_paragraphs).scan(/\n\n/).size
99
+ end
100
+
101
+ def test_initial_should_not_return_nil
102
+ srand(11) #This would force the nil case in the old implementation
103
+ assert_not_nil Random.initial
104
+ end
105
+
106
+ def test_should_return_random_date_using_range
107
+ Date.expects(:today).returns(Date.civil(2007,9,15))
108
+ assert_equal '1998-03-30', Random.date(-5000..-1000).to_s
109
+ end
110
+
111
+ def test_should_return_random_date_between_using_range_of_dates
112
+ min = Date.parse('1966-11-15')
113
+ max = Date.parse('1990-01-01')
114
+ assert_equal '1982-04-25', Random.date_between(min..max).to_s
115
+ end
116
+
117
+ def test_should_return_random_date_between_using_range_of_strings
118
+ assert_equal '1982-04-25', Random.date_between('1966-11-15'..'1990-01-01').to_s
119
+ end
120
+
121
+ def test_should_return_random_boolean_true
122
+ srand(99)
123
+ assert_equal true, Random.boolean
124
+ end
125
+
126
+ def test_should_return_random_boolean_false
127
+ assert_equal false, Random.boolean
128
+ end
129
+
130
+ def test_should_return_random_number_less_than_10
131
+ assert_equal 8, Random.number(9)
132
+ end
133
+
134
+ def test_should_return_random_bit
135
+ assert_equal 0, Random.bit
136
+ end
137
+
138
+ def test_should_return_random_bits
139
+ assert_equal 10, Random.bits(10).size
140
+ assert_equal [0, 1, 0, 0, 0, 0, 1, 0, 0, 1], Random.bits(10)
141
+ end
142
+
143
+ def test_should_return_random_number_within_range
144
+ assert_equal 13, Random.number(5..15)
145
+ end
146
+
147
+ def test_should_return_random_grammatical_construct
148
+ assert_equal "Bob bites Rover",
149
+ Random::grammatical_construct({:story => [:man, " bites ", :dog],
150
+ :man => { :bob => "Bob"},
151
+ :dog => {:a =>"Rex", :b =>"Rover"}},
152
+ :story)
153
+ end
154
+
155
+ def test_roulette_wheel_with_positive_weights
156
+ weights=[10,5,1,1];
157
+ results = []
158
+ 17.times do
159
+ results << weights.roulette
160
+ end
161
+ assert_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0],
162
+ results);
163
+ results = []
164
+ weights.roulette(17) do |i|
165
+ results << i
166
+ end
167
+ assert_equal([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
168
+ results);
169
+ end
170
+
171
+ def test_roulette_wheel_with_zero_weights
172
+ weights=[0,0,0];
173
+ assert_raise RuntimeError do
174
+ weights.roulette
175
+ end
176
+ assert_raise RuntimeError do
177
+ x=0
178
+ weights.roulette(1){|i| x=i}
179
+ end
180
+ end
181
+
182
+ def test_roulette_wheel_with_negative_weights
183
+ weights=[2,-1,2];
184
+ assert_raise RuntimeError do
185
+ weights.roulette
186
+ end
187
+ assert_raise RuntimeError do
188
+ x=0
189
+ weights.roulette(1){|i| x=i}
190
+ end
191
+ end
192
+ end
193
+
194
+ class TestRandomDataMethodMissing < Test::Unit::TestCase
195
+
196
+ def sports
197
+ @sports ||= %w(hockey basketball water\ polo five-a-side football judo pole\ vault steeple\ chase)
198
+ end
199
+
200
+ def lines
201
+ @lines ||= sports.join("\r\n")
202
+ end
203
+
204
+ def path
205
+ @path ||= File.join("lib","sport.dat")
206
+ end
207
+
208
+ def setup
209
+ @file = stub('file', :read => lines)
210
+ $:.stubs(:each).yields("lib")
211
+ File.stubs(:exist?).returns(true)
212
+ File.stubs(:open).yields(@file)
213
+ end
214
+
215
+ def test_should_search_load_path_for_file
216
+ $:.expects(:each).yields("lib")
217
+ Random.sport
218
+ end
219
+
220
+ def test_should_join_path_with_methodname_dot_dat
221
+ File.expects(:join).with("lib","sport.dat").returns("lib/sport.dat")
222
+ Random.sport
223
+ end
224
+
225
+ def test_should_test_for_existence_of_filename_matching_method_missing_call
226
+ File.expects(:exist?).with(path).returns(true)
227
+ Random.sport
228
+ end
229
+
230
+ def test_should_call_super_if_unable_to_find_file
231
+ # can't test super call directly, so we test whether MethodMissing gets raised properly
232
+ File.stubs(:exist?).returns(false)
233
+ assert_raise(NoMethodError) { Random.horse }
234
+ end
235
+
236
+ def test_should_open_file_matching_method_missing_call
237
+ File.expects(:open).with(path,"r").yields(@file)
238
+ Random.sport
239
+ end
240
+
241
+ def test_should_read_lines_from_file
242
+ @file.expects(:read).returns(@lines)
243
+ Random.sport
244
+ end
245
+
246
+ def test_should_return_random_line_from_a_file
247
+ srand(101)
248
+ assert_equal 'steeple chase', Random.sport
249
+ assert_equal 'five-a-side', Random.sport
250
+ end
251
+
252
+ end
253
+
254
+ class TestRandomDataMarkovGenerator < Test::Unit::TestCase
255
+ def test_markov_generator
256
+ markov_machine = RandomData::MarkovGenerator.new(3)
257
+ text = IO.read "#{File.dirname(__FILE__)}/henry_v_prolog.txt"
258
+ text.split(/\s+/).each do |word|
259
+ markov_machine.insert(word)
260
+ end
261
+ assert_equal(["PROLOGUE", "Enter", "CHORUS",
262
+ "CHORUS.", "O", "for", "a", "Muse",
263
+ "of", "fire,", "that", "would", "ascend",
264
+ "The", "brightest", "heaven", "of",
265
+ "invention,", "A", "kingdom"],
266
+ markov_machine.generate(20));
267
+ markov_machine = RandomData::MarkovGenerator.new(3)
268
+ text.scan(/\S{2}|\s+/).each do |word|
269
+ markov_machine.insert(word)
270
+ end
271
+ assert_equal(["PR", "OL", "OG", "UE", "\n\n", "En",
272
+ "te", " ", "CH", "OR", "US", "\n\n",
273
+ "CH", "OR", "US", "\n\n", "CH", "OR",
274
+ "US", "\n\n"],
275
+ markov_machine.generate(20));
276
+ end
277
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jjulian-random_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Subelsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
17
+ email: mike@subelsky.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - History.txt
24
+ - Manifest.txt
25
+ - README.rdoc
26
+ files:
27
+ - History.txt
28
+ - License.txt
29
+ - Manifest.txt
30
+ - README.rdoc
31
+ - Rakefile
32
+ - lib/random_data.rb
33
+ - lib/random_data/array_randomizer.rb
34
+ - lib/random_data/booleans.rb
35
+ - lib/random_data/contact_info.rb
36
+ - lib/random_data/dates.rb
37
+ - lib/random_data/grammar.rb
38
+ - lib/random_data/locations.rb
39
+ - lib/random_data/markov.rb
40
+ - lib/random_data/names.rb
41
+ - lib/random_data/numbers.rb
42
+ - lib/random_data/text.rb
43
+ - lib/random_data/version.rb
44
+ - script/console
45
+ - script/destroy
46
+ - script/generate
47
+ - script/txt2html
48
+ - test/henry_v_prolog.txt
49
+ - test/test_helper.rb
50
+ - test/test_random_data.rb
51
+ has_rdoc: true
52
+ homepage: http://random-data.rubyforge.org/
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --main
56
+ - README.rdoc
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: random-data
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: Generate random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
78
+ test_files:
79
+ - test/test_random_data.rb