The project is in a healthy, maintained state
A lightweight runtime assertion library for Ruby with chainable matchers, soft assertions, and Design by Contract preconditions.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-assert

Tests Gem Version License

Standalone runtime assertion library with chainable matchers for Ruby.

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem 'philiprehberger-assert'

Then run:

bundle install

Or install directly:

gem install philiprehberger-assert

Usage

require 'philiprehberger/assert'

Basic Assertions

Philiprehberger::Assert.that(42).is_a(Integer)
Philiprehberger::Assert.that('hello').not_blank.matches(/^hel/)

Chainable Matchers

Philiprehberger::Assert.that(age).is_a(Integer).gte(0).lte(150)
Philiprehberger::Assert.that(config).includes_key(:host)
Philiprehberger::Assert.that(items).not_empty

Custom Messages

Philiprehberger::Assert.that(value, 'value must be a positive number').is_a(Integer).gt(0)

Soft Assertions

Collect all failures instead of stopping at the first one:

Philiprehberger::Assert.soft do |a|
  a.call(name).not_blank
  a.call(age).is_a(Integer).gte(0)
  a.call(email).matches(/@/)
end
# raises MultipleFailures with all failed assertions

Preconditions (Design by Contract)

Philiprehberger::Assert.precondition(user.active?, 'user must be active')

API

Method Description
Assert.that(value, message = nil) Start a chainable assertion
Assert.soft { |a| ... } Collect failures, raise at end
Assert.precondition(condition, message) Design by Contract check
Assertion#is_a(type) Assert value is an instance of type
Assertion#gte(num) Assert value >= num
Assertion#lte(num) Assert value <= num
Assertion#gt(num) Assert value > num
Assertion#lt(num) Assert value < num
Assertion#matches(pattern) Assert value matches regex pattern
Assertion#not_blank Assert value is not nil or blank
Assertion#not_empty Assert value is not empty
Assertion#includes_key(key) Assert hash includes key

Development

bundle install
bundle exec rspec      # Run tests
bundle exec rubocop    # Check code style

License

MIT