Class: Minitest::Markdown::RubyCodeBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/markdown/ruby_code_block.rb

Overview

A Ruby code block representing test code, with 0 or more assertions or a ‘state’ block

Constant Summary collapse

ASSERT_ =
'assert_'
ASSERT_REGEXP =
/\A#{ASSERT_}/
SKIP =
{ skip: :skip }.freeze
DEFAULT_ASSERTION =
:assert_equal
ASSERTION_KEYS =
%i[ruby assertion test_args].freeze
STATE_BLOCK_TYPES =
%i[setup teardown before_all after_all around around_all].freeze
MAGIC_COMMENT_REGEXP =
/^\s*#\s*=>.*$/
MAGIC_COMMENT_SCAN_REGEXP =

strips delimiter prefix

/^\s*#\s*=>\s*(.*)$/
MISSING_ASSERTION_OR_VALUE =
"Magic comment missing assertion or value. Something must follow '# =>'"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fenced_block_str) ⇒ RubyCodeBlock

Returns a new instance of RubyCodeBlock.



35
36
37
# File 'lib/minitest/markdown/ruby_code_block.rb', line 35

def initialize(fenced_block_str)
  @fenced_block_str = fenced_block_str
end

Instance Attribute Details

#fenced_block_strObject (readonly)

Returns the value of attribute fenced_block_str.



25
26
27
# File 'lib/minitest/markdown/ruby_code_block.rb', line 25

def fenced_block_str
  @fenced_block_str
end

Class Method Details

.assertions_mapObject



31
32
33
# File 'lib/minitest/markdown/ruby_code_block.rb', line 31

def self.assertions_map
  @assertions_map ||= asserts.inject(SKIP) { |memo, m| memo.merge(m.to_s.split(ASSERT_)[1..].join.to_sym => m) }
end

.assertsObject



27
28
29
# File 'lib/minitest/markdown/ruby_code_block.rb', line 27

def self.asserts
  @asserts ||= Assertions.instance_methods.grep(ASSERT_REGEXP) - [:assert_send] # deprecated
end

Instance Method Details

#assertionsObject



45
46
47
# File 'lib/minitest/markdown/ruby_code_block.rb', line 45

def assertions
  assertions_arr.map { |arr| Hash[*ASSERTION_KEYS.zip(arr).flatten] }
end

#typeObject



39
40
41
42
43
# File 'lib/minitest/markdown/ruby_code_block.rb', line 39

def type
  STATE_BLOCK_TYPES.each { |type| return type if fenced_block_str.lines.first&.match?(/^\s*#\s*#{type}\s*\n$/) }

  :test
end