minitest-markdown 0.1.0.pre → 0.2.1.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 +10 -8
- data/lib/minitest/markdown/test_class.rb +6 -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: 959c342f3e4ec34238e2385e3578faf1d5133beb2f74d9b52d4ffc9ab0dc8d58
|
4
|
+
data.tar.gz: 8b4bae7c2888040d6b2124fa2bd5d103c67405c883bf3de606f220e317912249
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3d59e3fc4f77092cdd8dda4f3a0b8a8d79e1b73cbfc37186947efd6dbb0e35f508ab0d32da4f6a88d4c816d72370ff92b563e819c44073da4ea44404b946acb
|
7
|
+
data.tar.gz: 2522d752e44a422f82be9ccb1a44c9ddec8346381d2cb0465990a7eee5b383e0a6b664c7f7b52c9ba860602c2a78411dc9f225c4a580bc287f65c26fbace05ad
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.1.pre] - 2025-05-07
|
4
|
+
|
5
|
+
- Fix issue with require in eval
|
6
|
+
- Fix bug in StubChain where instance was mutated after #call
|
7
|
+
|
8
|
+
## [0.2.0.pre] - 2025-05-06
|
9
|
+
|
10
|
+
- Add ability to pass any number of stubs to a test code block
|
11
|
+
|
12
|
+
### Breaking changes
|
13
|
+
- StubChain class replaces Stubb module see https://gitlab.com/matzfan/minitest-markdown#stubbing
|
14
|
+
|
3
15
|
## [0.1.0.pre] - 2025-05-01
|
4
16
|
|
5
17
|
- Add ability to pass stubs to test class
|
data/README.md
CHANGED
@@ -103,14 +103,16 @@ 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
|
-
|
113
|
-
stubs[
|
113
|
+
@stub_chain = Minitest::StubChain.new([set_stubs_new, array_stubs_size]) # initialized with zero or more stub procs
|
114
|
+
stubs[8] = @stub_chain
|
115
|
+
stubs[9] = @stub_chain # StubChain instances themseves may be reused
|
114
116
|
|
115
117
|
Markdown.generate_markdown_tests(self, stubs: stubs)
|
116
118
|
end
|
@@ -119,15 +121,15 @@ end
|
|
119
121
|
The 2 stubs above are demonstated in the following examples:
|
120
122
|
```ruby
|
121
123
|
# This is test_block8
|
122
|
-
Set.new([1, 'c', :s]) # Set.new stubbed to return an Array
|
123
|
-
# =>
|
124
|
+
Set.new([1, 'c', :s]).size # Here Set.new was stubbed to return an Array and Array#size was stubbed to return 42
|
125
|
+
# => 42
|
124
126
|
```
|
127
|
+
Example showing the reuse of a StubChain:
|
125
128
|
```ruby
|
126
129
|
# This is test_block9
|
127
|
-
Set.new([
|
130
|
+
Set.new([]).size
|
128
131
|
# => 42
|
129
132
|
```
|
130
|
-
Presently only one stub per test is possible.
|
131
133
|
|
132
134
|
## Configuration
|
133
135
|
|
@@ -3,6 +3,8 @@
|
|
3
3
|
require_relative '../assertions_extensions'
|
4
4
|
require_relative 'test_code_block'
|
5
5
|
|
6
|
+
$LOAD_PATH << '.' # fix #2
|
7
|
+
|
6
8
|
module Minitest
|
7
9
|
module Markdown
|
8
10
|
# knows how to build a markdown test
|
@@ -59,13 +61,14 @@ module Minitest
|
|
59
61
|
@ruby_blocks.delete(blocks.first).fenced_block_str # hook method blocks can't be test methods
|
60
62
|
end
|
61
63
|
|
62
|
-
def define_test_method(block, meth_index,
|
63
|
-
|
64
|
+
def define_test_method(block, meth_index, stub_chain = nil)
|
65
|
+
stub_chain ||= StubChain.new
|
64
66
|
instance = self # scope
|
65
67
|
klass.define_method(:"test_block#{meth_index}") do
|
66
|
-
|
68
|
+
test_code_proc = proc do
|
67
69
|
block.assertions.each { |assertion_hash| instance.send(:evaluation_assertions, assertion_hash, binding) }
|
68
70
|
end
|
71
|
+
stub_chain.call(test_code_proc)
|
69
72
|
end
|
70
73
|
end
|
71
74
|
|
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
|
+
call_recursive [*stubs, test_code_proc]
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def validate(test_code_proc)
|
29
|
+
raise ArgumentError, NOT_CALLABLE_ERR unless test_code_proc.respond_to?(:call)
|
30
|
+
|
31
|
+
test_code_proc
|
32
|
+
end
|
33
|
+
|
34
|
+
def call_recursive(call_chain)
|
35
|
+
prok = call_chain.shift
|
36
|
+
return prok.call if call_chain.empty?
|
37
|
+
|
38
|
+
prok.call { call_recursive(call_chain) }
|
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.1.
|
4
|
+
version: 0.2.1.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
|