lib/suggest.rb in suggest_rb-0.2.0 vs lib/suggest.rb in suggest_rb-0.3.0

- old
+ new

@@ -2,16 +2,30 @@ require "set" module Suggest SUGGEST_MODS = Set.new([ Array, + BasicObject, + Comparable, + Complex, Enumerable, - String, + FalseClass, + Float, Hash, - Regexp, Integer, - Set + Math, + NilClass, + Numeric, + Range, + Regexp, + Regexp, + Set, + String, + Struct, + Symbol, + Time, + TrueClass, ]) UNSAFE_WITH_BLOCK = Set.new([ [Array, :cycle], [Enumerable, :cycle] @@ -21,16 +35,28 @@ [Array, :sample], [Array, :shuffle], [Array, :shuffle!] ]) + TOO_COMPLICATED = Set.new([ + [String, :freeze], + [Set, :freeze], + [Set, :taint], + [Set, :untaint], + [Numeric, :singleton_method_added], + [Numeric, :clone], + [Numeric, :dup], + ]) + module Mixin 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]) + SUGGEST_MODS.include?(m.owner) && + !INCONSISTENT.include?([m.owner, m.name]) && + !TOO_COMPLICATED.include?([m.owner, m.name]) end applicable_methods.select do |m| arity = m.arity next unless arity == -1 || arity == args.count @@ -52,11 +78,13 @@ 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]) + SUGGEST_MODS.include?(m.owner) && + !INCONSISTENT.include?([m.owner, m.name]) && + !TOO_COMPLICATED.include?([m.owner, m.name]) end applicable_methods.select do |m| arity = m.arity next unless arity == -1 || arity == args.count @@ -78,9 +106,22 @@ end end def self.eq?(result, expected) result.is_a?(expected.class) && result == expected + end + + def self.suggestable_methods + candidates = [] + SUGGEST_MODS.each do |mod| + owned_methods = mod.instance_methods.select { |m| mod.instance_method(m).owner == mod } + next if owned_methods.none? + candidates += [mod].product(owned_methods) + end + + candidates.reject do |m| + INCONSISTENT.include?(m) || TOO_COMPLICATED.include?(m) + end end end Object.include(Suggest::Mixin)