fixture_replacement 3.0.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.rdoc +5 -0
  3. data/README.rdoc +2 -14
  4. data/VERSION +1 -1
  5. data/lib/fixture_replacement/attribute_builder.rb +5 -3
  6. data/lib/fixture_replacement/class_methods.rb +10 -2
  7. data/lib/fixture_replacement/version.rb +2 -2
  8. data/lib/fixture_replacement.rb +0 -1
  9. metadata +26 -109
  10. data/.gitignore +0 -2
  11. data/Rakefile +0 -8
  12. data/etc/bug_reports/2007_09_28_linoj.txt +0 -51
  13. data/etc/google_analytics +0 -6
  14. data/etc/patches/2007_09_14_default_model_name_with_arguments.diff +0 -26
  15. data/etc/patches/2007_10_14_active_record_specs.diff +0 -396
  16. data/etc/patches/2007_10_14_protected_attributes.diff +0 -26
  17. data/etc/patches/2007_10_14_send_patch.diff +0 -79
  18. data/etc/patches/2007_10_14_spelling_error_in_comments.diff +0 -13
  19. data/etc/patches/2007_10_17_protected_attributes_second_go.diff +0 -212
  20. data/etc/patches/2007_10_18_README.diff +0 -137
  21. data/etc/patches/2007_10_19_readme_tweak.diff +0 -12
  22. data/etc/patches/2007_10_19_silence_migration.diff +0 -12
  23. data/etc/patches/2007_10_25_changed_classify_to_camelize.diff +0 -38
  24. data/etc/patches/2007_11_20_fixture_replacement_generator_should_not_reload_environment_or_boot.patch +0 -13
  25. data/etc/patches/2007_11_20_string_random_refactor_and_extension.patch +0 -104
  26. data/etc/patches/2007_11_20_string_random_refactor_and_extension_v2.patch +0 -108
  27. data/fixture_replacement.gemspec +0 -126
  28. data/rake_tasks/code_quality.rb +0 -37
  29. data/rake_tasks/docs.rb +0 -59
  30. data/rake_tasks/gem.rb +0 -27
  31. data/rake_tasks/specs.rb +0 -7
  32. data/rake_tasks/tests.rb +0 -7
  33. data/rake_tasks/website.rb +0 -11
  34. data/spec/fixture_replacement/attribute_builder_spec.rb +0 -162
  35. data/spec/fixture_replacement/fixture_replacement_spec.rb +0 -96
  36. data/spec/fixture_replacement/fixtures/classes.rb +0 -78
  37. data/spec/fixture_replacement/fr_spec.rb +0 -7
  38. data/spec/fixture_replacement/integration/attr_protected_attributes_spec.rb +0 -71
  39. data/spec/fixture_replacement/integration/attributes_for_with_invalid_keys_spec.rb +0 -14
  40. data/spec/fixture_replacement/integration/attributes_from_spec_without_block.rb +0 -52
  41. data/spec/fixture_replacement/integration/create_method_spec.rb +0 -61
  42. data/spec/fixture_replacement/integration/cyclic_dependency_spec.rb +0 -32
  43. data/spec/fixture_replacement/integration/default_warnings_spec.rb +0 -29
  44. data/spec/fixture_replacement/integration/extend_spec.rb +0 -37
  45. data/spec/fixture_replacement/integration/has_and_belongs_to_many_spec.rb +0 -64
  46. data/spec/fixture_replacement/integration/new_method_spec.rb +0 -96
  47. data/spec/fixture_replacement/integration/private_methods_spec.rb +0 -43
  48. data/spec/fixture_replacement/integration/public_methods_spec.rb +0 -21
  49. data/spec/fixture_replacement/integration/valid_attributes_spec.rb +0 -41
  50. data/spec/fixture_replacement/integration/validation_spec.rb +0 -54
  51. data/spec/fixture_replacement/regressions/2008_01_21_random_string_with_sti_spec.rb +0 -71
  52. data/spec/fixture_replacement/version_spec.rb +0 -7
  53. data/spec/spec.opts +0 -1
  54. data/spec/spec_helper.rb +0 -16
  55. data/spec/spec_helpers.rb +0 -117
  56. data/test/test_helper.rb +0 -21
  57. data/test/unit/user_test.rb +0 -57
@@ -1,212 +0,0 @@
1
- Index: lib/fixture_replacement/fixture_replacement.rb
2
- ===================================================================
3
- --- lib/fixture_replacement/fixture_replacement.rb (revision 52)
4
- +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
- @@ -70,7 +70,12 @@
6
- hash_given = args[0] || Hash.new
7
- merged_hash = self.send(attributes_method).merge(hash_given)
8
- evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
9
- - obj = class_name.create!(evaluated_hash)
10
- +
11
- + # we are NOT doing the following, because of attr_protected:
12
- + # obj = class_name.create!(evaluated_hash)
13
- + obj = class_name.new
14
- + evaluated_hash.each { |key, value| obj.send("#{key}=", value) }
15
- + obj.save!
16
- obj
17
- end
18
- end
19
- @@ -86,7 +91,11 @@
20
- hash_given = args[0] || Hash.new
21
- merged_hash = self.send(attributes_method).merge(hash_given)
22
- evaluated_hash = Generator.merge_unevaluated_method(self, :create, merged_hash)
23
- - class_name.new(evaluated_hash)
24
- +
25
- + # we are also doing the following because of attr_protected:
26
- + obj = class_name.new
27
- + evaluated_hash.each { |key, value| obj.send("#{key}=", value) }
28
- + obj
29
- end
30
- end
31
- end
32
- Index: spec/fixture_replacement/fixture_replacement_spec.rb
33
- ===================================================================
34
- --- spec/fixture_replacement/fixture_replacement_spec.rb (revision 52)
35
- +++ spec/fixture_replacement/fixture_replacement_spec.rb (working copy)
36
- @@ -9,9 +9,88 @@
37
- belongs_to :gender
38
- end
39
-
40
- +class Admin < ActiveRecord::Base
41
- + attr_protected :admin_status
42
- +end
43
- +
44
- class Gender < ActiveRecord::Base; end
45
-
46
- +
47
- +
48
- +
49
- module FixtureReplacement
50
- +
51
- + describe "create_user with attr_protected attributes" do
52
- + before :each do
53
- + FixtureReplacement.module_eval do
54
- + def admin_attributes
55
- + {
56
- + :admin_status => true,
57
- + :name => "Scott"
58
- + }
59
- + end
60
- + end
61
- +
62
- + @generator = Generator.new("admin")
63
- + @generator.generate_create_method
64
- + end
65
- +
66
- + it "should not complain when an apparent mass assignment has happened with default values" do
67
- + lambda {
68
- + create_admin
69
- + }.should_not raise_error
70
- + end
71
- +
72
- + it "should have admin_status equal to the default value (when it has not been overwritten)" do
73
- + create_admin.admin_status.should == true
74
- + end
75
- +
76
- + it "should have admin_status equal to the overwritten value" do
77
- + create_admin(:admin_status => false).admin_status.should be_false
78
- + end
79
- +
80
- + it "should have the other attributes assigned when the attr_value has been overwritten" do
81
- + create_admin(:admin_status => false).name.should == "Scott"
82
- + end
83
- +
84
- + it "should have the other attributes assigned even when the attr_value has not been overwritten" do
85
- + create_admin.name.should == "Scott"
86
- + end
87
- + end
88
- +
89
- + describe "new_user with attr_protected attributes" do
90
- + before :each do
91
- + FixtureReplacement.module_eval do
92
- + def admin_attributes
93
- + {
94
- + :admin_status => true,
95
- + :name => "Scott"
96
- + }
97
- + end
98
- + end
99
- +
100
- + @generator = Generator.new("admin")
101
- + @generator.generate_new_method
102
- + end
103
- +
104
- + it "should have admin_status equal to the default value (when it has not been overwritten)" do
105
- + new_admin.admin_status.should == true
106
- + end
107
- +
108
- + it "should have admin_status equal to the overwritten value" do
109
- + new_admin(:admin_status => false).admin_status.should be_false
110
- + end
111
- +
112
- + it "should have the other attributes assigned when the attr_value has been overwritten" do
113
- + new_admin(:admin_status => false).name.should == "Scott"
114
- + end
115
- +
116
- + it "should have the other attributes assigned even when the attr_value has not been overwritten" do
117
- + new_admin.name.should == "Scott"
118
- + end
119
- + end
120
- +
121
- +
122
- describe Generator, "creation" do
123
- before :each do
124
- @generator = Generator.new("user")
125
- @@ -121,9 +200,14 @@
126
- create_user.should_not be_a_new_record
127
- end
128
-
129
- - it "should save the user with create!" do
130
- + it "should save the user with save!" do
131
- @generator.generate_create_method
132
- - User.should_receive(:create!).with({:key => "val"})
133
- +
134
- + @user = mock('User', :null_object => true)
135
- + @user.stub!(:save!).and_return true
136
- + User.stub!(:new).and_return @user
137
- +
138
- + @user.should_receive(:save!).with(no_args)
139
- create_user
140
- end
141
-
142
- @@ -175,13 +259,20 @@
143
- created_gender.sex.should == "Female"
144
- end
145
-
146
- - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
147
- - Gender.should_receive(:create!).with({:sex => "Male"})
148
- + it "should call save! when the default_gender method is evaluated by default_gender" do
149
- + @gender = mock('Gender', :null_object => true)
150
- + Gender.stub!(:new).and_return @gender
151
- +
152
- + @user = mock('User', :null_object => true)
153
- + User.stub!(:new).and_return @user
154
- + @user.stub!(:gender=).and_return @gender
155
- +
156
- + @gender.should_receive(:save!).with(no_args)
157
- create_user
158
- end
159
-
160
- - it "should not call Gender.create! if the default_gender is overwritten by another value" do
161
- - Gender.should_not_receive(:create!)
162
- + it "should not call Gender.save! if the default_gender is overwritten by another value" do
163
- + Gender.should_not_receive(:save!)
164
- create_user(:gender => Gender.new)
165
- end
166
- end
167
- @@ -324,18 +415,24 @@
168
- new_gender.sex.should == "unknown"
169
- end
170
-
171
- - it "should call Gender.create! when the default_gender method is evaluated by default_gender" do
172
- - Gender.should_receive(:create!).with({:sex => "Male"})
173
- + it "should call Gender.save! when the default_gender method is evaluated by default_gender" do
174
- + @gender = mock('Gender', :null_object => true)
175
- + Gender.stub!(:new).and_return @gender
176
- + @user = mock('User')
177
- + @user.stub!(:gender=).and_return @gender
178
- + User.stub!(:new).and_return @user
179
- +
180
- + @gender.should_receive(:save!)
181
- new_user
182
- end
183
-
184
- - it "should not call Gender.create! if the default_gender is overwritten by another value" do
185
- - Gender.should_not_receive(:create!)
186
- + it "should not call Gender.save! if the default_gender is overwritten by another value" do
187
- + Gender.should_not_receive(:save!)
188
- new_user(:gender => Gender.new)
189
- end
190
-
191
- it "should be able to overwrite a default_* method" do
192
- - Gender.should_not_receive(:create!).with({:sex => "Male"})
193
- + Gender.should_not_receive(:save!)
194
- new_user(:gender => Gender.create!(:sex => "Female"))
195
- end
196
- end
197
- Index: spec/spec_helper.rb
198
- ===================================================================
199
- --- spec/spec_helper.rb (revision 52)
200
- +++ spec/spec_helper.rb (working copy)
201
- @@ -21,6 +21,11 @@
202
- t.column :gender_id, :string
203
- end
204
-
205
- + create_table :admins do |t|
206
- + t.column :admin_status, :boolean
207
- + t.column :name, :string
208
- + end
209
- +
210
- end
211
-
212
-
@@ -1,137 +0,0 @@
1
- Index: README
2
- ===================================================================
3
- --- README (revision 60)
4
- +++ README (working copy)
5
- @@ -1,32 +1,92 @@
6
- = FixtureReplacement
7
-
8
- -=== How to use FixtureReplacement
9
- +== What is FixtureReplacement
10
-
11
- -Full Documentation is coming, when time permits. For now, watch this screencast (and forward
12
- -through my stupidity):
13
- +FixtureReplacement is a Rails[http://rubyonrails.org/] plugin that provides a simple way to quickly populate your test database with model objects without having to manage multiple, brittle fixture files. You can easily set up complex object graphs (with models which reference other models) and add new objects on the fly.
14
-
15
- -http://railsnewbie.com/files/fixture_replacement_demo.mov
16
- +Not only can FixtureReplacement make your test data easier to maintain, it can also help to make your tests and specs much more readable and intention-revealing by allowing you to omit extraneous details and focus only on the attributes that are important for a particular behaviour. It works well with both RSpec[http://rspec.rubyforge.org/] and Test::Unit[http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit.html].
17
-
18
- -There is also some documentation at the following link, although the screencast is still advised:
19
- +== How to use FixtureReplacement
20
-
21
- -http://wincent.com/knowledge-base/FixtureReplacement_cheatsheet
22
- +=== Defining default attributes
23
-
24
- +At the heart of FixtureReplacement is the <tt>db/example_data.rb</tt> file where you define the default attributes for each of your test models. This example shows a <tt>user_attributes</tt> method that returns the attributes for an instance of the <tt>User</tt> model:
25
-
26
- -=== Installation
27
- + module FixtureReplacement
28
- + def user_attributes
29
- + password = String.random
30
- + {
31
- + :value => "a value",
32
- + :other => "other value",
33
- + :another => String.random, # random string 10 characters long
34
- + :one_more => String.random(15), # 15 characters long
35
- + :password => password,
36
- + :password_confirmation => password,
37
- + :associated_object => default_bar # expects bar_attributes to be defined
38
- + }
39
- + end
40
- + end
41
-
42
- +Note that:
43
- +
44
- +- the method returns a hash of attributes
45
- +- a String.random method is provided for attributes whose exact value isn't important; this means you can create multiple, unique model instances
46
- +- you can perform arbitrary set-up and execute any Ruby code prior to returning the hash (as shown here where a <tt>password</tt> is generated and then used for both the <tt>:password</tt> and <tt>:password_confirmation</tt> attributes)
47
- +- a <tt>default_modelname</tt> method is automatically provided that allows you to set up dependent model objects (in this case an instance of the <tt>Bar</tt> model)
48
- +
49
- +=== Available methods
50
- +
51
- +Based on the above definition FixtureReplacement makes the following methods available:
52
- +
53
- +- String.random: generates a random string as shown above
54
- +- <tt>new_user</tt>: equivalent to <tt>User.new(user_attributes)</tt>.
55
- +- <tt>create_user</tt>: equivalent to <tt>User.create!(user_attributes)</tt>.
56
- +- <tt>default_user</tt>: for use inside <tt>model_attributes</tt> definitions; this basically returns a <tt>Proc</tt> object which allows the actual creation of the object to be deferred until it is actually needed: in this way unnecessary object creation is avoided until it is known for sure that a particular attribute is not going to be overridden.
57
- +
58
- +=== Overriding attributes
59
- +
60
- +Overrides of specific attributes can be performed as follows:
61
- +
62
- + new_user(:thing => "overridden")
63
- + create_user(:thing => "overridden")
64
- +
65
- +=== Screencast
66
- +
67
- +Further documentation is forthcoming, but for now the following screencast provides a powerful demonstration of how FixtureReplacement can help you to set up complex, flexible object graphs with ease:
68
- +
69
- +http://railsnewbie.com/files/fixture_replacement_demo.mov
70
- +
71
- +== Installation
72
- +
73
- ruby script/plugin install http://thmadb.com/public_svn/plugins/fixture_replacement/
74
-
75
- Or use externals:
76
-
77
- ruby script/plugin install -x http://thmadb.com/public_svn/plugins/fixture_replacement/
78
-
79
- -Run the generator if you don't have the file db/example_data.rb
80
- +Run the generator if you don't have the file <tt>db/example_data.rb</tt>:
81
-
82
- ruby script/generate fixture_replacement
83
-
84
- +=== Using from within <tt>script/console</tt>
85
-
86
- -=== Running the Specs/Tests for FixtureReplacement
87
- + % script/console
88
- + >> include FixtureReplacement
89
-
90
- +=== Using from with RSpec
91
- +
92
- +Add the following to your <tt>spec/spec_helper.rb</tt> file:
93
- +
94
- + include FixtureReplacement
95
- +
96
- +=== Using with Test::Unit
97
- +
98
- +Add the following to your <tt>test/test_helper.rb</tt> file:
99
- +
100
- + include FixtureReplacement
101
- +
102
- +== Running the Specs/Tests for FixtureReplacement
103
- +
104
- You will need rspec (version 1.0.8 or later) to run the specs, as well as the sqlite3-ruby gem
105
- (and sqlite3 installed):
106
-
107
- @@ -37,16 +97,16 @@
108
-
109
- % cd vendor/plugins/fixture_replacement
110
-
111
- -Then run with rake
112
- +Then run with <tt>rake<tt>
113
-
114
- % rake
115
-
116
- -=== Specdocs
117
- +== Specdocs
118
-
119
- Specdocs can be found here[http://replacefixtures.rubyforge.org/specdoc.html]
120
-
121
-
122
- -=== Patches, Contributions:
123
- +== Patches, Contributions:
124
-
125
- Thanks to the following:
126
-
127
- @@ -62,8 +122,7 @@
128
-
129
- If you would like to change how this software works, please submit a patch with specs to scott@railsnewbie.com
130
-
131
- +== License
132
-
133
- -=== License
134
- -
135
- This software is released under the MIT License. See the license agreement
136
- -in lib/fixture_replacement.rb
137
- +in <tt>lib/fixture_replacement.rb</tt>
@@ -1,12 +0,0 @@
1
- Index: README
2
- ===================================================================
3
- --- README (revision 65)
4
- +++ README (working copy)
5
- @@ -144,7 +144,6 @@
6
- - default_* methods can take a hash (applied in rev. 11)
7
- - Wincent Colaiuta (Huge Thanks)
8
- - patch for spelling error in comments (applied in revision 31)
9
- - - extra documentation (http://wincent.com/knowledge-base/FixtureReplacement)
10
- - patch for specs with sqlite3 (applied in revision 35)
11
- - patch to ignore attr_protected in mass assignment (applied in revision 57)
12
- - README Documentation (applied in revision 62)
@@ -1,12 +0,0 @@
1
- Index: spec/spec_helper.rb
2
- ===================================================================
3
- --- spec/spec_helper.rb (revision 65)
4
- +++ spec/spec_helper.rb (working copy)
5
- @@ -5,6 +5,7 @@
6
-
7
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
8
-
9
- +ActiveRecord::Migration.verbose = false
10
- ActiveRecord::Schema.define do
11
-
12
- create_table :users do |t|
@@ -1,38 +0,0 @@
1
- Index: lib/fixture_replacement/fixture_replacement.rb
2
- ===================================================================
3
- --- lib/fixture_replacement/fixture_replacement.rb (revision 70)
4
- +++ lib/fixture_replacement/fixture_replacement.rb (working copy)
5
- @@ -40,7 +40,7 @@
6
-
7
- def initialize(method_name, fixture_mod=FixtureReplacement)
8
- @model_name = method_name
9
- - @model_class = method_name.classify
10
- + @model_class = method_name.camelize
11
- @fixture_module = fixture_mod
12
-
13
- add_to_class_singleton(@model_class)
14
- Index: spec/fixture_replacement/fixture_replacement_spec.rb
15
- ===================================================================
16
- --- spec/fixture_replacement/fixture_replacement_spec.rb (revision 70)
17
- +++ spec/fixture_replacement/fixture_replacement_spec.rb (working copy)
18
- @@ -14,6 +14,7 @@
19
- end
20
-
21
- class Gender < ActiveRecord::Base; end
22
- +class Actress < ActiveRecord::Base; end
23
-
24
-
25
-
26
- @@ -118,6 +119,12 @@
27
- }.should raise_error
28
- end
29
-
30
- + it "should not raise an error if the model ends with 's'" do
31
- + lambda {
32
- + Generator.new("actress")
33
- + }.should_not raise_error
34
- + end
35
- +
36
- it "should be able to respond to generate_default_method" do
37
- @generator.should respond_to(:generate_default_method)
38
- end
@@ -1,13 +0,0 @@
1
- Index: vendor/plugins/fixture_replacement/generators/fixture_replacement/fixture_replacement_generator.rb
2
- ===================================================================
3
- --- vendor/plugins/fixture_replacement/generators/fixture_replacement/fixture_replacement_generator.rb (revision 83)
4
- +++ vendor/plugins/fixture_replacement/generators/fixture_replacement/fixture_replacement_generator.rb (working copy)
5
- @@ -1,6 +1,6 @@
6
- require 'rbconfig'
7
- -require 'config/boot'
8
- -require 'config/environment'
9
- +require 'config/environment' unless RAILS_GEM_VERSION
10
- +require 'config/boot' unless RAILS_ROOT
11
-
12
- class FixtureReplacementGenerator < Rails::Generator::Base
13
- DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
@@ -1,104 +0,0 @@
1
- Index: lib/fixture_replacement/string.rb
2
- ===================================================================
3
- --- lib/fixture_replacement/string.rb (revision 83)
4
- +++ lib/fixture_replacement/string.rb (working copy)
5
- @@ -1,10 +1,21 @@
6
- +class String
7
- + def random_chr
8
- + self[rand(size)].chr
9
- + end
10
- +
11
- + def random_str(length=10)
12
- + (1..length).collect {random_chr}.join
13
- + end
14
- +
15
- + def formatted_random_str(format, length=10)
16
- + format.split(/(%s)/).map {|str| str.sub('%s', random_str(length))}.join
17
- + end
18
-
19
- -class String
20
- def self.random(length=10)
21
- - chars = ("a".."z").to_a
22
- - string = ""
23
- - 1.upto(length) { |i| string << chars[rand(chars.size-1)]}
24
- - return string
25
- + ("a".."z").sort.join.random_str(length)
26
- end
27
- +
28
- + def self.formatted_random(format, length=10)
29
- + ("a".."z").sort.join.formatted_random_str(format, length)
30
- + end
31
- end
32
- -
33
- Index: spec/fixture_replacement/extentions_spec.rb
34
- ===================================================================
35
- --- spec/fixture_replacement/extentions_spec.rb (revision 83)
36
- +++ spec/fixture_replacement/extentions_spec.rb (working copy)
37
- @@ -2,7 +2,9 @@
38
-
39
- describe "String.random" do
40
- it "should not be the same as another randomly generated string" do
41
- - String.random.should_not == String.random
42
- + (1..100).each do |ctr|
43
- + String.random.should_not == String.random
44
- + end
45
- end
46
-
47
- it "should by default be 10 characters long" do
48
- @@ -12,9 +14,51 @@
49
- it "should be able to specify the length of the random string" do
50
- String.random(100).size.should == 100
51
- end
52
- -
53
- +
54
- it "should only generate lowercase letters" do
55
- - s = String.random(100)
56
- - s.upcase.should == s.swapcase
57
- + String.random(100).should =~ /^[a-z]{100}/
58
- end
59
- -end
60
-
61
- +end
62
- +
63
- +describe "String.formatted_random" do
64
- + it "should require a format argument to be given" do
65
- + lambda { String.formatted_random }.should raise_error(ArgumentError)
66
- + end
67
- +
68
- + it "should be able to generate simple formatted random strings" do
69
- + String.formatted_random('%s@%s.%s').should =~ /^[a-z]{10}@[a-z]{10}\.[a-z]{10}$/
70
- + end
71
- +end
72
- +
73
- +describe "String#random_chr" do
74
- + before(:each) do
75
- + @string = "abcde"
76
- + end
77
- +
78
- + it "should always provide a random character from the string" do
79
- + (1..100).each do |ctr|
80
- + @string.include?(@string.random_chr).should be_true
81
- + end
82
- + end
83
- +
84
- + it "should rarely generate the same character twice in a row given a very large, random distribution" do
85
- + string = String.random(100)
86
- + ctr = 0
87
- + (1..100).each do |i|
88
- + string.random_chr == string.random_chr && ctr += 1
89
- + end
90
- + ctr.should < 10
91
- + end
92
- +end
93
- +
94
- +describe "String#random_str" do
95
- + it "should be able to generate random strings from a given string of characters" do
96
- + "Jim Beth Sam Cassie".random_str(100).should =~ /^[ BCJSaehimst]{100}$/
97
- + end
98
- +end
99
- +
100
- +describe "String#formatted_random_str" do
101
- + it "should be able to generate random strings from a given string of characters" do
102
- + "Jim Beth Sam Cassie".formatted_random_str('%s@%s.%s').should =~ /^[ BCJSaehimst]{10}@[ BCJSaehimst]{10}\.[ BCJSaehimst]{10}$/
103
- + end
104
- +end
@@ -1,108 +0,0 @@
1
- Index: lib/fixture_replacement/string.rb
2
- ===================================================================
3
- --- lib/fixture_replacement/string.rb (revision 83)
4
- +++ lib/fixture_replacement/string.rb (working copy)
5
- @@ -1,10 +1,25 @@
6
- +class String
7
- + def random_chr
8
- + self[rand(size)].chr
9
- + end
10
- +
11
- + def random_str(length=10)
12
- + (1..length).collect {random_chr}.join
13
- + end
14
- +
15
- + def formatted_random_str(format, length=10)
16
- + result = format.dup
17
- + while result.match(/(%(\d*)s)/) do
18
- + result.sub!($1, random_str($2.empty? ? length : $2.to_i))
19
- + end
20
- + result
21
- + end
22
-
23
- -class String
24
- def self.random(length=10)
25
- - chars = ("a".."z").to_a
26
- - string = ""
27
- - 1.upto(length) { |i| string << chars[rand(chars.size-1)]}
28
- - return string
29
- + ("a".."z").sort.join.random_str(length)
30
- end
31
- +
32
- + def self.formatted_random(format, length=10)
33
- + ("a".."z").sort.join.formatted_random_str(format, length)
34
- + end
35
- end
36
- -
37
- Index: spec/fixture_replacement/extentions_spec.rb
38
- ===================================================================
39
- --- spec/fixture_replacement/extentions_spec.rb (revision 83)
40
- +++ spec/fixture_replacement/extentions_spec.rb (working copy)
41
- @@ -2,7 +2,9 @@
42
-
43
- describe "String.random" do
44
- it "should not be the same as another randomly generated string" do
45
- - String.random.should_not == String.random
46
- + (1..100).each do |ctr|
47
- + String.random.should_not == String.random
48
- + end
49
- end
50
-
51
- it "should by default be 10 characters long" do
52
- @@ -12,9 +14,51 @@
53
- it "should be able to specify the length of the random string" do
54
- String.random(100).size.should == 100
55
- end
56
- -
57
- +
58
- it "should only generate lowercase letters" do
59
- - s = String.random(100)
60
- - s.upcase.should == s.swapcase
61
- + String.random(100).should =~ /^[a-z]{100}/
62
- end
63
- -end
64
-
65
- +end
66
- +
67
- +describe "String.formatted_random" do
68
- + it "should require a format argument to be given" do
69
- + lambda { String.formatted_random }.should raise_error(ArgumentError)
70
- + end
71
- +
72
- + it "should be able to generate simple formatted random strings" do
73
- + String.formatted_random('%s@%s.%s').should =~ /^[a-z]{10}@[a-z]{10}\.[a-z]{10}$/
74
- + end
75
- +end
76
- +
77
- +describe "String#random_chr" do
78
- + before(:each) do
79
- + @string = "abcde"
80
- + end
81
- +
82
- + it "should always provide a random character from the string" do
83
- + (1..100).each do |ctr|
84
- + @string.include?(@string.random_chr).should be_true
85
- + end
86
- + end
87
- +
88
- + it "should rarely generate the same character twice in a row given a very large, random distribution" do
89
- + string = String.random(100)
90
- + ctr = 0
91
- + (1..100).each do |i|
92
- + string.random_chr == string.random_chr && ctr += 1
93
- + end
94
- + ctr.should < 10
95
- + end
96
- +end
97
- +
98
- +describe "String#random_str" do
99
- + it "should be able to generate random strings from a given string of characters" do
100
- + "Jim Beth Sam Cassie".random_str(100).should =~ /^[ BCJSaehimst]{100}$/
101
- + end
102
- +end
103
- +
104
- +describe "String#formatted_random_str" do
105
- + it "should be able to generate random strings from a given string of characters" do
106
- + "Jim Beth Sam Cassie".formatted_random_str('%s@%s.%s').should =~ /^[ BCJSaehimst]{10}@[ BCJSaehimst]{10}\.[ BCJSaehimst]{10}$/
107
- + end
108
- +end