philiprehberger-assert 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +17 -0
- data/LICENSE +21 -0
- data/README.md +105 -0
- data/lib/philiprehberger/assert/assertion.rb +64 -0
- data/lib/philiprehberger/assert/errors.rb +18 -0
- data/lib/philiprehberger/assert/version.rb +7 -0
- data/lib/philiprehberger/assert.rb +37 -0
- metadata +55 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0c31f52ec0931f246b6319415cd94403637975101e7072556cd1b527ce306b02
|
|
4
|
+
data.tar.gz: ceb55d858cdfcff1a456f6bd3a95ded5ff1c43e44a4b6b0da7eaf16cbb043bb0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 19fee319b29dfa793a3f6925b3a222195b1b35685faecd98f9b3047b232a4bc726444ae1b41adea18733550f309296d9938b8328a2c0d50babcf1c0dd44fc6fa
|
|
7
|
+
data.tar.gz: c517317adb20571513cb20cfe770ebb767a8484a3b809e6955f73f2b25f80d1f2567bec611eb1401902e0b218173124125ed8909b8f3dab437e478d4cbff37d4
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-03-15
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Initial release
|
|
14
|
+
- Chainable assertion matchers
|
|
15
|
+
- Soft assertions collecting all failures
|
|
16
|
+
- Precondition/postcondition helpers
|
|
17
|
+
- Type comparison and pattern checks
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# philiprehberger-assert
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/rb-assert/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/philiprehberger-assert)
|
|
5
|
+
|
|
6
|
+
Standalone runtime assertion library with chainable matchers for Ruby.
|
|
7
|
+
|
|
8
|
+
## Requirements
|
|
9
|
+
|
|
10
|
+
- Ruby >= 3.1
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Add to your Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'philiprehberger-assert'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Then run:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
bundle install
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or install directly:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
gem install philiprehberger-assert
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require 'philiprehberger/assert'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Basic Assertions
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
Philiprehberger::Assert.that(42).is_a(Integer)
|
|
42
|
+
Philiprehberger::Assert.that('hello').not_blank.matches(/^hel/)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Chainable Matchers
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
Philiprehberger::Assert.that(age).is_a(Integer).gte(0).lte(150)
|
|
49
|
+
Philiprehberger::Assert.that(config).includes_key(:host)
|
|
50
|
+
Philiprehberger::Assert.that(items).not_empty
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Custom Messages
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
Philiprehberger::Assert.that(value, 'value must be a positive number').is_a(Integer).gt(0)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Soft Assertions
|
|
60
|
+
|
|
61
|
+
Collect all failures instead of stopping at the first one:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
Philiprehberger::Assert.soft do |a|
|
|
65
|
+
a.call(name).not_blank
|
|
66
|
+
a.call(age).is_a(Integer).gte(0)
|
|
67
|
+
a.call(email).matches(/@/)
|
|
68
|
+
end
|
|
69
|
+
# raises MultipleFailures with all failed assertions
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Preconditions (Design by Contract)
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
Philiprehberger::Assert.precondition(user.active?, 'user must be active')
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
| Method | Description |
|
|
81
|
+
|--------|-------------|
|
|
82
|
+
| `Assert.that(value, message = nil)` | Start a chainable assertion |
|
|
83
|
+
| `Assert.soft { \|a\| ... }` | Collect failures, raise at end |
|
|
84
|
+
| `Assert.precondition(condition, message)` | Design by Contract check |
|
|
85
|
+
| `Assertion#is_a(type)` | Assert value is an instance of type |
|
|
86
|
+
| `Assertion#gte(num)` | Assert value >= num |
|
|
87
|
+
| `Assertion#lte(num)` | Assert value <= num |
|
|
88
|
+
| `Assertion#gt(num)` | Assert value > num |
|
|
89
|
+
| `Assertion#lt(num)` | Assert value < num |
|
|
90
|
+
| `Assertion#matches(pattern)` | Assert value matches regex pattern |
|
|
91
|
+
| `Assertion#not_blank` | Assert value is not nil or blank |
|
|
92
|
+
| `Assertion#not_empty` | Assert value is not empty |
|
|
93
|
+
| `Assertion#includes_key(key)` | Assert hash includes key |
|
|
94
|
+
|
|
95
|
+
## Development
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
bundle install
|
|
99
|
+
bundle exec rspec # Run tests
|
|
100
|
+
bundle exec rubocop # Check code style
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module Assert
|
|
5
|
+
# Chainable assertion object returned by Assert.that.
|
|
6
|
+
class Assertion
|
|
7
|
+
def initialize(value, message: nil, failures: nil)
|
|
8
|
+
@value = value
|
|
9
|
+
@message = message
|
|
10
|
+
@failures = failures
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def is_a(type) # rubocop:disable Naming/PredicatePrefix
|
|
14
|
+
check(@value.is_a?(type), "Expected #{@value.inspect} to be a #{type}")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def gte(num)
|
|
18
|
+
check(@value >= num, "Expected #{@value.inspect} to be >= #{num}")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def lte(num)
|
|
22
|
+
check(@value <= num, "Expected #{@value.inspect} to be <= #{num}")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def gt(num)
|
|
26
|
+
check(@value > num, "Expected #{@value.inspect} to be > #{num}")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def lt(num)
|
|
30
|
+
check(@value < num, "Expected #{@value.inspect} to be < #{num}")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def matches(pattern)
|
|
34
|
+
check(pattern.match?(@value.to_s), "Expected #{@value.inspect} to match #{pattern.inspect}")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def not_blank
|
|
38
|
+
check(!@value.nil? && !@value.to_s.strip.empty?, 'Expected value to not be blank')
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def not_empty
|
|
42
|
+
check(@value.respond_to?(:empty?) && !@value.empty?, 'Expected value to not be empty')
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def includes_key(key)
|
|
46
|
+
check(@value.respond_to?(:key?) && @value.key?(key), "Expected #{@value.inspect} to include key #{key.inspect}")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def check(condition, default_message)
|
|
52
|
+
msg = @message || default_message
|
|
53
|
+
if condition
|
|
54
|
+
self
|
|
55
|
+
elsif @failures
|
|
56
|
+
@failures << msg
|
|
57
|
+
self
|
|
58
|
+
else
|
|
59
|
+
raise AssertionError, msg
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module Assert
|
|
5
|
+
# Raised when a single assertion fails.
|
|
6
|
+
class AssertionError < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Raised after a soft assertion block when one or more assertions failed.
|
|
9
|
+
class MultipleFailures < StandardError
|
|
10
|
+
attr_reader :messages
|
|
11
|
+
|
|
12
|
+
def initialize(messages)
|
|
13
|
+
@messages = messages
|
|
14
|
+
super("Multiple assertion failures:\n#{messages.map { |m| " - #{m}" }.join("\n")}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'assert/version'
|
|
4
|
+
require_relative 'assert/errors'
|
|
5
|
+
require_relative 'assert/assertion'
|
|
6
|
+
|
|
7
|
+
module Philiprehberger
|
|
8
|
+
module Assert
|
|
9
|
+
# Create a chainable assertion for the given value.
|
|
10
|
+
#
|
|
11
|
+
# @param value the value to assert against
|
|
12
|
+
# @param message [String, nil] optional custom failure message
|
|
13
|
+
# @return [Assertion]
|
|
14
|
+
def self.that(value, message = nil)
|
|
15
|
+
Assertion.new(value, message: message)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Collect assertion failures instead of raising immediately.
|
|
19
|
+
#
|
|
20
|
+
# @yield [proc] a proc that creates soft assertions
|
|
21
|
+
# @raise [MultipleFailures] if any assertions failed
|
|
22
|
+
def self.soft
|
|
23
|
+
failures = []
|
|
24
|
+
yield ->(value, message = nil) { Assertion.new(value, message: message, failures: failures) }
|
|
25
|
+
raise MultipleFailures, failures unless failures.empty?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Design by Contract precondition check.
|
|
29
|
+
#
|
|
30
|
+
# @param condition [Boolean] the condition to verify
|
|
31
|
+
# @param message [String] failure message
|
|
32
|
+
# @raise [AssertionError] if condition is false
|
|
33
|
+
def self.precondition(condition, message)
|
|
34
|
+
raise AssertionError, message unless condition
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: philiprehberger-assert
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Philip Rehberger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-15 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A lightweight runtime assertion library for Ruby with chainable matchers,
|
|
14
|
+
soft assertions, and Design by Contract preconditions.
|
|
15
|
+
email:
|
|
16
|
+
- me@philiprehberger.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/philiprehberger/assert.rb
|
|
25
|
+
- lib/philiprehberger/assert/assertion.rb
|
|
26
|
+
- lib/philiprehberger/assert/errors.rb
|
|
27
|
+
- lib/philiprehberger/assert/version.rb
|
|
28
|
+
homepage: https://github.com/philiprehberger/rb-assert
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://github.com/philiprehberger/rb-assert
|
|
33
|
+
source_code_uri: https://github.com/philiprehberger/rb-assert
|
|
34
|
+
changelog_uri: https://github.com/philiprehberger/rb-assert/blob/main/CHANGELOG.md
|
|
35
|
+
rubygems_mfa_required: 'true'
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
require_paths:
|
|
39
|
+
- lib
|
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: 3.1.0
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubygems_version: 3.5.22
|
|
52
|
+
signing_key:
|
|
53
|
+
specification_version: 4
|
|
54
|
+
summary: Standalone runtime assertion library with chainable matchers
|
|
55
|
+
test_files: []
|