philiprehberger-assert
Standalone runtime assertion library with chainable matchers for Ruby.
Requirements
- Ruby >= 3.1
Installation
Add to your Gemfile:
gem 'philiprehberger-assert'Then run:
bundle installOr install directly:
gem install philiprehberger-assertUsage
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_emptyCustom 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 assertionsPreconditions (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 styleLicense
MIT