Sha256: eaf23d55df5977d313b2fa57bdeebdf6ae1c80bc135f4ab9a55fce5ee4d56963

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 KB

Contents

require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class DirtyTest < Test::Unit::TestCase
  class DirtyModel
    include VirtualBox::AbstractModel::Dirty

    def initialize
      @foo = "foo"
      @bar = "bar"
    end

    def foo=(value)
      set_dirty!("foo", @foo, value)
      @foo = value
    end

    def bar=(value)
      set_dirty!(:bar, @bar, value)
      @bar = value
    end
  end

  context "dirty attributes" do
    setup do
      @model = DirtyModel.new
    end

    should "not be dirty initially" do
      assert !@model.changed?
    end

    should "be dirty after changing an attribute" do
      assert !@model.changed? # sanity
      @model.foo = "my value"
      assert @model.changed?
    end

    should "be able to clear dirty state" do
      assert !@model.changed?
      @model.foo = "my value"
      assert @model.changed?
      @model.clear_dirty!(:foo)
      assert !@model.changed?
    end

    should "be able to clear dirty state on entire model" do
      @model.foo = "changed"
      @model.bar = "changed"
      assert @model.changed?
      @model.clear_dirty!
      assert !@model.changed?
    end

    should "show changes on specific field" do
      assert !@model.changed?
      @model.foo = "my value"
      assert @model.foo_changed?
      assert_equal ["foo", "my value"], @model.foo_change
      assert_equal "foo", @model.foo_was
    end

    should "return nil for field_was if its not changed" do
      assert !@model.foo_changed?
      assert_nil @model.foo_was
    end

    should "show changes for the whole model" do
      assert !@model.changed?
      @model.foo = "foo2"
      @model.bar = "bar2"

      assert @model.changed?
      changes = @model.changes
      assert_equal 2, changes.length
      assert_equal ["foo", "foo2"], changes[:foo]
      assert_equal ["bar", "bar2"], changes[:bar]
    end

    should "still forward non-dirty magic methods up method_missing" do
      assert_raises(NoMethodError) { @model.foobarbaz }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
virtualbox-0.7.5 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.7.4 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.7.3 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.7.2 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.7.1 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.7.0 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.6.1 test/virtualbox/abstract_model/dirty_test.rb
virtualbox-0.6.0 test/virtualbox/abstract_model/dirty_test.rb