Class: ParseTreeTestCase

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/pt_testcase.rb

Constant Summary collapse

VER_RE =
"(1[89]|2[0123])"
@@testcase_order =

Shared TestCases:

%w(Ruby ParseTree)
@@testcases =
Hash.new { |h,k| h[k] = {} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#processorObject

to be defined by subclass



36
37
38
# File 'lib/pt_testcase.rb', line 36

def processor
  @processor
end

Class Method Details

.add_18tests(name, hash) ⇒ Object



74
75
76
# File 'lib/pt_testcase.rb', line 74

def self.add_18tests name, hash
  add_tests "#{name}__18", hash
end

.add_19edgecases(ruby, sexp, cases) ⇒ Object



82
83
84
85
86
# File 'lib/pt_testcase.rb', line 82

def self.add_19edgecases ruby, sexp, cases
  cases.each do |name, code|
    add_19tests name, "Ruby" => code, "ParseTree" => sexp, "Ruby2Ruby" => ruby
  end
end

.add_19tests(name, hash) ⇒ Object



78
79
80
# File 'lib/pt_testcase.rb', line 78

def self.add_19tests name, hash
  add_tests "#{name}__19_20_21_22_23", hash # HACK?
end

.add_test(name, data, klass = self.name[4..-1]) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pt_testcase.rb', line 49

def self.add_test name, data, klass = self.name[4..-1]
  name = name.to_s
  klass = klass.to_s

  if testcases.has_key? name then
    if testcases[name].has_key? klass then
      warn "testcase #{klass}##{name} already has data"
    else
      testcases[name][klass] = data
    end
  else
    warn "testcase #{name} does not exist"
  end
end

.add_tests(name, hash) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/pt_testcase.rb', line 64

def self.add_tests name, hash
  name = name.to_s

  hash.each do |klass, data|
    warn "testcase #{klass}##{name} already has data" if
      testcases[name].has_key? klass
    testcases[name][klass] = data
  end
end

.clone_sameObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/pt_testcase.rb', line 88

def self.clone_same
  @@testcases.each do |node, data|
    data.each do |key, val|
      if val == :same then
        prev_key = self.previous(key)
        data[key] = data[prev_key].deep_clone
      end
    end
  end
end

.copy_test_case(nonverbose, klass) ⇒ Object



99
100
101
102
# File 'lib/pt_testcase.rb', line 99

def self.copy_test_case nonverbose, klass
  verbose = nonverbose + "_mri_verbose_flag"
  testcases[verbose][klass] = testcases[nonverbose][klass]
end

.generate_test(klass, node, data, input_name, output_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
# File 'lib/pt_testcase.rb', line 106

def self.generate_test klass, node, data, input_name, output_name
  klass.send :define_method, "test_#{node}" do
    flunk "Processor is nil" if processor.nil?

    tversions = node[/(?:_#{VER_RE})+$/]
    if tversions then
      cversion = self.class.name[/#{VER_RE}/]

      # can't push this up because it may be generating into an
      # abstract test class and the actual subclass is versioned.
      return "version specific test" unless tversions.include? cversion if cversion
    end

    assert data.has_key?(input_name), "Unknown input data"
    assert data.has_key?(output_name), "Missing test data"

    $missing[node] << output_name unless data.has_key? output_name

    input    = data[input_name].deep_clone
    expected = data[output_name].deep_clone

    case expected
    when :unsupported then
      assert_raises(UnsupportedNodeError) do
        processor.process(input)
      end
    else
      extra_expected = []
      extra_input = []

      _, expected, extra_expected = *expected if
        Array === expected and expected.first == :defx
      _, input, extra_input = *input if
        Array === input and input.first == :defx

      # OMG... I can't believe I have to do this this way.  these
      # hooks are here instead of refactoring this define_method
      # body into an assertion. It'll allow subclasses to hook in
      # and add behavior before or after the processor does it's
      # thing. If you go the body refactor route, some of the
      # RawParseTree test cases fail for completely bogus reasons.

      before_process_hook klass, node, data, input_name, output_name
      refute_nil data[input_name], "testcase does not exist?"
      @result = processor.process input
      assert_equal(expected, @result,
                   "failed on input: #{data[input_name].inspect}")
      after_process_hook klass, node, data, input_name, output_name

      extra_input.each do |extra|
        processor.process(extra)
      end
      extra = processor.extra_methods rescue []
      assert_equal extra_expected, extra
    end
  end
end

.generate_tests(klass) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pt_testcase.rb', line 164

def self.generate_tests klass
  install_missing_reporter
  clone_same

  output_name = klass.name.to_s.sub(/^Test/, '')

  input_name = self.previous(output_name)

  @@testcases.each do |node, data|
    next if [:skip, :unsupported].include? data[input_name]
    next if data[output_name] == :skip

    generate_test klass, node, data, input_name, output_name
  end
end

.inherited(klass) ⇒ Object



180
181
182
183
184
# File 'lib/pt_testcase.rb', line 180

def self.inherited klass
  super

  generate_tests klass unless klass.name =~ /TestCase/
end

.install_missing_reporterObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/pt_testcase.rb', line 186

def self.install_missing_reporter
  unless defined? $missing then
    $missing = Hash.new { |h,k| h[k] = [] }
    at_exit {
      at_exit {
        warn ""
        $missing.sort.each do |name, klasses|
          warn "add_tests(#{name.inspect},"
          klasses.map! { |klass| "          #{klass.inspect} => :same" }
          warn klasses.join("\n") + ")"
        end
        warn ""
      }
    }
  end
end

.previous(key, extra = 0) ⇒ Object

FIX: remove R2C code



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/pt_testcase.rb', line 203

def self.previous(key, extra=0) # FIX: remove R2C code
  idx = @@testcase_order.index(key)

  raise "Unknown class #{key} in @@testcase_order" if idx.nil?

  case key
  when "RubyToRubyC" then
    idx -= 1
  end
  @@testcase_order[idx - 1 - extra]
end

.testcase_orderObject



215
# File 'lib/pt_testcase.rb', line 215

def self.testcase_order; @@testcase_order; end

.testcasesObject



216
# File 'lib/pt_testcase.rb', line 216

def self.testcases; @@testcases; end

.unsupported_tests(*tests) ⇒ Object



218
219
220
221
222
# File 'lib/pt_testcase.rb', line 218

def self.unsupported_tests *tests
  tests.flatten.each do |name|
    add_test name, :unsupported
  end
end

Instance Method Details

#after_process_hook(klass, node, data, input_name, output_name) ⇒ Object



43
44
# File 'lib/pt_testcase.rb', line 43

def after_process_hook klass, node, data, input_name, output_name
end

#before_process_hook(klass, node, data, input_name, output_name) ⇒ Object



46
47
# File 'lib/pt_testcase.rb', line 46

def before_process_hook klass, node, data, input_name, output_name
end

#setupObject



38
39
40
41
# File 'lib/pt_testcase.rb', line 38

def setup
  super
  @processor = nil
end