Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Error with Random.state on 1.9.x #2

Merged
merged 4 commits into from

2 participants

@jacaetevha

Random.state raises a private method error when run on 1.9.x because the core Random class has a private state method (which means something very different, obviously). So, I added a RandomData::Locations#state_code method and deprecated the RandomData::Locations#state method.

@jacaetevha

I have also added Names.company_name

@tomharris tomharris merged commit 2fb4d0d into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 30, 2011
  1. deprecated RandomData::Locations#state

    Jason Rogers authored
Commits on Dec 13, 2011
  1. added company_name

    Jason Rogers authored
  2. improve randomization of company names

    Jason Rogers authored
This page is out of date. Refresh to see the latest.
View
7 Gemfile
@@ -0,0 +1,7 @@
+source 'http://rubygems.org'
+
+group :development do
+ gem 'mocha'
+ gem 'rake'
+ gem 'rdoc'
+end
View
20 Gemfile.lock
@@ -0,0 +1,20 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ json (1.6.3)
+ json (1.6.3-java)
+ metaclass (0.0.1)
+ mocha (0.10.0)
+ metaclass (~> 0.0.1)
+ rake (0.9.2)
+ rdoc (3.11)
+ json (~> 1.4)
+
+PLATFORMS
+ java
+ ruby
+
+DEPENDENCIES
+ mocha
+ rake
+ rdoc
View
16 Rakefile
@@ -1,6 +1,10 @@
-require 'rake'
-require 'rake/testtask'
-require 'rake/rdoctask'
-
-# TODO - want other tests/tasks run by default? Add them to the list
-# task :default => [:spec, :features]
+require 'rake'
+require 'rake/testtask'
+require 'rdoc/task'
+
+RDoc::Task.new do |rdoc|
+end
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# task :default => [:spec, :features]
+task :default => :test
View
10 lib/random_data/locations.rb
@@ -80,10 +80,18 @@ def uk_post_code
# Returns a state 2-character abbreviation
# Random.state = "IL"
- def state
+ def state_code
@@us_states.rand[1]
end
+ # This method is deprecated, and will not
+ # work in 1.9.x because Random already defines
+ # state as a private method. Use state_code
+ # instead.
+ def state
+ state_code
+ end
+
# Returns a full state name
#Random.state_full = "Texas"
View
27 lib/random_data/names.rb
@@ -14,7 +14,34 @@ def initial
KIRBY KIRK LAKE LEE LEWIS MARTIN MARTINEZ MAJOR MILLER MOORE OATES PETERS PETERSON ROBERTSON
ROBINSON RODRIGUEZ SMITH SMYTHE STEVENS TAYLOR THATCHER THOMAS THOMPSON WALKER WASHINGTON WHITE
WILLIAMS WILSON YORKE)
+ @@incorporation_types = %w{LLC Inc Inc. Ltd. LP LLP Corp. PLLC}
+ @@company_types = %w{Clothier Publishing Computing Consulting Engineering Industries Marketing Manufacturing}
+ # Returns a random company name
+ #
+ # >> Random.company_name
+ #
+ # "Harris & Thomas"
+
+ def companyname
+ num = rand(5)
+ if num == 0
+ num = 1
+ end
+ final = num.times.collect { @@lastnames.rand.capitalize }
+
+ if final.count == 1
+ "#{final.first} #{@@company_types.rand}, #{@@incorporation_types.rand}"
+ else
+ incorporation_type = rand(17) % 2 == 0 ? @@incorporation_types.rand : nil
+ company_type = rand(17) % 2 == 0 ? @@company_types.rand : nil
+ trailer = company_type.nil? ? "" : " #{company_type}"
+ trailer << ", #{incorporation_type}" unless incorporation_type.nil?
+ "#{final[0..-1].join(', ')} & #{final.last}#{trailer}"
+ end
+ end
+
+ alias company_name companyname
# Returns a random lastname
#
# >> Random.lastname
View
606 test/test_random_data.rb
@@ -1,298 +1,308 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestRandomData < Test::Unit::TestCase
-
- def setup
- # to make random numbers deterministic for testing purposes
- srand(100)
- end
-
- def test_should_return_random_phone_number
- assert_equal "620-892-9039", Random.phone
- end
-
- def test_should_return_random_international_phone
- assert_equal "011-9-34-9039", Random.international_phone
- end
-
- def test_should_return_random_email
- assert_equal "ijones@webmail.com", Random.email
- end
-
- def test_should_return_random_date
- Date.expects(:today).returns(Date.civil(2007,9,15))
- assert_equal "2007-09-13", Random.date.to_s
- end
-
- def test_should_return_random_date_with_range
- Date.expects(:today).returns(Date.civil(2007,9,15))
- assert_equal "2007-10-29", Random.date(1500).to_s
- end
-
- def test_should_return_random_address_line_1
- assert_equal "38408 Highland Blvd", Random.address_line_1
- end
-
- def test_should_return_random_address_line_2
- assert_equal "Lot 792", Random.address_line_2
- end
-
- def test_should_return_random_zipcode
- assert_equal "38408", Random.zipcode
- end
-
- def test_should_return_random_uk_post_code
- assert_equal "BM8 24DB", Random.uk_post_code
- end
-
- def test_should_return_random_state
- assert_equal "DE", Random.state
- end
-
- def test_should_return_random_state_full
- assert_equal "Delaware", Random.state_full
- end
-
- def test_should_return_random_country
- assert_equal "Armenia", Random.country
- end
-
- def test_should_return_random_city
- assert_equal "Georgetown", Random.city
- end
-
- def test_should_return_random_firstname
- assert_equal "Donald", Random.firstname
- end
-
- def test_should_return_random_first_name
- assert_equal "Donald", Random.first_name
- end
-
-
- def test_should_return_random_firstnamemale
- assert_equal "Donald", Random.firstname_male
- end
-
- def test_should_return_random_first_name_male
- assert_equal "Donald", Random.first_name_male
- end
-
- def test_should_return_random_firstnamefemale
- assert_equal "Charlotte", Random.firstname_female
- end
-
- def test_should_return_random_first_name_female
- assert_equal "Charlotte", Random.first_name_female
- end
-
- def test_should_return_random_initial
- assert_equal "I", Random.initial
- end
-
- def test_should_return_random_lastname
- assert_equal "Clarke", Random.lastname
- end
-
- def test_should_return_random_last_name
- assert_equal "Clarke", Random.last_name
- end
-
- def test_should_return_random_full_name
- assert_equal "Donald Jones", Random.full_name
- end
-
- def test_should_return_random_alphanumeric
- assert_equal "8O3dNFmAUqYr2YEY", Random.alphanumeric
- end
-
- def test_should_return_random_alphanumeric_with_range
- assert_equal "8O3dNFm", Random.alphanumeric(7)
- end
-
- def test_should_return_random_paragraphs
- assert_equal 2, Random.paragraphs(2).scan(/\n\n/).size
- end
-
- def test_should_return_random_paragraphs_with_limit
- num_paragraphs = 5
- assert_equal num_paragraphs, Random.paragraphs(num_paragraphs).scan(/\n\n/).size
- end
-
- def test_initial_should_not_return_nil
- srand(11) #This would force the nil case in the old implementation
- assert_not_nil Random.initial
- end
-
- def test_should_return_random_date_using_range
- Date.expects(:today).returns(Date.civil(2007,9,15))
- assert_equal '1998-03-30', Random.date(-5000..-1000).to_s
- end
-
- def test_should_return_random_date_between_using_range_of_dates
- min = Date.parse('1966-11-15')
- max = Date.parse('1990-01-01')
- assert_equal '1982-04-25', Random.date_between(min..max).to_s
- end
-
- def test_should_return_random_date_between_using_range_of_strings
- assert_equal '1982-04-25', Random.date_between('1966-11-15'..'1990-01-01').to_s
- end
-
- def test_should_return_random_boolean_true
- srand(99)
- assert_equal true, Random.boolean
- end
-
- def test_should_return_random_boolean_false
- assert_equal false, Random.boolean
- end
-
- def test_should_return_random_number_less_than_10
- assert_equal 8, Random.number(9)
- end
-
- def test_should_return_random_bit
- assert_equal 0, Random.bit
- end
-
- def test_should_return_random_bits
- assert_equal 10, Random.bits(10).size
- assert_equal [0, 1, 0, 0, 0, 0, 1, 0, 0, 1], Random.bits(10)
- end
-
- def test_should_return_random_number_within_range
- assert_equal 13, Random.number(5..15)
- end
-
- def test_should_return_random_grammatical_construct
- assert_equal "Bob bites Rex",
- Random::grammatical_construct({:story => [:man, " bites ", :dog],
- :man => { :bob => "Bob"},
- :dog => {:a =>"Rex", :b =>"Rover"}},
- :story)
- end
-
- def test_roulette_wheel_with_positive_weights
- weights=[10,5,1,1];
- results = []
- 17.times do
- results << weights.roulette
- end
- assert_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0],
- results);
- results = []
- weights.roulette(17) do |i|
- results << i
- end
- assert_equal([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- results);
- end
-
- def test_roulette_wheel_with_zero_weights
- weights=[0,0,0];
- assert_raise RuntimeError do
- weights.roulette
- end
- assert_raise RuntimeError do
- x=0
- weights.roulette(1){|i| x=i}
- end
- end
-
- def test_roulette_wheel_with_negative_weights
- weights=[2,-1,2];
- assert_raise RuntimeError do
- weights.roulette
- end
- assert_raise RuntimeError do
- x=0
- weights.roulette(1){|i| x=i}
- end
- end
-end
-
-class TestRandomDataMethodMissing < Test::Unit::TestCase
-
- def sports
- @sports ||= %w(hockey basketball water\ polo five-a-side football judo pole\ vault steeple\ chase)
- end
-
- def lines
- @lines ||= sports.join("\r\n")
- end
-
- def path
- @path ||= File.join("lib","sport.dat")
- end
-
- def setup
- @file = stub('file', :read => lines)
- $:.stubs(:each).yields("lib")
- File.stubs(:exist?).returns(true)
- File.stubs(:open).yields(@file)
- end
-
- def test_should_search_load_path_for_file
- $:.expects(:each).yields("lib")
- Random.sport
- end
-
- def test_should_join_path_with_methodname_dot_dat
- File.expects(:join).with("lib","sport.dat").returns("lib/sport.dat")
- Random.sport
- end
-
- def test_should_test_for_existence_of_filename_matching_method_missing_call
- File.expects(:exist?).with(path).returns(true)
- Random.sport
- end
-
- def test_should_call_super_if_unable_to_find_file
- # can't test super call directly, so we test whether MethodMissing gets raised properly
- File.stubs(:exist?).returns(false)
- assert_raise(NoMethodError) { Random.horse }
- end
-
- def test_should_open_file_matching_method_missing_call
- File.expects(:open).with(path,"r").yields(@file)
- Random.sport
- end
-
- def test_should_read_lines_from_file
- @file.expects(:read).returns(@lines)
- Random.sport
- end
-
- def test_should_return_random_line_from_a_file
- srand(101)
- assert_equal 'steeple chase', Random.sport
- assert_equal 'five-a-side', Random.sport
- end
-
-end
-
-class TestRandomDataMarkovGenerator < Test::Unit::TestCase
- def test_markov_generator
- markov_machine = RandomData::MarkovGenerator.new(3)
- text = IO.read "#{File.dirname(__FILE__)}/henry_v_prolog.txt"
- text.split(/\s+/).each do |word|
- markov_machine.insert(word)
- end
- assert_equal(["PROLOGUE", "Enter", "CHORUS",
- "CHORUS.", "O", "for", "a", "Muse",
- "of", "fire,", "that", "would", "ascend",
- "The", "brightest", "heaven", "of",
- "invention,", "A", "kingdom"],
- markov_machine.generate(20));
- markov_machine = RandomData::MarkovGenerator.new(3)
- text.scan(/\S{2}|\s+/).each do |word|
- markov_machine.insert(word)
- end
- assert_equal(["PR", "OL", "OG", "UE", "\n\n", "En",
- "te", " ", "CH", "OR", "US", "\n\n",
- "CH", "OR", "US", "\n\n", "CH", "OR",
- "US", "\n\n"],
- markov_machine.generate(20));
- end
-end
+require File.dirname(__FILE__) + '/test_helper.rb'
+
+class TestRandomData < Test::Unit::TestCase
+
+ def setup
+ # to make random numbers deterministic for testing purposes
+ srand(100)
+ end
+
+ def test_should_return_random_phone_number
+ assert_equal "620-892-9039", Random.phone
+ end
+
+ def test_should_return_random_international_phone
+ assert_equal "011-9-34-9039", Random.international_phone
+ end
+
+ def test_should_return_random_email
+ assert_equal "ijones@webmail.com", Random.email
+ end
+
+ def test_should_return_random_date
+ Date.expects(:today).returns(Date.civil(2007,9,15))
+ assert_equal "2007-09-13", Random.date.to_s
+ end
+
+ def test_should_return_random_date_with_range
+ Date.expects(:today).returns(Date.civil(2007,9,15))
+ assert_equal "2007-10-29", Random.date(1500).to_s
+ end
+
+ def test_should_return_random_address_line_1
+ assert_equal "38408 Highland Blvd", Random.address_line_1
+ end
+
+ def test_should_return_random_address_line_2
+ assert_equal "Lot 792", Random.address_line_2
+ end
+
+ def test_should_return_random_zipcode
+ assert_equal "38408", Random.zipcode
+ end
+
+ def test_should_return_random_uk_post_code
+ assert_equal "BM8 24DB", Random.uk_post_code
+ end
+
+ def test_should_return_random_state
+ assert_equal "DE", Random.state_code
+ end
+
+ def test_should_return_random_state_full
+ assert_equal "Delaware", Random.state_full
+ end
+
+ def test_should_return_random_country
+ assert_equal "Armenia", Random.country
+ end
+
+ def test_should_return_random_city
+ assert_equal "Georgetown", Random.city
+ end
+
+ def test_should_return_random_firstname
+ assert_equal "Donald", Random.firstname
+ end
+
+ def test_should_return_random_first_name
+ assert_equal "Donald", Random.first_name
+ end
+
+
+ def test_should_return_random_firstnamemale
+ assert_equal "Donald", Random.firstname_male
+ end
+
+ def test_should_return_random_first_name_male
+ assert_equal "Donald", Random.first_name_male
+ end
+
+ def test_should_return_random_firstnamefemale
+ assert_equal "Charlotte", Random.firstname_female
+ end
+
+ def test_should_return_random_first_name_female
+ assert_equal "Charlotte", Random.first_name_female
+ end
+
+ def test_should_return_random_initial
+ assert_equal "I", Random.initial
+ end
+
+ def test_should_return_random_lastname
+ assert_equal "Clarke", Random.lastname
+ end
+
+ def test_should_return_random_last_name
+ assert_equal "Clarke", Random.last_name
+ end
+
+ def test_should_return_random_full_name
+ assert_equal "Donald Jones", Random.full_name
+ end
+
+ def test_should_return_random_companyname
+ srand(17)
+ assert_equal "Garcia Marketing, Corp.", Random.companyname
+ end
+
+ def test_should_return_random_company_name
+ srand(9999999999)
+ assert_equal "Anthony, Edwards & Edwards Publishing", Random.company_name
+ end
+
+ def test_should_return_random_alphanumeric
+ assert_equal "8O3dNFmAUqYr2YEY", Random.alphanumeric
+ end
+
+ def test_should_return_random_alphanumeric_with_range
+ assert_equal "8O3dNFm", Random.alphanumeric(7)
+ end
+
+ def test_should_return_random_paragraphs
+ assert_equal 2, Random.paragraphs(2).scan(/\n\n/).size
+ end
+
+ def test_should_return_random_paragraphs_with_limit
+ num_paragraphs = 5
+ assert_equal num_paragraphs, Random.paragraphs(num_paragraphs).scan(/\n\n/).size
+ end
+
+ def test_initial_should_not_return_nil
+ srand(11) #This would force the nil case in the old implementation
+ assert_not_nil Random.initial
+ end
+
+ def test_should_return_random_date_using_range
+ Date.expects(:today).returns(Date.civil(2007,9,15))
+ assert_equal '1998-03-30', Random.date(-5000..-1000).to_s
+ end
+
+ def test_should_return_random_date_between_using_range_of_dates
+ min = Date.parse('1966-11-15')
+ max = Date.parse('1990-01-01')
+ assert_equal '1982-04-25', Random.date_between(min..max).to_s
+ end
+
+ def test_should_return_random_date_between_using_range_of_strings
+ assert_equal '1982-04-25', Random.date_between('1966-11-15'..'1990-01-01').to_s
+ end
+
+ def test_should_return_random_boolean_true
+ srand(99)
+ assert_equal true, Random.boolean
+ end
+
+ def test_should_return_random_boolean_false
+ assert_equal false, Random.boolean
+ end
+
+ def test_should_return_random_number_less_than_10
+ assert_equal 8, Random.number(9)
+ end
+
+ def test_should_return_random_bit
+ assert_equal 0, Random.bit
+ end
+
+ def test_should_return_random_bits
+ assert_equal 10, Random.bits(10).size
+ assert_equal [0, 1, 0, 0, 0, 0, 1, 0, 0, 1], Random.bits(10)
+ end
+
+ def test_should_return_random_number_within_range
+ assert_equal 13, Random.number(5..15)
+ end
+
+ def test_should_return_random_grammatical_construct
+ assert_equal "Bob bites Rex",
+ Random::grammatical_construct({:story => [:man, " bites ", :dog],
+ :man => { :bob => "Bob"},
+ :dog => {:a =>"Rex", :b =>"Rover"}},
+ :story)
+ end
+
+ def test_roulette_wheel_with_positive_weights
+ weights=[10,5,1,1];
+ results = []
+ 17.times do
+ results << weights.roulette
+ end
+ assert_equal([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0],
+ results);
+ results = []
+ weights.roulette(17) do |i|
+ results << i
+ end
+ assert_equal([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
+ results);
+ end
+
+ def test_roulette_wheel_with_zero_weights
+ weights=[0,0,0];
+ assert_raise RuntimeError do
+ weights.roulette
+ end
+ assert_raise RuntimeError do
+ x=0
+ weights.roulette(1){|i| x=i}
+ end
+ end
+
+ def test_roulette_wheel_with_negative_weights
+ weights=[2,-1,2];
+ assert_raise RuntimeError do
+ weights.roulette
+ end
+ assert_raise RuntimeError do
+ x=0
+ weights.roulette(1){|i| x=i}
+ end
+ end
+end
+
+class TestRandomDataMethodMissing < Test::Unit::TestCase
+
+ def sports
+ @sports ||= %w(hockey basketball water\ polo five-a-side football judo pole\ vault steeple\ chase)
+ end
+
+ def lines
+ @lines ||= sports.join("\r\n")
+ end
+
+ def path
+ @path ||= File.join("lib","sport.dat")
+ end
+
+ def setup
+ @file = stub('file', :read => lines)
+ $:.stubs(:each).yields("lib")
+ File.stubs(:exist?).returns(true)
+ File.stubs(:open).yields(@file)
+ end
+
+ def test_should_search_load_path_for_file
+ $:.expects(:each).yields("lib")
+ Random.sport
+ end
+
+ def test_should_join_path_with_methodname_dot_dat
+ File.expects(:join).with("lib","sport.dat").returns("lib/sport.dat")
+ Random.sport
+ end
+
+ def test_should_test_for_existence_of_filename_matching_method_missing_call
+ File.expects(:exist?).with(path).returns(true)
+ Random.sport
+ end
+
+ def test_should_call_super_if_unable_to_find_file
+ # can't test super call directly, so we test whether MethodMissing gets raised properly
+ File.stubs(:exist?).returns(false)
+ assert_raise(NoMethodError) { Random.horse }
+ end
+
+ def test_should_open_file_matching_method_missing_call
+ File.expects(:open).with(path,"r").yields(@file)
+ Random.sport
+ end
+
+ def test_should_read_lines_from_file
+ @file.expects(:read).returns(@lines)
+ Random.sport
+ end
+
+ def test_should_return_random_line_from_a_file
+ srand(101)
+ assert_equal 'steeple chase', Random.sport
+ assert_equal 'five-a-side', Random.sport
+ end
+
+end
+
+class TestRandomDataMarkovGenerator < Test::Unit::TestCase
+ def test_markov_generator
+ markov_machine = RandomData::MarkovGenerator.new(3)
+ text = IO.read "#{File.dirname(__FILE__)}/henry_v_prolog.txt"
+ text.split(/\s+/).each do |word|
+ markov_machine.insert(word)
+ end
+ assert_equal(["PROLOGUE", "Enter", "CHORUS",
+ "CHORUS.", "O", "for", "a", "Muse",
+ "of", "fire,", "that", "would", "ascend",
+ "The", "brightest", "heaven", "of",
+ "invention,", "A", "kingdom"],
+ markov_machine.generate(20));
+ markov_machine = RandomData::MarkovGenerator.new(3)
+ text.scan(/\S{2}|\s+/).each do |word|
+ markov_machine.insert(word)
+ end
+ assert_equal(["PR", "OL", "OG", "UE", "\n\n", "En",
+ "te", " ", "CH", "OR", "US", "\n\n",
+ "CH", "OR", "US", "\n\n", "CH", "OR",
+ "US", "\n\n"],
+ markov_machine.generate(20));
+ end
+end
Something went wrong with that request. Please try again.