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.
Instance Method Summary collapse
-
#all_entries(entry_matcher) ⇒ AllEntries
Factory method for all_entries() matcher.
-
#all_items(item_matcher) ⇒ AllItems
Factory method for all_items() matcher.
-
#all_of(*matchers) ⇒ All
Factory method for all_of() combinator.
-
#anything ⇒ Anything
Factory method for anything() placeholder matcher.
-
#assert_that(actual = nil, message = nil) { ... } ⇒ Asserter
Creates a fluent asserter for the given value or block.
-
#between(min, max, exclusive: false) ⇒ Between
Factory method for between() matcher.
-
#blank ⇒ Blank
Factory method for blank() matcher.
-
#contains(*items) ⇒ Contains
Factory method for contains() matcher.
-
#contains_exactly(*items) ⇒ ContainsExactly
Factory method for contains_exactly() matcher.
-
#descends_from(expected_type) ⇒ DescendsFrom
Factory method for descends_from() matcher.
-
#empty ⇒ Empty
Factory method for empty() matcher.
-
#ends_with(suffix) ⇒ EndsWith
Factory method for ends_with() matcher.
-
#equals(expected) ⇒ Equals
Factory method for equals() matcher.
-
#falsy ⇒ Falsy
Factory method for falsy() matcher.
-
#has_attribute(name, value_matcher = nil) ⇒ HasAttribute
Factory method for has_attribute() matcher.
-
#has_key(*keys) ⇒ HasKey
Factory method for has_key() matcher.
-
#has_size(expected) ⇒ HasSize
Factory method for has_size() matcher.
-
#has_value(*values) ⇒ HasValue
Factory method for has_value() matcher.
-
#includes(*items) ⇒ Includes
Factory method for includes() matcher.
-
#instance_of(expected_type) ⇒ InstanceOf
Factory method for instance_of() matcher.
-
#is(expected) ⇒ Is
Factory method for is() matcher.
-
#is_close_to(expected, delta) ⇒ IsCloseTo
Factory method for is_close_to() matcher.
-
#is_greater_than(expected) ⇒ IsGreaterThan
Factory method for is_greater_than() matcher.
-
#is_greater_than_or_equal_to(expected) ⇒ IsGreaterThanOrEqualTo
Factory method for is_greater_than_or_equal_to() matcher.
-
#is_in(collection) ⇒ IsIn
Factory method for is_in() matcher.
-
#is_less_than(expected) ⇒ IsLessThan
Factory method for is_less_than() matcher.
-
#is_less_than_or_equal_to(expected) ⇒ IsLessThanOrEqualTo
Factory method for is_less_than_or_equal_to() matcher.
-
#matches_pattern(pattern) ⇒ MatchesPattern
Factory method for matches_pattern() matcher.
-
#never(matcher) ⇒ Not
(also: #is_not, #does_not)
Factory method for never() combinator.
-
#nil_value ⇒ NilValue
Factory method for nil_value() matcher.
-
#no_entry(entry_matcher) ⇒ NoEntry
Factory method for no_entry() matcher.
-
#no_items(item_matcher) ⇒ NoItems
Factory method for no_items() matcher.
-
#none_of(*matchers) ⇒ None
Factory method for none_of() combinator.
-
#responds_to(*methods) ⇒ RespondsTo
Factory method for responds_to() matcher.
-
#some_entry(entry_matcher) ⇒ SomeEntry
Factory method for some_entry() matcher.
-
#some_items(item_matcher) ⇒ SomeItems
Factory method for some_items() matcher.
-
#some_of(*matchers) ⇒ Some
Factory method for some_of() combinator.
-
#starts_with(prefix) ⇒ StartsWith
Factory method for starts_with() matcher.
-
#truthy ⇒ Truthy
Factory method for truthy() matcher.
Instance Method Details
#all_entries(entry_matcher) ⇒ AllEntries
Factory method for all_entries() matcher.
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.
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.
387 388 389 |
# File 'lib/minicrest/assertions.rb', line 387 def all_of(*matchers) Minicrest::All.new(*matchers) end |
#anything ⇒ Anything
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.
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.
43 44 45 |
# File 'lib/minicrest/assertions.rb', line 43 def assert_that(actual = nil, = nil, &) Asserter.new(actual, , &) end |
#between(min, max, exclusive: false) ⇒ Between
Factory method for between() matcher.
221 222 223 |
# File 'lib/minicrest/assertions.rb', line 221 def between(min, max, exclusive: false) Minicrest::Between.new(min, max, exclusive:) end |
#blank ⇒ Blank
Factory method for blank() 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).
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.
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.
84 85 86 |
# File 'lib/minicrest/assertions.rb', line 84 def descends_from(expected_type) Minicrest::DescendsFrom.new(expected_type) end |
#empty ⇒ Empty
Factory method for empty() matcher.
Matches empty strings, arrays, hashes, or any object that responds to empty? and returns true.
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.
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.
51 52 53 |
# File 'lib/minicrest/assertions.rb', line 51 def equals(expected) Minicrest::Equals.new(expected) end |
#falsy ⇒ Falsy
Factory method for 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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
375 376 377 |
# File 'lib/minicrest/assertions.rb', line 375 def never(matcher) Minicrest::Not.new(matcher) end |
#nil_value ⇒ NilValue
Factory method for nil_value() matcher.
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.
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.
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.
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.
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.
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.
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.
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.
129 130 131 |
# File 'lib/minicrest/assertions.rb', line 129 def starts_with(prefix) Minicrest::StartsWith.new(prefix) end |