Class: Tryouts::Translators::MinitestTranslator
- Inherits:
-
Object
- Object
- Tryouts::Translators::MinitestTranslator
- Defined in:
- lib/tryouts/translators/minitest_translator.rb
Overview
Translates Tryouts test files to Minitest format
IMPORTANT: Context Mode Differences
Tryouts supports two context modes that behave differently than Minitest:
-
Tryouts Shared Context (default):
- Setup runs once, all tests share the same context object
- Tests can modify variables/state and affect subsequent tests
- Behaves like a Ruby script executing top-to-bottom
- Designed for documentation-style tests where examples build on each other
-
Tryouts Fresh Context (--no-shared-context):
- Setup @instance_variables are copied to each test's fresh context
- Tests are isolated but inherit setup state
- Similar to Minitest's setup method but with setup state inheritance
Minitest Translation Behavior:
- Uses setup method which runs before each test (Minitest standard)
- Each test method gets fresh context (Minitest standard)
- Tests that rely on shared state between test cases WILL FAIL
- This is intentional and reveals inappropriate test dependencies
Example that works in Tryouts shared mode but fails in Minitest:
## TEST 1
@counter = 1
@counter
#=> 1
## TEST 2
@counter += 1 # Will be reset to 1 by setup, then fail
@counter
#=> 2
Recommendation: Write tryouts tests that work in fresh context mode if you plan to use Minitest translation.
Class Method Summary collapse
-
.parameterize(string) ⇒ Object
Simple string parameterization for method names.
Instance Method Summary collapse
- #generate_code(testrun) ⇒ Object
-
#initialize ⇒ MinitestTranslator
constructor
A new instance of MinitestTranslator.
- #translate(testrun) ⇒ Object
Constructor Details
#initialize ⇒ MinitestTranslator
Returns a new instance of MinitestTranslator.
55 56 57 58 59 |
# File 'lib/tryouts/translators/minitest_translator.rb', line 55 def initialize require 'minitest/test' rescue LoadError raise 'Minitest gem is required for Minitest translation' end |
Class Method Details
.parameterize(string) ⇒ Object
Simple string parameterization for method names.
Defined on the class because translate calls it from inside a
Class.new(Minitest::Test) do ... end block, where self is the
anonymous test class rather than the translator instance.
51 52 53 |
# File 'lib/tryouts/translators/minitest_translator.rb', line 51 def self.parameterize(string) string.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/^_|_$/, '') end |
Instance Method Details
#generate_code(testrun) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/tryouts/translators/minitest_translator.rb', line 125 def generate_code(testrun) file_basename = File.basename(testrun.source_file, '.rb') class_name = "Test#{file_basename.gsub(/[^A-Za-z0-9]/, '')}" lines = [] lines << '' lines << "require 'minitest/test'" lines << "require 'minitest/autorun'" lines << '' lines << "class #{class_name} < Minitest::Test" if testrun.setup && !testrun.setup.empty? lines << ' def setup' testrun.setup.code.lines.each { |line| lines << " #{line.chomp}" } lines << ' end' lines << '' end testrun.test_cases.each_with_index do |test_case, index| # Orphan blocks become plain statements in the class body, in source order if test_case.is_a?(Tryouts::OrphanBlock) unless test_case.empty? test_case.code.lines.each { |line| lines << " #{line.chomp}" } lines << '' end next end next if test_case.empty? || !test_case.expectations? method_name = "test_#{index.to_s.rjust(3, '0')}_#{MinitestTranslator.parameterize(test_case.description)}" lines << " def #{method_name}" if test_case.exception_expectations? # Handle exception expectations lines << ' error = assert_raises(StandardError) do' unless test_case.code.strip.empty? test_case.code.lines.each { |line| lines << " #{line.chomp}" } end lines << ' end' test_case.exception_expectations.each do |expectation| lines << " assert #{expectation}, \"Exception expectation failed: #{expectation}\"" end else # Handle regular expectations unless test_case.code.strip.empty? lines << ' result = begin' test_case.code.lines.each { |line| lines << " #{line.chomp}" } lines << ' end' end test_case.expectations.each do |expectation| lines << " assert_equal #{expectation}, result" end end lines << ' end' lines << '' end if testrun.teardown && !testrun.teardown.empty? lines << ' def teardown' testrun.teardown.code.lines.each { |line| lines << " #{line.chomp}" } lines << ' end' end lines << 'end' lines.join("\n") end |
#translate(testrun) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tryouts/translators/minitest_translator.rb', line 61 def translate(testrun) file_basename = File.basename(testrun.source_file, '.rb') class_name = "Test#{file_basename.gsub(/[^A-Za-z0-9]/, '')}" test_class = Class.new(Minitest::Test) do # Setup method if testrun.setup && !testrun.setup.empty? define_method(:setup) do instance_eval(testrun.setup.code) end end # Generate test methods testrun.test_cases.each_with_index do |test_case, index| # Orphan blocks become plain statements in the class body, in source order if test_case.is_a?(Tryouts::OrphanBlock) unless test_case.empty? class_eval(test_case.code, testrun.source_file, test_case.eval_start_line) end next end next if test_case.empty? || !test_case.expectations? method_name = "test_#{index.to_s.rjust(3, '0')}_#{MinitestTranslator.parameterize(test_case.description)}" define_method(method_name) do if test_case.exception_expectations? # Handle exception expectations. The raised exception is bound to # a local named `error`, which is what #=!> expectations refer to, # so the expectation is eval'd against this binding rather than # instance_eval'd. error = assert_raises(StandardError) do instance_eval(test_case.code) unless test_case.code.strip.empty? end test_case.exception_expectations.each do |expectation| result = eval(expectation.content, binding) # rubocop:disable Security/Eval assert result, "Exception expectation failed: #{expectation.content}" end else # Handle regular expectations result = instance_eval(test_case.code) unless test_case.code.strip.empty? test_case.regular_expectations.each do |expectation| expected_value = instance_eval(expectation.content) assert_equal expected_value, result end end end end # Teardown method if testrun.teardown && !testrun.teardown.empty? define_method(:teardown) do instance_eval(testrun.teardown.code) end end end # Set the class name dynamically Object.const_set(class_name, test_class) unless Object.const_defined?(class_name) test_class end |