rspec-fix 1.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +21 -0
  3. data/README.md +104 -0
  4. data/lib/rspec/fix.rb +80 -0
  5. metadata +83 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dc09902badd1650af837bb0a84c1f15c9e1a225390068e9a03646faf94c8fe19
4
+ data.tar.gz: '08c5a956d554fdf1a3ac3d93ced1e078fd7d9d02103beab032f70392d08a7b66'
5
+ SHA512:
6
+ metadata.gz: 575277c4c0917f7c9b6c7976e341661830e2c7606d2cb0b2a38819f09a54f88b6276e38413a5a4410cd7644ae98580a4feb9d1f4347edf7b4f0e2c56e88f52e1
7
+ data.tar.gz: b8f93fdc3366f63ba9b3334d50341ead29f95a483662a09c7c74def1dd54c7a8fb3f2e586ed2d9bc22235e160a9a0dc314571b96a7973bdbc61ed3f857bd2b0a
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2026 Cyril Kato
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # RSpec::Fix
2
+
3
+ [![Version](https://img.shields.io/github/v/tag/fixrb/rspec-fix?label=Version&logo=github)](https://github.com/fixrb/rspec-fix/tags)
4
+ [![Yard documentation](https://img.shields.io/badge/Yard-documentation-blue.svg?logo=github)](https://gemdocs.org/gems/rspec-fix/latest/)
5
+ [![License](https://img.shields.io/github/license/fixrb/rspec-fix?label=License&logo=github)](https://github.com/fixrb/rspec-fix/raw/main/LICENSE.md)
6
+
7
+ RSpec::Fix brings the specifications of [Fix](https://github.com/fixrb/fix) to [RSpec](https://rspec.info/), as a matcher: `expect(value).to fix(:Name)` is met when the value meets every expectation of the specification, and its message tells which it does not. It is the way to add a specification of Fix to an RSpec suite, and to keep the suite.
8
+
9
+ [fixrb.dev](https://fixrb.dev/gems/rspec-fix/) presents the gem.
10
+
11
+ ## Installation
12
+
13
+ ```ruby
14
+ # Gemfile
15
+ gem "rspec-fix"
16
+ ```
17
+
18
+ RSpec::Fix requires Ruby 3.3 or later, and depends on [Fix](https://github.com/fixrb/fix) and on rspec-expectations, 3.13 or later.
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ # duck_spec.rb
24
+ require "rspec/fix"
25
+
26
+ Fix :Duck do
27
+ on :walk do
28
+ it MUST eq "Klop klop!"
29
+ end
30
+
31
+ on :swim do
32
+ it SHOULD eq "Swoosh..."
33
+ end
34
+
35
+ on :speak do
36
+ it MAY eq "Quaaack!"
37
+ end
38
+ end
39
+
40
+ class Duck
41
+ def walk = "Klop klop!"
42
+ def swim = "Swoosh..."
43
+ def speak = "Quaaack!"
44
+ end
45
+
46
+ class Robot
47
+ def walk = "Klop klop!"
48
+ def swim = "Glug glug..."
49
+ end
50
+
51
+ class RubberDuck
52
+ def swim = "Swoosh..."
53
+ def speak = "Squeak!"
54
+ end
55
+
56
+ RSpec.configure { |config| config.include RSpec::Fix }
57
+
58
+ RSpec.describe Duck do
59
+ it { is_expected.to fix(:Duck) }
60
+ end
61
+
62
+ RSpec.describe Robot do
63
+ it { is_expected.to fix(:Duck) }
64
+ end
65
+
66
+ RSpec.describe RubberDuck do
67
+ it { is_expected.to fix(:Duck) }
68
+ end
69
+ ```
70
+
71
+ `rspec duck_spec.rb` runs three examples, and one fails:
72
+
73
+ ```text
74
+ 1) RubberDuck is expected to fix Duck
75
+ Failure/Error: it { is_expected.to fix(:Duck) }
76
+
77
+ expected the value to fix Duck:
78
+ duck_spec.rb:6: Error: NoMethodError: undefined method 'walk' for an instance of RubberDuck
79
+ duck_spec.rb:14: Failure: expected "Squeak!" to eq "Quaaack!"
80
+ ```
81
+
82
+ `require "rspec/fix"` requires Fix, and `config.include RSpec::Fix` gives the word `fix` to every example; a group may include the module for itself instead. `fix(:Name)` finds the specification registered by `Fix :Name do … end`, and raises `KeyError` when there is none. The specification is written where the suite can require it once: in the spec file as above, or in a file of its own, which a test of Fix can require too.
83
+
84
+ The matcher is met when every expectation passes, a warning or an info included: the robot above passes, though it swims its own way and does not speak, and RSpec says nothing of it. `Fix[:Duck].test { Robot.new }` writes every line, the warning and the info among them. Not met, the matcher gives the lines of the failures and of the errors, each pointing at its expectation; `expect(value).not_to fix(:Name)` is met when one expectation is not.
85
+
86
+ It composes as the matchers of RSpec do: `fix(:Duck).and fix(:Bird)`, `be_a(Duck).or fix(:Duck)`, `all(fix(:Duck))` for the values of a collection, and as an argument of another matcher, `include(pet: fix(:Duck))`.
87
+
88
+ The methods of the specification are sent to the value itself, in the process of the example, where its transaction, its doubles and its coverage tool are. A method that changes the value changes it for the contexts that follow, and for the rest of the example.
89
+
90
+ `fix(:Name, fork: true)` runs each context in a child process, as Fix does it, on a system with `fork`: the methods are sent to a copy of the value, and the one the example holds is left as it was. It is for a value whose state is all in the process, since what lives outside is not copied. In a Rails application with transactional tests, a child does not see a row the example has created, and a row it writes is not rolled back; a double does not see what a child received.
91
+
92
+ The matcher takes a value, not a block: `expect { … }.to fix(:Name)` is refused by RSpec. Since the value is given once, the contexts cannot build it anew with their lets: a specification meant for a matcher is written without `with`. What is not an outcome of an expectation is raised: an `Interrupt`, and with `fork: true` a child that died or a Ruby without `fork`.
93
+
94
+ ## Versioning
95
+
96
+ RSpec::Fix follows [Semantic Versioning 2.0](https://semver.org/).
97
+
98
+ ## License
99
+
100
+ The [gem](https://rubygems.org/gems/rspec-fix) is available as open source under the terms of the [MIT License](https://github.com/fixrb/rspec-fix/raw/main/LICENSE.md).
101
+
102
+ ## Sponsors
103
+
104
+ This project is sponsored by [Sashité](https://sashite.com/)
data/lib/rspec/fix.rb ADDED
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fix"
4
+ require "rspec/expectations"
5
+
6
+ # The examples of RSpec: this gem adds the word `fix` to them.
7
+ module RSpec
8
+ # The word `fix` for the examples of RSpec:
9
+ # `expect(value).to fix(:Name)` is met when the value meets every
10
+ # expectation of the specification of Fix registered under that name, and
11
+ # tells which it does not. Included in the example groups:
12
+ # `config.include RSpec::Fix`.
13
+ #
14
+ # @example
15
+ # RSpec.configure { |config| config.include RSpec::Fix }
16
+ #
17
+ # RSpec.describe Duck do
18
+ # it { is_expected.to fix(:Duck) }
19
+ # end
20
+ module Fix
21
+ # @param name [Symbol] the name of a registered specification
22
+ # @param fork [Boolean] whether each context runs in a child process, on a
23
+ # copy of the value, as Fix does it; by default, the methods of the
24
+ # specification are sent to the value itself, in the process of the
25
+ # example
26
+ # @return [RSpec::Fix::Matcher]
27
+ # @raise [KeyError] when no specification has that name
28
+ def fix(name, fork: false)
29
+ Matcher.new(::Fix[name], fork)
30
+ end
31
+
32
+ # A matcher of RSpec, composable as its own are: with `and` and `or`, and
33
+ # as an argument of another matcher. RSpec asks for the message after
34
+ # the answer, so the matcher keeps the results of the last value it was
35
+ # given.
36
+ #
37
+ # @api private
38
+ class Matcher
39
+ include Matchers::Composable
40
+
41
+ # @param spec [Fix::Spec] the specification
42
+ # @param fork [Boolean] whether each context runs in a child process
43
+ def initialize(spec, fork)
44
+ @spec = spec
45
+ @fork = fork
46
+ @results = [].freeze
47
+ end
48
+
49
+ # Tests a value: it is the subject of every expectation.
50
+ #
51
+ # @param value [Object] the value
52
+ # @return [Boolean] whether every expectation is met
53
+ # @raise [NotImplementedError] with a child per context, when this Ruby
54
+ # cannot fork
55
+ # @raise [Aw::ChildError] with a child per context, when one died or
56
+ # delivered no result; what is raised outside an expectation is
57
+ # raised here again, with children or without
58
+ def matches?(value)
59
+ @results = @spec.run(fork: @fork) { value }
60
+ @results.all?(&:passed?)
61
+ end
62
+
63
+ # @return [String] `fix NAME`
64
+ def description
65
+ @spec.to_s
66
+ end
67
+
68
+ # @return [String] the expectations the last value did not meet, one
69
+ # line each
70
+ def failure_message
71
+ ["expected the value to #{@spec}:", *@results.reject(&:passed?)].join("\n ")
72
+ end
73
+
74
+ # @return [String] the expectations the last value met, one line each
75
+ def failure_message_when_negated
76
+ ["expected the value not to #{@spec}:", *@results].join("\n ")
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-fix
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril Kato
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: fix
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.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.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec-expectations
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '3.13'
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '5'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '3.13'
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '5'
46
+ description: 'RSpec::Fix adds the word fix to the examples of RSpec: expect(value).to
47
+ fix(:Name) is met when the value meets every expectation of that specification of
48
+ Fix, and its message tells which it does not. A specification of Fix, beside the
49
+ suite a project already has.'
50
+ email: contact@cyril.email
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - LICENSE.md
56
+ - README.md
57
+ - lib/rspec/fix.rb
58
+ homepage: https://fixrb.dev/gems/rspec-fix/
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ bug_tracker_uri: https://github.com/fixrb/rspec-fix/issues
63
+ documentation_uri: https://gemdocs.org/gems/rspec-fix/1.0.0/
64
+ rubygems_mfa_required: 'true'
65
+ source_code_uri: https://github.com/fixrb/rspec-fix
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 3.3.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 4.0.23
81
+ specification_version: 4
82
+ summary: A matcher of RSpec that tests a value against a specification of Fix
83
+ test_files: []