From 2dd2fcf89673afbcf95240ecebaf34826a195164 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Tue, 18 Feb 2014 16:13:23 -0500 Subject: Introduce `Rails.gem_version` This method return `Gem::Version.new(Rails.version)`, suggesting a more reliable way to perform version comparison. Example: Rails.version #=> "4.1.2" Rails.gem_version #=> # Rails.version > "4.1.10" #=> false Rails.gem_version > Gem::Version.new("4.1.10") #=> true Gem::Requirement.new("~> 4.1.2") =~ Rails.gem_version #=> true This was originally introduced as `.version` by @charliesome in #8501 but got reverted in #10002 since it was not backward compatible. Also, updating template for `rake update_versions`. --- activemodel/lib/active_model/gem_version.rb | 15 +++++++++++++++ activemodel/lib/active_model/version.rb | 11 ++++------- 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 activemodel/lib/active_model/gem_version.rb (limited to 'activemodel') diff --git a/activemodel/lib/active_model/gem_version.rb b/activemodel/lib/active_model/gem_version.rb new file mode 100644 index 0000000000..964b24398d --- /dev/null +++ b/activemodel/lib/active_model/gem_version.rb @@ -0,0 +1,15 @@ +module ActiveModel + # Returns the version of the currently loaded ActiveModel as a Gem::Version + def self.gem_version + Gem::Version.new VERSION::STRING + end + + module VERSION + MAJOR = 4 + MINOR = 2 + TINY = 0 + PRE = "alpha" + + STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") + end +end diff --git a/activemodel/lib/active_model/version.rb b/activemodel/lib/active_model/version.rb index 5c9d2bc6bc..b1f9082ea7 100644 --- a/activemodel/lib/active_model/version.rb +++ b/activemodel/lib/active_model/version.rb @@ -1,11 +1,8 @@ +require_relative 'gem_version' + module ActiveModel - # Returns the version of the currently loaded ActiveModel as a Gem::Version + # Returns the version of the currently loaded ActiveModel as a Gem::Version def self.version - Gem::Version.new "4.2.0.alpha" - end - - module VERSION #:nodoc: - MAJOR, MINOR, TINY, PRE = ActiveModel.version.segments - STRING = ActiveModel.version.to_s + gem_version end end -- cgit v1.2.3 From 62e8145430e0a327fa14975b84b9079cedf60863 Mon Sep 17 00:00:00 2001 From: Pavel Penkov Date: Mon, 19 Aug 2013 11:52:31 +0400 Subject: Tests for indifferent access using full_messages_for. Closes #11916 --- activemodel/test/cases/errors_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activemodel') diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index def28578f8..42d0365521 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -82,6 +82,13 @@ class ErrorsTest < ActiveModel::TestCase assert_equal({ foo: "omg" }, errors.messages) end + test "error access is indifferent" do + errors = ActiveModel::Errors.new(self) + errors[:foo] = "omg" + + assert_equal ["omg"], errors["foo"] + end + test "values returns an array of messages" do errors = ActiveModel::Errors.new(self) errors.set(:foo, "omg") -- cgit v1.2.3 From f6ab778297350aa3dad1dbf23ccd2d8a74e3dab7 Mon Sep 17 00:00:00 2001 From: Peter Markou Date: Fri, 7 Mar 2014 16:08:08 +0200 Subject: Fix errors for four of the code samples The four code samples that fail to run are: - Add attribute magic to objects. Fixed by introducing a Person instance variable. - Tracking value changes. Fixed by replacing `attr_accessor` with `define_attribute_methods`, providing getter and setter methods for `name` and providing the missing `Person#save` method. A call to `Person#save` has to precede the `person.name = 'robert'` assignment, if we want `previous_changes` to include 'bob'. - Adding `errors` interface to objects. Fixed by introducing a Person instance variable, assigning `nil` to its name and calling `Person#validate!`. - Custom validators. Fixed by defining `HasNameValidator` before it is used by `ValidatorPerson`. All the code samples can now be run smoothly. Call Dirty#changes_applied in Person#save, instead of modifying instance vars. --- activemodel/README.rdoc | 79 ++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 30 deletions(-) (limited to 'activemodel') diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index 4c00755532..500be2a04a 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -49,7 +49,8 @@ behavior out of the box: send("#{attr}=", nil) end end - + + person = Person.new person.clear_name person.clear_age @@ -78,7 +79,21 @@ behavior out of the box: class Person include ActiveModel::Dirty - attr_accessor :name + define_attribute_methods :name + + def name + @name + end + + def name=(val) + name_will_change! unless val == @name + @name = val + end + + def save + # do persistence work + changes_applied + end end person = Person.new @@ -88,6 +103,7 @@ behavior out of the box: person.changed? # => true person.changed # => ['name'] person.changes # => { 'name' => [nil, 'bob'] } + person.save person.name = 'robert' person.save person.previous_changes # => {'name' => ['bob, 'robert']} @@ -116,7 +132,10 @@ behavior out of the box: "Name" end end - + + person = Person.new + person.name = nil + person.validate! person.errors.full_messages # => ["Name cannot be nil"] @@ -180,41 +199,41 @@ behavior out of the box: * Validation support - class Person - include ActiveModel::Validations + class Person + include ActiveModel::Validations - attr_accessor :first_name, :last_name + attr_accessor :first_name, :last_name - validates_each :first_name, :last_name do |record, attr, value| - record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z - end - end + validates_each :first_name, :last_name do |record, attr, value| + record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z + end + end - person = Person.new - person.first_name = 'zoolander' - person.valid? # => false + person = Person.new + person.first_name = 'zoolander' + person.valid? # => false {Learn more}[link:classes/ActiveModel/Validations.html] * Custom validators + + class HasNameValidator < ActiveModel::Validator + def validate(record) + record.errors[:name] = "must exist" if record.name.blank? + end + end + + class ValidatorPerson + include ActiveModel::Validations + validates_with HasNameValidator + attr_accessor :name + end - class ValidatorPerson - include ActiveModel::Validations - validates_with HasNameValidator - attr_accessor :name - end - - class HasNameValidator < ActiveModel::Validator - def validate(record) - record.errors[:name] = "must exist" if record.name.blank? - end - end - - p = ValidatorPerson.new - p.valid? # => false - p.errors.full_messages # => ["Name must exist"] - p.name = "Bob" - p.valid? # => true + p = ValidatorPerson.new + p.valid? # => false + p.errors.full_messages # => ["Name must exist"] + p.name = "Bob" + p.valid? # => true {Learn more}[link:classes/ActiveModel/Validator.html] -- cgit v1.2.3 From 9ffeb36265928e0fb6de7ffc4b4f3cb3e7fa3581 Mon Sep 17 00:00:00 2001 From: Zuhao Wan Date: Sat, 8 Mar 2014 01:59:57 +0800 Subject: Run ActiveModel test suites in random order. This gets the whole ActiveModel test suites working even if `self.i_suck_and_my_tests_are_order_dependent!` is disabled in `ActiveSupport::TestCase`. Two places are found that potentially leak global state. This patch makes sure states are restored so that none of the changes happen in a single test will be carried over to subsequence tests. --- activemodel/test/cases/secure_password_test.rb | 32 +++++++++++++++++----- activemodel/test/cases/translation_test.rb | 4 +++ .../validations/confirmation_validation_test.rb | 15 ++++++---- .../test/cases/validations/i18n_validation_test.rb | 1 + 4 files changed, 39 insertions(+), 13 deletions(-) (limited to 'activemodel') diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 82fd291064..ffc534f0f4 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -147,7 +147,7 @@ class SecurePasswordTest < ActiveModel::TestCase test "setting a nil password should clear an existing password" do @existing_user.password = nil assert_equal nil, @existing_user.password_digest - end + end test "authenticate" do @user.password = "secret" @@ -157,24 +157,42 @@ class SecurePasswordTest < ActiveModel::TestCase end test "Password digest cost defaults to bcrypt default cost when min_cost is false" do + original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = false - @user.password = "secret" - assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost + begin + @user.password = "secret" + assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost + ensure + ActiveModel::SecurePassword.min_cost = original_min_cost + end end test "Password digest cost honors bcrypt cost attribute when min_cost is false" do + original_min_cost = ActiveModel::SecurePassword.min_cost + original_cost = BCrypt::Engine.cost + ActiveModel::SecurePassword.min_cost = false BCrypt::Engine.cost = 5 - @user.password = "secret" - assert_equal BCrypt::Engine.cost, @user.password_digest.cost + begin + @user.password = "secret" + assert_equal BCrypt::Engine.cost, @user.password_digest.cost + ensure + ActiveModel::SecurePassword.min_cost = original_min_cost + BCrypt::Engine.cost = original_cost + end end test "Password digest cost can be set to bcrypt min cost to speed up tests" do + original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = true - @user.password = "secret" - assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost + begin + @user.password = "secret" + assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost + ensure + ActiveModel::SecurePassword.min_cost = original_min_cost + end end end diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index deb4e1ed0a..cedc812ec7 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -7,6 +7,10 @@ class ActiveModelI18nTests < ActiveModel::TestCase I18n.backend = I18n::Backend::Simple.new end + def teardown + I18n.backend.reload! + end + def test_translated_model_attributes I18n.backend.store_translations 'en', activemodel: { attributes: { person: { name: 'person name attribute' } } } assert_equal 'person name attribute', Person.human_attribute_name('name') diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index 4957ba5d0a..4c3d7a7615 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -63,12 +63,15 @@ class ConfirmationValidationTest < ActiveModel::TestCase Topic.validates_confirmation_of(:title) - t = Topic.new("title" => "We should be confirmed","title_confirmation" => "") - assert t.invalid? - assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation] - - I18n.load_path.replace @old_load_path - I18n.backend = @old_backend + begin + t = Topic.new("title" => "We should be confirmed","title_confirmation" => "") + assert t.invalid? + assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation] + ensure + I18n.load_path.replace @old_load_path + I18n.backend = @old_backend + I18n.backend.reload! + end end test "does not override confirmation reader if present" do diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index d10010537e..96084a32ba 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -19,6 +19,7 @@ class I18nValidationTest < ActiveModel::TestCase Person.clear_validators! I18n.load_path.replace @old_load_path I18n.backend = @old_backend + I18n.backend.reload! end def test_full_message_encoding -- cgit v1.2.3 From cc6bc1cc46538d14ef65c14a5d0a2484d7b59977 Mon Sep 17 00:00:00 2001 From: Zuhao Wan Date: Mon, 10 Mar 2014 21:22:22 +0800 Subject: Completely remove potential global state leaks in ActiveModel tests. ActiveModel tests can now be run in random order. --- activemodel/test/cases/attribute_methods_test.rb | 76 +++++++++++++++------- activemodel/test/cases/secure_password_test.rb | 34 ++++------ .../cases/serializers/json_serialization_test.rb | 48 ++++++++------ .../cases/serializers/xml_serialization_test.rb | 66 +++++++++---------- .../validations/confirmation_validation_test.rb | 18 ++--- .../validations/numericality_validation_test.rb | 2 + activemodel/test/cases/validations_test.rb | 2 + 7 files changed, 136 insertions(+), 110 deletions(-) (limited to 'activemodel') diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index e9cb5ccc96..e81b7ac424 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -104,10 +104,14 @@ class AttributeMethodsTest < ActiveModel::TestCase end test '#define_attribute_method generates attribute method' do - ModelWithAttributes.define_attribute_method(:foo) + begin + ModelWithAttributes.define_attribute_method(:foo) - assert_respond_to ModelWithAttributes.new, :foo - assert_equal "value of foo", ModelWithAttributes.new.foo + assert_respond_to ModelWithAttributes.new, :foo + assert_equal "value of foo", ModelWithAttributes.new.foo + ensure + ModelWithAttributes.undefine_attribute_methods + end end test '#define_attribute_method does not generate attribute method if already defined in attribute module' do @@ -134,24 +138,36 @@ class AttributeMethodsTest < ActiveModel::TestCase end test '#define_attribute_method generates attribute method with invalid identifier characters' do - ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b') + begin + ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b') - assert_respond_to ModelWithWeirdNamesAttributes.new, :'a?b' - assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b') + assert_respond_to ModelWithWeirdNamesAttributes.new, :'a?b' + assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b') + ensure + ModelWithWeirdNamesAttributes.undefine_attribute_methods + end end test '#define_attribute_methods works passing multiple arguments' do - ModelWithAttributes.define_attribute_methods(:foo, :baz) + begin + ModelWithAttributes.define_attribute_methods(:foo, :baz) - assert_equal "value of foo", ModelWithAttributes.new.foo - assert_equal "value of baz", ModelWithAttributes.new.baz + assert_equal "value of foo", ModelWithAttributes.new.foo + assert_equal "value of baz", ModelWithAttributes.new.baz + ensure + ModelWithAttributes.undefine_attribute_methods + end end test '#define_attribute_methods generates attribute methods' do - ModelWithAttributes.define_attribute_methods(:foo) + begin + ModelWithAttributes.define_attribute_methods(:foo) - assert_respond_to ModelWithAttributes.new, :foo - assert_equal "value of foo", ModelWithAttributes.new.foo + assert_respond_to ModelWithAttributes.new, :foo + assert_equal "value of foo", ModelWithAttributes.new.foo + ensure + ModelWithAttributes.undefine_attribute_methods + end end test '#alias_attribute generates attribute_aliases lookup hash' do @@ -164,26 +180,38 @@ class AttributeMethodsTest < ActiveModel::TestCase end test '#define_attribute_methods generates attribute methods with spaces in their names' do - ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') + begin + ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') - assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar' - assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar') + assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar' + assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar') + ensure + ModelWithAttributesWithSpaces.undefine_attribute_methods + end end test '#alias_attribute works with attributes with spaces in their names' do - ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') - ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar') + begin + ModelWithAttributesWithSpaces.define_attribute_methods(:'foo bar') + ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar') - assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.foo_bar + assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.foo_bar + ensure + ModelWithAttributesWithSpaces.undefine_attribute_methods + end end test '#alias_attribute works with attributes named as a ruby keyword' do - ModelWithRubyKeywordNamedAttributes.define_attribute_methods([:begin, :end]) - ModelWithRubyKeywordNamedAttributes.alias_attribute(:from, :begin) - ModelWithRubyKeywordNamedAttributes.alias_attribute(:to, :end) - - assert_equal "value of begin", ModelWithRubyKeywordNamedAttributes.new.from - assert_equal "value of end", ModelWithRubyKeywordNamedAttributes.new.to + begin + ModelWithRubyKeywordNamedAttributes.define_attribute_methods([:begin, :end]) + ModelWithRubyKeywordNamedAttributes.alias_attribute(:from, :begin) + ModelWithRubyKeywordNamedAttributes.alias_attribute(:to, :end) + + assert_equal "value of begin", ModelWithRubyKeywordNamedAttributes.new.from + assert_equal "value of end", ModelWithRubyKeywordNamedAttributes.new.to + ensure + ModelWithRubyKeywordNamedAttributes.undefine_attribute_methods + end end test '#undefine_attribute_methods removes attribute methods' do diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index ffc534f0f4..d603dee73e 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -4,6 +4,7 @@ require 'models/visitor' class SecurePasswordTest < ActiveModel::TestCase setup do + @original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = true @user = User.new @@ -15,7 +16,7 @@ class SecurePasswordTest < ActiveModel::TestCase end teardown do - ActiveModel::SecurePassword.min_cost = false + ActiveModel::SecurePassword.min_cost = @original_min_cost end test "create and updating without validations" do @@ -157,42 +158,29 @@ class SecurePasswordTest < ActiveModel::TestCase end test "Password digest cost defaults to bcrypt default cost when min_cost is false" do - original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = false - begin - @user.password = "secret" - assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost - ensure - ActiveModel::SecurePassword.min_cost = original_min_cost - end + @user.password = "secret" + assert_equal BCrypt::Engine::DEFAULT_COST, @user.password_digest.cost end test "Password digest cost honors bcrypt cost attribute when min_cost is false" do - original_min_cost = ActiveModel::SecurePassword.min_cost - original_cost = BCrypt::Engine.cost - - ActiveModel::SecurePassword.min_cost = false - BCrypt::Engine.cost = 5 - begin + original_bcrypt_cost = BCrypt::Engine.cost + ActiveModel::SecurePassword.min_cost = false + BCrypt::Engine.cost = 5 + @user.password = "secret" assert_equal BCrypt::Engine.cost, @user.password_digest.cost ensure - ActiveModel::SecurePassword.min_cost = original_min_cost - BCrypt::Engine.cost = original_cost + BCrypt::Engine.cost = original_bcrypt_cost end end test "Password digest cost can be set to bcrypt min cost to speed up tests" do - original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = true - begin - @user.password = "secret" - assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost - ensure - ActiveModel::SecurePassword.min_cost = original_min_cost - end + @user.password = "secret" + assert_equal BCrypt::Engine::MIN_COST, @user.password_digest.cost end end diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index bc185c737f..60414a6570 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -30,11 +30,6 @@ class JsonSerializationTest < ActiveModel::TestCase @contact.preferences = { 'shows' => 'anime' } end - def teardown - # set to the default value - Contact.include_root_in_json = false - end - test "should not include root in json (class method)" do json = @contact.to_json @@ -47,19 +42,25 @@ class JsonSerializationTest < ActiveModel::TestCase end test "should include root in json if include_root_in_json is true" do - Contact.include_root_in_json = true - json = @contact.to_json - - assert_match %r{^\{"contact":\{}, json - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json + begin + original_include_root_in_json = Contact.include_root_in_json + Contact.include_root_in_json = true + json = @contact.to_json + + assert_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json + ensure + Contact.include_root_in_json = original_include_root_in_json + end end test "should include root in json (option) even if the default is set to false" do json = @contact.to_json(root: true) + assert_match %r{^\{"contact":\{}, json end @@ -145,13 +146,18 @@ class JsonSerializationTest < ActiveModel::TestCase end test "as_json should return a hash if include_root_in_json is true" do - Contact.include_root_in_json = true - json = @contact.as_json - - assert_kind_of Hash, json - assert_kind_of Hash, json['contact'] - %w(name age created_at awesome preferences).each do |field| - assert_equal @contact.send(field), json['contact'][field] + begin + original_include_root_in_json = Contact.include_root_in_json + Contact.include_root_in_json = true + json = @contact.as_json + + assert_kind_of Hash, json + assert_kind_of Hash, json['contact'] + %w(name age created_at awesome preferences).each do |field| + assert_equal @contact.send(field), json['contact'][field] + end + ensure + Contact.include_root_in_json = original_include_root_in_json end end diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb index 11ee17bb27..73f316d5e0 100644 --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -57,48 +57,48 @@ class XmlSerializationTest < ActiveModel::TestCase end test "should serialize default root" do - @xml = @contact.to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml + xml = @contact.to_xml + assert_match %r{^}, xml + assert_match %r{$}, xml end test "should serialize namespaced root" do - @xml = Admin::Contact.new(@contact.attributes).to_xml - assert_match %r{^}, @xml - assert_match %r{$}, @xml + xml = Admin::Contact.new(@contact.attributes).to_xml + assert_match %r{^}, xml + assert_match %r{$}, xml end test "should serialize default root with namespace" do - @xml = @contact.to_xml namespace: "http://xml.rubyonrails.org/contact" - assert_match %r{^}, @xml - assert_match %r{$}, @xml + xml = @contact.to_xml namespace: "http://xml.rubyonrails.org/contact" + assert_match %r{^}, xml + assert_match %r{$}, xml end test "should serialize custom root" do - @xml = @contact.to_xml root: 'xml_contact' - assert_match %r{^}, @xml - assert_match %r{$}, @xml + xml = @contact.to_xml root: 'xml_contact' + assert_match %r{^}, xml + assert_match %r{$}, xml end test "should allow undasherized tags" do - @xml = @contact.to_xml root: 'xml_contact', dasherize: false - assert_match %r{^}, @xml - assert_match %r{$}, @xml - assert_match %r{}, xml + assert_match %r{$}, xml + assert_match %r{}, @xml - assert_match %r{$}, @xml - assert_match %r{}, xml + assert_match %r{$}, xml + assert_match %r{}, @xml - assert_match %r{$}, @xml - assert_match %r{}, xml + assert_match %r{$}, xml + assert_match %r{aaron stack}, @xml - assert_match %r{25}, @xml - assert_no_match %r{}, @xml + xml = @contact.to_xml + assert_match %r{aaron stack}, xml + assert_match %r{25}, xml + assert_no_match %r{}, xml end test "should allow skipped types" do - @xml = @contact.to_xml skip_types: true - assert_match %r{25}, @xml + xml = @contact.to_xml skip_types: true + assert_match %r{25}, xml end test "should include yielded additions" do - @xml = @contact.to_xml do |xml| + xml = @contact.to_xml do |xml| xml.creator "David" end - assert_match %r{David}, @xml + assert_match %r{David}, xml end test "should serialize string" do @@ -162,7 +162,7 @@ class XmlSerializationTest < ActiveModel::TestCase assert_match %r{unknown}, xml end - test 'should supply serializable to second proc argument' do + test "should supply serializable to second proc argument" do proc = Proc.new { |options, record| options[:builder].tag!('name-reverse', record.name.reverse) } xml = @contact.to_xml(procs: [ proc ]) assert_match %r{kcats noraa}, xml diff --git a/activemodel/test/cases/validations/confirmation_validation_test.rb b/activemodel/test/cases/validations/confirmation_validation_test.rb index 4c3d7a7615..65a2a1eb49 100644 --- a/activemodel/test/cases/validations/confirmation_validation_test.rb +++ b/activemodel/test/cases/validations/confirmation_validation_test.rb @@ -53,17 +53,17 @@ class ConfirmationValidationTest < ActiveModel::TestCase end def test_title_confirmation_with_i18n_attribute - @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend - I18n.load_path.clear - I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en', { - errors: { messages: { confirmation: "doesn't match %{attribute}" } }, - activemodel: { attributes: { topic: { title: 'Test Title'} } } - }) + begin + @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend + I18n.load_path.clear + I18n.backend = I18n::Backend::Simple.new + I18n.backend.store_translations('en', { + errors: { messages: { confirmation: "doesn't match %{attribute}" } }, + activemodel: { attributes: { topic: { title: 'Test Title'} } } + }) - Topic.validates_confirmation_of(:title) + Topic.validates_confirmation_of(:title) - begin t = Topic.new("title" => "We should be confirmed","title_confirmation" => "") assert t.invalid? assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation] diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index f77cf47fb7..e1657407cf 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -119,6 +119,7 @@ class NumericalityValidationTest < ActiveModel::TestCase invalid!([3, 4]) valid!([5, 6]) + ensure Topic.send(:remove_method, :min_approved) end @@ -128,6 +129,7 @@ class NumericalityValidationTest < ActiveModel::TestCase invalid!([6]) valid!([4, 5]) + ensure Topic.send(:remove_method, :max_approved) end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index cfce8fb8e6..4ea5670d32 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -139,6 +139,8 @@ class ValidationsTest < ActiveModel::TestCase assert_equal 4, hits assert_equal %w(gotcha gotcha), t.errors[:title] assert_equal %w(gotcha gotcha), t.errors[:content] + ensure + CustomReader.clear_validators! end def test_validate_block -- cgit v1.2.3 From 6868265fe36e280e0b1ce96f3d378d0e0e14dc90 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 11 Mar 2014 08:34:51 +0100 Subject: comment why we are modifying global state. [ci skip] --- activemodel/test/cases/secure_password_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activemodel') diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index d603dee73e..bcd1e04a0f 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -4,6 +4,7 @@ require 'models/visitor' class SecurePasswordTest < ActiveModel::TestCase setup do + # Used only to speed up tests @original_min_cost = ActiveModel::SecurePassword.min_cost ActiveModel::SecurePassword.min_cost = true -- cgit v1.2.3