Class: ObjectOculus::MethodFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/object_oculus/method_finder.rb,
lib/object_oculus/classic.rb

Overview

A singleton class used to iterate over the methods of an object trying to match any returned values with an expected result. ny matches will then be pretty printed to the console.

Constant Summary collapse

@@blacklist =

A list of symbols indicated which methods to always ignore

%w[
  daemonize
  debug
  debugger
  display
  ed
  emacs
  exec
  exit!
  fork
  mate
  nano
  sleep
  stub
  stub!
  stub_chain
  syscall
  system
  unstub
  unstub!
  vi
  vim
].map(&:to_sym)
@@infixes =

A list of symbols for infix operators for which Ruby has special syntax

%w[+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>].map(&:to_sym)
@@prefixes =

A list of symbols for prefix operators for which Ruby has special syntax

%w[+@ -@ ~ !].map(&:to_sym)

Class Method Summary collapse

Class Method Details

.build_check_lambda(expected_result, opts = {}) ⇒ Object

Builds a lambda for checking against the provided expected_result given a hash of options. Given the value of a method, the result of this lambda will determine whether that method and value are included in the output of a object_oculus method.

Options

  • :exclude_blank - Exclude blank values

  • :force_exact - Force values to be exactly equal

  • :force_regex - Coerce the expected_result into a regular expression for pattern matching

  • :show_all - Show the results of all methods



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/object_oculus/method_finder.rb', line 49

def build_check_lambda(expected_result, opts = {})
  if opts[:force_regex]
    expected_result = Regexp.new(expected_result.to_s) unless expected_result.is_a?(Regexp)
    ->(value) { expected_result === value.to_s }
  elsif expected_result.is_a?(Regexp) && !opts[:force_exact]
    ->(value) { expected_result === value.to_s }
  elsif opts[:force_exact]
    ->(value) { expected_result.eql?(value) }
  elsif opts[:show_all]
    if opts[:exclude_blank]
      ->(value) { !value.nil? && !(value.respond_to?(:empty?) && value.empty?) }
    else
      ->(_value) { true }
    end
  else
    ->(value) { expected_result == value }
  end
end

.find(an_object, expected_result, opts = {}, *args, &block) ⇒ Object

Find all methods on an_object which, when called with args return expected_result



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/object_oculus/method_finder.rb', line 69

def find(an_object, expected_result, opts = {}, *args, &block)
  check_result = build_check_lambda(expected_result, opts)

  # Prevent any writing to the terminal
  stdout, stderr = $stdout, $stderr
  unless $stdout.is_a?(DummyOut)
    $stdout = $stderr = DummyOut.new
    restore_std = true
  end

  # Use only methods with valid arity that aren't blacklisted
  methods = an_object.methods
  methods.select! { |n| an_object.method(n).arity <= args.size && !@@blacklist.include?(n) }

  # Collect all methods equaling the expected result
  results = methods.each_with_object({}) do |name, res|
    stdout.print ""
    begin
      value = an_object.clone.method(name).call(*args, &block)
      res[name] = value if check_result.call(value)
    rescue StandardError
    end
  end

  # Restore printing to the terminal
  $stdout, $stderr = stdout, stderr if restore_std
  results
end

.show(an_object, expected_result, opts = {}, *args) ⇒ Object

Pretty prints the results of #find



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/object_oculus/method_finder.rb', line 99

def show(an_object, expected_result, opts = {}, *args, &)
  opts = {
    exclude_blank: false,
    force_exact: false,
    force_regex: false,
    show_all: false,
  }.merge(opts)

  found = find(an_object, expected_result, opts, *args, &)
  prettified = prettify_found(an_object, found, *args)
  max_length = prettified.map { |k, _v| k.length }.max

  prettified.each { |k, v| puts "#{k.ljust max_length} == #{v}" }

  found
end