Module: Varar::Minitest

Defined in:
lib/varar/minitest.rb

Overview

Minitest adapter. One call turns every spec matched by varar.config.json into a generated Minitest::Test subclass — one class per spec file, one test method per example. Mirrors var-unittest.

# test/var_test.rb
require "varar/minitest"
Varar::Minitest.generate_tests

Constant Summary collapse

VERSION =
'0.5.2'

Class Method Summary collapse

Class Method Details

.build_test_case(spec_path, root, loaded, store, update) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/varar/minitest.rb', line 33

def build_test_case(spec_path, root, loaded, store, update)
  rel = Runner.rel_posix(spec_path, root)
  source = File.read(spec_path, encoding: 'UTF-8')
  plan = Runner.plan_spec(File.basename(spec_path), source, loaded.registry)
  pairs = Runner.examples_with_runs(plan, loaded.create_context, Runner::RecordingReporter.new)

  klass = Class.new(::Minitest::Test)
  seen = Hash.new(0)
  pairs.each do |example, run|
    base = example.scope_stack.last || example.name
    stem = identifier(base)
    idx = seen[stem]
    seen[stem] += 1
    method_name = idx.zero? ? "test_#{stem}" : "test_#{stem}_#{idx}"
    klass.define_method(method_name) do
      run.call
    rescue StandardError => e
      raise ::Minitest::Assertion, Runner.render_failure(e, source, rel) if Minitest.var_diff_error?(e)

      raise
    end
  end

  Core::Drifts.reconcile_drift(store, rel, source, plan.var_doc, plan, update: update).each do |drift|
    message = Core::Diagnostics.drift_detected(drift.name, drift.span).message
    klass.define_method("test_var_drift_#{drift.line}") { raise ::Minitest::Assertion, message }
  end

  klass
end

.generate_tests(namespace = Object, root: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/varar/minitest.rb', line 19

def generate_tests(namespace = Object, root: nil)
  root ||= File.dirname(caller_locations(1, 1).first.path)
  root = File.expand_path(root)
  cfg = Config.read_var_config(root)
  loaded = Runner.load_steps(cfg.steps, root)
  store = Runner.create_file_baseline_store(root)
  update = %w[1 true].include?(ENV.fetch('VAR_UPDATE', nil))

  Runner.find_specs(cfg.docs_include, cfg.docs_exclude, root).each do |spec_path|
    klass = build_test_case(spec_path, root, loaded, store, update)
    namespace.const_set("Var_#{identifier(Runner.rel_posix(spec_path, root))}", klass)
  end
end

.identifier(text) ⇒ Object

Project arbitrary text onto a valid identifier fragment.



72
73
74
75
76
77
# File 'lib/varar/minitest.rb', line 72

def identifier(text)
  ident = text.gsub(/\W+/, '_').gsub(/\A_+|_+\z/, '')
  ident = 'example' if ident.empty?
  ident = "_#{ident}" if ident.match?(/\A\d/)
  ident
end

.var_diff_error?(error) ⇒ Boolean

A markdown/return mismatch is a test failure (Minitest::Assertion); any other exception propagates as an error.

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/varar/minitest.rb', line 66

def var_diff_error?(error)
  error.is_a?(Core::CellMismatchError) || error.is_a?(Core::DocStringMismatchError) ||
    error.is_a?(Core::ReturnShapeError) || error.is_a?(Core::UnexpectedPassError)
end