test_tube 2.0.0 → 2.1.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 +4 -4
- data/README.md +31 -40
- data/lib/test_tube.rb +10 -10
- data/lib/test_tube/base.rb +8 -0
- data/lib/test_tube/invoker.rb +6 -6
- data/lib/test_tube/passer.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6aa09337dc5d35dd53da1f517ff92bced3b0d0c4f9c25eef1cd9287b63e14bbf
|
4
|
+
data.tar.gz: 36026bb1f5f3532e70ff51ebc25a1ddcf8c6098214c438e6a6fb6ad0305b249c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 668493142591c985ece8e150691d7e60d6834b5da976361ff4c4aea34204b0c645aea61352424daf6964366014df424f881fd6b27fb416c581399395b9c7f8af
|
7
|
+
data.tar.gz: ea4ce58827e8643b54811082bee5861502d339daa4489ac69355fc06c172ca15f6c1129c6db2bbee171184d06211dba70cca56995302e18d176b3801e61e9f8f
|
data/README.md
CHANGED
@@ -58,12 +58,8 @@ of code:
|
|
58
58
|
```ruby
|
59
59
|
block_of_code = -> { "101010".to_i(2) }
|
60
60
|
|
61
|
-
experiment = TestTube.invoke(
|
62
|
-
|
63
|
-
matcher: BeTheAnswer.new,
|
64
|
-
negate: false,
|
65
|
-
&block_of_code
|
66
|
-
)
|
61
|
+
experiment = TestTube.invoke(isolate: false, matcher: BeTheAnswer.new, negate: false, &block_of_code)
|
62
|
+
# => <TestTube actual=42 error=nil got=true>
|
67
63
|
|
68
64
|
experiment.actual # => 42
|
69
65
|
experiment.error # => nil
|
@@ -75,11 +71,8 @@ An alternative would be to `pass` directly the actual value as a parameter:
|
|
75
71
|
```ruby
|
76
72
|
actual_value = "101010".to_i(2)
|
77
73
|
|
78
|
-
experiment = TestTube.pass(
|
79
|
-
|
80
|
-
matcher: BeTheAnswer.new,
|
81
|
-
negate: false
|
82
|
-
)
|
74
|
+
experiment = TestTube.pass(actual_value, matcher: BeTheAnswer.new, negate: false)
|
75
|
+
# => <TestTube actual=42 error=nil got=true>
|
83
76
|
|
84
77
|
experiment.actual # => 42
|
85
78
|
experiment.error # => nil
|
@@ -105,12 +98,11 @@ An example of successful experience:
|
|
105
98
|
|
106
99
|
```ruby
|
107
100
|
experiment = TestTube.invoke(
|
108
|
-
|
109
|
-
matcher:
|
110
|
-
negate:
|
111
|
-
)
|
112
|
-
|
113
|
-
end
|
101
|
+
isolate: false,
|
102
|
+
matcher: Matchi::Matcher::RaiseException.new(NoMethodError),
|
103
|
+
negate: false
|
104
|
+
) { "foo".blank? }
|
105
|
+
# => <TestTube actual=#<NoMethodError: undefined method `blank?' for "foo":String> error=nil got=true>
|
114
106
|
|
115
107
|
experiment.actual # => #<NoMethodError: undefined method `blank?' for "foo":String>
|
116
108
|
experiment.error # => nil
|
@@ -121,11 +113,11 @@ Another example of an experiment that fails:
|
|
121
113
|
|
122
114
|
```ruby
|
123
115
|
experiment = TestTube.invoke(
|
124
|
-
|
125
|
-
matcher:
|
126
|
-
negate:
|
116
|
+
isolate: false,
|
117
|
+
matcher: Matchi::Matcher::Equal.new(0.3),
|
118
|
+
negate: false,
|
127
119
|
&-> { 0.1 + 0.2 }
|
128
|
-
)
|
120
|
+
) # => <TestTube actual=0.30000000000000004 error=nil got=false>
|
129
121
|
|
130
122
|
experiment.actual # => 0.30000000000000004
|
131
123
|
experiment.error # => nil
|
@@ -136,10 +128,11 @@ Finally, an experiment which causes an error:
|
|
136
128
|
|
137
129
|
```ruby
|
138
130
|
experiment = TestTube.invoke(
|
139
|
-
|
140
|
-
matcher:
|
141
|
-
negate:
|
131
|
+
isolate: false,
|
132
|
+
matcher: Matchi::Matcher::Match.new(/^foo$/),
|
133
|
+
negate: false
|
142
134
|
) { BOOM }
|
135
|
+
# => <TestTube actual=nil error=#<NameError: uninitialized constant BOOM> got=nil>
|
143
136
|
|
144
137
|
experiment.actual # => nil
|
145
138
|
experiment.error # => #<NameError: uninitialized constant BOOM>
|
@@ -149,27 +142,25 @@ experiment.got # => nil
|
|
149
142
|
### Code isolation
|
150
143
|
|
151
144
|
When experimenting tests, side-effects may occur. Because they may or may not be
|
152
|
-
desired, an `
|
145
|
+
desired, an `isolate` option is available.
|
153
146
|
|
154
147
|
Let's for instance consider this block of code:
|
155
148
|
|
156
149
|
```ruby
|
157
150
|
greeting = "Hello, world!"
|
158
|
-
block_of_code = -> { greeting.gsub!("world", "Alice") }
|
151
|
+
block_of_code = -> { greeting.gsub!("world", "Alice") } # => #<Proc:0x00007f87f71b9690 (irb):42 (lambda)>
|
159
152
|
```
|
160
153
|
|
161
|
-
By setting the `
|
154
|
+
By setting the `isolate` option to `true`, we can experiment while avoiding
|
162
155
|
side effects:
|
163
156
|
|
164
157
|
```ruby
|
165
158
|
experiment = TestTube.invoke(
|
166
|
-
|
167
|
-
matcher:
|
168
|
-
negate:
|
159
|
+
isolate: true,
|
160
|
+
matcher: Matchi::Matcher::Eql.new("Hello, Alice!"),
|
161
|
+
negate: false,
|
169
162
|
&block_of_code
|
170
|
-
)
|
171
|
-
|
172
|
-
experiment.inspect # => <TestTube actual="Hello, Alice!" error=nil got=true>
|
163
|
+
) # => <TestTube actual="Hello, Alice!" error=nil got=true>
|
173
164
|
|
174
165
|
greeting # => "Hello, world!"
|
175
166
|
```
|
@@ -178,13 +169,11 @@ Otherwise, we can experiment without any code isolation:
|
|
178
169
|
|
179
170
|
```ruby
|
180
171
|
experiment = TestTube.invoke(
|
181
|
-
|
182
|
-
matcher:
|
183
|
-
negate:
|
172
|
+
isolate: false,
|
173
|
+
matcher: Matchi::Matcher::Eql.new("Hello, Alice!"),
|
174
|
+
negate: false,
|
184
175
|
&block_of_code
|
185
|
-
)
|
186
|
-
|
187
|
-
experiment.inspect # => <TestTube actual="Hello, Alice!" error=nil got=true>
|
176
|
+
) # => <TestTube actual="Hello, Alice!" error=nil got=true>
|
188
177
|
|
189
178
|
greeting # => "Hello, Alice!"
|
190
179
|
```
|
@@ -192,6 +181,8 @@ greeting # => "Hello, Alice!"
|
|
192
181
|
## Contact
|
193
182
|
|
194
183
|
* Source code: https://github.com/fixrb/test_tube
|
184
|
+
* Chinese blog post: https://ruby-china.org/topics/41390
|
185
|
+
* Japanese blog post: https://qiita.com/cyril/items/36174b619ff1852c80ec
|
195
186
|
|
196
187
|
## Versioning
|
197
188
|
|
@@ -199,7 +190,7 @@ __Test Tube__ follows [Semantic Versioning 2.0](https://semver.org/).
|
|
199
190
|
|
200
191
|
## License
|
201
192
|
|
202
|
-
The [gem](https://rubygems.org/gems/test_tube) is available as open source under the terms of the [MIT License](https://
|
193
|
+
The [gem](https://rubygems.org/gems/test_tube) is available as open source under the terms of the [MIT License](https://github.com/fixrb/test_tube/raw/main/LICENSE.md).
|
203
194
|
|
204
195
|
***
|
205
196
|
|
data/lib/test_tube.rb
CHANGED
@@ -7,10 +7,10 @@ require_relative File.join("test_tube", "passer")
|
|
7
7
|
#
|
8
8
|
# @api public
|
9
9
|
module TestTube
|
10
|
-
# @param
|
11
|
-
# @param matcher
|
12
|
-
# @param negate
|
13
|
-
# @param input
|
10
|
+
# @param isolate [Boolean] Compute in a subprocess.
|
11
|
+
# @param matcher [#matches?] A matcher.
|
12
|
+
# @param negate [Boolean] Invert the matcher or not.
|
13
|
+
# @param input [Proc] The callable object to test.
|
14
14
|
#
|
15
15
|
# @example
|
16
16
|
# require "test_tube"
|
@@ -21,18 +21,18 @@ module TestTube
|
|
21
21
|
# end
|
22
22
|
# end
|
23
23
|
#
|
24
|
-
# TestTube.invoke(
|
24
|
+
# TestTube.invoke(isolate: false, matcher: BeTheAnswer.new, negate: false) do
|
25
25
|
# "101010".to_i(2)
|
26
26
|
# end
|
27
27
|
#
|
28
28
|
# @return [Invoker] A software experiment.
|
29
|
-
def self.invoke(
|
30
|
-
Invoker.new(
|
29
|
+
def self.invoke(isolate:, matcher:, negate:, &input)
|
30
|
+
Invoker.new(isolate: isolate, matcher: matcher, negate: negate, &input)
|
31
31
|
end
|
32
32
|
|
33
|
-
# @param input
|
34
|
-
# @param matcher
|
35
|
-
# @param negate
|
33
|
+
# @param input [#object_id] The actual value to test.
|
34
|
+
# @param matcher [#matches?] A matcher.
|
35
|
+
# @param negate [Boolean] Invert the matcher or not.
|
36
36
|
#
|
37
37
|
# @example
|
38
38
|
# require "test_tube"
|
data/lib/test_tube/base.rb
CHANGED
@@ -8,21 +8,29 @@ module TestTube
|
|
8
8
|
# Expectation's actual value.
|
9
9
|
#
|
10
10
|
# @return [#object_id] The actual value.
|
11
|
+
#
|
12
|
+
# @api public
|
11
13
|
attr_reader :actual
|
12
14
|
|
13
15
|
# Expectation's raised error.
|
14
16
|
#
|
15
17
|
# @return [Exception, nil] The raised error.
|
18
|
+
#
|
19
|
+
# @api public
|
16
20
|
attr_reader :error
|
17
21
|
|
18
22
|
# Expectation's returned boolean value.
|
19
23
|
#
|
20
24
|
# @return [Boolean, nil] The returned boolean value.
|
25
|
+
#
|
26
|
+
# @api public
|
21
27
|
attr_reader :got
|
22
28
|
|
23
29
|
# A string containing a human-readable representation of the experiment.
|
24
30
|
#
|
25
31
|
# @return [String] The human-readable representation of the experiment.
|
32
|
+
#
|
33
|
+
# @api public
|
26
34
|
def inspect
|
27
35
|
"<TestTube actual=#{actual.inspect} error=#{error.inspect} got=#{got.inspect}>"
|
28
36
|
end
|
data/lib/test_tube/invoker.rb
CHANGED
@@ -13,15 +13,15 @@ module TestTube
|
|
13
13
|
#
|
14
14
|
# rubocop:disable Lint/RescueException, Metrics/MethodLength
|
15
15
|
#
|
16
|
-
# @param
|
17
|
-
# @param matcher
|
18
|
-
# @param negate
|
19
|
-
# @param input
|
20
|
-
def initialize(
|
16
|
+
# @param isolate [Boolean] Compute in a subprocess.
|
17
|
+
# @param matcher [#matches?] A matcher.
|
18
|
+
# @param negate [Boolean] Invert the matcher or not.
|
19
|
+
# @param input [Proc] The callable object to test.
|
20
|
+
def initialize(isolate:, matcher:, negate:, &input)
|
21
21
|
super()
|
22
22
|
|
23
23
|
@got = negate ^ matcher.matches? do
|
24
|
-
value = if
|
24
|
+
value = if isolate
|
25
25
|
send_call.to!(input)
|
26
26
|
else
|
27
27
|
send_call.to(input)
|
data/lib/test_tube/passer.rb
CHANGED
@@ -9,7 +9,7 @@ module TestTube
|
|
9
9
|
class Passer < Base
|
10
10
|
# Class initializer.
|
11
11
|
#
|
12
|
-
# @param input [#object_id]
|
12
|
+
# @param input [#object_id] The actual value to test.
|
13
13
|
# @param matcher [#matches?] A matcher.
|
14
14
|
# @param negate [Boolean] Invert the matcher or not.
|
15
15
|
def initialize(input, matcher:, negate:)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_tube
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: defi
|