Class: ParseTreeTestCase

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

Constant Summary collapse

VER_RE =
"(1[89]|2[0123456])"
@@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



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

def processor
  @processor
end

Class Method Details

.add_18tests(name, hash) ⇒ Object



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

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

.add_19edgecases(ruby, sexp, cases) ⇒ Object



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

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



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

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

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



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

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



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

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



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

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



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

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



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
163
164
165
# File 'lib/pt_testcase.rb', line 107

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}/]

      assert true # shut up prove_it!

      # 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.sexp_type == :defx
      _, input, extra_input = *input if
        Array === input and input.sexp_type == :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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pt_testcase.rb', line 167

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



183
184
185
186
187
# File 'lib/pt_testcase.rb', line 183

def self.inherited klass
  super

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

.install_missing_reporterObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/pt_testcase.rb', line 189

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



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/pt_testcase.rb', line 206

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



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

def self.testcase_order; @@testcase_order; end

.testcasesObject



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

def self.testcases; @@testcases; end

.unsupported_tests(*tests) ⇒ Object



221
222
223
224
225
# File 'lib/pt_testcase.rb', line 221

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



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

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

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



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

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

#setupObject



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

def setup
  super
  @processor = nil
end