class TestedPublicMethods::Detector
Constants
- CLASS_METHOD_REG_EXP
- CLASS_NAME_REG_EXP
- INSTANCE_METHOD_REG_EXP
Public Instance Methods
analyze()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 6 def analyze @problem_counter = 0 @buffer = {missing_test: [], missing_spec: []} list_of_classes.keys.each do |klass| if has_spec_file? klass untested_instance_methods(klass).each do |method_name| buffer_warning("* missing test for #{klass.name}##{method_name}", :missing_test) end untested_class_methods(klass).each do |method_name| buffer_warning("* missing test for #{klass.name}.#{method_name}", :missing_test) end elsif !class_skipped? klass buffer_warning("* missing spec file for #{klass.name}", :missing_spec) end end print_summary end
Private Instance Methods
buffer_warning(text, type)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 33 def buffer_warning(text, type) @buffer[type].push(text) @problem_counter += 1 end
class_methods_in_class(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 95 def class_methods_in_class(klass) klass.public_methods(false).select do |method_name| klass.method(method_name).source_location.try(:first) == source_file_path(klass) end end
class_methods_in_spec(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 105 def class_methods_in_spec(klass) File.read(spec_file_path(klass)).scan(CLASS_METHOD_REG_EXP).flatten.map(&:to_sym) end
class_skipped?(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 52 def class_skipped?(klass) config_skip_classes.include? klass.to_s end
config_skip_classes()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 113 def config_skip_classes TestedPublicMethods.configuration.skip_classes.map(&:to_s) end
config_skip_methods()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 109 def config_skip_methods TestedPublicMethods.configuration.skip_methods.stringify_keys end
has_spec_file?(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 48 def has_spec_file?(klass) File.exists?(spec_file_path(klass)) end
instance_methods_in_class(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 89 def instance_methods_in_class(klass) klass.instance_methods(false).select do |method_name| klass.instance_method(method_name).source_location.try(:first) == source_file_path(klass) end end
instance_methods_in_spec(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 101 def instance_methods_in_spec(klass) File.read(spec_file_path(klass)).scan(INSTANCE_METHOD_REG_EXP).flatten.map(&:to_sym) end
list_of_classes()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 64 def list_of_classes @list_of_classes ||= Dir.glob(File.join(source_dir, "**/*.rb")).each_with_object({}) do |file_path, memo| klass_names = File.read(file_path).scan(CLASS_NAME_REG_EXP).flatten klass_names.each do |klass_name| begin memo[klass_name.constantize] = file_path rescue puts "WARNING: can't analyze unsupported class #{klass_name} in #{file_path}".yellow end end end end
print_summary()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 38 def print_summary puts @buffer[:missing_spec].join("\n").red puts @buffer[:missing_test].join("\n").red if @problem_counter > 0 puts "\nFound #{@problem_counter} issues".red else puts "\nGreat, all public methods are tested!".green end end
skipped_methods(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 85 def skipped_methods(klass) (config_skip_methods[klass.to_s] || []).map(&:to_sym) end
source_dir()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 25 def source_dir File.join(Rails.root, 'app') end
source_file_path(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 56 def source_file_path(klass) list_of_classes[klass] end
spec_dir()
click to toggle source
# File lib/tested_public_methods/detector.rb, line 29 def spec_dir File.join(Rails.root, 'spec') end
spec_file_path(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 60 def spec_file_path(klass) source_file_path(klass).gsub(source_dir, spec_dir).gsub('.rb', '_spec.rb') end
untested_class_methods(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 81 def untested_class_methods(klass) class_methods_in_class(klass) - class_methods_in_spec(klass) - skipped_methods(klass) end
untested_instance_methods(klass)
click to toggle source
# File lib/tested_public_methods/detector.rb, line 77 def untested_instance_methods(klass) instance_methods_in_class(klass) - instance_methods_in_spec(klass) - skipped_methods(klass) end