Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josep M. Bach
committed
Feb 14, 2012
1 parent
7739ad0
commit 4dc8323
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |