malvestuto_random_data 1.5.1

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,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,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: malvestuto_random_data
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 5
9
+ - 1
10
+ version: 1.5.1
11
+ platform: ruby
12
+ authors:
13
+ - Mike Subelsky
14
+ - Bruno Malvestuto
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2009-06-27 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
24
+ email: random_data@mikeshop.net
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - PostInstall.txt
36
+ - README.rdoc
37
+ - Rakefile
38
+ - VERSION
39
+ - config/website.yml
40
+ - lib/random_data.rb
41
+ - lib/random_data/array_randomizer.rb
42
+ - lib/random_data/booleans.rb
43
+ - lib/random_data/contact_info.rb
44
+ - lib/random_data/dates.rb
45
+ - lib/random_data/grammar.rb
46
+ - lib/random_data/locations.rb
47
+ - lib/random_data/markov.rb
48
+ - lib/random_data/papers.rb
49
+ - lib/random_data/names.rb
50
+ - lib/random_data/numbers.rb
51
+ - lib/random_data/text.rb
52
+ - lib/random_data/version.rb
53
+ - script/console
54
+ - script/destroy
55
+ - script/generate
56
+ - script/txt2html
57
+ - test/henry_v_prolog.txt
58
+ - test/test_helper.rb
59
+ - test/test_random_data.rb
60
+ has_rdoc: true
61
+ homepage:
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 2
93
+ summary: A Ruby gem that provides a Random class with a series of methods for generating random test data including names, mailing addresses, dates, phone numbers, e-mail addresses, and text.
94
+ test_files:
95
+ - test/test_helper.rb
96
+ - test/test_random_data.rb