minitest-markdown 0.0.0.pre → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d1ee2803d0dd9c5009efdbfb7510395c875617f325d9f506b3e6f5dcf011565
4
- data.tar.gz: 39534d84dbfdef4d58e1c55e9e5479e3b9d697637cd04dd82a75b3e2892b57c2
3
+ metadata.gz: c4e1e286d2eead001d4b6e0d361aa9bb5c0a7019a5eb8d72eacf6635003e59da
4
+ data.tar.gz: 3f95349b886aa8752280e2fb2b5083d5bfc754ec8c4faed065dbb2a6e9b4d7e5
5
5
  SHA512:
6
- metadata.gz: 1703b4fe13cd34c08c999dfc5dbbe8a5cd85a439e9411b3c645575424d0df365798c85745707c275dd59e750ff34eb1ee67ec30724d4f45700d78f66c297a59e
7
- data.tar.gz: fc492517b52b8c6542d444dba33f2b5c0f532c156b19d6d2c336a0f11e3bf6ee717aab2652334b7a2f37ecce1cbb6cbfbbe56a9affa25e06339cffa60213edb3
6
+ metadata.gz: cd12cc6d7df1724100119b6e03e5960411d7d83b4de26beb0d4bbde0cc3e3f7a66e94901e5f99f051a82f70e058ffa1b103ee0726d103765bb9c405d661387e1
7
+ data.tar.gz: 11a4a61ba9044aec6811b9df724a111fa5345b29d837d91c8ee76a73e95939ca760243423e40127597d3b3ea0f040e633e31001d4268f33c8e06b5545efa0e06
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- require:
2
+ plugins:
3
3
  - rubocop-minitest
4
4
  - rubocop-performance
5
5
  - rubocop-rake
@@ -7,6 +7,9 @@ require:
7
7
  AllCops:
8
8
  TargetRubyVersion: 3.1
9
9
  NewCops: enable
10
+ Exclude:
11
+ - 'vendor/**/*' # GitLab caching..
12
+ - test/fixtures/klass.rb
10
13
 
11
14
  Lint/InterpolationCheck:
12
15
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.0.pre] - 2025-05-01
4
+
5
+ - Add ability to pass stubs to test class
6
+ - Add dependency; minitest-stub_any_instance
7
+
8
+ ## [0.0.1.pre] - 2025-04-24
9
+
10
+ - Bump minitest dep to >= 5.25.2
11
+ - Fix 'method redefined' warning in tests
12
+ - Fix undefined method 'assert' for module 'Minitest::Assertions' in AssertionExtensions
13
+ - Fix bug with expected value strings with multiple spaces
14
+
3
15
  ## [0.0.0.pre] - 2024-09-26
4
16
 
5
17
  - Initial release
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/minitest-markdown.svg)](https://badge.fury.io/rb/minitest-markdown)
2
+ [![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)
3
+
1
4
  # Minitest extension for testing Ruby code blocks in your README and other Markdown files
2
5
 
3
6
  ## \_why?
@@ -27,7 +30,7 @@ To test the Ruby blocks in your README file, create file `test_readme.rb` (for e
27
30
  require 'minitest/autorun' # or in your test_helper
28
31
  require 'minitest/markdown' # ditto
29
32
 
30
- class ReadmeTest < MyTest # your own subclass of Minitest::Test
33
+ class ReadmeTest < Minitest::Test # or your own subclass of Minitest::Test
31
34
  Markdown.generate_markdown_tests(self)
32
35
  end
33
36
  # => nil
@@ -38,24 +41,23 @@ To test Ruby blocks in another Markdown file, create another test file and pass
38
41
 
39
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:
40
43
  ```ruby
41
- # the code will of course be in your lib's, but for demonstration purposes:
42
- class Foo
43
- def bar
44
- 'Hello Markdown!'
45
- end
46
- end
44
+ File.read 'test/fixtures/klass.rb'
45
+ # => "class Klass\n def hello\n 'Hello Markdown!'\n end\nend"
46
+
47
+ require 'test/fixtures/klass' # a demonstration
48
+ # => true
47
49
 
48
50
  # ordinary comments are ignored.
49
51
 
50
- foo = Foo.new # inline comments are also ignored
52
+ Klass.new # inline comments are also ignored
51
53
  # The assertion and expected value are:-
52
- # => instance_of Foo
54
+ # => instance_of Klass
53
55
 
54
56
  # No keywword here, so the default assertion is used (assert_equal)
55
- Foo.new.bar
57
+ Klass.new.hello
56
58
  # => 'Hello Markdown!'
57
59
 
58
- Foo.bar
60
+ Klass.hello
59
61
  # => raises NoMethodError
60
62
  ```
61
63
  Plain old `assert` has been aliased as `assert_truthy`, so when expecting a truthy value you should do this:
@@ -99,6 +101,34 @@ The hook methods defined in the [minitest-hooks](https://github.com/jeremyevans/
99
101
 
100
102
  Everything in the code blocks above runs as test code. [minitest-proveit](https://github.com/seattlerb/minitest-proveit) would complain otherwise ;-)
101
103
 
104
+ ## Stubbing
105
+
106
+ It is possible to pass stubs to the generated tests. This is done by passing a hash of procs using the stubs keyword. Hash keys represent the index of the text code block and the key is a proc which is called around the relevant test code. 2 convenience methods are provided by the `Markdown::Stubb` module. In your markdown test:
107
+ ```ruby
108
+ class MarkdownTest < Minitest::Test
109
+ extend Markdown::Stubb # for the stubb & stubb_any_instance methods
110
+
111
+ stubs = {}
112
+ stubs[8] = stubb(Set, :new, []) # returns a proc
113
+ stubs[9] = stubb_any_instance(Set, :size, 42) # uses the bundled `minitest-stub_any_instance` extension
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]) # 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
128
+ # => 42
129
+ ```
130
+ Presently only one stub per test is possible.
131
+
102
132
  ## Configuration
103
133
 
104
134
  No configuation is required if you use Bundler. If not, set your `project_root` path using the setter method:
@@ -118,7 +148,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
118
148
 
119
149
  ## Contributing
120
150
 
121
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/minitest-markdown. Please checkout a suitably named branch before submitting a PR.
151
+ 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.
122
152
 
123
153
  ## License
124
154
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'minitest/assertions'
4
+
3
5
  module Minitest
4
6
  # patch
5
7
  module Assertions
@@ -7,11 +9,11 @@ module Minitest
7
9
  alias assert_included_in assert_includes
8
10
 
9
11
  # extensions
10
- module AssertionExtensions
12
+ module AssertionsExtensions
11
13
  WITH_BLOCK_EVAL = %i[assert_output assert_pattern assert_raises assert_silent assert_throws refute_pattern].freeze
12
14
  EXPECTED_ACTUAL_REVERSED = %i[assert_includes assert_operator assert_predicate assert_respond_to].freeze
13
15
  end
14
16
 
15
- Assertions.prepend AssertionExtensions
17
+ Assertions.prepend AssertionsExtensions
16
18
  end
17
19
  end
@@ -0,0 +1,16 @@
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
@@ -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,17 +59,20 @@ 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 = nil)
63
+ stub ||= proc { |&blk| blk.call } # empty proc if no stub provided
63
64
  instance = self # scope
64
65
  klass.define_method(:"test_block#{meth_index}") do
65
- block.assertions.each { |assertion_hash| instance.send(:evaluation_assertions, assertion_hash, binding) }
66
+ stub.call do
67
+ block.assertions.each { |assertion_hash| instance.send(:evaluation_assertions, assertion_hash, binding) }
68
+ end
66
69
  end
67
70
  end
68
71
 
69
72
  def evaluation_assertions(assertion_hash, bind)
70
73
  ruby, assertion, args = TestCodeBlock::ASSERTION_KEYS.map { |key| assertion_hash[key] } # order dep
71
74
 
72
- lmbda = -> { eval(ruby) } # rubocop:disable Security/Eval # TODO: assign a binding
75
+ lmbda = -> { eval(ruby) } # rubocop:disable Security/Eval
73
76
  return unless assertion
74
77
  return eval_with_block(bind, assertion, lmbda, args) if Assertions::WITH_BLOCK_EVAL.include? assertion
75
78
 
@@ -45,9 +45,15 @@ module Minitest
45
45
  end
46
46
 
47
47
  def parse(magic_comment_string)
48
- arr = magic_comment_string.split
49
- assertion = self.class.assertions_map[arr.first.to_sym]
50
- assertion ? [assertion, "[#{arr[1..].join(' ')}]"] : [DEFAULT_ASSERTION, "[#{arr.join(' ')}]"] # args in []
48
+ assertion = parse_assertion(magic_comment_string)
49
+ return [DEFAULT_ASSERTION, "[#{magic_comment_string}]"] if assertion.nil?
50
+ return [assertion, '[]'] if magic_comment_string.split.size == 1
51
+
52
+ [assertion, "[#{magic_comment_string.sub(/#{assertion.to_s.sub(ASSERT_, '')}\s+/, '')}]"]
53
+ end
54
+
55
+ def parse_assertion(str)
56
+ self.class.assertions_map[str.split.first.to_sym]
51
57
  end
52
58
 
53
59
  def magic_comments
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Markdown
5
- VERSION = '0.0.0.pre'
5
+ VERSION = '0.1.0.pre'
6
6
  end
7
7
  end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'minitest/stub_any_instance'
4
+
3
5
  require_relative 'markdown/configuration'
4
6
  require_relative 'markdown/error'
7
+ require_relative 'markdown/stubb'
5
8
  require_relative 'markdown/version'
6
9
  require_relative 'markdown/test_class'
7
10
 
@@ -17,8 +20,8 @@ module Minitest
17
20
  yield config
18
21
  end
19
22
 
20
- def generate_markdown_tests(klass, path: nil)
21
- TestClass.new(klass, path: path).define_methods
23
+ def generate_markdown_tests(klass, path: nil, stubs: {})
24
+ TestClass.new(klass, path: path).define_methods(stubs: stubs)
22
25
  end
23
26
  end
24
27
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.pre
4
+ version: 0.1.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - MatzFan
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-09-26 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: minifyrb
@@ -37,6 +36,9 @@ dependencies:
37
36
  - - "~>"
38
37
  - !ruby/object:Gem::Version
39
38
  version: '5.25'
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 5.25.2
40
42
  type: :runtime
41
43
  prerelease: false
42
44
  version_requirements: !ruby/object:Gem::Requirement
@@ -44,8 +46,24 @@ dependencies:
44
46
  - - "~>"
45
47
  - !ruby/object:Gem::Version
46
48
  version: '5.25'
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
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'
47
66
  description: Generates tests for Ruby code blocks in any Markdown file.
48
- email:
49
67
  executables: []
50
68
  extensions: []
51
69
  extra_rdoc_files: []
@@ -60,6 +78,7 @@ files:
60
78
  - lib/minitest/markdown.rb
61
79
  - lib/minitest/markdown/configuration.rb
62
80
  - lib/minitest/markdown/error.rb
81
+ - lib/minitest/markdown/stubb.rb
63
82
  - lib/minitest/markdown/test_class.rb
64
83
  - lib/minitest/markdown/test_code_block.rb
65
84
  - lib/minitest/markdown/version.rb
@@ -71,7 +90,6 @@ metadata:
71
90
  source_code_uri: https://gitlab.com/matzfan/minitest-markdown
72
91
  changelog_uri: https://gitlab.com/matzfan/minitest-markdown/-/blob/master/CHANGELOG.md
73
92
  rubygems_mfa_required: 'true'
74
- post_install_message:
75
93
  rdoc_options: []
76
94
  require_paths:
77
95
  - lib
@@ -86,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
104
  - !ruby/object:Gem::Version
87
105
  version: '0'
88
106
  requirements: []
89
- rubygems_version: 3.5.19
90
- signing_key:
107
+ rubygems_version: 3.6.8
91
108
  specification_version: 4
92
109
  summary: Turn your README.md Ruby code blocks into testable code.
93
110
  test_files: []