Standalone runtime assertion library with chainable matchers
gem install philiprehberger-assertStandalone runtime assertion library with chainable matchers
Add to your Gemfile:
gem "philiprehberger-assert"
Or install directly:
gem install philiprehberger-assert
require "philiprehberger/assert"
Philiprehberger::Assert.that(42).is_a(Integer)
Philiprehberger::Assert.that('hello').not_blank.matches(/^hel/)
Philiprehberger::Assert.that(age).is_a(Integer).gte(0).lte(150)
Philiprehberger::Assert.that(config).includes_key(:host)
Philiprehberger::Assert.that(items).not_empty
Assert.that('hello world').starts_with('hello')
Assert.that('hello world').ends_with('world')
Assert.that(age).between(0, 150)
Assert.that(status).one_of(:active, :inactive, :pending)
Assert.that(handler).responds_to(:call, :arity)
Assert.that(age).satisfies('must be voting age') { |v| v >= 18 }
Assert.that(email).satisfies { |v| v.include?('@') && v.include?('.') }
Philiprehberger::Assert.that(value, 'value must be a positive number').is_a(Integer).gt(0)
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
Philiprehberger::Assert.precondition(user.active?, 'user must be active')
def withdraw(amount)
Philiprehberger::Assert.precondition(amount.positive?, 'amount must be positive')
result = perform_withdrawal(amount)
Philiprehberger::Assert.postcondition(result.balance >= 0, 'balance must not go negative')
result
end
def transfer(from, to, amount)
before = from.balance + to.balance
perform_transfer(from, to, amount)
Philiprehberger::Assert.invariant(from.balance + to.balance == before, 'total balance must be conserved')
end
| 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 precondition check |
Assert.postcondition(condition, message) | Design by Contract postcondition check |
Assert.invariant(condition, message) | Design by Contract class-invariant 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#starts_with(prefix) | Assert string value starts with prefix |
Assertion#ends_with(suffix) | Assert string value ends with suffix |
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 |
.between(min, max) | Assert value is between min and max (inclusive) |
.one_of(*values) | Assert value is included in the list |
.responds_to(*methods) | Assert value responds to all listed methods |
.satisfies(description = nil, &block) | Assert block returns truthy for value |
bundle install
bundle exec rspec
bundle exec rubocop
If you find this project useful: