0

In a Ruby unit test, how do I assert that a string matches another string even though the casing might be different? I want to avoid sanitizing the two strings to match incase I need to come back and do some investigation but at the same time they are the same outcomes.

e.g assert_match 'Test', 'TEST'

I've tried fudging with assert_match to make an case insensitive compare but I have had no luck thus far and I cannot get past the old implicit conversion of Regexp into String.

module Test::Unit::Assertions
  def assert_match matcher, obj, msg = nil
    msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
    assert_respond_to matcher, :"=~"
    matcher = Regexp.new Regexp.escape matcher if String === matcher
    assert matcher =~ /#{obj}/i, /#{msg}/i
  end
end
1
  • while downcase is definitely the easiest for this case you could use assert_match /Test/i, 'TEST' Example. assert_match expects a "matcher" (Regex to match against) and the "obj" the value to try and match. "msg" is simply the message to display on failure. To make this dynamic you could simply use assert_match /#{matcher}/i, value where matcher is the string to match and value is the string to compare. Commented Jul 25, 2016 at 17:40

1 Answer 1

3

I would use downcase on both of the strings.

assert_equal "expected".downcase, actual.downcase

or write your own method:

def assert_equal_case_insensitive(expected, actual)
  assert_equal expected.downcase, actual.downcase
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.