minitest-markdown 0.2.1.pre → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 959c342f3e4ec34238e2385e3578faf1d5133beb2f74d9b52d4ffc9ab0dc8d58
4
- data.tar.gz: 8b4bae7c2888040d6b2124fa2bd5d103c67405c883bf3de606f220e317912249
3
+ metadata.gz: fabeb5456de7296b1986c66822818a5fec9834e2c23e19b33a4c3c198e262ae6
4
+ data.tar.gz: bebbc7f77cd2443e91e58224888e5b69ab2d072b69e49c4cab86557cb0c3eeec
5
5
  SHA512:
6
- metadata.gz: e3d59e3fc4f77092cdd8dda4f3a0b8a8d79e1b73cbfc37186947efd6dbb0e35f508ab0d32da4f6a88d4c816d72370ff92b563e819c44073da4ea44404b946acb
7
- data.tar.gz: 2522d752e44a422f82be9ccb1a44c9ddec8346381d2cb0465990a7eee5b383e0a6b664c7f7b52c9ba860602c2a78411dc9f225c4a580bc287f65c26fbace05ad
6
+ metadata.gz: 5b369fab40830b0a31951df1ddb8dc25868471f2766f81fd2c438bba7219921e45ef47e48d9c73b2224655d39a1005efd23e8c0a268481c3f9b0a3d628019ee2
7
+ data.tar.gz: 2789d00a505fa70ea7ce6c29352807c066951c4498862f4b1afe6190e568e3b7da31ce63b3a5baf74e51cd24759c183a432121cb1209d883faf672ec08a1dd13
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.3] - 2025-05-22
4
+
5
+ - New release to catch up with 'pre' versioning
6
+
7
+ ## [0.0.1] - 2025-05-22
8
+
9
+ - Fix bug with Minitest::Hooks method definitions
10
+
11
+ ## [0.0.0] - 2025-05-10
12
+
13
+ - Fix bug in TestCodeBlock error message
14
+ - Add binding to Kernel.eval fixes #1
15
+ - pass kwargs to stub methods in StubProc
16
+
17
+ ## [0.2.2.pre] - 2025-05-08
18
+
19
+ - Improve magic comment & hook comment parsing
20
+ - Fix bug with empty sting after magic comment
21
+ - Fix bug in config if Bundler undefined
22
+
3
23
  ## [0.2.1.pre] - 2025-05-07
4
24
 
5
25
  - Fix issue with require in eval
data/README.md CHANGED
@@ -5,10 +5,11 @@
5
5
 
6
6
  ## \_why?
7
7
 
8
- Document your Gem's usage, examples etc. with fully testable code! Better still, use your README as a BDD aid and specify functionaility in your README/wiki code blocks *before* you write your code!!
8
+ Document your Gem's usage, examples etc. with fully testable code! Better still, use your README as a BDD aid and specify functionality in your README/wiki code blocks *before* you write your code!!
9
9
 
10
10
  ## Installation
11
11
 
12
+
12
13
  Add the gem to the application's Gemfile:
13
14
 
14
15
  ```bash
@@ -21,6 +22,17 @@ If bundler is not being used to manage dependencies, install the gem by executin
21
22
  gem install minitest-markdown
22
23
  ```
23
24
 
25
+ ## Configuration
26
+
27
+ No configuration is required if you use Bundler. If not, set your `project_root` path using the setter method:
28
+ ```ruby
29
+ @config = Minitest::Markdown.config
30
+ # => instance_of Configuration
31
+
32
+ Configuration.instance_methods(false)
33
+ # => includes :project_root=
34
+ ```
35
+
24
36
  ## Usage
25
37
 
26
38
  ### In your test class
@@ -29,17 +41,24 @@ To test the Ruby blocks in your README file, create file `test_readme.rb` (for e
29
41
  ```ruby
30
42
  require 'minitest/autorun' # or in your test_helper
31
43
  require 'minitest/markdown' # ditto
44
+ require 'minitest/hooks/test' # optional, see 'State' section below
32
45
 
33
46
  class ReadmeTest < Minitest::Test # or your own subclass of Minitest::Test
47
+ include Minitest::Hooks # optional, see 'State' section below
48
+
34
49
  Markdown.generate_markdown_tests(self)
35
50
  end
36
- # => nil
51
+ # => truthy
37
52
  ```
38
53
  To test Ruby blocks in another Markdown file, create another test file and pass the path to your Markdown file using the `:path` keyword arg i.e. `Markdown.generate_markdown_tests(self, path: '/path/to/your/markdown/file.md')`
39
54
 
40
55
  ### In your Markdown - magic comments become assertions
41
56
 
42
- Each Markdown file is represented by a single test class and each Ruby block in a file becomes a test method with zero or more assertions. The syntax used is `# => ` followed by an assertion keyword. Keywords may be one of the [Minitest "assert_" assertions](https://docs.seattlerb.org/minitest/Minitest/Assertions.html) less the "assert_" prefix (refutations are not implemented at this time). If the keyword is omitted, the default assertion; `assert_equal` is used. The actual value passed to the assertion is the result of the evaluation of the Ruby code above each magic comment. The following block (a single test) includes 3 assertions:
57
+ Each Markdown file is represented by a single test class and each Ruby block* in a file becomes a test method with zero or more assertions. Test methods are named according to their index; `test_block0`, `test_block1` etc.
58
+
59
+ \*Any 'state' blocks are excluded from indexing - see State section below
60
+
61
+ The syntax used for assertions is `# => ` followed by an assertion keyword. Keywords may be any one of the [Minitest "assert_" assertions](https://docs.seattlerb.org/minitest/Minitest/Assertions.html) less the "assert_" prefix (refutations are not implemented at this time). If the keyword is omitted, the default assertion; `assert_equal` is used. The actual value passed to the assertion is the result of the evaluation of the Ruby code above each magic comment. The following block (a single test) includes 6 assertions:
43
62
  ```ruby
44
63
  File.read 'test/fixtures/klass.rb'
45
64
  # => "class Klass\n def hello\n 'Hello Markdown!'\n end\nend"
@@ -53,12 +72,15 @@ Klass.new # inline comments are also ignored
53
72
  # The assertion and expected value are:-
54
73
  # => instance_of Klass
55
74
 
56
- # No keywword here, so the default assertion is used (assert_equal)
75
+ # No keyword here, so the default assertion is used (assert_equal)
57
76
  Klass.new.hello
58
77
  # => 'Hello Markdown!'
59
78
 
60
79
  Klass.hello
61
80
  # => raises NoMethodError
81
+
82
+ self
83
+ # => instance_of Markdown::TestClass
62
84
  ```
63
85
  Plain old `assert` has been aliased as `assert_truthy`, so when expecting a truthy value you should do this:
64
86
  ```ruby
@@ -72,7 +94,7 @@ For convenience, the assertion `assert_includes` has also been aliased so that i
72
94
  2
73
95
  # => included_in [1, 2, 3]
74
96
  ```
75
- Everything on the magic comment line after the keyword, or `# => `, if one is omitted, is evaluated as Ruby code. Note: **inline comments are NOT ALLOWED on magic comment lines**. Where an assertion takes multiple positional args, these are simply separated by commas. Note that the assertion keyword itself is **not an argument**. The syntax is as follows:
97
+ Everything on the magic comment line after the assertion keyword, or `# => ` if one is omitted, is evaluated as Ruby code. Note: **inline comments are NOT ALLOWED on magic comment lines**. Where an assertion takes multiple positional args, these are simply separated by commas. Note that the assertion keyword itself is **not an argument**. The syntax is as follows:
76
98
  ```ruby
77
99
  22/7.0
78
100
  # => in_delta Math::PI, 0.01
@@ -82,65 +104,102 @@ To skip a test, use `skip`, as you would in a regular test:
82
104
  "some code which you don't want to test yet"
83
105
  # => skip 'give a reason for the skip here'
84
106
  ```
107
+ Test failures will look like this - note the method name `test_block10` in this example:
108
+ ```
109
+ Minitest::Markdown::ReadmeTest#test_block10 [lib/minitest/markdown/test_class.rb:118]:
110
+ Expected: 42
111
+ Actual: 0
112
+ ```
85
113
 
86
114
  ### State
87
115
 
88
- Minitest's `setup` and `teardown` methods are generated by using the appropriate comment on the first line of a code block. Magic comments are ignored in such blocks, as these are not tests. E.g.
116
+ Instance vars are shared across test methods within a class, but as Minitest's default is to run tests in random order you may want to use a setup block in order to ensure a stored value is available to all test blocks (tests) in the Markdown file test class (see below):
117
+ ```ruby
118
+ @instance_var
119
+ # => 7
120
+
121
+ @before_all_instance_var # see hook methods below
122
+ # => 'foo'
123
+ ```
124
+ Minitest's `setup` and `teardown` methods are generated by using the appropriate comment on the first line of a code block. Assertion magic comments are ignored in such blocks, as these are not tests. E.g.
125
+
89
126
  ```ruby
90
127
  # setup
91
128
 
92
- @instance_var = 42
129
+ # do some setup task - or:-
130
+ @instance_var = 7 # now available in all test method blocks, including the one above
131
+ # => IGNORED
93
132
  ```
94
- The instance var set is now available to all test blocks (tests) in the Markdown file.
95
133
  ```ruby
96
- @instance_var
97
- # => 42
134
+ # teardown
135
+
136
+ # do some teardown task
98
137
  ```
99
- The hook methods defined in the [minitest-hooks](https://github.com/jeremyevans/minitest-hooks?tab=readme-ov-file#in-tests-minitesttest-) extension (`before_all`, `after_all`, `around` & `around_all`)are also available in this way if `minitest-hooks` is installed and `Minitest::Hooks` is included in your test class.
138
+ The hook methods defined in the [minitest-hooks](https://github.com/jeremyevans/minitest-hooks?tab=readme-ov-file#in-tests-minitesttest-) extension (`before_all`, `after_all`, `around` & `around_all`)are also available in this way if `minitest-hooks` is installed and `Minitest::Hooks` is included in your markdown test class. See the 'In your test class' section above for an example. You can now do this:
100
139
 
140
+ ```ruby
141
+ # before_all
142
+ @before_all_instance_var = 'foo'
143
+ ```
101
144
 
102
- Everything in the code blocks above runs as test code. [minitest-proveit](https://github.com/seattlerb/minitest-proveit) would complain otherwise ;-)
145
+ Everything in the Ruby code blocks above and below here runs as test code. [minitest-proveit](https://github.com/seattlerb/minitest-proveit) would complain otherwise ;-)
146
+
147
+ ### Mocks
148
+
149
+ Mocks use the "mock" keyword for [assert_mock](https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-assert_mock) in place of the equivalent `mock.verify`:
150
+ ```ruby
151
+ @mymock = Minitest::Mock.new
152
+ @mymock.expect(:puts, nil, ['Hello World!'])
153
+
154
+ @mymock.puts 'Hello World!'
155
+ # => mock @mymock
156
+ ```
103
157
 
104
- ## Stubbing
158
+ ### Stubbing
105
159
 
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:
160
+ 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. **Important: any 'state' blocks are ignored for test code block indexing**. The hash value must be an instance of `Minitest::StubChain` which holds a proc for each stub in the chain. For convenience `StubChain.stubproc` returns a suitable stub proc object which is called around the relevant test code. Zero or more of these reusable procs can be used to instantiate a StubChain object:
107
161
  ```ruby
108
162
  class MarkdownTest < Minitest::Test
109
163
  set_stubs_new = Minitest::StubChain.stubproc(Set, :new, []) # returns a proc which stubs Set.new to return an empty array
110
164
  array_stubs_size = Minitest::StubChain.stubproc(Array, :size, 42, any_instance: true) # uses the bundled minitest-stub_any_instance gem
111
165
 
166
+ stub_chain = Minitest::StubChain.new([set_stubs_new]) # initialized with zero or more stub procs
167
+ stub_chain.stubs << array_stubs_size # add another like this if need be
168
+
112
169
  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
170
+ stubs[10] = stub_chain
171
+ stubs[11] = stub_chain # StubChain instances themselves may be reused
116
172
 
117
173
  Markdown.generate_markdown_tests(self, stubs: stubs)
118
174
  end
119
- # => nil
175
+ # => truthy
120
176
  ```
121
- The 2 stubs above are demonstated in the following examples:
177
+ The 2 stubs in the `StubChain` instance above are demonstrated in the following example:
122
178
  ```ruby
123
- # This is test_block8
124
- Set.new([1, 'c', :s]).size # Here Set.new was stubbed to return an Array and Array#size was stubbed to return 42
179
+ # This is test_block10
180
+ Set.new([1, 'c', :s]).size
125
181
  # => 42
182
+
183
+ # Because we added `stubs[10] = stub_chain` above, this is exactly equivalent to:
184
+ #
185
+ # def test_block10
186
+ # Set.stub(:new, []) do
187
+ # Array.stub_any_instance(:size, 42) do
188
+ # assert_equal 42, Set.new([1, 'c', :s]).size
189
+ # end
190
+ # end
191
+ # end
126
192
  ```
127
193
  Example showing the reuse of a StubChain:
128
194
  ```ruby
129
- # This is test_block9
130
- Set.new([]).size
195
+ # This is test_block11
196
+ Set.new.size
131
197
  # => 42
132
198
  ```
133
199
 
134
- ## Configuration
135
-
136
- No configuation is required if you use Bundler. If not, set your `project_root` path using the setter method:
137
- ```ruby
138
- @config = Markdown.config
139
- # => instance_of Configuration
200
+ ### Errors
140
201
 
141
- Configuration.instance_methods(false)
142
- # => includes :project_root=
143
- ```
202
+ All errors subclass `Markdown::Error`
144
203
 
145
204
  ## Development
146
205
 
@@ -150,7 +209,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
150
209
 
151
210
  ## Contributing
152
211
 
153
- 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.
212
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/matzfan/minitest-markdown. Please checkout a suitably named branch before submitting a PR.
154
213
 
155
214
  ## License
156
215
 
@@ -2,29 +2,32 @@
2
2
 
3
3
  require 'pathname'
4
4
 
5
- require_relative 'error'
5
+ require_relative 'errors'
6
6
 
7
7
  module Minitest
8
8
  module Markdown
9
9
  # configuration for gem
10
10
  class Configuration
11
- attr_accessor :project_root, :prove_it
11
+ PROJECT_ROOT_UNDETERMINED = "Project root can't be determined, set with 'Markdown.config.project_root = /path'"
12
+
13
+ attr_writer :project_root
14
+ attr_accessor :prove_it
12
15
 
13
16
  def initialize
14
- @project_root = determine_project_root
17
+ @project_root = self.class.determine_project_root
15
18
  @prove_it = true
16
19
  end
17
20
 
18
- def readme_path
19
- project_root.join 'README.md'
21
+ def self.determine_project_root
22
+ Bundler.root if defined? Bundler
20
23
  end
21
24
 
22
- private
23
-
24
- def determine_project_root
25
- return Bundler.root if defined? Bundler
25
+ def project_root
26
+ @project_root || (raise Error, PROJECT_ROOT_UNDETERMINED)
27
+ end
26
28
 
27
- raise Error::MarkdownError, "Project root can't be determined, set with 'Config.project_root = /root/path'"
29
+ def readme_path
30
+ project_root.join 'README.md'
28
31
  end
29
32
  end
30
33
  end
@@ -2,8 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Markdown
5
- module Error
6
- class MarkdownError < StandardError; end
7
- end
5
+ class Error < StandardError; end
8
6
  end
9
7
  end
@@ -1,39 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'minifyrb' # DEP
3
+ require 'minifyrb'
4
4
 
5
5
  require_relative '../assertions_extensions' # for assert_truthy
6
- require_relative '../markdown/error'
6
+ require_relative '../markdown/errors'
7
7
 
8
8
  module Minitest
9
9
  module Markdown
10
- # A Ruby code block reprenting test code, with 0 or more assertions
11
- class TestCodeBlock
12
- ASSERT_ = 'assert_'
13
- ASSERT_REGEXP = /\A#{ASSERT_}/
14
- SKIP = { skip: :skip }.freeze
10
+ # A Ruby code block representing test code, with 0 or more assertions or a 'state' block
11
+ class RubyCodeBlock
12
+ ASSERT_ = 'assert_'
13
+ ASSERT_REGEXP = /\A#{ASSERT_}/
14
+ SKIP = { skip: :skip }.freeze
15
15
  DEFAULT_ASSERTION = :assert_equal
16
16
 
17
- ASSERTION_KEYS = %i[ruby assertion test_args].freeze
17
+ ASSERTION_KEYS = %i[ruby assertion test_args].freeze
18
+ STATE_BLOCK_TYPES = %i[setup teardown before_all after_all around around_all].freeze
18
19
 
19
- MAGIC_COMMENT_DELIMITER = '# => '
20
- MAGIC_COMMENT_REGEXP = /^#{MAGIC_COMMENT_DELIMITER}.*$/
21
- MAGIC_COMMENT_SCAN_REGEXP = /^#{MAGIC_COMMENT_DELIMITER}(.*)$/ # strips delimiter prefix
20
+ MAGIC_COMMENT_REGEXP = /^\s*#\s*=>.*$/
21
+ MAGIC_COMMENT_SCAN_REGEXP = /^\s*#\s*=>\s*(.*)$/ # strips delimiter prefix
22
+
23
+ MISSING_ASSERTION_OR_VALUE = "Magic comment missing assertion or value. Something must follow '# =>'"
22
24
 
23
25
  attr_reader :fenced_block_str
24
26
 
25
27
  def self.asserts
26
- Assertions.instance_methods.grep(ASSERT_REGEXP) - [:assert_send] # deprecated
28
+ @asserts ||= Assertions.instance_methods.grep(ASSERT_REGEXP) - [:assert_send] # deprecated
27
29
  end
28
30
 
29
31
  def self.assertions_map
30
- asserts.inject({}) { |memo, m| memo.merge(m.to_s.split(ASSERT_)[1..].join.to_sym => m) }.merge(SKIP)
32
+ @assertions_map ||= asserts.inject(SKIP) { |memo, m| memo.merge(m.to_s.split(ASSERT_)[1..].join.to_sym => m) }
31
33
  end
32
34
 
33
35
  def initialize(fenced_block_str)
34
36
  @fenced_block_str = fenced_block_str
35
37
  end
36
38
 
39
+ def type
40
+ STATE_BLOCK_TYPES.each { |type| return type if fenced_block_str.lines.first&.match?(/^\s*#\s*#{type}\s*\n$/) }
41
+
42
+ :test
43
+ end
44
+
37
45
  def assertions
38
46
  assertions_arr.map { |arr| Hash[*ASSERTION_KEYS.zip(arr).flatten] }
39
47
  end
@@ -45,6 +53,8 @@ module Minitest
45
53
  end
46
54
 
47
55
  def parse(magic_comment_string)
56
+ raise ArgumentError, MISSING_ASSERTION_OR_VALUE if magic_comment_string.strip.empty?
57
+
48
58
  assertion = parse_assertion(magic_comment_string)
49
59
  return [DEFAULT_ASSERTION, "[#{magic_comment_string}]"] if assertion.nil?
50
60
  return [assertion, '[]'] if magic_comment_string.split.size == 1
@@ -53,7 +63,7 @@ module Minitest
53
63
  end
54
64
 
55
65
  def parse_assertion(str)
56
- self.class.assertions_map[str.split.first.to_sym]
66
+ self.class.assertions_map[str.split.first.to_sym] # 'nil' => :assert_nil, but 'nil?' => nil
57
67
  end
58
68
 
59
69
  def magic_comments
@@ -64,7 +74,7 @@ module Minitest
64
74
  @fenced_block_str.split(MAGIC_COMMENT_REGEXP).map do |str|
65
75
  Minifyrb::Minifier.new(str).minify.strip
66
76
  rescue SyntaxError
67
- raise Error::MarkdownError, "Syntax error in:\n\n#{str}"
77
+ raise Error, "Syntax error in:\n\n#{str}"
68
78
  end
69
79
  end
70
80
  end
@@ -1,67 +1,74 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../assertions_extensions'
4
- require_relative 'test_code_block'
4
+ require_relative 'ruby_code_block'
5
5
 
6
6
  $LOAD_PATH << '.' # fix #2
7
7
 
8
8
  module Minitest
9
9
  module Markdown
10
- # knows how to build a markdown test
10
+ # knows how to build a markdown test class, test methods and 'state' methods
11
11
  class TestClass
12
- FENCED_BLOCK_REGEXP = /^```ruby\n(.*?)\n```/m # TODO: use Lexer/RDoc::Parser? which can deal with md comments
13
- HOOK_METHODS = %i[before_all after_all around around_all].freeze # minitest-hooks extension
14
- NON_TEST_METHODS = %i[setup teardown].freeze
12
+ FENCED_BLOCK_REGEXP = /^```ruby\n(.*?)\n```/m
13
+
14
+ BAD_KLASS = 'TestClass must be instantiated with Minitest::Test or subclass thereof'
15
+ BAD_PATH = 'Path does not exist, is not readable or is not a Markdown file:'
16
+ MD = '.md'
17
+
18
+ HOOK_METHODS_BEFORE = %i[before_all].freeze
19
+ HOOK_METHODS_AFTER = %i[after_all around around_all].freeze
15
20
 
16
21
  attr_reader :klass, :path
17
22
 
18
23
  def initialize(klass, path: nil)
19
- @klass = klass
20
- @config = Markdown.config
21
- @path = path || @config.readme_path
22
- @ruby_blocks = parse_ruby_blocks
24
+ @klass = validate_class klass
25
+ @path = validate_path path
26
+ @state_methods_defined = []
23
27
  end
24
28
 
25
29
  def define_methods(stubs: {})
26
- define_non_test_methods
27
- @ruby_blocks.each_with_index { |block, i| define_test_method(block, i, stubs[i]) }
28
- nil
30
+ i = 0
31
+ parse_ruby_blocks.each do |block|
32
+ next define_non_test_method(block) unless block.type == :test
33
+
34
+ define_test_method(block, i, stub_chain: stubs[i])
35
+ i += 1
36
+ end
29
37
  end
30
38
 
31
39
  private
32
40
 
33
- def parse_ruby_blocks
34
- File.new(path).read.scan(FENCED_BLOCK_REGEXP).flatten.map { |str| TestCodeBlock.new(str) }
41
+ def validate_class(klass)
42
+ raise ArgumentError, BAD_KLASS unless klass.respond_to?(:ancestors) && klass.ancestors.include?(Minitest::Test)
43
+
44
+ klass
35
45
  end
36
46
 
37
- def define_non_test_methods
38
- recognized_non_test_methods.each { |name| define_non_test_method(name, parse_non_test_method_block(name)) }
47
+ def validate_path(pth)
48
+ return Markdown.config.readme_path if pth.nil?
49
+ raise ArgumentError, "#{BAD_PATH} #{pth}" unless File.exist?(pth) && File.file?(pth) && File.extname(pth) == MD
50
+
51
+ pth
39
52
  end
40
53
 
41
- def recognized_non_test_methods
42
- Minitest.const_defined?(:Hooks) ? NON_TEST_METHODS + HOOK_METHODS : NON_TEST_METHODS
54
+ def parse_ruby_blocks
55
+ File.new(path).read.scan(FENCED_BLOCK_REGEXP).flatten.map { |str| RubyCodeBlock.new(str) }
43
56
  end
44
57
 
45
- def define_non_test_method(type, block)
46
- return unless block
58
+ def define_non_test_method(block)
59
+ type = block.type
60
+ raise Error, "State method: #{type} is already defined" if @state_methods_defined.include? type
47
61
 
48
62
  bind = binding
49
63
  klass.define_method(type) do
50
- super() if type == :before_all
51
- bind.eval(block)
52
- super() if %i[after_all around around_all].include? type
64
+ super() if HOOK_METHODS_BEFORE.include? type
65
+ bind.eval(block.fenced_block_str)
66
+ super() if HOOK_METHODS_AFTER.include? type
53
67
  end
68
+ @state_methods_defined << type
54
69
  end
55
70
 
56
- def parse_non_test_method_block(type)
57
- blocks = @ruby_blocks.map.select { |blk| blk.fenced_block_str.lines.first == "# #{type}\n" }
58
- return if blocks.empty?
59
- raise Error::MarkdownError, "Multiple #{type} blocks are not allowed" if blocks.size > 1
60
-
61
- @ruby_blocks.delete(blocks.first).fenced_block_str # hook method blocks can't be test methods
62
- end
63
-
64
- def define_test_method(block, meth_index, stub_chain = nil)
71
+ def define_test_method(block, meth_index, stub_chain:)
65
72
  stub_chain ||= StubChain.new
66
73
  instance = self # scope
67
74
  klass.define_method(:"test_block#{meth_index}") do
@@ -73,9 +80,9 @@ module Minitest
73
80
  end
74
81
 
75
82
  def evaluation_assertions(assertion_hash, bind)
76
- ruby, assertion, args = TestCodeBlock::ASSERTION_KEYS.map { |key| assertion_hash[key] } # order dep
83
+ ruby, assertion, args = RubyCodeBlock::ASSERTION_KEYS.map { |key| assertion_hash[key] } # order dep
77
84
 
78
- lmbda = -> { eval(ruby) } # rubocop:disable Security/Eval
85
+ lmbda = -> { kernel_eval(ruby) }
79
86
  return unless assertion
80
87
  return eval_with_block(bind, assertion, lmbda, args) if Assertions::WITH_BLOCK_EVAL.include? assertion
81
88
 
@@ -85,20 +92,27 @@ module Minitest
85
92
  def eval_with_block(bind, assertion, actual, args)
86
93
  return bind.receiver.send(assertion, &actual) if args.empty?
87
94
 
88
- bind.receiver.send(assertion, *eval(args), &actual) # rubocop:disable Security/Eval
95
+ bind.receiver.send(assertion, *kernel_eval(args), &actual)
89
96
  end
90
97
 
91
98
  def eval_without_block(bind, assertion, lmbda, args)
92
99
  actual = lmbda.call
93
100
  return bind.receiver.send(assertion, actual) if args == '[]'
94
101
 
95
- expected, *rest = eval(args) # rubocop:disable Security/Eval
102
+ expected, *rest = kernel_eval(args)
96
103
  expected, actual = actual, expected if Assertions::EXPECTED_ACTUAL_REVERSED.include? assertion
97
104
  kwargs = rest.last.is_a?(Hash) ? rest.last : nil
98
105
  return bind.receiver.send(assertion, expected, actual, *rest) unless kwargs
99
106
 
100
107
  bind.receiver.send(assertion, expected, actual, *rest, **kwargs) # assert_respond_to takes a kwarg..
101
108
  end
109
+
110
+ def kernel_eval(str)
111
+ bind = binding
112
+ bind.eval str
113
+ rescue SyntaxError
114
+ raise ArgumentError, "Invalid test code, failed to parse #{str}"
115
+ end
102
116
  end
103
117
  end
104
118
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Markdown
5
- VERSION = '0.2.1.pre'
5
+ VERSION = '0.2.3'
6
6
  end
7
7
  end
@@ -4,7 +4,7 @@ require 'minitest/stub_any_instance'
4
4
 
5
5
  require_relative 'stub_chain'
6
6
  require_relative 'markdown/configuration'
7
- require_relative 'markdown/error'
7
+ require_relative 'markdown/errors'
8
8
  require_relative 'markdown/version'
9
9
  require_relative 'markdown/test_class'
10
10
 
@@ -12,10 +12,10 @@ module Minitest
12
12
  @stubs = *stubs
13
13
  end
14
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
15
+ def self.stubproc(klass, *args, any_instance: false, **kwargs)
16
+ return proc { |&blk| klass.stub(*args, **kwargs) { blk.call } } unless any_instance
17
17
 
18
- proc { |&blk| klass.stub_any_instance(method, ret_val) { blk.call } }
18
+ proc { |&blk| klass.stub_any_instance(*args, **kwargs) { blk.call } }
19
19
  end
20
20
 
21
21
  def call(test_code_proc)
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.2.1.pre
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - MatzFan
@@ -77,9 +77,9 @@ files:
77
77
  - lib/minitest/assertions_extensions.rb
78
78
  - lib/minitest/markdown.rb
79
79
  - lib/minitest/markdown/configuration.rb
80
- - lib/minitest/markdown/error.rb
80
+ - lib/minitest/markdown/errors.rb
81
+ - lib/minitest/markdown/ruby_code_block.rb
81
82
  - lib/minitest/markdown/test_class.rb
82
- - lib/minitest/markdown/test_code_block.rb
83
83
  - lib/minitest/markdown/version.rb
84
84
  - lib/minitest/stub_chain.rb
85
85
  - sig/minitest/markdown.rbs