random_data_despegar 2.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,56 @@
1
+ module RandomDataDespegar
2
+ module Numbers
3
+ #n can be an Integer or a Range. If it is an Integer, it just returns a random
4
+ #number greater than or equal to 0 and less than n. If it is a Range, it
5
+ #returns a random number within the range
6
+ # Examples
7
+ #
8
+ # >> Random.number(5)
9
+ # => 4
10
+ # >> Random.number(5)
11
+ # => 2
12
+ # >> Random.number(5)
13
+ # => 1
14
+ def number(n)
15
+ n.is_a?(Range) ? n.to_a.rand : rand(n)
16
+ end
17
+
18
+ # number_size is the size of the number to be generated.
19
+ # >> random_number_with_size (5)
20
+ # => "12345"
21
+ # >> random_number_with_size(10)
22
+ # => "1234321234"
23
+ # >> random_number_with_size(20)
24
+ # => "25895277473360037460"
25
+ def number_with_size(number_size)
26
+ Array.new(number_size){rand(9)}.join
27
+ end
28
+
29
+ # Returns an alphanumeric number with size n
30
+ def alphanumeric_with_size(number_size)
31
+ Random.alphanumeric(number_size)
32
+ end
33
+
34
+ # return a random bit, 0 or 1.
35
+ def bit
36
+ rand(2)
37
+ end
38
+
39
+ # return an array of n random bits.
40
+ def bits(n)
41
+ x = []
42
+ n.times {x << bit}
43
+ x
44
+ end
45
+
46
+ # Return card numbers, based on a regex and card number length.
47
+ # regex will be a string
48
+ # length will be an integer
49
+ #
50
+ def random_card_number (regex, length)
51
+ regex = regex.chomp("$").chomp("+")+"{"+(length.to_s)+"}"
52
+ /#{regex}/.random_example.slice(0,length)
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,70 @@
1
+ module RandomDataDespegar
2
+
3
+ module Text
4
+
5
+ # Methods to create random strings and paragraphs.
6
+
7
+ # Returns a string of random upper- and lowercase alphanumeric characters. Accepts a size parameters, defaults to 16 characters.
8
+ #
9
+ # >> Random.alphanumeric
10
+ #
11
+ # "Ke2jdknPYAI8uCXj"
12
+ #
13
+ # >> Random.alphanumeric(5)
14
+ #
15
+ # "7sj7i"
16
+
17
+ def alphanumeric(size=16)
18
+ s = ""
19
+ size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
20
+ s
21
+ end
22
+
23
+ # TODO make these more coherent #:nodoc:
24
+
25
+ @@sentences = [
26
+ "Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
27
+ "Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
28
+ "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur",
29
+ "Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum",
30
+ "There's a voice that keeps on calling me",
31
+ "Down the road that's where I'll always be",
32
+ "Every stop I make I make a new friend; Can't stay for long just turn around and I'm gone again",
33
+ "Maybe tomorrow I'll want to settle down - until tomorrow I'll just keep moving on",
34
+ "Hong Kong Phooey number one super guy, Hong Kong Phooey quicker than the human eye",
35
+ "He's got style, a groovy style, and a car that just won't stop",
36
+ "Hey there where ya goin, not exactly knowin'",
37
+ "Who says you have to call just one place home?",
38
+ "He's going everywhere, B.J. McKay and his best friend Bear",
39
+ "He just keeps on movin' and ladies keep improvin'",
40
+ "Every day is better than the last, with new dreams and better scenes and best of all I don't pay property tax",
41
+ "Rolling down to Dallas - who is providin' my palace?",
42
+ "Off to New Orleans or who knows where",
43
+ "Soaring through all the galaxies in search of Earth flying in to the night",
44
+ "Ulysses, fighting evil and tyranny with all his power and with all of his might",
45
+ "No-one else can do the things you do, like a bolt of thunder from the blue",
46
+ "Always fighting all the evil forces bringing peace and justice to all",
47
+ "I've gotten burned over Cheryl Tiegs and blown up for Raquel Welch, but when I end up in the hay it's only hay, hey hey",
48
+ "I might jump an open drawbridge or Tarzan from a vine, beause I'm the unknown stuntman that makes Eastwood look so fine"]
49
+
50
+ # Returns a given number of paragraphs delimited by two newlines (defaults to two paragraphs), using a small pool of generic sentences.
51
+ # >> Random.paragraphs
52
+ #
53
+ # "I might jump an open drawbridge or Tarzan from a vine, beause I'm the unknown stuntman that makes Eastwood look so fine.\n\n \Always fighting all the evil forces bringing peace and justice to all. \n\n"
54
+
55
+ def paragraphs(num = 2)
56
+ text = ''
57
+
58
+ num.times do
59
+ (rand(5)+1).times do
60
+ text += @@sentences.rand + '. '
61
+ end
62
+ text += "\n\n"
63
+ end
64
+
65
+ return text
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module RandomDataDespegar #:nodoc:
2
+ VERSION = '2.1' #:nodoc:
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "random_data_despegar/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "random_data_despegar"
7
+ s.version = RandomDataDespegar::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mike Subelsky", "Tom Harris", "Ezequiel Uhrig", "Marina Girotti"]
10
+ s.email = "mgirotti@despegar.com"
11
+ s.homepage = "https://github.com/despegar/random_data-helper"
12
+ s.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."
13
+ s.description = "Random data generator"
14
+
15
+ s.rubygems_version = "1.3.7"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- test/*`.split("\n")
19
+ s.extra_rdoc_files = [ "README.rdoc" ]
20
+ s.rdoc_options = ["--charset=UTF-8"]
21
+ s.require_path = "lib"
22
+ end
@@ -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_despegar.rb'}"
9
+ puts "Loading random_data gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -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)
@@ -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)
@@ -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,25 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestRandomData < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ # to make random numbers deterministic for testing purposes
8
+ srand(100)
9
+ end
10
+
11
+ def test_passenger_birthdate_parsed
12
+ %w{adult child infant}.each { |type|
13
+ puts type
14
+ assert Random.passenger_birthdate_parsed(type)
15
+ }
16
+ end
17
+
18
+ def test_passenger_birthdate
19
+ %w{adult child infant}.each { |type|
20
+ p type
21
+ assert Random.passenger_birthdate(type)
22
+ }
23
+ end
24
+
25
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'expects'
4
+ require 'pp'
5
+ require 'pry'
6
+ require "regexp-examples"
7
+ require "rspec"
8
+ require File.dirname(__FILE__) + '/../lib/random_data_despegar'
@@ -0,0 +1,37 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestRandomData < Test::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ # to make random numbers deterministic for testing purposes
8
+ srand(100)
9
+ end
10
+
11
+ def test_invoice_tests
12
+ sites = %w{cr ec pa pe ve mx co cl ar br}
13
+ sites.each { |site|
14
+
15
+ assert invoice = Random.invoice_data_for(site)
16
+ invoice.each do |key, value|
17
+ if value.is_a?(Hash)
18
+ value.each do |k,v|
19
+ assert k, "#{value}" if k
20
+ assert v, "#{value}"
21
+ end
22
+ else
23
+ assert value,"#{invoice}"
24
+ end
25
+ end
26
+ }
27
+ end
28
+
29
+ def test_id_for_site_tests
30
+ sites = %w{cr ec pa pe ve mx co cl ar br}
31
+ sites.each { |site|
32
+ assert Random.id_for(site), "#{site}"
33
+ }
34
+ assert Random.argentinean_cuil, "Cuil generator failed"
35
+ end
36
+
37
+ end
@@ -0,0 +1,309 @@
1
+ require_relative "test_helper"
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
+
11
+ def test_should_return_random_phone_number
12
+ assert_equal "620-892-9039", Random.phone
13
+ end
14
+
15
+ def test_should_return_random_international_phone
16
+ assert_equal "011-9-34-9039", Random.international_phone
17
+ end
18
+
19
+ def test_should_return_random_email
20
+ assert_equal "ijones@webmail.com", Random.email
21
+ end
22
+
23
+ def test_should_return_random_date
24
+ Date.expects(:today).returns(Date.civil(2007,9,15))
25
+ assert_equal "2007-09-13", Random.date.to_s
26
+ end
27
+
28
+ def test_should_return_random_date_with_range
29
+ Date.expects(:today).returns(Date.civil(2007,9,15))
30
+ assert_equal "2007-10-29", Random.date(1500).to_s
31
+ end
32
+
33
+ def test_should_return_random_address_line_1
34
+ assert_equal "38408 Highland Blvd", Random.address_line_1
35
+ end
36
+
37
+ def test_should_return_random_address_line_2
38
+ assert_equal "Lot 792", Random.address_line_2
39
+ end
40
+
41
+ def test_should_return_random_zipcode
42
+ assert_equal "38408", Random.zipcode
43
+ end
44
+
45
+ def test_should_return_random_uk_post_code
46
+ assert_equal "BM8 24DB", Random.uk_post_code
47
+ end
48
+
49
+ def test_should_return_random_state
50
+ assert_equal "DE", Random.state_code
51
+ end
52
+
53
+ def test_should_return_random_state_full
54
+ assert_equal "Delaware", Random.state_full
55
+ end
56
+
57
+ def test_should_return_random_country
58
+ assert_equal "Armenia", Random.country
59
+ end
60
+
61
+ def test_should_return_random_city
62
+ assert_equal "Georgetown", Random.city
63
+ end
64
+
65
+ def test_should_return_random_firstname
66
+ assert_equal "Donald", Random.firstname
67
+ end
68
+
69
+ def test_should_return_random_first_name
70
+ assert_equal "Donald", Random.first_name
71
+ end
72
+
73
+
74
+ def test_should_return_random_firstnamemale
75
+ assert_equal "Donald", Random.firstname_male
76
+ end
77
+
78
+ def test_should_return_random_first_name_male
79
+ assert_equal "Donald", Random.first_name_male
80
+ end
81
+
82
+ def test_should_return_random_firstnamefemale
83
+ assert_equal "Charlotte", Random.firstname_female
84
+ end
85
+
86
+ def test_should_return_random_first_name_female
87
+ assert_equal "Charlotte", Random.first_name_female
88
+ end
89
+
90
+ def test_should_return_random_initial
91
+ assert_equal "I", Random.initial
92
+ end
93
+
94
+ def test_should_return_random_lastname
95
+ assert_equal "Clarke", Random.lastname
96
+ end
97
+
98
+ def test_should_return_random_last_name
99
+ assert_equal "Clarke", Random.last_name
100
+ end
101
+
102
+ def test_should_return_random_full_name
103
+ assert_equal "Donald Jones", Random.full_name
104
+ end
105
+
106
+ def test_should_return_random_companyname
107
+ srand(17)
108
+ assert_equal "Garcia Marketing, Corp.", Random.companyname
109
+ end
110
+
111
+ def test_should_return_random_company_name
112
+ srand(9999999999)
113
+ assert_equal "Anthony, Edwards & Edwards Publishing", Random.company_name
114
+ end
115
+
116
+ def test_should_return_random_alphanumeric
117
+ assert_equal "8O3dNFmAUqYr2YEY", Random.alphanumeric
118
+ end
119
+
120
+ def test_should_return_random_alphanumeric_with_range
121
+ assert_equal "8O3dNFm", Random.alphanumeric(7)
122
+ end
123
+
124
+ def test_should_return_random_paragraphs
125
+ assert_equal 2, Random.paragraphs(2).scan(/\n\n/).size
126
+ end
127
+
128
+ def test_should_return_random_paragraphs_with_limit
129
+ num_paragraphs = 5
130
+ assert_equal num_paragraphs, Random.paragraphs(num_paragraphs).scan(/\n\n/).size
131
+ end
132
+
133
+ def test_initial_should_not_return_nil
134
+ srand(11) #This would force the nil case in the old implementation
135
+ assert_not_nil Random.initial
136
+ end
137
+
138
+ def test_should_return_random_date_using_range
139
+ Date.expects(:today).returns(Date.civil(2007,9,15))
140
+ assert_equal '1998-03-30', Random.date(-5000..-1000).to_s
141
+ end
142
+
143
+ def test_should_return_random_date_between_using_range_of_dates
144
+ min = Date.parse('1966-11-15')
145
+ max = Date.parse('1990-01-01')
146
+ assert_equal '1982-04-25', Random.date_between(min..max).to_s
147
+ end
148
+
149
+ def test_should_return_random_date_between_using_range_of_strings
150
+ assert_equal '1982-04-25', Random.date_between('1966-11-15'..'1990-01-01').to_s
151
+ end
152
+
153
+ def test_should_return_random_boolean_true
154
+ srand(99)
155
+ assert_equal true, Random.boolean
156
+ end
157
+
158
+ def test_should_return_random_boolean_false
159
+ assert_equal false, Random.boolean
160
+ end
161
+
162
+ def test_should_return_random_number_less_than_10
163
+ assert_equal 8, Random.number(9)
164
+ end
165
+
166
+ def test_should_return_random_bit
167
+ assert_equal 0, Random.bit
168
+ end
169
+
170
+ def test_should_return_random_bits
171
+ assert_equal 10, Random.bits(10).size
172
+ assert_equal [0, 1, 0, 0, 0, 0, 1, 0, 0, 1], Random.bits(10)
173
+ end
174
+
175
+ def test_should_return_random_number_within_range
176
+ assert_equal 13, Random.number(5..15)
177
+ end
178
+
179
+ def test_should_return_random_grammatical_construct
180
+ assert_equal "Bob bites Rex",
181
+ Random::grammatical_construct({:story => [:man, " bites ", :dog],
182
+ :man => { :bob => "Bob"},
183
+ :dog => {:a =>"Rex", :b =>"Rover"}},
184
+ :story)
185
+ end
186
+
187
+ def test_roulette_wheel_with_positive_weights
188
+ weights=[10,5,1,1];
189
+ results = []
190
+ 17.times do
191
+ results << weights.roulette
192
+ end
193
+ assert_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0],
194
+ results);
195
+ results = []
196
+ weights.roulette(17) do |i|
197
+ results << i
198
+ end
199
+ assert_equal([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
200
+ results);
201
+ end
202
+
203
+ def test_roulette_wheel_with_zero_weights
204
+ weights=[0,0,0];
205
+ assert_raise RuntimeError do
206
+ weights.roulette
207
+ end
208
+ assert_raise RuntimeError do
209
+ x=0
210
+ weights.roulette(1){|i| x=i}
211
+ end
212
+ end
213
+
214
+ def test_roulette_wheel_with_negative_weights
215
+ weights=[2,-1,2];
216
+ assert_raise RuntimeError do
217
+ weights.roulette
218
+ end
219
+ assert_raise RuntimeError do
220
+ x=0
221
+ weights.roulette(1){|i| x=i}
222
+ end
223
+ end
224
+ end
225
+
226
+ class TestRandomDataMethodMissing < Test::Unit::TestCase
227
+
228
+ def sports
229
+ @sports ||= %w(hockey basketball water\ polo five-a-side football judo pole\ vault steeple\ chase)
230
+ end
231
+
232
+ def lines
233
+ @lines ||= sports.join("\r\n")
234
+ end
235
+
236
+ def path
237
+ @path ||= File.join("lib","sport.dat")
238
+ end
239
+
240
+ def setup
241
+ @file = stub('file', :read => lines)
242
+ $:.stubs(:each).yields("lib")
243
+ File.stubs(:exist?).returns(true)
244
+ File.stubs(:open).yields(@file)
245
+ end
246
+
247
+ def test_should_search_load_path_for_file
248
+ $:.expects(:each).yields("lib")
249
+ Random.sport
250
+ end
251
+
252
+ def test_should_join_path_with_methodname_dot_dat
253
+ File.expects(:join).with("lib","sport.dat").returns("lib/sport.dat")
254
+ Random.sport
255
+ end
256
+
257
+ def test_should_test_for_existence_of_filename_matching_method_missing_call
258
+ File.expects(:exist?).with(path).returns(true)
259
+ Random.sport
260
+ end
261
+
262
+ def test_should_call_super_if_unable_to_find_file
263
+ # can't test super call directly, so we test whether MethodMissing gets raised properly
264
+ File.stubs(:exist?).returns(false)
265
+ assert_raise(NoMethodError) { Random.horse }
266
+ end
267
+
268
+ def test_should_open_file_matching_method_missing_call
269
+ File.expects(:open).with(path,"r").yields(@file)
270
+ Random.sport
271
+ end
272
+
273
+ def test_should_read_lines_from_file
274
+ @file.expects(:read).returns(@lines)
275
+ Random.sport
276
+ end
277
+
278
+ def test_should_return_random_line_from_a_file
279
+ srand(101)
280
+ assert_equal 'steeple chase', Random.sport
281
+ assert_equal 'five-a-side', Random.sport
282
+ end
283
+
284
+ end
285
+
286
+ class TestRandomDataMarkovGenerator < Test::Unit::TestCase
287
+ def test_markov_generator
288
+ markov_machine = RandomDataDespegar::MarkovGenerator.new(3)
289
+ text = IO.read "#{File.dirname(__FILE__)}/henry_v_prolog.txt"
290
+ text.split(/\s+/).each do |word|
291
+ markov_machine.insert(word)
292
+ end
293
+ assert_equal(["PROLOGUE", "Enter", "CHORUS",
294
+ "CHORUS.", "O", "for", "a", "Muse",
295
+ "of", "fire,", "that", "would", "ascend",
296
+ "The", "brightest", "heaven", "of",
297
+ "invention,", "A", "kingdom"],
298
+ markov_machine.generate(20));
299
+ markov_machine = RandomDataDespegar::MarkovGenerator.new(3)
300
+ text.scan(/\S{2}|\s+/).each do |word|
301
+ markov_machine.insert(word)
302
+ end
303
+ assert_equal(["PR", "OL", "OG", "UE", "\n\n", "En",
304
+ "te", " ", "CH", "OR", "US", "\n\n",
305
+ "CH", "OR", "US", "\n\n", "CH", "OR",
306
+ "US", "\n\n"],
307
+ markov_machine.generate(20));
308
+ end
309
+ end