liveBook · Manning

Assertion

As explained in The Well-Grounded Rubyist, Fourth Edition

A zero-width assertion is a regular-expression condition that checks text or position without consuming any characters.

An assertion checks a condition without itself becoming ordinary matched content. In regular expressions, it constrains where or when matching may continue; in testing, its truth determines whether a test passes. section 11.5.3 section 15.5.1

Regular-expression assertions include anchors for line and string positions, word-boundary checks, and lookaround conditions. The surrounding pattern must satisfy the assertion, but the assertion consumes no characters. section 11.5.3

Common regular-expression assertion forms include:

  • Use \b and \B to require or reject a transition between word and non-word characters. section 11.5.3

Filtering Ruby comment lines

The regexp /^\s*#/ asserts that matching begins at the start of a line, permits leading whitespace, and then requires a hash mark. It matches " # Pure comment!" but not " x = 1 # Code plus comment!".

Assertions in MiniTest

A test assertion expresses an expected result: assert_equal(51, @deck.size) verifies that dealing one card leaves 51 cards, while a second test verifies that dealing five leaves 47. MiniTest reports the number of assertions and failures. section 15.5.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains non-consumption, positional forms, lookaround syntax, and concrete tests without developing assertion internals.
Examples
Demonstrates comment-line filtering, lookaround patterns, and deck-of-cards tests using assert_equal.
What it assumes
Assumes familiarity with Ruby strings and regular-expression syntax while introducing MiniTest test structure through a complete example.
What you can do after
Apply assertions to constrain Ruby regexp matches and verify Ruby program behavior with MiniTest.
How it connects
Connects assertions with regular expressions, anchors, word boundaries, lookaround, MiniTest, test methods, and expected values.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.