Module: Minicrest::Assertions

Defined in:
lib/minicrest/assertions.rb

Overview

Assertions module that provides Hamcrest-style assert_that with fluent API.

Include this module in your test class or Minitest::Test to get access to the assert_that fluent assertion and matcher factory methods.

Examples:

Including in a test class

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

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

Instance Method Summary collapse

Instance Method Details

#all_entries(entry_matcher) ⇒ AllEntries

Factory method for all_entries() matcher.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher for entries

Returns:



323
324
325
# File 'lib/minicrest/assertions.rb', line 323

def all_entries(entry_matcher)
  Minicrest::AllEntries.new(entry_matcher)
end

#all_items(item_matcher) ⇒ AllItems

Factory method for all_items() matcher.

Checks if all items in a collection match the given matcher.

Parameters:

  • item_matcher (Matcher)

    the matcher to apply to each item

Returns:



295
296
297
# File 'lib/minicrest/assertions.rb', line 295

def all_items(item_matcher)
  Minicrest::AllItems.new(item_matcher)
end

#all_of(*matchers) ⇒ All

Factory method for all_of() combinator.

All matchers must match.

Parameters:

  • matchers (Array<Matcher>)

    matchers that must all match

Returns:

  • (All)

    ALL combinator



387
388
389
# File 'lib/minicrest/assertions.rb', line 387

def all_of(*matchers)
  Minicrest::All.new(*matchers)
end

#anythingAnything

Factory method for anything() placeholder matcher.

Use this when you want to assert the structure of data but don’t care about specific values, or when testing that something exists without caring what it is.

Examples:

Basic usage

assert_that(value).matches(anything)

With negation (unusual but valid)

assert_that(value).does_not(anything)  # Always fails

Returns:

  • (Anything)

    matcher that matches any value



68
69
70
# File 'lib/minicrest/assertions.rb', line 68

def anything
  Minicrest::Anything.new
end

#assert_that(actual = nil, message = nil) { ... } ⇒ Asserter

Creates a fluent asserter for the given value or block.

This is the main entry point for Hamcrest-style assertions. Returns an Asserter that provides chainable matcher methods.

Examples:

Basic assertions

assert_that(42).equals(42)
assert_that(obj).is(same_obj)
assert_that(value).is_not(equals(nil))

With combined matchers

assert_that(value).matches(equals(1) | equals(2))
assert_that(value).matches(is_not(equals(nil)) & is_not(equals("")))

With custom message

assert_that(result, "computation result").equals(expected)

With block for error assertions

assert_that { raise "boom" }.raises_error
assert_that { safe_operation }.raises_nothing

Parameters:

  • actual (Object, nil) (defaults to: nil)

    the value to check (nil if using block)

  • message (String, nil) (defaults to: nil)

    optional custom message prefix

Yields:

  • block to execute for error assertions

Returns:

  • (Asserter)

    fluent asserter for chaining



43
44
45
# File 'lib/minicrest/assertions.rb', line 43

def assert_that(actual = nil, message = nil, &)
  Asserter.new(actual, message, &)
end

#between(min, max, exclusive: false) ⇒ Between

Factory method for between() matcher.

Parameters:

  • min (Comparable)

    the minimum value

  • max (Comparable)

    the maximum value

  • exclusive (Boolean) (defaults to: false)

    whether the range is exclusive

Returns:



221
222
223
# File 'lib/minicrest/assertions.rb', line 221

def between(min, max, exclusive: false)
  Minicrest::Between.new(min, max, exclusive:)
end

#blankBlank

Factory method for blank() matcher.

Returns:

  • (Blank)

    blank string matcher



152
153
154
# File 'lib/minicrest/assertions.rb', line 152

def blank
  Minicrest::Blank.new
end

#contains(*items) ⇒ Contains

Factory method for contains() matcher.

Checks if a collection contains exactly the specified items (in any order).

Parameters:

  • items (Array)

    items that must be contained

Returns:



275
276
277
# File 'lib/minicrest/assertions.rb', line 275

def contains(*items)
  Minicrest::Contains.new(*items)
end

#contains_exactly(*items) ⇒ ContainsExactly

Factory method for contains_exactly() matcher.

Checks if an array contains exactly the specified items in the specified order.

Parameters:

  • items (Array)

    items that must be contained in order

Returns:



285
286
287
# File 'lib/minicrest/assertions.rb', line 285

def contains_exactly(*items)
  Minicrest::ContainsExactly.new(*items)
end

#descends_from(expected_type) ⇒ DescendsFrom

Factory method for descends_from() matcher.

Parameters:

  • expected_type (Class, Module)

    the expected type

Returns:



84
85
86
# File 'lib/minicrest/assertions.rb', line 84

def descends_from(expected_type)
  Minicrest::DescendsFrom.new(expected_type)
end

#emptyEmpty

Factory method for empty() matcher.

Matches empty strings, arrays, hashes, or any object that responds to empty? and returns true.

Returns:

  • (Empty)

    empty matcher



162
163
164
# File 'lib/minicrest/assertions.rb', line 162

def empty
  Minicrest::Empty.new
end

#ends_with(suffix) ⇒ EndsWith

Factory method for ends_with() matcher.

Parameters:

  • suffix (String)

    the expected suffix

Returns:



137
138
139
# File 'lib/minicrest/assertions.rb', line 137

def ends_with(suffix)
  Minicrest::EndsWith.new(suffix)
end

#equals(expected) ⇒ Equals

Factory method for equals() matcher.

Parameters:

  • expected (Object)

    the expected value

Returns:

  • (Equals)

    value equality matcher



51
52
53
# File 'lib/minicrest/assertions.rb', line 51

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

#falsyFalsy

Factory method for falsy() matcher.

Returns:

  • (Falsy)

    falsy matcher



113
114
115
# File 'lib/minicrest/assertions.rb', line 113

def falsy
  Minicrest::Falsy.new
end

#has_attribute(name, value_matcher = nil) ⇒ HasAttribute

Factory method for has_attribute() matcher.

Checks if an object has a specific attribute. Works with objects with attr_reader and hashes.

Parameters:

  • name (Symbol)

    the attribute name

  • value_matcher (Matcher, nil) (defaults to: nil)

    optional matcher for the value

Returns:



365
366
367
# File 'lib/minicrest/assertions.rb', line 365

def has_attribute(name, value_matcher = nil)
  Minicrest::HasAttribute.new(name, value_matcher)
end

#has_key(*keys) ⇒ HasKey

Factory method for has_key() matcher.

Checks if a hash contains all specified keys.

Parameters:

  • keys (Array<Symbol, String>)

    keys that must be present

Returns:

  • (HasKey)

    has_key matcher



255
256
257
# File 'lib/minicrest/assertions.rb', line 255

def has_key(*keys)
  Minicrest::HasKey.new(*keys)
end

#has_size(expected) ⇒ HasSize

Factory method for has_size() matcher.

Matches values with a specific size. Works with strings, arrays, hashes, and any object that responds to size.

Examples:

With integer

has_size(5).matches?('hello')  # => true

With matcher

has_size(equals(5)).matches?('hello')  # => true

Parameters:

  • expected (Integer, Matcher)

    the expected size or a matcher for the size

Returns:



179
180
181
# File 'lib/minicrest/assertions.rb', line 179

def has_size(expected)
  Minicrest::HasSize.new(expected)
end

#has_value(*values) ⇒ HasValue

Factory method for has_value() matcher.

Checks if a hash contains all specified values.

Parameters:

  • values (Array)

    values that must be present

Returns:



265
266
267
# File 'lib/minicrest/assertions.rb', line 265

def has_value(*values)
  Minicrest::HasValue.new(*values)
end

#includes(*items) ⇒ Includes

Factory method for includes() matcher.

For strings: checks for substrings For arrays: checks for elements For hashes: checks for key-value pairs

Parameters:

  • items (Array)

    items that must be included

Returns:



245
246
247
# File 'lib/minicrest/assertions.rb', line 245

def includes(*items)
  Minicrest::Includes.new(*items)
end

#instance_of(expected_type) ⇒ InstanceOf

Factory method for instance_of() matcher.

Parameters:

  • expected_type (Class)

    the expected type

Returns:



92
93
94
# File 'lib/minicrest/assertions.rb', line 92

def instance_of(expected_type)
  Minicrest::InstanceOf.new(expected_type)
end

#is(expected) ⇒ Is

Factory method for is() matcher.

Parameters:

  • expected (Object)

    the expected reference

Returns:

  • (Is)

    reference equality matcher



76
77
78
# File 'lib/minicrest/assertions.rb', line 76

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

#is_close_to(expected, delta) ⇒ IsCloseTo

Factory method for is_close_to() matcher.

Matches if the actual value is within delta of the expected value. Useful for floating-point comparisons.

Parameters:

  • expected (Numeric)

    the expected value

  • delta (Numeric)

    the acceptable difference

Returns:



233
234
235
# File 'lib/minicrest/assertions.rb', line 233

def is_close_to(expected, delta)
  Minicrest::IsCloseTo.new(expected, delta)
end

#is_greater_than(expected) ⇒ IsGreaterThan

Factory method for is_greater_than() matcher.

Parameters:

  • expected (Comparable)

    the value to compare against

Returns:



187
188
189
# File 'lib/minicrest/assertions.rb', line 187

def is_greater_than(expected)
  Minicrest::IsGreaterThan.new(expected)
end

#is_greater_than_or_equal_to(expected) ⇒ IsGreaterThanOrEqualTo

Factory method for is_greater_than_or_equal_to() matcher.

Parameters:

  • expected (Comparable)

    the value to compare against

Returns:



203
204
205
# File 'lib/minicrest/assertions.rb', line 203

def is_greater_than_or_equal_to(expected)
  Minicrest::IsGreaterThanOrEqualTo.new(expected)
end

#is_in(collection) ⇒ IsIn

Factory method for is_in() matcher.

Checks if a value is present in a collection. For arrays: checks element membership For hashes: checks key membership For strings: checks substring membership For ranges: checks value inclusion

Parameters:

  • collection (Array, Hash, String, Range)

    the collection to check

Returns:

  • (IsIn)

    is_in matcher



353
354
355
# File 'lib/minicrest/assertions.rb', line 353

def is_in(collection)
  Minicrest::IsIn.new(collection)
end

#is_less_than(expected) ⇒ IsLessThan

Factory method for is_less_than() matcher.

Parameters:

  • expected (Comparable)

    the value to compare against

Returns:



195
196
197
# File 'lib/minicrest/assertions.rb', line 195

def is_less_than(expected)
  Minicrest::IsLessThan.new(expected)
end

#is_less_than_or_equal_to(expected) ⇒ IsLessThanOrEqualTo

Factory method for is_less_than_or_equal_to() matcher.

Parameters:

  • expected (Comparable)

    the value to compare against

Returns:



211
212
213
# File 'lib/minicrest/assertions.rb', line 211

def is_less_than_or_equal_to(expected)
  Minicrest::IsLessThanOrEqualTo.new(expected)
end

#matches_pattern(pattern) ⇒ MatchesPattern

Factory method for matches_pattern() matcher.

Parameters:

  • pattern (Regexp)

    the expected pattern

Returns:



145
146
147
# File 'lib/minicrest/assertions.rb', line 145

def matches_pattern(pattern)
  Minicrest::MatchesPattern.new(pattern)
end

#never(matcher) ⇒ Not Also known as: is_not, does_not

Factory method for never() combinator.

Use this to negate any matcher.

Parameters:

  • matcher (Matcher)

    the matcher to negate

Returns:

  • (Not)

    negated matcher



375
376
377
# File 'lib/minicrest/assertions.rb', line 375

def never(matcher)
  Minicrest::Not.new(matcher)
end

#nil_valueNilValue

Factory method for nil_value() matcher.

Returns:



99
100
101
# File 'lib/minicrest/assertions.rb', line 99

def nil_value
  Minicrest::NilValue.new
end

#no_entry(entry_matcher) ⇒ NoEntry

Factory method for no_entry() matcher.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher for entries

Returns:



339
340
341
# File 'lib/minicrest/assertions.rb', line 339

def no_entry(entry_matcher)
  Minicrest::NoEntry.new(entry_matcher)
end

#no_items(item_matcher) ⇒ NoItems

Factory method for no_items() matcher.

Checks if no items in a collection match the given matcher.

Parameters:

  • item_matcher (Matcher)

    the matcher to apply to each item

Returns:



315
316
317
# File 'lib/minicrest/assertions.rb', line 315

def no_items(item_matcher)
  Minicrest::NoItems.new(item_matcher)
end

#none_of(*matchers) ⇒ None

Factory method for none_of() combinator.

None of the matchers should match.

Parameters:

  • matchers (Array<Matcher>)

    matchers that must all fail

Returns:

  • (None)

    NONE combinator



397
398
399
# File 'lib/minicrest/assertions.rb', line 397

def none_of(*matchers)
  Minicrest::None.new(*matchers)
end

#responds_to(*methods) ⇒ RespondsTo

Factory method for responds_to() matcher.

Parameters:

  • methods (Array<Symbol>)

    the methods to check for

Returns:



121
122
123
# File 'lib/minicrest/assertions.rb', line 121

def responds_to(*methods)
  Minicrest::RespondsTo.new(*methods)
end

#some_entry(entry_matcher) ⇒ SomeEntry

Factory method for some_entry() matcher.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher for entries

Returns:



331
332
333
# File 'lib/minicrest/assertions.rb', line 331

def some_entry(entry_matcher)
  Minicrest::SomeEntry.new(entry_matcher)
end

#some_items(item_matcher) ⇒ SomeItems

Factory method for some_items() matcher.

Checks if at least one item in a collection matches the given matcher.

Parameters:

  • item_matcher (Matcher)

    the matcher to apply to each item

Returns:



305
306
307
# File 'lib/minicrest/assertions.rb', line 305

def some_items(item_matcher)
  Minicrest::SomeItems.new(item_matcher)
end

#some_of(*matchers) ⇒ Some

Factory method for some_of() combinator.

At least one matcher must match.

Parameters:

  • matchers (Array<Matcher>)

    matchers where at least one must match

Returns:

  • (Some)

    SOME combinator



407
408
409
# File 'lib/minicrest/assertions.rb', line 407

def some_of(*matchers)
  Minicrest::Some.new(*matchers)
end

#starts_with(prefix) ⇒ StartsWith

Factory method for starts_with() matcher.

Parameters:

  • prefix (String)

    the expected prefix

Returns:



129
130
131
# File 'lib/minicrest/assertions.rb', line 129

def starts_with(prefix)
  Minicrest::StartsWith.new(prefix)
end

#truthyTruthy

Factory method for truthy() matcher.

Returns:



106
107
108
# File 'lib/minicrest/assertions.rb', line 106

def truthy
  Minicrest::Truthy.new
end