Module: Minicrest

Defined in:
lib/minicrest.rb,
lib/minicrest/is.rb,
lib/minicrest/blank.rb,
lib/minicrest/empty.rb,
lib/minicrest/is_in.rb,
lib/minicrest/equals.rb,
lib/minicrest/between.rb,
lib/minicrest/has_key.rb,
lib/minicrest/matcher.rb,
lib/minicrest/version.rb,
lib/minicrest/anything.rb,
lib/minicrest/asserter.rb,
lib/minicrest/contains.rb,
lib/minicrest/has_size.rb,
lib/minicrest/includes.rb,
lib/minicrest/no_items.rb,
lib/minicrest/all_items.rb,
lib/minicrest/ends_with.rb,
lib/minicrest/has_value.rb,
lib/minicrest/assertions.rb,
lib/minicrest/some_items.rb,
lib/minicrest/combinators.rb,
lib/minicrest/is_close_to.rb,
lib/minicrest/responds_to.rb,
lib/minicrest/starts_with.rb,
lib/minicrest/is_less_than.rb,
lib/minicrest/has_attribute.rb,
lib/minicrest/string_matcher.rb,
lib/minicrest/value_matchers.rb,
lib/minicrest/is_greater_than.rb,
lib/minicrest/matches_pattern.rb,
lib/minicrest/contains_exactly.rb,
lib/minicrest/comparison_matcher.rb,
lib/minicrest/hash_entry_matchers.rb,
lib/minicrest/collection_item_matcher.rb,
lib/minicrest/is_less_than_or_equal_to.rb,
lib/minicrest/is_greater_than_or_equal_to.rb

Overview

Minicrest provides Hamcrest-style composable matchers for Minitest.

This library enables expressive, readable assertions with detailed failure messages using a fluent API. Matchers can be combined using logical operators to create complex matching conditions.

Examples:

Basic setup

require "minicrest"

class MyTest < Minitest::Test
  include Minicrest::Assertions

  def test_value_equality
    assert_that(42).equals(42)
  end

  def test_reference_equality
    obj = Object.new
    assert_that(obj).is(obj)
  end

  def test_reference_inequality
    obj1 = Object.new
    obj2 = Object.new
    assert_that(obj1).is_not(obj2)
  end

  def test_negation
    assert_that(42).does_not(equals(0))
  end
end

Combined matchers

# Either/or matching
assert_that(value).matches(equals(1) | equals(2))

# Both conditions must match
assert_that(value).matches(does_not(equals(nil)) & does_not(equals("")))

With ActiveSupport::TestCase

class ApplicationTest < ActiveSupport::TestCase
  include Minicrest::Assertions
end

Defined Under Namespace

Modules: Assertions Classes: All, AllEntries, AllItems, And, Anything, Asserter, Between, Blank, CollectionItemMatcher, ComparisonMatcher, Contains, ContainsExactly, DescendsFrom, Empty, EndsWith, Equals, Error, Falsy, HasAttribute, HasKey, HasSize, HasValue, HashEntryMatcher, Includes, InstanceOf, Is, IsCloseTo, IsGreaterThan, IsGreaterThanOrEqualTo, IsIn, IsLessThan, IsLessThanOrEqualTo, Matcher, MatchesPattern, NilValue, NoEntry, NoItems, None, Not, Or, RespondsTo, Some, SomeEntry, SomeItems, StartsWith, StringMatcher, Truthy

Constant Summary collapse

VERSION =
'1.0.21'

Class Method Summary collapse

Class Method Details

.equals(expected) ⇒ Equals

Factory method to create an Equals matcher.

Examples:

assert_that result, equals(42)
assert_that data, equals({name: "test", value: 123})

Parameters:

  • expected (Object)

    the expected value

Returns:

  • (Equals)

    a new value equality matcher



193
194
195
# File 'lib/minicrest/equals.rb', line 193

def equals(expected)
  Equals.new(expected)
end

.is(expected) ⇒ Is

Factory method to create an Is matcher.

Examples:

assert_that obj, is(same_obj)

Parameters:

  • expected (Object)

    the expected object reference

Returns:

  • (Is)

    a new reference equality matcher



89
90
91
# File 'lib/minicrest/is.rb', line 89

def is(expected)
  Is.new(expected)
end

.register_matcher(name) {|*args| ... } ⇒ Object

Registers a custom matcher factory method on the Assertions module.

Examples:

Minicrest.register_matcher(:greater_than) { |expected| GreaterThan.new(expected) }

Parameters:

  • name (Symbol)

    the name of the factory method

Yields:

  • (*args)

    block that creates the matcher instance

Raises:

  • (ArgumentError)

    if no block is provided

  • (Minicrest::Error)

    if the matcher name is already registered



60
61
62
63
64
65
66
67
# File 'lib/minicrest.rb', line 60

def register_matcher(name, &)
  raise ArgumentError, 'block required' unless block_given?

  raise Error, "matcher :#{name} is already registered" if registered_matchers.include?(name.to_sym)

  registered_matchers << name.to_sym
  Assertions.define_method(name, &)
end

.registered_matchersArray<Symbol>

Returns the list of registered custom matcher names.

Returns:

  • (Array<Symbol>)

    registered matcher names



72
73
74
# File 'lib/minicrest.rb', line 72

def registered_matchers
  @registered_matchers ||= []
end