Skip to content

Commit

Permalink
Add properties lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Feb 14, 2012
1 parent 7739ad0 commit 4dc8323
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pelusa/lint.rb
Expand Up @@ -4,6 +4,7 @@
require 'pelusa/lint/demeter_law'
require 'pelusa/lint/indentation_level'
require 'pelusa/lint/else_clauses'
require 'pelusa/lint/properties'

module Pelusa
# Public: A Lint is a quality standard, applicable on a given piece of code to
Expand All @@ -17,6 +18,7 @@ def self.all
DemeterLaw,
IndentationLevel,
ElseClauses,
Properties,
]
end
end
Expand Down
37 changes: 37 additions & 0 deletions lib/pelusa/lint/properties.rb
@@ -0,0 +1,37 @@
module Pelusa
module Lint
class Properties
def initialize
@violations = Set.new
end

def check(klass)
initialize
iterate_lines!(klass)

return SuccessfulAnalysis.new(name) if @violations.empty?

FailedAnalysis.new(name, @violations) do |violations|
"There are getters, setters or properties in lines #{violations.to_a.join(', ')}"
end
end

private

def name
"Doesn't use getters, setters or properties"
end

def iterate_lines!(klass)
iterator = Iterator.new do |node|
if node.is_a?(Rubinius::AST::Send)
if [:attr_accessor, :attr_writer, :attr_reader].include? node.name
@violations << node.line
end
end
end
Array(klass).each(&iterator)
end
end
end
end
44 changes: 44 additions & 0 deletions test/pelusa/lint/properties_test.rb
@@ -0,0 +1,44 @@
require 'test_helper'

module Pelusa
module Lint
describe Properties do
before do
@lint = Properties.new
end

describe '#check' do
describe 'when the class does not use getters, setters or properties' do
it 'returns a SuccessAnalysis' do
klass = """
class Foo
def initialize
@name = 'john'
end
def name
@name
end
end""".to_ast

analysis = @lint.check(klass)
analysis.successful?.must_equal true
end
end

describe 'when the class uses getters, setters or properties' do
it 'returns a FailureAnalysis' do
klass = """
class Foo
attr_accessor :name
attr_reader :foo
end""".to_ast

analysis = @lint.check(klass)
analysis.failed?.must_equal true
end
end
end
end
end
end

0 comments on commit 4dc8323

Please sign in to comment.