Class: Minicrest::Asserter

Inherits:
Object
  • Object
show all
Defined in:
lib/minicrest/asserter.rb

Overview

Fluent assertion wrapper that enables comma-free assertion syntax.

Examples:

Basic usage

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

With combined matchers

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

With block for error assertions

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

Instance Method Summary collapse

Constructor Details

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

Creates a new asserter for the given value or block.

Parameters:

  • actual (Object, nil)

    the value to assert against

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

    optional message prefix for failures

Yields:

  • block to execute for error assertions



27
28
29
30
31
# File 'lib/minicrest/asserter.rb', line 27

def initialize(actual, message = nil, &block)
  @actual = actual
  @message = message
  @block = block
end

Instance Method Details

#all_entries(entry_matcher) ⇒ true

Asserts that all entries match.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



259
260
261
# File 'lib/minicrest/asserter.rb', line 259

def all_entries(entry_matcher)
  matches(AllEntries.new(entry_matcher))
end

#all_items(item_matcher) ⇒ true

Asserts that all items match.

Parameters:

  • item_matcher (Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



235
236
237
# File 'lib/minicrest/asserter.rb', line 235

def all_items(item_matcher)
  matches(AllItems.new(item_matcher))
end

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

Asserts that actual is between min and max.

Parameters:

  • min (Comparable)

    minimum

  • max (Comparable)

    maximum

  • exclusive (Boolean) (defaults to: false)

    whether exclusive

Returns:

  • (true)

    if assertion passes



178
179
180
# File 'lib/minicrest/asserter.rb', line 178

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

#blanktrue

Asserts that actual is blank.

Returns:

  • (true)

    if assertion passes



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

def blank
  matches(Blank.new)
end

#contains(*items) ⇒ true

Asserts that actual contains items in any order.

Parameters:

  • items (Array)

    items

Returns:

  • (true)

    if assertion passes



219
220
221
# File 'lib/minicrest/asserter.rb', line 219

def contains(*items)
  matches(Contains.new(*items))
end

#contains_exactly(*items) ⇒ true

Asserts that actual contains items in order.

Parameters:

  • items (Array)

    items

Returns:

  • (true)

    if assertion passes



227
228
229
# File 'lib/minicrest/asserter.rb', line 227

def contains_exactly(*items)
  matches(ContainsExactly.new(*items))
end

#descends_from(expected_type) ⇒ true

Asserts that actual is an instance of expected_type.

Parameters:

  • expected_type (Class, Module)

    the expected type

Returns:

  • (true)

    if assertion passes



53
54
55
# File 'lib/minicrest/asserter.rb', line 53

def descends_from(expected_type)
  matches(DescendsFrom.new(expected_type))
end

#emptytrue

Asserts that actual is empty.

Returns:

  • (true)

    if assertion passes



128
129
130
# File 'lib/minicrest/asserter.rb', line 128

def empty
  matches(Empty.new)
end

#ends_with(suffix) ⇒ true

Asserts that actual ends with suffix.

Parameters:

  • suffix (String)

    the suffix

Returns:

  • (true)

    if assertion passes



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

def ends_with(suffix)
  matches(EndsWith.new(suffix))
end

#equals(expected) ⇒ true

Asserts that actual equals expected by value (deep equality).

Parameters:

  • expected (Object)

    the expected value

Returns:

  • (true)

    if assertion passes



37
38
39
# File 'lib/minicrest/asserter.rb', line 37

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

#falsytrue

Asserts that actual is falsy.

Returns:

  • (true)

    if assertion passes



82
83
84
# File 'lib/minicrest/asserter.rb', line 82

def falsy
  matches(Falsy.new)
end

#has_attribute(name, value_matcher = nil) ⇒ true

Asserts that actual has attribute.

Parameters:

  • name (Symbol)

    attribute name

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

    value matcher

Returns:

  • (true)

    if assertion passes



292
293
294
# File 'lib/minicrest/asserter.rb', line 292

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

#has_key(*keys) ⇒ true

Asserts that actual has keys.

Parameters:

  • keys (Array)

    keys

Returns:

  • (true)

    if assertion passes



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

def has_key(*keys)
  matches(HasKey.new(*keys))
end

#has_size(expected) ⇒ true

Asserts that actual has expected size.

Parameters:

  • expected (Integer, Matcher)

    the size or matcher

Returns:

  • (true)

    if assertion passes



136
137
138
# File 'lib/minicrest/asserter.rb', line 136

def has_size(expected)
  matches(HasSize.new(expected))
end

#has_value(*values) ⇒ true

Asserts that actual has values.

Parameters:

  • values (Array)

    values

Returns:

  • (true)

    if assertion passes



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

def has_value(*values)
  matches(HasValue.new(*values))
end

#includes(*items) ⇒ true

Asserts that actual includes items.

Parameters:

  • items (Array)

    items

Returns:

  • (true)

    if assertion passes



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

def includes(*items)
  matches(Includes.new(*items))
end

#instance_of(expected_type) ⇒ true

Asserts that actual is an instance of expected_type.

Parameters:

  • expected_type (Class)

    the expected type

Returns:

  • (true)

    if assertion passes



61
62
63
# File 'lib/minicrest/asserter.rb', line 61

def instance_of(expected_type)
  matches(InstanceOf.new(expected_type))
end

#is(expected) ⇒ true

Asserts that actual is the same object as expected (reference equality).

Parameters:

  • expected (Object)

    the expected object reference

Returns:

  • (true)

    if assertion passes



45
46
47
# File 'lib/minicrest/asserter.rb', line 45

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

#is_close_to(expected, delta) ⇒ true

Asserts that actual is close to expected.

Parameters:

  • expected (Numeric)

    expected

  • delta (Numeric)

    delta

Returns:

  • (true)

    if assertion passes



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

def is_close_to(expected, delta)
  matches(IsCloseTo.new(expected, delta))
end

#is_greater_than(expected) ⇒ true

Asserts that actual is greater than expected.

Parameters:

  • expected (Comparable)

    the value

Returns:

  • (true)

    if assertion passes



144
145
146
# File 'lib/minicrest/asserter.rb', line 144

def is_greater_than(expected)
  matches(IsGreaterThan.new(expected))
end

#is_greater_than_or_equal_to(expected) ⇒ true

Asserts that actual is greater than or equal to expected.

Parameters:

  • expected (Comparable)

    the value

Returns:

  • (true)

    if assertion passes



160
161
162
# File 'lib/minicrest/asserter.rb', line 160

def is_greater_than_or_equal_to(expected)
  matches(IsGreaterThanOrEqualTo.new(expected))
end

#is_in(collection) ⇒ true

Asserts that actual is in collection.

Parameters:

  • collection (Enumerable)

    collection

Returns:

  • (true)

    if assertion passes



283
284
285
# File 'lib/minicrest/asserter.rb', line 283

def is_in(collection)
  matches(IsIn.new(collection))
end

#is_less_than(expected) ⇒ true

Asserts that actual is less than expected.

Parameters:

  • expected (Comparable)

    the value

Returns:

  • (true)

    if assertion passes



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

def is_less_than(expected)
  matches(IsLessThan.new(expected))
end

#is_less_than_or_equal_to(expected) ⇒ true

Asserts that actual is less than or equal to expected.

Parameters:

  • expected (Comparable)

    the value

Returns:

  • (true)

    if assertion passes



168
169
170
# File 'lib/minicrest/asserter.rb', line 168

def is_less_than_or_equal_to(expected)
  matches(IsLessThanOrEqualTo.new(expected))
end

#matches(matcher) ⇒ true

Asserts that actual matches the given matcher.

Use this for combined matchers or custom matchers.

Examples:

With combined matchers

assert_that(value).matches(equals(1) | equals(2))

Parameters:

  • matcher (Matcher)

    the matcher to use

Returns:

  • (true)

    if assertion passes

Raises:

  • (Minitest::Assertion)

    if assertion fails



321
322
323
324
325
326
327
328
# File 'lib/minicrest/asserter.rb', line 321

def matches(matcher)
  return true if matcher.matches?(@actual)

  failure_msg = matcher.failure_message(@actual)
  failure_msg = "#{@message}: #{failure_msg}" if @message

  raise Minitest::Assertion, failure_msg
end

#matches_pattern(pattern) ⇒ true

Asserts that actual matches pattern.

Parameters:

  • pattern (Regexp)

    the pattern

Returns:

  • (true)

    if assertion passes



114
115
116
# File 'lib/minicrest/asserter.rb', line 114

def matches_pattern(pattern)
  matches(MatchesPattern.new(pattern))
end

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

Asserts that actual does NOT match the given matcher.

Examples:

assert_that(42).never(equals(0))
assert_that(value).never(equals(nil))

Parameters:

  • matcher (Matcher)

    the matcher that should not match

Returns:

  • (true)

    if assertion passes

Raises:

  • (Minitest::Assertion)

    if assertion fails



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

def never(matcher)
  matches(Not.new(matcher))
end

#nil_valuetrue

Asserts that actual is nil.

Returns:

  • (true)

    if assertion passes



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

def nil_value
  matches(NilValue.new)
end

#no_entry(entry_matcher) ⇒ true

Asserts that no entries match.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



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

def no_entry(entry_matcher)
  matches(NoEntry.new(entry_matcher))
end

#no_items(item_matcher) ⇒ true

Asserts that no items match.

Parameters:

  • item_matcher (Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



251
252
253
# File 'lib/minicrest/asserter.rb', line 251

def no_items(item_matcher)
  matches(NoItems.new(item_matcher))
end

#raises_error(expected_class = nil, message_matcher = nil) ⇒ true

Asserts that the block raises an error.

Examples:

Any error

assert_that { raise "boom" }.raises_error

Specific error class

assert_that { raise ArgumentError }.raises_error(ArgumentError)

With message matcher

assert_that { raise ArgumentError, "bad" }.raises_error(ArgumentError, includes("bad"))

Parameters:

  • expected_class (Class, nil) (defaults to: nil)

    optional error class to match

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

    optional matcher for error message

Returns:

  • (true)

    if assertion passes

Raises:

  • (Minitest::Assertion)

    if assertion fails



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/minicrest/asserter.rb', line 345

def raises_error(expected_class = nil, message_matcher = nil)
  raise ArgumentError, 'raises_error requires a block' unless @block

  begin
    @block.call
    # Block didn't raise - this is a failure
    failure_msg = if expected_class
                    "expected block to raise #{expected_class}, but no error was raised"
                  else
                    'expected block to raise an error, but no error was raised'
                  end
    failure_msg = "#{@message}: #{failure_msg}" if @message
    raise Minitest::Assertion, failure_msg
  rescue Minitest::Assertion
    raise
  rescue StandardError => e
    # Block raised an error - check if it matches expectations
    if expected_class && !e.is_a?(expected_class)
      failure_msg = "expected block to raise #{expected_class}, but raised #{e.class}: #{e.message}"
      failure_msg = "#{@message}: #{failure_msg}" if @message
      raise Minitest::Assertion, failure_msg
    end

    if message_matcher && !message_matcher.matches?(e.message)
      failure_msg = "expected block to raise #{expected_class || 'an error'} " \
                    "with message #{message_matcher.description}, but message was #{e.message.inspect}"
      failure_msg = "#{@message}: #{failure_msg}" if @message
      raise Minitest::Assertion, failure_msg
    end

    true
  end
end

#raises_nothingtrue

Asserts that the block does not raise any error.

Examples:

assert_that { safe_operation }.raises_nothing

Returns:

  • (true)

    if assertion passes

Raises:

  • (Minitest::Assertion)

    if block raises an error



386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/minicrest/asserter.rb', line 386

def raises_nothing
  raise ArgumentError, 'raises_nothing requires a block' unless @block

  begin
    @block.call
    true
  rescue StandardError => e
    failure_msg = "expected block not to raise an error, but raised #{e.class}: #{e.message}"
    failure_msg = "#{@message}: #{failure_msg}" if @message
    raise Minitest::Assertion, failure_msg
  end
end

#responds_to(*methods) ⇒ true

Asserts that actual responds to given methods.

Parameters:

  • methods (Array<Symbol>)

    the methods to check

Returns:

  • (true)

    if assertion passes



90
91
92
# File 'lib/minicrest/asserter.rb', line 90

def responds_to(*methods)
  matches(RespondsTo.new(*methods))
end

#some_entry(entry_matcher) ⇒ true

Asserts that at least one entry matches.

Parameters:

  • entry_matcher (Proc, Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



267
268
269
# File 'lib/minicrest/asserter.rb', line 267

def some_entry(entry_matcher)
  matches(SomeEntry.new(entry_matcher))
end

#some_items(item_matcher) ⇒ true

Asserts that some items match.

Parameters:

  • item_matcher (Matcher)

    matcher

Returns:

  • (true)

    if assertion passes



243
244
245
# File 'lib/minicrest/asserter.rb', line 243

def some_items(item_matcher)
  matches(SomeItems.new(item_matcher))
end

#starts_with(prefix) ⇒ true

Asserts that actual starts with prefix.

Parameters:

  • prefix (String)

    the prefix

Returns:

  • (true)

    if assertion passes



98
99
100
# File 'lib/minicrest/asserter.rb', line 98

def starts_with(prefix)
  matches(StartsWith.new(prefix))
end

#truthytrue

Asserts that actual is truthy.

Returns:

  • (true)

    if assertion passes



75
76
77
# File 'lib/minicrest/asserter.rb', line 75

def truthy
  matches(Truthy.new)
end