Class: ParseTreeTestCase

Inherits:
MiniTest::Unit::TestCase
  • Object
show all
Defined in:
lib/pt_testcase.rb

Constant Summary collapse

@@testcase_order =

Shared TestCases:

%w(Ruby RawParseTree 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_19tests(name, hash) ⇒ Object



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

def self.add_19tests name, hash
  add_tests "#{name}__19", hash
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



82
83
84
85
86
87
88
89
90
91
# File 'lib/pt_testcase.rb', line 82

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



93
94
95
96
# File 'lib/pt_testcase.rb', line 93

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



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
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
# File 'lib/pt_testcase.rb', line 98

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?

    if node =~ /(1[89])$/ then
      version = $1
      skip "version specific test" unless self.class.name =~ /#{version}/
    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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/pt_testcase.rb', line 152

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



168
169
170
171
172
# File 'lib/pt_testcase.rb', line 168

def self.inherited klass
  super

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

.install_missing_reporterObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pt_testcase.rb', line 174

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



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

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



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

def self.testcase_order; @@testcase_order; end

.testcasesObject



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

def self.testcases; @@testcases; end

.unsupported_tests(*tests) ⇒ Object



206
207
208
209
210
# File 'lib/pt_testcase.rb', line 206

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