minitest-markdown 0.1.0.pre → 0.2.0.pre
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/CHANGELOG.md +7 -0
- data/README.md +5 -11
- data/lib/minitest/markdown/test_class.rb +4 -3
- data/lib/minitest/markdown/version.rb +1 -1
- data/lib/minitest/markdown.rb +7 -1
- data/lib/minitest/stub_chain.rb +41 -0
- metadata +3 -3
- data/lib/minitest/markdown/stubb.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50ca2163359f3e4b038a8e4837bcb38511bca5e5b00bc2603873816bba2408e1
|
4
|
+
data.tar.gz: 15fcbc54bcc387111b92b86fe5c291ce82d05f9391f12d41955aaa0d191c2735
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26bba54a86204e035f3890032eb985ac494133a697fc79c937ed863270e7659acb8675e916aba14b83062b1cb162b5ca7ef7a73e3283ad2c80b95889e8fac356
|
7
|
+
data.tar.gz: f6556b67590972e176b120c879d499d00fc2bc0574c1f134bec9f76c12f6f37907b2b1c09fb19aab7e73d41bd697bdf37c289f2180797b8f4940043935441043
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0.pre] - 2025-05-06
|
4
|
+
|
5
|
+
- Add ability to pass any number of stubs to a test code block
|
6
|
+
|
7
|
+
### Breaking changes
|
8
|
+
- StubChain class replaces Stubb module see https://gitlab.com/matzfan/minitest-markdown#stubbing
|
9
|
+
|
3
10
|
## [0.1.0.pre] - 2025-05-01
|
4
11
|
|
5
12
|
- Add ability to pass stubs to test class
|
data/README.md
CHANGED
@@ -103,14 +103,14 @@ Everything in the code blocks above runs as test code. [minitest-proveit](https:
|
|
103
103
|
|
104
104
|
## Stubbing
|
105
105
|
|
106
|
-
It is possible to pass stubs to the generated tests. This is done
|
106
|
+
It is possible to pass stubs to the generated tests. This is done using the stubs keyword. Hash keys represent the index of the test code block and the key is an instance of `StubChain`. `StubChain.stubproc` returns a procified stub which is called around the relevant test code. Zero or more of these reusable procs can be used to instantiate a StubChain object:
|
107
107
|
```ruby
|
108
108
|
class MarkdownTest < Minitest::Test
|
109
|
-
|
109
|
+
set_stubs_new = Minitest::StubChain.stubproc(Set, :new, []) # returns a proc which stubs Set.new to return an empty array
|
110
|
+
array_stubs_size = Minitest::StubChain.stubproc(Array, :size, 42, any_instance: true) # uses the bundled minitest-stub_any_instance gem
|
110
111
|
|
111
112
|
stubs = {}
|
112
|
-
stubs[8] =
|
113
|
-
stubs[9] = stubb_any_instance(Set, :size, 42) # uses the bundled `minitest-stub_any_instance` extension
|
113
|
+
stubs[8] = Minitest::StubChain.new([set_stubs_new, array_stubs_size]) # initialized with zero or more stub procs
|
114
114
|
|
115
115
|
Markdown.generate_markdown_tests(self, stubs: stubs)
|
116
116
|
end
|
@@ -119,15 +119,9 @@ end
|
|
119
119
|
The 2 stubs above are demonstated in the following examples:
|
120
120
|
```ruby
|
121
121
|
# This is test_block8
|
122
|
-
Set.new([1, 'c', :s]) # Set.new stubbed to return an Array
|
123
|
-
# => instance_of Array
|
124
|
-
```
|
125
|
-
```ruby
|
126
|
-
# This is test_block9
|
127
|
-
Set.new([1, 'c', :s]).size # Set#size stubbed to return 42
|
122
|
+
Set.new([1, 'c', :s]).size # Here Set.new was stubbed to return an Array and Array#size was stubbed to return 42
|
128
123
|
# => 42
|
129
124
|
```
|
130
|
-
Presently only one stub per test is possible.
|
131
125
|
|
132
126
|
## Configuration
|
133
127
|
|
@@ -59,13 +59,14 @@ module Minitest
|
|
59
59
|
@ruby_blocks.delete(blocks.first).fenced_block_str # hook method blocks can't be test methods
|
60
60
|
end
|
61
61
|
|
62
|
-
def define_test_method(block, meth_index,
|
63
|
-
|
62
|
+
def define_test_method(block, meth_index, stub_chain = nil)
|
63
|
+
stub_chain ||= StubChain.new
|
64
64
|
instance = self # scope
|
65
65
|
klass.define_method(:"test_block#{meth_index}") do
|
66
|
-
|
66
|
+
test_code_proc = proc do
|
67
67
|
block.assertions.each { |assertion_hash| instance.send(:evaluation_assertions, assertion_hash, binding) }
|
68
68
|
end
|
69
|
+
stub_chain.call(test_code_proc)
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
data/lib/minitest/markdown.rb
CHANGED
@@ -2,15 +2,17 @@
|
|
2
2
|
|
3
3
|
require 'minitest/stub_any_instance'
|
4
4
|
|
5
|
+
require_relative 'stub_chain'
|
5
6
|
require_relative 'markdown/configuration'
|
6
7
|
require_relative 'markdown/error'
|
7
|
-
require_relative 'markdown/stubb'
|
8
8
|
require_relative 'markdown/version'
|
9
9
|
require_relative 'markdown/test_class'
|
10
10
|
|
11
11
|
module Minitest
|
12
12
|
# namespace
|
13
13
|
module Markdown
|
14
|
+
ARG_ERR = 'stubs keyword takes a hash. Keys are integers and values are StubChain instances'
|
15
|
+
|
14
16
|
class << self
|
15
17
|
def config
|
16
18
|
@config ||= Configuration.new
|
@@ -21,6 +23,10 @@ module Minitest
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def generate_markdown_tests(klass, path: nil, stubs: {})
|
26
|
+
raise ArgumentError, ARG_ERR unless stubs.is_a? Hash
|
27
|
+
raise ArgumentError, ARG_ERR unless stubs.keys.all? { |o| o.instance_of? Integer }
|
28
|
+
raise ArgumentError, ARG_ERR unless stubs.values.all? { |o| o.instance_of? StubChain }
|
29
|
+
|
24
30
|
TestClass.new(klass, path: path).define_methods(stubs: stubs)
|
25
31
|
end
|
26
32
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minitest
|
4
|
+
# respresentaion of zero or more stub blocks around one or more test assertions
|
5
|
+
class StubChain
|
6
|
+
NOT_CALLABLE_ERR = 'StubChain#call takes a callable argument'
|
7
|
+
MUST_NOT_CALL_A_BLOCK_ERR = 'StubChain#call takes a callable argument which must not call a block'
|
8
|
+
|
9
|
+
attr_reader :stubs
|
10
|
+
|
11
|
+
def initialize(stubs = [])
|
12
|
+
@stubs = *stubs
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.stubproc(klass, method, ret_val, any_instance: false)
|
16
|
+
return proc { |&blk| klass.stub(method, ret_val) { blk.call } } unless any_instance
|
17
|
+
|
18
|
+
proc { |&blk| klass.stub_any_instance(method, ret_val) { blk.call } }
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(test_code_proc)
|
22
|
+
validate(test_code_proc)
|
23
|
+
stubs << test_code_proc
|
24
|
+
call_recursive
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def validate(test_code_proc)
|
30
|
+
raise ArgumentError, NOT_CALLABLE_ERR unless test_code_proc.respond_to?(:call)
|
31
|
+
|
32
|
+
test_code_proc
|
33
|
+
end
|
34
|
+
|
35
|
+
def call_recursive
|
36
|
+
(prok = stubs.shift).call # "undefined method 'call' for nil" if proc calls a block - i.e. stubs
|
37
|
+
rescue NoMethodError
|
38
|
+
prok.call { call_recursive }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minitest-markdown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MatzFan
|
@@ -78,10 +78,10 @@ files:
|
|
78
78
|
- lib/minitest/markdown.rb
|
79
79
|
- lib/minitest/markdown/configuration.rb
|
80
80
|
- lib/minitest/markdown/error.rb
|
81
|
-
- lib/minitest/markdown/stubb.rb
|
82
81
|
- lib/minitest/markdown/test_class.rb
|
83
82
|
- lib/minitest/markdown/test_code_block.rb
|
84
83
|
- lib/minitest/markdown/version.rb
|
84
|
+
- lib/minitest/stub_chain.rb
|
85
85
|
- sig/minitest/markdown.rbs
|
86
86
|
homepage: https://gitlab.com/matzfan/minitest-markdown
|
87
87
|
licenses:
|
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.7.0.dev
|
108
108
|
specification_version: 4
|
109
109
|
summary: Turn your README.md Ruby code blocks into testable code.
|
110
110
|
test_files: []
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Minitest
|
4
|
-
module Markdown
|
5
|
-
# a proc'ed stub
|
6
|
-
module Stubb
|
7
|
-
def stubb(object, method, ret_val, *args, **kwargs)
|
8
|
-
proc { |&blk| object.stub(method, ret_val, *args, **kwargs) { blk.call } }
|
9
|
-
end
|
10
|
-
|
11
|
-
def stubb_any_instance(object, method, ret_val, *args, **kwargs)
|
12
|
-
proc { |&blk| object.stub_any_instance(method, ret_val, *args, **kwargs) { blk.call } }
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|