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.

Raises:

  • (NotImplementedError)


370
371
372
373
374
375
376
377
378
379
380
# File 'lib/minitest/assertions.rb', line 370

def assert_pattern
  raise NotImplementedError, "only available in Ruby 3.0+" unless RUBY_VERSION >= "3.0"
  flunk "assert_pattern requires a block to capture errors." unless block_given?

  begin # TODO: remove after ruby 2.6 dropped
    yield
    pass
  rescue NoMatchingPatternError => e
    flunk e.message
  end
end