Skip to content

Instantly share code, notes, and snippets.

@drusepth
Created July 5, 2018 20:22
Show Gist options
  • Save drusepth/1c605ebcb518718bfb35e777a3d471ce to your computer and use it in GitHub Desktop.
Save drusepth/1c605ebcb518718bfb35e777a3d471ce to your computer and use it in GitHub Desktop.
require 'pry'
class MethodProfile
attr_accessor :method_reference
# Method delegates
def arity; @method_reference.arity; end
def initialize(method_reference)
@method_reference = method_reference
end
# foo(n) needs fuzz_params([0..100])
# foo(a, b) needs fuzz_params([0..100, 0..100]) for a and b fuzzing, respectively
# foo(a, b) can fuzz just `a` by using fuzz_params([0..100, 5])
# Please note the big-oh complexity of using multiple params; this is gonna be hella slow
def fuzz_params(param_values)
arity = param_values.length
# TODO arity > 1
inputs = {}
outputs = {}
# arity == 1
param_values.to_a.each do |param_value|
# [0..100]
param_value.each do |param|
output = begin
@method_reference.call(param)
rescue Exception => e
e.class
end
outputs[output] ||= []
outputs[output] << param
inputs[param] = output
end
end
{
inputs: inputs,
outputs: outputs
}
end
def integer_range
fuzz_params([-20..20])
end
def boolean_range
#fuzz_params([true, false])
end
def to_s
puts [
" # Number of parameters: #{@method_reference.arity.to_s}",
" # output=>input map: #{integer_range[:outputs].inspect}",
" # integer input=>output map: #{integer_range[:inputs].inspect}",
" # boolean output=>input map: #{boolean_range[:outputs].inspect}",
" # boolean input=>output map: #{boolean_range[:inputs].inspect}"
].join("\n")
end
def to_html
# TODO
end
end
@initial_object_constants = Object.constants
# Usage: ruby inspect.rb "fibonacci"
require_relative ARGV.shift
objects_to_test = Object.constants - @initial_object_constants
puts "Testing the following objects:"
puts objects_to_test.map { |o| " - #{o}" }
objects_to_test.each do |subject|
class_definition = Object::const_get(subject)
puts " => Class methods"
class_methods = class_definition.methods - Class.methods
class_methods.each do |class_method|
puts " - #{subject}\##{class_method}"
puts MethodProfile.new(class_definition.method(class_method)).to_s
binding.pry
end
end
#binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment