spectus 5.0.1 → 6.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11f7d2775c0de9fcba11f795927752e4f15aa1de85b63e2d5bd806b82c434bca
4
- data.tar.gz: ebf62bbee1e7d30003995918b368f0905ee2305a3fb79a242f940837d94c793b
3
+ metadata.gz: 92e8a3d87559a6f4c65aba5de5bb1a4430a97643a575fdf6bfae472089c3d735
4
+ data.tar.gz: f6e072160332e5f43b49e90030a2ab285f91ce5a3d62787212a795de41e28919
5
5
  SHA512:
6
- metadata.gz: 978d85aa4e676d8edea4c94644976a06284820b912602cb98808d1a39cf3ef3e2538bee688da3d7401593710de86b03f4b28b05cc0e6555c45741ca0782c78ad
7
- data.tar.gz: 9c51ec6b30adb0590bdd514c5c6c74474508ab146a72dbd7ebddc375a6dedc765a7201e18022514cc5634c5f4fb377d064acac2ae0a8832f491437a614fbd83c
6
+ metadata.gz: 538a2ad991c57d8e5c93abc35985a2e0eef3e1dcee66cc80e73e31a7916f7aa33d1fcecfc6d5ed4ecbd2bb54948d5e1f5df25222a27b5a9492518df16c67513f
7
+ data.tar.gz: 053e1923192a01dd17eb3fd5fe4b34b810d8b2f66891fc74d09d5e4c5d7483604b591f6302a204e2a0d556c4b71c54a4fb1a253818409a897f0070ac7d56c3d4
data/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2014-2024 Cyril Kato
3
+ Copyright (c) 2014-2026 Cyril Kato
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -2,122 +2,94 @@
2
2
 
3
3
  [![Version](https://img.shields.io/github/v/tag/fixrb/spectus?label=Version&logo=github)](https://github.com/fixrb/spectus/tags)
4
4
  [![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://rubydoc.info/github/fixrb/spectus/main)
5
- [![Ruby](https://github.com/fixrb/spectus/workflows/Ruby/badge.svg?branch=main)](https://github.com/fixrb/spectus/actions?query=workflow%3Aruby+branch%3Amain)
6
- [![RuboCop](https://github.com/fixrb/spectus/workflows/RuboCop/badge.svg?branch=main)](https://github.com/fixrb/spectus/actions?query=workflow%3Arubocop+branch%3Amain)
7
5
  [![License](https://img.shields.io/github/license/fixrb/spectus?label=License&logo=github)](https://github.com/fixrb/spectus/raw/main/LICENSE.md)
8
6
 
9
- > A Ruby library for defining expectations with precision, using [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt) compliance levels. 🚥
7
+ Spectus expresses expectations with the requirement levels of [RFC 2119](https://www.rfc-editor.org/rfc/rfc2119): what a value MUST do, SHOULD do, or MAY do. An expectation is a level, a negation and a matcher; called with a block, it gives a result that says how it went, and never raises for an expectation that is not met.
10
8
 
11
9
  ## Installation
12
10
 
13
- Add this line to your application's Gemfile:
14
-
15
11
  ```ruby
12
+ # Gemfile
16
13
  gem "spectus"
14
+ gem "matchi" # the matchers of Fix, or any object with match? and to_s
17
15
  ```
18
16
 
19
- And then execute:
20
-
21
- ```sh
22
- bundle install
23
- ```
24
-
25
- Or install it yourself as:
26
-
27
- ```sh
28
- gem install spectus
29
- ```
17
+ Spectus requires Ruby 3.3 or later, and depends on [Defi](https://github.com/fixrb/defi).
30
18
 
31
19
  ## Usage
32
20
 
33
- The __Spectus__ library is basically a module defining methods that can be used to qualify expectations in specifications.
34
-
35
- To make __Spectus__ available:
36
-
37
21
  ```ruby
38
22
  require "spectus"
39
- ```
40
-
41
- For convenience, we will also instantiate some matchers from the [Matchi library](https://github.com/fixrb/matchi):
42
-
43
- ```ruby
44
23
  require "matchi"
45
- ```
46
24
 
47
- All examples here assume that this has been done.
25
+ requirement = Spectus.must(Matchi::Eq.new(42))
26
+ result = requirement.call { 6 * 7 }
27
+ result.passed? # => true
28
+ result.outcome # => :success
29
+ result.to_s # => "Success: expected 42 to eq 42"
48
30
 
49
- ### Absolute Requirement
50
-
51
- There is exactly one bat:
52
-
53
- ```ruby
54
- definition = Spectus.must Matchi::Be.new(1)
55
- definition.call { "🦇".size }
56
- # => Expresenter::Pass(actual: 1, definition: "be 1", error: nil, expected: 1, got: true, negate: false, level: :MUST)
31
+ Spectus.must_not(Matchi::Eq.new(42)).call { 42 }.to_s # => "Failure: expected 42 not to eq 42"
32
+ Spectus.should(Matchi::Be.new(3)).call { 2 }.to_s # => "Warning: expected 2 to be 3"
33
+ Spectus.may(Matchi::Predicate.new(:be_foo)).call { 42 }.outcome # => :info
34
+ Spectus.must(Matchi::Eq.new(42)).call { 1 / 0 }.to_s # => "Error: ZeroDivisionError: divided by 0"
57
35
  ```
58
36
 
59
- The test is passed.
37
+ `Spectus.must`, `must_not`, `should`, `should_not` and `may` build a `Spectus::Requirement`; `may` has no negative form, the RFC having none. A requirement is `Data`: its `level` (`:MUST`, `:SHOULD` or `:MAY`), its `negate` and its `matcher`, which is any object with a `match?` method that takes the block and answers `true` or `false`, and a `to_s` that describes it. The matcher is neither copied nor frozen. The five functions are the normal entry; `Spectus::Requirement.new(level:, negate:, matcher:)` is the general one.
60
38
 
61
- ### Absolute Prohibition
39
+ ## Outcomes
62
40
 
63
- Truth and lies:
41
+ Two questions tell the levels apart. Does the method exist? MUST and SHOULD require it, MAY does not. Does it do what is specified? MUST and MAY require it, SHOULD recommends it. Hence five outcomes:
64
42
 
65
- ```ruby
66
- definition = Spectus.must_not Matchi::Be.new(true)
67
- definition.call { false }
68
- # => Expresenter::Pass(actual: false, definition: "be true", error: nil, expected: true, got: true, negate: true, level: :MUST)
69
- ```
43
+ | Level | Match is true | Match is false | Missing method | Other exception |
44
+ |--------|---------------|----------------|----------------|-----------------|
45
+ | MUST | success | failure | error | error |
46
+ | SHOULD | success | warning | error | error |
47
+ | MAY | success | failure | info | error |
70
48
 
71
- ### Recommended
49
+ A negation swaps the first two columns. A success, a warning and an info pass (`passed?` is true); a failure and an error do not.
72
50
 
73
- A well-known joke. The addition of `0.1` and `0.2` is deadly precise:
51
+ A missing method is a `NoMethodError` raised by `match?` whose receiver is the very value the block returned (the last one, when the matcher calls the block more than once): `Spectus.may(Matchi::Predicate.new(:be_blank)).call { "" }` is an info when `String#blank?` does not exist, a success or a failure when it does. A `NoMethodError` raised by the block itself, or on another object, is an error. Spectus does not know which method the matcher meant to test, so the rule has its limits: a private method counts as missing, as does a method that exists but calls a missing one on `self`, or a missing method the matcher's own code calls on the value.
74
52
 
75
- ```ruby
76
- definition = Spectus.should Matchi::Be.new(0.3)
77
- definition.call { 0.1 + 0.2 }
78
- # => Expresenter::Pass(actual: 0.30000000000000004, definition: "be 0.3", error: nil, expected: 0.3, got: false, negate: false, level: :SHOULD)
79
- ```
53
+ `match?` must answer `true` or `false`: any other answer is an error, as if `match?` had raised a `TypeError`.
80
54
 
81
- ### Not Recommended
55
+ ## Results
82
56
 
83
- This should not be wrong:
57
+ A `Spectus::Result` is plain data, frozen: symbols, booleans and frozen strings in UTF-8. It can be marshalled, shared between ractors and compared, and nothing in it runs user code again.
84
58
 
85
- ```ruby
86
- definition = Spectus.should_not Matchi::Match.new("123456")
87
-
88
- definition.call do
89
- require "securerandom"
90
-
91
- SecureRandom.hex(3)
92
- end
93
- # => Expresenter::Pass(actual: "bb5716", definition: "match \"123456\"", error: nil, expected: "123456", got: true, negate: true, level: :SHOULD)
94
- ```
59
+ | Member | Content |
60
+ |-----------------|---------|
61
+ | `level` | `:MUST`, `:SHOULD` or `:MAY` |
62
+ | `negate` | `true` or `false` |
63
+ | `outcome` | `:success`, `:warning`, `:info`, `:failure` or `:error` |
64
+ | `description` | the matcher, as its `to_s` wrote it: `eq 42` |
65
+ | `actual` | the value the block returned, as its `inspect` wrote it, or the exception it raised, whether the matcher rescued it or not; `nil` when the matcher did not call the block |
66
+ | `error_class` | the class of the exception that came out of `match?`, or `TypeError` for an answer that was not `true` or `false`; `nil` otherwise |
67
+ | `error_message` | its message |
68
+ | `backtrace` | its backtrace, when it has one, library frames included: a runner filters by path |
95
69
 
96
- In any case, as long as there are no exceptions, the test passes.
70
+ `summary` writes `expected ACTUAL to DESCRIPTION`, with `not` for a negation and without the actual value when the block was not called, or `CLASS: MESSAGE` for an exception. `to_s` puts the outcome first: `Success: expected 42 to eq 42`, `Info: NoMethodError: undefined method 'blank?' for an instance of String` (as Ruby 3.4 writes it). No letter, emoji or colour: a runner derives them from `outcome`.
97
71
 
98
- ### Optional
72
+ The texts are read defensively: an `inspect` or a `message` that raises or does not answer a `String` gives a fixed text (`an object of Point`, `a message that cannot be read`), a class without a name is `an anonymous exception`, a `backtrace` that raises or is not an `Array` of `String` is `nil`, and bytes that are not valid UTF-8 become U+FFFD. A result carries the level and the negation of its requirement, so that a runner holding results alone can still write the requirement they answer; a runner may also build a `Spectus::Result` of its own for what never reached `call`.
99
73
 
100
- An empty array is blank, right?
74
+ ## Exceptions
101
75
 
102
- ```ruby
103
- definition = Spectus.may Matchi::Be.new(true)
104
- definition.call { [].blank? }
105
- # => Expresenter::Pass(actual: nil, definition: "be true", error: #<NoMethodError: undefined method `blank?' for []:Array>, expected: true, got: nil, negate: false, level: :MAY)
106
- ```
76
+ `call` raises `ArgumentError` without a block and `TypeError` when the `to_s` of the matcher is not a `String`; the matcher is test code, trusted, and what its `to_s` raises goes through. The block and `match?` run under the policy of Defi: the exceptions a method raises on its own, a `StandardError`, a `ScriptError` (such as `NotImplementedError`), a `SystemStackError` or the `SystemExit` of an `exit`, end in the result (as an error, or as an info for a MAY), while an `Interrupt`, a `SignalException`, a `NoMemoryError`, the exception a `Timeout` uses to end its block and any other subclass of `Exception` go through, whether the block or the matcher raised them. Spectus never sees what a matcher rescues itself: in matchi 5.0, `Matchi::RaiseException` rescues `Exception`, so an `exit` inside its block is not an error.
107
77
 
108
- My bad! ActiveSupport was not imported. 🤦‍♂️
78
+ The block runs inside `match?`, as many times as the matcher calls it, and without arguments: a lambda that expects some raises `ArgumentError`, which ends in the result. `actual` is the block's last value. The exception it raises is raised again inside the matcher, the same object with its own cause, as Defi does it.
109
79
 
110
- Anyways, the test passes because the exception produced is `NoMethodError`, meaning that the functionality is not implemented.
80
+ ## Changes in 6.0
111
81
 
112
- ## Contact
82
+ Compared with 5.0.2:
113
83
 
114
- * Home page: https://github.com/fixrb/spectus
115
- * Bugs/issues: https://github.com/fixrb/spectus/issues
116
- * Blog post: https://cyrilllllll.medium.com/a-spectus-tutorial-expectations-with-rfc-2119-compliance-1fc769861c1
84
+ - `call` returns a `Spectus::Result` and never raises for a failed expectation, where 5.0.2 returned an `Expresenter::Pass` or raised an `Expresenter::Fail`: read `passed?` or `outcome`. An `expect { requirement.call { … } }.not_to raise_error` written for 5.0.2 passes whatever happens.
85
+ - `Spectus::Requirement` is one `Data` class with a `level`, in place of `Requirement::Required`, `Recommended` and `Optional`; `Spectus::Requirement.new(level:, negate:, matcher:)` builds one, with an `ArgumentError` for a MAY that is negated, a level other than the three symbols, a negation that is not `true` or `false`, or a matcher without `match?`. `call` without a block raises `ArgumentError`, where 5.0.2 passed.
86
+ - `match?` must answer `true` or `false`: 5.0.2 passed any truthy value and, when negated, `nil`. A missing method is a `NoMethodError` on the value the block returned: 5.0.2 counted every `NoMethodError` as one. A `Timeout` and an `Interrupt` go through, where 5.0.2 swallowed them; a `SystemExit` is an error, as it was.
87
+ - The result exposes `description`, `actual`, `error_class`, `error_message` and `backtrace` as strings, `summary` and `to_s` without a trailing period, the outcome as its title; `char`, `emoji`, `colored_char`, `colored_string`, `to_sym`, `success?`, `warning?`, `info?`, `failure?` and `error?` are gone, as are the dependencies on test_tube and expresenter.
88
+ - Ruby 3.3 or later; tested on 3.3, 3.4, 4.0 and head.
117
89
 
118
90
  ## Versioning
119
91
 
120
- __Spectus__ follows [Semantic Versioning 2.0](https://semver.org/).
92
+ Spectus follows [Semantic Versioning 2.0](https://semver.org/).
121
93
 
122
94
  ## License
123
95
 
@@ -1,13 +1,121 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "defi"
4
+
5
+ require_relative "result"
6
+ require_relative "text"
7
+
3
8
  module Spectus
4
- # Namespace for the results.
9
+ # The levels of RFC 2119, each with the outcome of a failed match. The
10
+ # keys are compared by identity: a level is one of these three symbols,
11
+ # not an object that hashes like one.
12
+ LEVELS = { MUST: :failure, SHOULD: :warning, MAY: :failure }.compare_by_identity.freeze
13
+
14
+ # The outcome of a missing method, level by level.
15
+ MISSING = { MUST: :error, SHOULD: :error, MAY: :info }.freeze
16
+
17
+ private_constant :LEVELS
18
+ private_constant :MISSING
19
+
20
+ # An expectation: a level, a negation and a matcher, frozen. The matcher
21
+ # is test code, trusted: it is kept as it is, its `match?` is called with
22
+ # the block, and its `to_s` describes it.
5
23
  #
6
- # @api private
7
- module Requirement
24
+ # @!attribute [r] level
25
+ # @return [Symbol] `:MUST`, `:SHOULD` or `:MAY`
26
+ # @!attribute [r] negate
27
+ # @return [Boolean] whether the match is expected to fail
28
+ # @!attribute [r] matcher
29
+ # @return [#match?] the matcher
30
+ Requirement = ::Data.define(:level, :negate, :matcher) do
31
+ # @param level [Symbol] `:MUST`, `:SHOULD` or `:MAY`
32
+ # @param negate [Boolean] whether the match is expected to fail; a MAY
33
+ # has no negative form
34
+ # @param matcher [#match?] the matcher
35
+ # @raise [ArgumentError] for any other level or negation, or a matcher
36
+ # that does not respond to `match?` (its own `respond_to?` is not asked)
37
+ def initialize(level:, negate:, matcher:)
38
+ raise ::ArgumentError, "the level must be :MUST, :SHOULD or :MAY" unless LEVELS.key?(level)
39
+ raise ::ArgumentError, "negate must be true or false" unless true.equal?(negate) || false.equal?(negate)
40
+ raise ::ArgumentError, "a MAY has no negative form" if negate && level.equal?(:MAY)
41
+ unless ::Kernel.instance_method(:respond_to?).bind_call(matcher, :match?)
42
+ raise ::ArgumentError, "the matcher must respond to match?"
43
+ end
44
+
45
+ super
46
+ end
47
+
48
+ # Runs the matcher with the block, and says how it went. Raises nothing
49
+ # of its own: the exceptions a method raises on its own (those Defi
50
+ # keeps, a `StandardError`, a `ScriptError`, a `SystemStackError` or a
51
+ # `SystemExit`) end in the result, while an `Interrupt`, a signal, a
52
+ # `NoMemoryError` and the end of a `Timeout` go through.
53
+ #
54
+ # @yieldreturn [Object] the actual value
55
+ # @return [Spectus::Result]
56
+ # @raise [ArgumentError] without a block
57
+ # @raise [TypeError] when the `to_s` of the matcher is not a String
58
+ def call(&block)
59
+ raise ::ArgumentError, "a block must be given" unless block
60
+
61
+ description = matcher.to_s
62
+ raise ::TypeError, "the to_s of the matcher must be a String" unless ::String === description
63
+
64
+ description = Text.utf8(description)
65
+ produced = nil
66
+ answer = ::Defi::Challenge.new(:match?) do
67
+ produced = ::Defi::Challenge.new(:call).to(block)
68
+ produced.call
69
+ end.to(matcher)
70
+ result(description, produced, answer)
71
+ end
72
+
73
+ private
74
+
75
+ # The result, from what the block produced (nil when the matcher did not
76
+ # call it) and what `match?` answered or raised.
77
+ def result(description, produced, answer)
78
+ actual = produced && Text.object(produced.object)
79
+ error_class, error_message, backtrace = error(answer)
80
+ Result.new(level:, negate:, outcome: outcome(produced, answer), description:, actual:, error_class:,
81
+ error_message:, backtrace:)
82
+ end
83
+
84
+ # The outcome: a method missing on the value the block produced is read
85
+ # by the level; an answer of true or false is read with the negation, an
86
+ # answer equal to the negation being a failed match; anything else is an
87
+ # error.
88
+ def outcome(produced, answer)
89
+ return MISSING.fetch(level) if missing_method?(produced, answer)
90
+ return :error unless verdict?(answer)
91
+
92
+ negate.equal?(answer.object) ? LEVELS.fetch(level) : :success
93
+ end
94
+
95
+ # Whether `match?` answered true or false, which no exception is.
96
+ def verdict?(answer)
97
+ true.equal?(answer.object) || false.equal?(answer.object)
98
+ end
99
+
100
+ # Whether `match?` raised a NoMethodError whose receiver is the value the
101
+ # block produced, compared by identity as BasicObject does. A
102
+ # NoMethodError built by hand may have no receiver.
103
+ def missing_method?(produced, answer)
104
+ return false unless produced && !produced.raised? && answer.raised? && ::NoMethodError === answer.object
105
+
106
+ receiver = ::NameError.instance_method(:receiver).bind_call(answer.object)
107
+ ::BasicObject.instance_method(:equal?).bind_call(receiver, produced.object)
108
+ rescue ::ArgumentError
109
+ false
110
+ end
111
+
112
+ # The texts of the exception, when there is one: the one `match?` raised,
113
+ # or a TypeError for an answer that is not true or false.
114
+ def error(answer)
115
+ return Text.exception(answer.object) if answer.raised?
116
+ return [nil, nil, nil] if verdict?(answer)
117
+
118
+ ["TypeError", "match? answered #{Text.object(answer.object)}, not true or false".freeze, nil]
119
+ end
8
120
  end
9
121
  end
10
-
11
- require_relative File.join("requirement", "required")
12
- require_relative File.join("requirement", "recommended")
13
- require_relative File.join("requirement", "optional")
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spectus
4
+ # How an expectation went: plain data, frozen, made of symbols, booleans
5
+ # and frozen UTF-8 strings. A result can be marshalled, shared between
6
+ # ractors, compared and printed without running any user code. It carries
7
+ # the level and the negation of its requirement, so that a runner holding
8
+ # results alone can still write the requirement they answer.
9
+ #
10
+ # @!attribute [r] level
11
+ # @return [Symbol] `:MUST`, `:SHOULD` or `:MAY`
12
+ # @!attribute [r] negate
13
+ # @return [Boolean] whether the match was expected to fail
14
+ # @!attribute [r] outcome
15
+ # @return [Symbol] `:success`, `:warning`, `:info`, `:failure` or `:error`
16
+ # @!attribute [r] description
17
+ # @return [String] the matcher, as its `to_s` wrote it
18
+ # @!attribute [r] actual
19
+ # @return [String, nil] the value the block produced, as its `inspect`
20
+ # wrote it, or the exception it raised (which is also the error when
21
+ # the matcher let it through); `nil` when the matcher did not call the
22
+ # block
23
+ # @!attribute [r] error_class
24
+ # @return [String, nil] the class of the exception that came out of
25
+ # `match?`, or `TypeError` for an answer that was not true or false
26
+ # @!attribute [r] error_message
27
+ # @return [String, nil] its message
28
+ # @!attribute [r] backtrace
29
+ # @return [Array<String>, nil] its backtrace, when it has one
30
+ Result = ::Data.define(:level, :negate, :outcome, :description, :actual, :error_class, :error_message, :backtrace) do
31
+ # @return [Boolean] whether the expectation is met: a success, a warning
32
+ # or an info
33
+ def passed?
34
+ %i[success warning info].include?(outcome)
35
+ end
36
+
37
+ # @return [String] what happened: `expected ACTUAL to DESCRIPTION`, with
38
+ # `not` when negated and without the actual value when the block was
39
+ # not called, or `CLASS: MESSAGE` for an exception
40
+ def summary
41
+ return "#{error_class}: #{error_message}" if error_class
42
+
43
+ ["expected", actual, ("not" if negate), "to", description].compact.join(" ")
44
+ end
45
+
46
+ # @return [String] the outcome as a title, then the summary:
47
+ # `Success: expected 42 to eq 42`
48
+ def to_s
49
+ "#{outcome.to_s.capitalize}: #{summary}"
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "defi"
4
+
5
+ module Spectus
6
+ # Texts read from user code, which may raise, return anything or hold
7
+ # bytes in any encoding: each one comes back as a frozen String in UTF-8,
8
+ # or as a stated fallback.
9
+ #
10
+ # @api private
11
+ module Text
12
+ # @param object [Object] any object, a BasicObject included
13
+ # @return [String] its inspect, or `an object of CLASS` when the inspect
14
+ # raises or is not a String (`an anonymous class` without a name)
15
+ def self.object(object)
16
+ text = object.inspect
17
+ ::String === text ? utf8(text) : fallback(object)
18
+ rescue *::Defi::Value::RAISED
19
+ fallback(object)
20
+ end
21
+
22
+ # @param exception [Exception]
23
+ # @return [Array(String, String, Array<String>)] the name of its class
24
+ # (`an anonymous exception` without a name), its message (`a message
25
+ # that cannot be read` when it raises or is not a String) and its
26
+ # backtrace, `nil` in place of the latter when it has none, it raises
27
+ # or it is not an Array of Strings
28
+ def self.exception(exception)
29
+ [class_name(exception) || "an anonymous exception", message(exception), backtrace(exception)]
30
+ end
31
+
32
+ # The text of an object whose inspect cannot be read.
33
+ def self.fallback(object)
34
+ "an object of #{class_name(object) || "an anonymous class"}".freeze
35
+ end
36
+
37
+ # The name of the class of an object, read as Kernel and Module do, or
38
+ # nil for an anonymous class.
39
+ def self.class_name(object)
40
+ klass = ::Kernel.instance_method(:class).bind_call(object)
41
+ name = ::Module.instance_method(:name).bind_call(klass)
42
+ name && utf8(name)
43
+ end
44
+
45
+ # The message of an exception, which user code may redefine.
46
+ def self.message(exception)
47
+ text = exception.message
48
+ ::String === text ? utf8(text) : "a message that cannot be read"
49
+ rescue *::Defi::Value::RAISED
50
+ "a message that cannot be read"
51
+ end
52
+
53
+ # The backtrace of an exception, which user code may redefine too: a
54
+ # plain copy of the Array is asked, not a subclass.
55
+ def self.backtrace(exception)
56
+ lines = exception.backtrace
57
+ return unless ::Array === lines
58
+
59
+ lines = ::Array.new(lines)
60
+ return unless lines.all?(::String)
61
+
62
+ lines.map { |line| utf8(line) }.freeze
63
+ rescue *::Defi::Value::RAISED
64
+ nil
65
+ end
66
+
67
+ # @param text [String] a String, in any encoding, of any class
68
+ # @return [String] a plain copy, valid UTF-8, frozen: invalid bytes and
69
+ # characters UTF-8 cannot hold become U+FFFD, and a String in an
70
+ # encoding Ruby cannot convert is read as UTF-8 bytes
71
+ def self.utf8(text)
72
+ copy = ::String.new(text)
73
+ begin
74
+ copy.encode(::Encoding::UTF_8, invalid: :replace, undef: :replace)
75
+ rescue ::EncodingError
76
+ copy.force_encoding(::Encoding::UTF_8).scrub
77
+ end.freeze
78
+ end
79
+
80
+ private_class_method :fallback, :class_name, :message, :backtrace
81
+ end
82
+
83
+ private_constant :Text
84
+ end
data/lib/spectus.rb CHANGED
@@ -1,107 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative File.join("spectus", "requirement")
3
+ require_relative "spectus/requirement"
4
4
 
5
- # Namespace for the Spectus library.
5
+ # Expectations with the requirement levels of RFC 2119. A requirement is a
6
+ # level (MUST, SHOULD or MAY), a negation and a matcher; called with a block,
7
+ # it gives a result, a value that says how the expectation went, and never
8
+ # raises for an expectation that is not met.
6
9
  #
7
- # This module defines methods that can be used to qualify expectations in
8
- # specifications.
10
+ # @example
11
+ # require "matchi"
12
+ #
13
+ # result = Spectus.must(Matchi::Eq.new(42)).call { 6 * 7 }
14
+ # result.passed? # => true
15
+ # result.to_s # => "Success: expected 42 to eq 42"
9
16
  module Spectus
10
- # This method mean that the definition is an absolute requirement of the
11
- # specification.
12
- #
13
- # @example An absolute requirement definition
14
- # require "spectus"
15
- # require "matchi/eq"
16
- #
17
- # Spectus.must Matchi::Eq.new("FOO")
18
- # # => #<MUST Matchi::Eq("FOO") negate=false>
19
- #
20
- # @param matcher [#match?] The matcher.
21
- #
22
- # @return [Requirement::Required] An absolute requirement level instance.
23
- #
24
- # @api public
17
+ # @param matcher [#match?] the matcher
18
+ # @return [Spectus::Requirement] an absolute requirement
25
19
  def self.must(matcher)
26
- Requirement::Required.new(negate: false, matcher:)
20
+ Requirement.new(level: :MUST, negate: false, matcher:)
27
21
  end
28
22
 
29
- # This method mean that the definition is an absolute prohibition of the specification.
30
- #
31
- # @example An absolute prohibition definition
32
- # require "spectus"
33
- # require "matchi/be"
34
- #
35
- # Spectus.must_not Matchi::Be.new(42)
36
- # # => #<MUST Matchi::Be(42) negate=true>
37
- #
38
- # @param matcher [#match?] The matcher.
39
- #
40
- # @return [Requirement::Required] An absolute prohibition level instance.
23
+ # @param matcher [#match?] the matcher
24
+ # @return [Spectus::Requirement] an absolute prohibition
41
25
  def self.must_not(matcher)
42
- Requirement::Required.new(negate: true, matcher:)
26
+ Requirement.new(level: :MUST, negate: true, matcher:)
43
27
  end
44
28
 
45
- # This method mean that there may exist valid reasons in particular
46
- # circumstances to ignore a particular item, but the full implications must be
47
- # understood and carefully weighed before choosing a different course.
48
- #
49
- # @example A recommended definition
50
- # require "spectus"
51
- # require "matchi/be"
52
- #
53
- # Spectus.should Matchi::Be.new(true)
54
- # # => #<SHOULD Matchi::Be(true) negate=false>
55
- #
56
- # @param matcher [#match?] The matcher.
57
- #
58
- # @return [Requirement::Recommended] A recommended requirement level instance.
29
+ # @param matcher [#match?] the matcher
30
+ # @return [Spectus::Requirement] a recommendation
59
31
  def self.should(matcher)
60
- Requirement::Recommended.new(negate: false, matcher:)
32
+ Requirement.new(level: :SHOULD, negate: false, matcher:)
61
33
  end
62
34
 
63
- # This method mean that there may exist valid reasons in particular
64
- # circumstances when the particular behavior is acceptable or even useful, but
65
- # the full implications should be understood and the case carefully weighed
66
- # before implementing any behavior described with this label.
67
- #
68
- # @example A not recommended definition
69
- # require "spectus"
70
- # require "matchi/raise_exception"
71
- #
72
- # Spectus.should_not Matchi::RaiseException.new(NoMethodError)
73
- # # => #<SHOULD Matchi::RaiseException(NoMethodError) negate=true>
74
- #
75
- # @param matcher [#match?] The matcher.
76
- #
77
- # @return [Requirement::Recommended] A not recommended requirement level
78
- # instance.
35
+ # @param matcher [#match?] the matcher
36
+ # @return [Spectus::Requirement] a negated recommendation
79
37
  def self.should_not(matcher)
80
- Requirement::Recommended.new(negate: true, matcher:)
38
+ Requirement.new(level: :SHOULD, negate: true, matcher:)
81
39
  end
82
40
 
83
- # This method mean that an item is truly optional.
84
- # One vendor may choose to include the item because a particular marketplace
85
- # requires it or because the vendor feels that it enhances the product while
86
- # another vendor may omit the same item. An implementation which does not
87
- # include a particular option must be prepared to interoperate with another
88
- # implementation which does include the option, though perhaps with reduced
89
- # functionality. In the same vein an implementation which does include a
90
- # particular option must be prepared to interoperate with another
91
- # implementation which does not include the option (except, of course, for the
92
- # feature the option provides).
93
- #
94
- # @example An optional definition
95
- # require "spectus"
96
- # require "matchi/match"
97
- #
98
- # Spectus.may Matchi::Match.new(/^foo$/)
99
- # # => #<MAY Matchi::Match(/^foo$/) negate=false>
100
- #
101
- # @param matcher [#match?] The matcher.
102
- #
103
- # @return [Requirement::Optional] An optional requirement level instance.
41
+ # @param matcher [#match?] the matcher
42
+ # @return [Spectus::Requirement] an option, which has no negative form
104
43
  def self.may(matcher)
105
- Requirement::Optional.new(negate: false, matcher:)
44
+ Requirement.new(level: :MAY, negate: false, matcher:)
106
45
  end
107
46
  end
metadata CHANGED
@@ -1,30 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spectus
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cyril Kato
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2024-12-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
- name: expresenter
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: 1.5.0
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: 1.5.0
26
- - !ruby/object:Gem::Dependency
27
- name: matchi
13
+ name: defi
28
14
  requirement: !ruby/object:Gem::Requirement
29
15
  requirements:
30
16
  - - "~>"
@@ -37,21 +23,8 @@ dependencies:
37
23
  - - "~>"
38
24
  - !ruby/object:Gem::Version
39
25
  version: '4.0'
40
- - !ruby/object:Gem::Dependency
41
- name: test_tube
42
- requirement: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: 4.0.0
47
- type: :runtime
48
- prerelease: false
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: 4.0.0
54
- description: "Expectation library with RFC 2119's requirement levels \U0001F6A5"
26
+ description: An expectation is a level (MUST, SHOULD or MAY), a negation and a matcher;
27
+ called with a block, it gives a result that says how it went, without raising.
55
28
  email: contact@cyril.email
56
29
  executables: []
57
30
  extensions: []
@@ -61,15 +34,17 @@ files:
61
34
  - README.md
62
35
  - lib/spectus.rb
63
36
  - lib/spectus/requirement.rb
64
- - lib/spectus/requirement/base.rb
65
- - lib/spectus/requirement/optional.rb
66
- - lib/spectus/requirement/recommended.rb
67
- - lib/spectus/requirement/required.rb
68
- homepage: https://github.com/fixrb/spectus
37
+ - lib/spectus/result.rb
38
+ - lib/spectus/text.rb
39
+ homepage: https://fixrb.dev/gems/spectus/
69
40
  licenses:
70
41
  - MIT
71
42
  metadata:
43
+ bug_tracker_uri: https://github.com/fixrb/spectus/issues
44
+ changelog_uri: https://github.com/fixrb/spectus#changes-in-60
45
+ documentation_uri: https://rubydoc.info/gems/spectus
72
46
  rubygems_mfa_required: 'true'
47
+ source_code_uri: https://github.com/fixrb/spectus
73
48
  rdoc_options: []
74
49
  require_paths:
75
50
  - lib
@@ -77,14 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
52
  requirements:
78
53
  - - ">="
79
54
  - !ruby/object:Gem::Version
80
- version: 3.2.0
55
+ version: 3.3.0
81
56
  required_rubygems_version: !ruby/object:Gem::Requirement
82
57
  requirements:
83
58
  - - ">="
84
59
  - !ruby/object:Gem::Version
85
60
  version: '0'
86
61
  requirements: []
87
- rubygems_version: 3.6.2
62
+ rubygems_version: 4.0.23
88
63
  specification_version: 4
89
- summary: "Expectation library with RFC 2119's requirement levels \U0001F6A5"
64
+ summary: Expectations with the requirement levels of RFC 2119
90
65
  test_files: []
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "expresenter"
4
- require "test_tube"
5
-
6
- module Spectus
7
- # Namespace for the requirement levels.
8
- module Requirement
9
- # Requirement level's base class.
10
- class Base
11
- # Initialize the requirement level class.
12
- #
13
- # @param matcher [#match?] The matcher.
14
- # @param negate [Boolean] Invert the matcher or not.
15
- def initialize(matcher:, negate:)
16
- @matcher = matcher
17
- @negate = negate
18
- end
19
-
20
- # Test result.
21
- #
22
- # @raise [::Expresenter::Fail] A failed spec exception.
23
- # @return [::Expresenter::Pass] A passed spec instance.
24
- #
25
- # @see https://github.com/fixrb/expresenter
26
- #
27
- # @api public
28
- def call(&)
29
- test = ::TestTube.invoke(matcher: @matcher, negate: @negate, &)
30
-
31
- ::Expresenter.call(passed?(test)).with(
32
- actual: test.actual,
33
- definition: @matcher.to_s,
34
- error: test.error,
35
- got: test.got,
36
- level: self.class.level,
37
- negate: @negate
38
- )
39
- end
40
-
41
- # :nocov:
42
-
43
- # A string containing a human-readable representation of the definition.
44
- #
45
- # @example The human-readable representation of an absolute requirement.
46
- # require "spectus"
47
- # require "matchi/be"
48
- #
49
- # definition = Spectus.must Matchi::Be.new(1)
50
- # definition.inspect
51
- # # => "#<MUST Matchi::Be(1) negate=false>"
52
- #
53
- # @return [String] The human-readable representation of the definition.
54
- #
55
- # @api public
56
- def inspect
57
- "#<#{self.class.level} #{@matcher.inspect} negate=#{@negate}>"
58
- end
59
-
60
- # :nocov:
61
-
62
- private
63
-
64
- # Code experiment result.
65
- #
66
- # @param test [::TestTube::Base] The state of the experiment.
67
- #
68
- # @see https://github.com/fixrb/test_tube
69
- #
70
- # @return [Boolean] The result of the test (passed or failed).
71
- def passed?(test)
72
- test.got.equal?(true)
73
- end
74
- end
75
- end
76
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Spectus
6
- module Requirement
7
- # Optional requirement level.
8
- class Optional < Base
9
- # Key word for use in RFCs to indicate requirement levels.
10
- #
11
- # @return [Symbol] The requirement level.
12
- def self.level
13
- :MAY
14
- end
15
-
16
- private
17
-
18
- # Code experiment result.
19
- #
20
- # @param (see Base#passed?)
21
- #
22
- # @return (see Base#passed?)
23
- def passed?(test)
24
- super || test.error.is_a?(::NoMethodError)
25
- end
26
- end
27
- end
28
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Spectus
6
- module Requirement
7
- # Recommended and not recommended requirement levels.
8
- class Recommended < Base
9
- # Key word for use in RFCs to indicate requirement levels.
10
- #
11
- # @return [Symbol] The requirement level.
12
- def self.level
13
- :SHOULD
14
- end
15
-
16
- private
17
-
18
- # Code experiment result.
19
- #
20
- # @param (see Base#passed?)
21
- #
22
- # @return (see Base#passed?)
23
- def passed?(test)
24
- super || test.error.nil?
25
- end
26
- end
27
- end
28
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Spectus
6
- module Requirement
7
- # Absolute requirement and absolute prohibition levels.
8
- class Required < Base
9
- # Key word for use in RFCs to indicate requirement levels.
10
- #
11
- # @return [Symbol] The requirement level.
12
- def self.level
13
- :MUST
14
- end
15
- end
16
- end
17
- end