Method: Minitest::Assertions#assert_pattern

Defined in:
lib/minitest/assertions.rb

#assert_patternObject

For testing with pattern matching (only supported with Ruby 3.0 and later)

# pass
assert_pattern { [1,2,3] => [Integer, Integer, Integer] }

# fail "length mismatch (given 3, expected 1)"
assert_pattern { [1,2,3] => [Integer] }

The bare => pattern will raise a NoMatchingPatternError on failure, which would normally be counted as a test error. This assertion rescues NoMatchingPatternError and generates a test failure. Any other exception will be raised as normal and generate a test error.



360
361
362
363
364
365
366
367
# File 'lib/minitest/assertions.rb', line 360

def assert_pattern
  flunk "assert_pattern requires a block to capture errors." unless block_given?

  yield
  pass
rescue NoMatchingPatternError => e
  flunk e.message
end