Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now checks for naughty overrides, starting with 'class'.

  • Loading branch information...
commit 4aff319e4adaf267337ab79bc7e2885010431e95 1 parent cdce8a9
Duncan Bayne authored
View
1  lib/roodi/checks.rb
@@ -16,3 +16,4 @@
require 'roodi/checks/module_name_check'
require 'roodi/checks/npath_complexity_method_check'
require 'roodi/checks/parameter_number_check'
+require 'roodi/checks/core_method_override_check'
View
25 lib/roodi/checks/core_method_override_check.rb
@@ -0,0 +1,25 @@
+require 'roodi/checks/check'
+
+module Roodi
+ module Checks
+ # Checks a class to make sure it doesn't override core methods on Object.
+ #
+ # An example is when the 'class' method of a class is overriden. This causes code
+ # that tests the class of an object to fail.
+ class CoreMethodOverrideCheck < Check
+
+ def interesting_nodes
+ [:defn]
+ end
+
+ def evaluate_start(node)
+ [ :class ].each do |core_name|
+ if node[1] == core_name
+ add_error("Class overrides method '#{core_name}'.")
+ end
+ end
+ end
+
+ end
+ end
+end
View
2  lib/roodi/checks/module_name_check.rb
@@ -4,7 +4,7 @@ module Roodi
module Checks
# Checks a module name to make sure it matches the specified pattern.
#
- # Keeping to a consistent nameing convention makes your code easier to read.
+ # Keeping to a consistent naming convention makes your code easier to read.
class ModuleNameCheck < NameCheck
DEFAULT_PATTERN = /^[A-Z][a-zA-Z0-9]*$/
View
2  roodi.yml
@@ -13,4 +13,4 @@ MethodNameCheck: { pattern: !ruby/regexp /^[_a-z<>=\[|+-\/\*`]+[
ModuleLineCountCheck: { line_count: 300 }
ModuleNameCheck: { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
ParameterNumberCheck: { parameter_count: 5 }
-
+CoreMethodOverrideCheck: { }
View
31 spec/roodi/checks/core_method_override_check_spec.rb
@@ -0,0 +1,31 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe Roodi::Checks::CoreMethodOverrideCheck do
+ before(:each) do
+ @roodi = Roodi::Core::Runner.new(Roodi::Checks::CoreMethodOverrideCheck.new)
+ end
+
+ it "should accept a class with a method" do
+ content = <<-END
+ class WellBehavedClass
+ def well_behaved_method
+ end
+ end
+ END
+ @roodi.check_content(content)
+ @roodi.errors.should be_empty
+ end
+
+ it "should reject a class which overrides the class method" do
+ content = <<-END
+ class BadClass
+ def class
+ end
+ end
+ END
+ @roodi.check_content(content)
+ errors = @roodi.errors
+ errors.should_not be_empty
+ errors[0].to_s.should eql("dummy-file.rb:2 - Class overrides method 'class'.")
+ end
+end
View
1  spec/roodi/roodi.yml
@@ -1 +1,2 @@
MethodNameCheck: { pattern: !ruby/regexp /^[A-Z]+$/ }
+CoreMethodOverrideCheck: { }
Please sign in to comment.
Something went wrong with that request. Please try again.