minitest-markdown 0.0.1.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 +12 -0
- data/README.md +24 -2
- data/lib/minitest/markdown/test_class.rb +8 -4
- data/lib/minitest/markdown/version.rb +1 -1
- data/lib/minitest/markdown.rb +11 -2
- data/lib/minitest/stub_chain.rb +41 -0
- metadata +17 -2
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,17 @@
|
|
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
|
+
|
10
|
+
## [0.1.0.pre] - 2025-05-01
|
11
|
+
|
12
|
+
- Add ability to pass stubs to test class
|
13
|
+
- Add dependency; minitest-stub_any_instance
|
14
|
+
|
3
15
|
## [0.0.1.pre] - 2025-04-24
|
4
16
|
|
5
17
|
- Bump minitest dep to >= 5.25.2
|
data/README.md
CHANGED
@@ -30,7 +30,7 @@ To test the Ruby blocks in your README file, create file `test_readme.rb` (for e
|
|
30
30
|
require 'minitest/autorun' # or in your test_helper
|
31
31
|
require 'minitest/markdown' # ditto
|
32
32
|
|
33
|
-
class ReadmeTest <
|
33
|
+
class ReadmeTest < Minitest::Test # or your own subclass of Minitest::Test
|
34
34
|
Markdown.generate_markdown_tests(self)
|
35
35
|
end
|
36
36
|
# => nil
|
@@ -101,6 +101,28 @@ The hook methods defined in the [minitest-hooks](https://github.com/jeremyevans/
|
|
101
101
|
|
102
102
|
Everything in the code blocks above runs as test code. [minitest-proveit](https://github.com/seattlerb/minitest-proveit) would complain otherwise ;-)
|
103
103
|
|
104
|
+
## Stubbing
|
105
|
+
|
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
|
+
```ruby
|
108
|
+
class MarkdownTest < Minitest::Test
|
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
|
111
|
+
|
112
|
+
stubs = {}
|
113
|
+
stubs[8] = Minitest::StubChain.new([set_stubs_new, array_stubs_size]) # initialized with zero or more stub procs
|
114
|
+
|
115
|
+
Markdown.generate_markdown_tests(self, stubs: stubs)
|
116
|
+
end
|
117
|
+
# => nil
|
118
|
+
```
|
119
|
+
The 2 stubs above are demonstated in the following examples:
|
120
|
+
```ruby
|
121
|
+
# This is test_block8
|
122
|
+
Set.new([1, 'c', :s]).size # Here Set.new was stubbed to return an Array and Array#size was stubbed to return 42
|
123
|
+
# => 42
|
124
|
+
```
|
125
|
+
|
104
126
|
## Configuration
|
105
127
|
|
106
128
|
No configuation is required if you use Bundler. If not, set your `project_root` path using the setter method:
|
@@ -120,7 +142,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
120
142
|
|
121
143
|
## Contributing
|
122
144
|
|
123
|
-
Bug reports and pull requests are welcome on GitHub at https://
|
145
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/matzfan/minitest-markdown. Please checkout a suitably named branch before submitting a PR.
|
124
146
|
|
125
147
|
## License
|
126
148
|
|
@@ -20,9 +20,9 @@ module Minitest
|
|
20
20
|
@ruby_blocks = parse_ruby_blocks
|
21
21
|
end
|
22
22
|
|
23
|
-
def define_methods
|
23
|
+
def define_methods(stubs: {})
|
24
24
|
define_non_test_methods
|
25
|
-
@ruby_blocks.each_with_index { |block, i| define_test_method(block, i) }
|
25
|
+
@ruby_blocks.each_with_index { |block, i| define_test_method(block, i, stubs[i]) }
|
26
26
|
nil
|
27
27
|
end
|
28
28
|
|
@@ -59,10 +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)
|
62
|
+
def define_test_method(block, meth_index, stub_chain = nil)
|
63
|
+
stub_chain ||= StubChain.new
|
63
64
|
instance = self # scope
|
64
65
|
klass.define_method(:"test_block#{meth_index}") do
|
65
|
-
|
66
|
+
test_code_proc = proc do
|
67
|
+
block.assertions.each { |assertion_hash| instance.send(:evaluation_assertions, assertion_hash, binding) }
|
68
|
+
end
|
69
|
+
stub_chain.call(test_code_proc)
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
data/lib/minitest/markdown.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'minitest/stub_any_instance'
|
4
|
+
|
5
|
+
require_relative 'stub_chain'
|
3
6
|
require_relative 'markdown/configuration'
|
4
7
|
require_relative 'markdown/error'
|
5
8
|
require_relative 'markdown/version'
|
@@ -8,6 +11,8 @@ require_relative 'markdown/test_class'
|
|
8
11
|
module Minitest
|
9
12
|
# namespace
|
10
13
|
module Markdown
|
14
|
+
ARG_ERR = 'stubs keyword takes a hash. Keys are integers and values are StubChain instances'
|
15
|
+
|
11
16
|
class << self
|
12
17
|
def config
|
13
18
|
@config ||= Configuration.new
|
@@ -17,8 +22,12 @@ module Minitest
|
|
17
22
|
yield config
|
18
23
|
end
|
19
24
|
|
20
|
-
def generate_markdown_tests(klass, path: nil)
|
21
|
-
|
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
|
+
|
30
|
+
TestClass.new(klass, path: path).define_methods(stubs: stubs)
|
22
31
|
end
|
23
32
|
end
|
24
33
|
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.0.
|
4
|
+
version: 0.2.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MatzFan
|
@@ -49,6 +49,20 @@ dependencies:
|
|
49
49
|
- - ">="
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: 5.25.2
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
name: minitest-stub_any_instance
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - "~>"
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '1.0'
|
59
|
+
type: :runtime
|
60
|
+
prerelease: false
|
61
|
+
version_requirements: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - "~>"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '1.0'
|
52
66
|
description: Generates tests for Ruby code blocks in any Markdown file.
|
53
67
|
executables: []
|
54
68
|
extensions: []
|
@@ -67,6 +81,7 @@ files:
|
|
67
81
|
- lib/minitest/markdown/test_class.rb
|
68
82
|
- lib/minitest/markdown/test_code_block.rb
|
69
83
|
- lib/minitest/markdown/version.rb
|
84
|
+
- lib/minitest/stub_chain.rb
|
70
85
|
- sig/minitest/markdown.rbs
|
71
86
|
homepage: https://gitlab.com/matzfan/minitest-markdown
|
72
87
|
licenses:
|
@@ -89,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
104
|
- !ruby/object:Gem::Version
|
90
105
|
version: '0'
|
91
106
|
requirements: []
|
92
|
-
rubygems_version: 3.
|
107
|
+
rubygems_version: 3.7.0.dev
|
93
108
|
specification_version: 4
|
94
109
|
summary: Turn your README.md Ruby code blocks into testable code.
|
95
110
|
test_files: []
|