Module: Suggest::Mixin

Defined in:
lib/suggest.rb

Instance Method Summary collapse

Instance Method Details

#what_mutates?(expected, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/suggest.rb', line 52

def what_mutates?(expected, opts = {})
  args = opts[:args] || []
  block = Proc.new if block_given?

  applicable_methods = self.methods.map(&method(:method)).select do |m|
    SUGGEST_MODS.include?(m.owner) && !INCONSISTENT.include?([m.owner, m.name])
  end

  applicable_methods.select do |m|
    arity = m.arity
    next unless arity == -1 || arity == args.count

    post = clone
    if block
      next if UNSAFE_WITH_BLOCK.include?([m.owner, m.name])
      result = post.public_send(m.name, *args, &block) rescue next
    else
      result = post.public_send(m.name, *args) rescue next
    end

    if opts.key?(:returns)
      next unless Suggest.eq?(result, opts[:returns])
    end

    Suggest.eq?(post, expected)
  end.map(&:name)
end

#what_returns?(expected, args: [], allow_mutation: false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/suggest.rb', line 27

def what_returns?(expected, args: [], allow_mutation: false)
  block = Proc.new if block_given?

  applicable_methods = self.methods.map(&method(:method)).select do |m|
    SUGGEST_MODS.include?(m.owner) && !INCONSISTENT.include?([m.owner, m.name])
  end

  applicable_methods.select do |m|
    arity = m.arity
    next unless arity == -1 || arity == args.count

    post = clone
    if block
      next if UNSAFE_WITH_BLOCK.include?([m.owner, m.name])
      result = post.public_send(m.name, *args, &block) rescue next
    else
      result = post.public_send(m.name, *args) rescue next
    end

    next unless allow_mutation || self == post

    Suggest.eq?(result, expected)
  end.map(&:name)
end