Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8138 (new defect)

Opened 2 years ago

[PATCH] Make assert_equal calls "==" in actual instead of expected

Reported by: divoxx Assigned to: core
Priority: low Milestone: 2.x
Component: ActiveSupport Version: edge
Severity: normal Keywords:
Cc:

Description

Currently, ruby's assert_equal compares the objects by calling

  expected == actual

That's usually okay, but if you've defined a custom "==" method you'd have problems.

Example:

  class Error
    attr_reader :id, :msg

    def initialize(id, :msg)
      @id = id
    end

    def ==(other)
      case other
        when self.class
          self.id == other.id
        when Symbol
          self.id == other
        else
          self.msg == other.to_s
      end
    end
  end

  # then inside a test
  # ....
  def test_equality_with_id
    foo = SomeFooness.new(:empty, "can't be empty")
    assert_equal :empty, foo
  end

  def test_equality_with_message
    foo = SomeFooness.new(:empty, "can't be empty")
    assert_equal "can't be empty", foo
  end

Without this patch, those tests would fail since assert_equal would call:

:empty == foo
# and
"can't be empty" == foo

While what you want is:

foo == :empty
# and
foo == "can't be empty"

This patchs override assert_equal to test the opposite:

actual == expected

Attachments

active_support_assert_equal_against_actual.6545.diff (0.9 kB) - added by divoxx on 04/21/07 12:42:27.

Change History

04/21/07 12:42:27 changed by divoxx

  • attachment active_support_assert_equal_against_actual.6545.diff added.