spec/support/hashlike_fuzzing_helper.rb
#
# Fuzz testing for hashlike behavior. This throws a whole bunch of
#
#
Fuzz testing for hashlike behavior. This throws a whole bunch of
Fuzz testing for hashlike behavior . This throws a whole bunch of
def behaves_the_same obj_1, obj_2, method_to_test, input
    input.unshift(1) if [:cycle, :partition].include?(method_to_test) && input.last.is_a?(Proc)
    if ((RUBY_VERSION < '1.9') &&
        [:reject!, :delete_if, :each_pair, :select, :reject].include?(method_to_test) &&
        input.last.is_a?(Proc) && (input.last.arity == 1))
      return
    end
    quiet_output do
      obj_1.should_receive(:warn){|str| stderr_output << str }.at_most(:once)
      begin
        expected = send_to(obj_1, method_to_test, input)
      rescue Exception => e
        expected = e
      end
      expected_stderr = $stderr.string
      obj_1.should_receive(:warn){|str| stderr_output << str }.at_most(:once)
      case expected
      when Exception
        lambda{ send_to(obj_2, method_to_test, input) }.should raise_error(expected.class, err_regex(expected))
      when Enumerator
        actual = send_to(obj_2, method_to_test, input)
        actual.should be_a(Enumerator)
        actual.inspect.gsub(/[\"\:]/, '').gsub(/0x[a-f\d]+/,'').should == expected.inspect.gsub(/[\"\:]/, '').gsub(/0x[a-f\d]+/,'')
      else 
        actual = send_to(obj_2, method_to_test, input)
        if expected.is_a?(Hash) && (RUBY_VERSION < '1.9')
          actual.should be_hash_eql(expected)
        elsif expected.is_a?(Array) && (RUBY_VERSION < '1.9')
          actual.should be_array_eql(expected)
        else
          actual.should == expected
        end
      end
      $stderr.string.sub(/.*\.rb:\d+:(?:in `send\w*':)? /, '').should == expected_stderr.sub(/.*\.rb:\d+:(?:in `send\w*':)? /, '')
    end
  end
def behaves_the_same obj_1 , obj_2 , method_to_test , input input . unshift ( 1 ) if [ :cycle , :partition ] . include? ( method_to_test ) && input . last . is_a? ( Proc ) if ( ( RUBY_VERSION < '1.9' ) && [ :reject! , :delete_if , :each_pair , :select , :reject ] . include? ( method_to_test ) && input . last . is_a? ( Proc ) && ( input . last . arity == 1 ) ) return end quiet_output do obj_1 . should_receive ( :warn ) { | str | stderr_output << str } . at_most ( :once ) begin expected = send_to ( obj_1 , method_to_test , input ) rescue Exception => e expected = e end expected_stderr = $stderr . string obj_1 . should_receive ( :warn ) { | str | stderr_output << str } . at_most ( :once ) case expected when Exception lambda { send_to ( obj_2 , method_to_test , input ) } . should raise_error ( expected . class , err_regex ( expected ) ) when Enumerator actual = send_to ( obj_2 , method_to_test , input ) actual . should be_a ( Enumerator ) actual . inspect . gsub ( / [ \" \: ] / , '' ) . gsub ( / 0x[a-f \d ]+ / , '' ) . should == expected . inspect . gsub ( / [ \" \: ] / , '' ) . gsub ( / 0x[a-f \d ]+ / , '' ) else actual = send_to ( obj_2 , method_to_test , input ) if expected . is_a? ( Hash ) && ( RUBY_VERSION < '1.9' ) actual . should be_hash_eql ( expected ) elsif expected . is_a? ( Array ) && ( RUBY_VERSION < '1.9' ) actual . should be_array_eql ( expected ) else actual . should == expected end end $stderr . string . sub ( / .* \. rb: \d +:(?:in `send \w *':)?  / , '' ) . should == expected_stderr . sub ( / .* \. rb: \d +:(?:in `send \w *':)?  / , '' ) end end
Fuzz testing for hashlike behavior.
Fuzz testing for hashlike behavior .
# Hash in 1.8.7 behaves differently when arity mismatched to method # run the method
obj_1
<not_specific>
obj_2
<not_specific>
method_to_test
<not_specific>
input
<not_specific>