simplabs-excellent 1.0.1 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. data/History.txt +19 -0
  2. data/README.rdoc +34 -0
  3. data/VERSION.yml +1 -1
  4. data/bin/excellent +21 -6
  5. data/lib/simplabs/excellent.rb +7 -5
  6. data/lib/simplabs/excellent/checks.rb +18 -1
  7. data/lib/simplabs/excellent/checks/abc_metric_method_check.rb +19 -56
  8. data/lib/simplabs/excellent/checks/assignment_in_conditional_check.rb +16 -16
  9. data/lib/simplabs/excellent/checks/base.rb +30 -21
  10. data/lib/simplabs/excellent/checks/case_missing_else_check.rb +13 -5
  11. data/lib/simplabs/excellent/checks/class_line_count_check.rb +10 -8
  12. data/lib/simplabs/excellent/checks/class_name_check.rb +11 -10
  13. data/lib/simplabs/excellent/checks/control_coupling_check.rb +13 -10
  14. data/lib/simplabs/excellent/checks/cyclomatic_complexity_block_check.rb +25 -9
  15. data/lib/simplabs/excellent/checks/cyclomatic_complexity_check.rb +4 -20
  16. data/lib/simplabs/excellent/checks/cyclomatic_complexity_method_check.rb +25 -10
  17. data/lib/simplabs/excellent/checks/duplication_check.rb +50 -0
  18. data/lib/simplabs/excellent/checks/empty_rescue_body_check.rb +10 -18
  19. data/lib/simplabs/excellent/checks/flog_block_check.rb +40 -0
  20. data/lib/simplabs/excellent/checks/flog_check.rb +27 -0
  21. data/lib/simplabs/excellent/checks/flog_class_check.rb +40 -0
  22. data/lib/simplabs/excellent/checks/flog_method_check.rb +40 -0
  23. data/lib/simplabs/excellent/checks/for_loop_check.rb +20 -4
  24. data/lib/simplabs/excellent/checks/line_count_check.rb +3 -21
  25. data/lib/simplabs/excellent/checks/method_line_count_check.rb +9 -7
  26. data/lib/simplabs/excellent/checks/method_name_check.rb +13 -9
  27. data/lib/simplabs/excellent/checks/module_line_count_check.rb +9 -7
  28. data/lib/simplabs/excellent/checks/module_name_check.rb +11 -7
  29. data/lib/simplabs/excellent/checks/name_check.rb +3 -8
  30. data/lib/simplabs/excellent/checks/nested_iterators_check.rb +33 -0
  31. data/lib/simplabs/excellent/checks/parameter_number_check.rb +13 -12
  32. data/lib/simplabs/excellent/checks/rails.rb +17 -0
  33. data/lib/simplabs/excellent/checks/rails/attr_accessible_check.rb +38 -0
  34. data/lib/simplabs/excellent/checks/rails/attr_protected_check.rb +39 -0
  35. data/lib/simplabs/excellent/checks/singleton_variable_check.rb +32 -0
  36. data/lib/simplabs/excellent/extensions/sexp.rb +21 -0
  37. data/lib/simplabs/excellent/extensions/string.rb +23 -0
  38. data/lib/simplabs/excellent/parsing.rb +12 -0
  39. data/lib/simplabs/excellent/parsing/abc_measure.rb +52 -0
  40. data/lib/simplabs/excellent/parsing/block_context.rb +43 -0
  41. data/lib/simplabs/excellent/parsing/call_context.rb +36 -0
  42. data/lib/simplabs/excellent/parsing/case_context.rb +31 -0
  43. data/lib/simplabs/excellent/parsing/class_context.rb +68 -0
  44. data/lib/simplabs/excellent/parsing/code_processor.rb +154 -0
  45. data/lib/simplabs/excellent/parsing/conditional_context.rb +25 -0
  46. data/lib/simplabs/excellent/parsing/cvar_context.rb +28 -0
  47. data/lib/simplabs/excellent/parsing/cyclomatic_complexity_measure.rb +73 -0
  48. data/lib/simplabs/excellent/parsing/flog_measure.rb +192 -0
  49. data/lib/simplabs/excellent/parsing/for_loop_context.rb +15 -0
  50. data/lib/simplabs/excellent/parsing/if_context.rb +38 -0
  51. data/lib/simplabs/excellent/parsing/method_context.rb +50 -0
  52. data/lib/simplabs/excellent/parsing/module_context.rb +29 -0
  53. data/lib/simplabs/excellent/{core → parsing}/parser.rb +4 -2
  54. data/lib/simplabs/excellent/parsing/resbody_context.rb +39 -0
  55. data/lib/simplabs/excellent/parsing/scopeable.rb +34 -0
  56. data/lib/simplabs/excellent/parsing/sexp_context.rb +125 -0
  57. data/lib/simplabs/excellent/parsing/singleton_method_context.rb +55 -0
  58. data/lib/simplabs/excellent/parsing/until_context.rb +24 -0
  59. data/lib/simplabs/excellent/parsing/while_context.rb +24 -0
  60. data/lib/simplabs/excellent/runner.rb +105 -0
  61. data/lib/simplabs/excellent/warning.rb +53 -0
  62. data/spec/checks/abc_metric_method_check_spec.rb +36 -8
  63. data/spec/checks/assignment_in_conditional_check_spec.rb +31 -14
  64. data/spec/checks/case_missing_else_check_spec.rb +8 -8
  65. data/spec/checks/class_line_count_check_spec.rb +24 -11
  66. data/spec/checks/class_name_check_spec.rb +9 -9
  67. data/spec/checks/control_coupling_check_spec.rb +84 -13
  68. data/spec/checks/cyclomatic_complexity_block_check_spec.rb +13 -17
  69. data/spec/checks/cyclomatic_complexity_method_check_spec.rb +32 -6
  70. data/spec/checks/duplication_check_spec.rb +139 -0
  71. data/spec/checks/empty_rescue_body_check_spec.rb +54 -16
  72. data/spec/checks/flog_block_check_spec.rb +28 -0
  73. data/spec/checks/flog_class_check_spec.rb +28 -0
  74. data/spec/checks/flog_method_check_spec.rb +46 -0
  75. data/spec/checks/for_loop_check_spec.rb +11 -11
  76. data/spec/checks/method_line_count_check_spec.rb +11 -12
  77. data/spec/checks/method_name_check_spec.rb +34 -13
  78. data/spec/checks/module_line_count_check_spec.rb +11 -12
  79. data/spec/checks/module_name_check_spec.rb +31 -7
  80. data/spec/checks/nested_iterators_check_spec.rb +44 -0
  81. data/spec/checks/parameter_number_check_spec.rb +48 -12
  82. data/spec/checks/rails/attr_accessible_check_spec.rb +79 -0
  83. data/spec/checks/rails/attr_protected_check_spec.rb +77 -0
  84. data/spec/checks/singleton_variable_check_spec.rb +66 -0
  85. data/spec/{core/extensions/underscore_spec.rb → extensions/string_spec.rb} +1 -1
  86. metadata +58 -15
  87. data/README.markdown +0 -30
  88. data/lib/simplabs/excellent/checks/class_variable_check.rb +0 -25
  89. data/lib/simplabs/excellent/core.rb +0 -2
  90. data/lib/simplabs/excellent/core/checking_visitor.rb +0 -34
  91. data/lib/simplabs/excellent/core/error.rb +0 -31
  92. data/lib/simplabs/excellent/core/extensions/underscore.rb +0 -27
  93. data/lib/simplabs/excellent/core/iterator_visitor.rb +0 -29
  94. data/lib/simplabs/excellent/core/parse_tree_runner.rb +0 -88
  95. data/lib/simplabs/excellent/core/visitable_sexp.rb +0 -31
  96. data/spec/checks/class_variable_check_spec.rb +0 -26
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Simplabs::Excellent::Checks::MethodNameCheck do
4
4
 
5
5
  before do
6
- @excellent = Simplabs::Excellent::Core::ParseTreeRunner.new(Simplabs::Excellent::Checks::MethodNameCheck.new)
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::MethodNameCheck.new)
7
7
  end
8
8
 
9
9
  describe '#evaluate' do
@@ -15,7 +15,7 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
15
15
  END
16
16
  @excellent.check_content(content)
17
17
 
18
- @excellent.errors.should be_empty
18
+ @excellent.warnings.should be_empty
19
19
  end
20
20
 
21
21
  it 'should accept method names with numbers' do
@@ -25,7 +25,7 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
25
25
  END
26
26
  @excellent.check_content(content)
27
27
 
28
- @excellent.errors.should be_empty
28
+ @excellent.warnings.should be_empty
29
29
  end
30
30
 
31
31
  it 'should accept method names ending with a question mark' do
@@ -35,7 +35,7 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
35
35
  END
36
36
  @excellent.check_content(content)
37
37
 
38
- @excellent.errors.should be_empty
38
+ @excellent.warnings.should be_empty
39
39
  end
40
40
 
41
41
  it 'should accept method names ending with an exclamation mark' do
@@ -45,7 +45,7 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
45
45
  END
46
46
  @excellent.check_content(content)
47
47
 
48
- @excellent.errors.should be_empty
48
+ @excellent.warnings.should be_empty
49
49
  end
50
50
 
51
51
  it 'should accept method names ending an equals sign' do
@@ -55,10 +55,10 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
55
55
  END
56
56
  @excellent.check_content(content)
57
57
 
58
- @excellent.errors.should be_empty
58
+ @excellent.warnings.should be_empty
59
59
  end
60
60
 
61
- ['<<', '>>', '==', '=', '<', '<=', '>', '>=', '[]', '[]=', '+', '-', '*', '~', '/', '%', '&', '^' '|'].each do |operator|
61
+ ['<<', '>>', '==', '<', '<=', '>', '>=', '[]', '[]=', '+', '-', '*', '~', '/', '%', '&', '^', '|'].each do |operator|
62
62
 
63
63
  it "should accept #{operator} as a method name" do
64
64
  content = <<-END
@@ -67,7 +67,7 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
67
67
  END
68
68
  @excellent.check_content(content)
69
69
 
70
- @excellent.errors.should be_empty
70
+ @excellent.warnings.should be_empty
71
71
  end
72
72
 
73
73
  end
@@ -80,12 +80,33 @@ describe Simplabs::Excellent::Checks::MethodNameCheck do
80
80
  end
81
81
  END
82
82
  @excellent.check_content(content)
83
- errors = @excellent.errors
83
+ warnings = @excellent.warnings
84
84
 
85
- errors.should_not be_empty
86
- errors[0].info.should == { :method => :badMethodName }
87
- errors[0].line_number.should == 1
88
- errors[0].message.should == 'Bad method name badMethodName.'
85
+ warnings.should_not be_empty
86
+ warnings[0].info.should == { :method => 'badMethodName' }
87
+ warnings[0].line_number.should == 1
88
+ warnings[0].message.should == 'Bad method name badMethodName.'
89
+ end
90
+
91
+ it "should correctly return the method's full name" do
92
+ content = <<-END
93
+ class Class
94
+ def badMethodName
95
+ end
96
+ def self.badMethodName2
97
+ end
98
+ end
99
+ END
100
+ @excellent.check_content(content)
101
+ warnings = @excellent.warnings
102
+
103
+ warnings.should_not be_empty
104
+ warnings[0].info.should == { :method => 'Class#badMethodName' }
105
+ warnings[0].line_number.should == 2
106
+ warnings[0].message.should == 'Bad method name Class#badMethodName.'
107
+ warnings[1].info.should == { :method => 'Class.badMethodName2' }
108
+ warnings[1].line_number.should == 4
109
+ warnings[1].message.should == 'Bad method name Class.badMethodName2.'
89
110
  end
90
111
 
91
112
  end
@@ -3,45 +3,44 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Simplabs::Excellent::Checks::ModuleLineCountCheck do
4
4
 
5
5
  before do
6
- @excellent = Simplabs::Excellent::Core::ParseTreeRunner.new(Simplabs::Excellent::Checks::ModuleLineCountCheck.new({ :threshold => 1 }))
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::ModuleLineCountCheck.new({ :threshold => 2 }))
7
7
  end
8
8
 
9
9
  describe '#evaluate' do
10
10
 
11
11
  it 'should accept modules with less lines than the threshold' do
12
12
  content = <<-END
13
- module ZeroLineModule; end
13
+ module OneLineModule; end
14
14
  END
15
15
  @excellent.check_content(content)
16
16
 
17
- @excellent.errors.should be_empty
17
+ @excellent.warnings.should be_empty
18
18
  end
19
19
 
20
20
  it 'should accept modules with the same number of lines as the threshold' do
21
21
  content = <<-END
22
- module OneLineModule
23
- @foo = 1
22
+ module TwoLinesModule
24
23
  end
25
24
  END
26
25
  @excellent.check_content(content)
27
26
 
28
- @excellent.errors.should be_empty
27
+ @excellent.warnings.should be_empty
29
28
  end
30
29
 
31
30
  it 'should reject modules with more lines than the threshold' do
32
31
  content = <<-END
33
- module TwoLineModule
32
+ module FourLinesModule
34
33
  @foo = 1
35
34
  @bar = 2
36
35
  end
37
36
  END
38
37
  @excellent.check_content(content)
39
- errors = @excellent.errors
38
+ warnings = @excellent.warnings
40
39
 
41
- errors.should_not be_empty
42
- errors[0].info.should == { :module => :TwoLineModule, :count => 2 }
43
- errors[0].line_number.should == 1
44
- errors[0].message.should == 'Module TwoLineModule has 2 lines.'
40
+ warnings.should_not be_empty
41
+ warnings[0].info.should == { :module => 'FourLinesModule', :count => 4 }
42
+ warnings[0].line_number.should == 1
43
+ warnings[0].message.should == 'FourLinesModule has 4 lines.'
45
44
  end
46
45
 
47
46
  end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Simplabs::Excellent::Checks::ModuleNameCheck do
4
4
 
5
5
  before do
6
- @excellent = Simplabs::Excellent::Core::ParseTreeRunner.new(Simplabs::Excellent::Checks::ModuleNameCheck.new)
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::ModuleNameCheck.new)
7
7
  end
8
8
 
9
9
  describe '#evaluate' do
@@ -15,7 +15,17 @@ describe Simplabs::Excellent::Checks::ModuleNameCheck do
15
15
  END
16
16
  @excellent.check_content(content)
17
17
 
18
- @excellent.errors.should be_empty
18
+ @excellent.warnings.should be_empty
19
+ end
20
+
21
+ it 'should accept namespaced modules' do
22
+ content = <<-END
23
+ module Outer::Inner::GoodModuleName
24
+ end
25
+ END
26
+ @excellent.check_content(content)
27
+
28
+ @excellent.warnings.should be_empty
19
29
  end
20
30
 
21
31
  it 'should reject module names with underscores' do
@@ -24,12 +34,26 @@ describe Simplabs::Excellent::Checks::ModuleNameCheck do
24
34
  end
25
35
  END
26
36
  @excellent.check_content(content)
27
- errors = @excellent.errors
37
+ warnings = @excellent.warnings
38
+
39
+ warnings.should_not be_empty
40
+ warnings[0].info.should == { :module => 'Bad_ModuleName' }
41
+ warnings[0].line_number.should == 1
42
+ warnings[0].message.should == 'Bad module name Bad_ModuleName.'
43
+ end
44
+
45
+ it 'should correctly report bad names of namespaced modules' do
46
+ content = <<-END
47
+ module Outer::Inner::Bad_ModuleName
48
+ end
49
+ END
50
+ @excellent.check_content(content)
51
+ warnings = @excellent.warnings
28
52
 
29
- errors.should_not be_empty
30
- errors[0].info.should == { :module => :Bad_ModuleName }
31
- errors[0].line_number.should == 1
32
- errors[0].message.should == 'Bad module name Bad_ModuleName.'
53
+ warnings.should_not be_empty
54
+ warnings[0].info.should == { :module => 'Outer::Inner::Bad_ModuleName' }
55
+ warnings[0].line_number.should == 1
56
+ warnings[0].message.should == 'Bad module name Outer::Inner::Bad_ModuleName.'
33
57
  end
34
58
 
35
59
  end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Simplabs::Excellent::Checks::NestedIteratorsCheck do
4
+
5
+ before do
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::NestedIteratorsCheck.new)
7
+ end
8
+
9
+ describe '#evaluate' do
10
+
11
+ it 'should reject a block inside a block' do
12
+ content = <<-END
13
+ method1 do
14
+ method2 do
15
+ end
16
+ end
17
+ END
18
+ @excellent.check_content(content)
19
+ warnings = @excellent.warnings
20
+
21
+ warnings.should_not be_empty
22
+ warnings[0].info.should == { :block => 'block', :parent => 'block' }
23
+ warnings[0].line_number.should == 2
24
+ warnings[0].message.should == 'block inside of block.'
25
+ end
26
+
27
+ it 'should accept 2 blocks inside a method that are not nested' do
28
+ content = <<-END
29
+ def method
30
+ method1 do
31
+ end
32
+ method2 do
33
+ end
34
+ end
35
+ END
36
+ @excellent.check_content(content)
37
+ warnings = @excellent.warnings
38
+
39
+ warnings.should be_empty
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Simplabs::Excellent::Checks::ParameterNumberCheck do
4
4
 
5
5
  before(:each) do
6
- @excellent = Simplabs::Excellent::Core::ParseTreeRunner.new(Simplabs::Excellent::Checks::ParameterNumberCheck.new({ :threshold => 1 }))
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::ParameterNumberCheck.new({ :threshold => 1 }))
7
7
  end
8
8
 
9
9
  describe '#evaluate' do
@@ -15,7 +15,7 @@ describe Simplabs::Excellent::Checks::ParameterNumberCheck do
15
15
  END
16
16
  @excellent.check_content(content)
17
17
 
18
- @excellent.errors.should be_empty
18
+ @excellent.warnings.should be_empty
19
19
  end
20
20
 
21
21
  it 'should accept methods with the same number of parameters as the threshold' do
@@ -25,7 +25,7 @@ describe Simplabs::Excellent::Checks::ParameterNumberCheck do
25
25
  END
26
26
  @excellent.check_content(content)
27
27
 
28
- @excellent.errors.should be_empty
28
+ @excellent.warnings.should be_empty
29
29
  end
30
30
 
31
31
  it 'should reject methods with more parameters than the threshold' do
@@ -34,28 +34,64 @@ describe Simplabs::Excellent::Checks::ParameterNumberCheck do
34
34
  end
35
35
  END
36
36
 
37
- verify_error_found(content)
37
+ verify_warning_found(content, 'two_parameter_method')
38
38
  end
39
39
 
40
- it 'should cope with default values on parameters' do
40
+ it 'should work with default values on parameters' do
41
41
  content = <<-END
42
42
  def two_parameter_method(first_parameter = 1, second_parameter = 2)
43
43
  end
44
44
  END
45
45
 
46
- verify_error_found(content)
46
+ verify_warning_found(content, 'two_parameter_method')
47
+ end
48
+
49
+ it 'should work with methods defined on objects' do
50
+ content = <<-END
51
+ def object.two_parameter_method(first_parameter = 1, second_parameter = 2)
52
+ end
53
+ END
54
+
55
+ verify_warning_found(content, 'object.two_parameter_method')
56
+ end
57
+
58
+ it 'should work with methods defined directly on classes' do
59
+ content = <<-END
60
+ def Class.two_parameter_method(first_parameter = 1, second_parameter = 2)
61
+ end
62
+ END
63
+
64
+ verify_warning_found(content, 'Class.two_parameter_method')
65
+ end
66
+
67
+ it 'should reject yield calls with more parameters than the threshold' do
68
+ content = <<-END
69
+ two_parameter_method do |first_parameter, second_parameter|
70
+ end
71
+ END
72
+
73
+ verify_warning_found(content, 'block')
74
+ end
75
+
76
+ it 'should reject yield calls on a receiver with more parameters than the threshold' do
77
+ content = <<-END
78
+ receiver.two_parameter_method do |first_parameter, second_parameter|
79
+ end
80
+ END
81
+
82
+ verify_warning_found(content, 'block')
47
83
  end
48
84
 
49
85
  end
50
86
 
51
- def verify_error_found(content)
87
+ def verify_warning_found(content, name)
52
88
  @excellent.check_content(content)
53
- errors = @excellent.errors
89
+ warnings = @excellent.warnings
54
90
 
55
- errors.should_not be_empty
56
- errors[0].info.should == { :method => :two_parameter_method, :parameter_count => 2 }
57
- errors[0].line_number.should == 1
58
- errors[0].message.should == 'Method two_parameter_method has 2 parameters.'
91
+ warnings.should_not be_empty
92
+ warnings[0].info.should == { :method => name, :parameters => 2 }
93
+ warnings[0].line_number.should == 1
94
+ warnings[0].message.should == "#{name} has 2 parameters."
59
95
  end
60
96
 
61
97
  end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Simplabs::Excellent::Checks::Rails::AttrAccessibleCheck do
4
+
5
+ before do
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::Rails::AttrAccessibleCheck.new)
7
+ end
8
+
9
+ describe '#evaluate' do
10
+
11
+ it 'should ignore classes that are not active record models' do
12
+ content = <<-END
13
+ class Test
14
+ end
15
+ END
16
+ @excellent.check_content(content)
17
+ warnings = @excellent.warnings
18
+
19
+ warnings.should be_empty
20
+ end
21
+
22
+ it 'should reject an active record model that does not specify attr_accessible' do
23
+ content = <<-END
24
+ class User < ActiveRecord::Base
25
+ end
26
+ END
27
+ @excellent.check_content(content)
28
+ warnings = @excellent.warnings
29
+
30
+ warnings.should_not be_empty
31
+ warnings[0].info.should == { :class => 'User' }
32
+ warnings[0].line_number.should == 1
33
+ warnings[0].message.should == 'User does not specify attr_accessible.'
34
+ end
35
+
36
+ it 'should reject an active record model that does specify attr_protected' do
37
+ content = <<-END
38
+ class User < ActiveRecord::Base
39
+ attr_protected :first_name
40
+ end
41
+ END
42
+ @excellent.check_content(content)
43
+ warnings = @excellent.warnings
44
+
45
+ warnings.should_not be_empty
46
+ warnings[0].info.should == { :class => 'User' }
47
+ warnings[0].line_number.should == 1
48
+ warnings[0].message.should == 'User does not specify attr_accessible.'
49
+ end
50
+
51
+ it 'should accept an active record model that does specify attr_accessible' do
52
+ content = <<-END
53
+ class User < ActiveRecord::Base
54
+ attr_accessible :first_name
55
+ end
56
+ END
57
+ @excellent.check_content(content)
58
+ warnings = @excellent.warnings
59
+
60
+ warnings.should be_empty
61
+ end
62
+
63
+ it 'should also work with namespaced models' do
64
+ content = <<-END
65
+ class Backend::User < ActiveRecord::Base
66
+ end
67
+ END
68
+ @excellent.check_content(content)
69
+ warnings = @excellent.warnings
70
+
71
+ warnings.should_not be_empty
72
+ warnings[0].info.should == { :class => 'Backend::User' }
73
+ warnings[0].line_number.should == 1
74
+ warnings[0].message.should == 'Backend::User does not specify attr_accessible.'
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ describe Simplabs::Excellent::Checks::Rails::AttrProtectedCheck do
4
+
5
+ before do
6
+ @excellent = Simplabs::Excellent::Runner.new(Simplabs::Excellent::Checks::Rails::AttrProtectedCheck.new)
7
+ end
8
+
9
+ describe '#evaluate' do
10
+
11
+ it 'should ignore classes that are not active record models' do
12
+ content = <<-END
13
+ class Test
14
+ end
15
+ END
16
+ @excellent.check_content(content)
17
+ warnings = @excellent.warnings
18
+
19
+ warnings.should be_empty
20
+ end
21
+
22
+ it 'should reject an active record model that does specify attr_protected' do
23
+ content = <<-END
24
+ class User < ActiveRecord::Base
25
+ attr_protected :first_name
26
+ end
27
+ END
28
+ @excellent.check_content(content)
29
+ warnings = @excellent.warnings
30
+
31
+ warnings.should_not be_empty
32
+ warnings[0].info.should == { :class => 'User' }
33
+ warnings[0].line_number.should == 1
34
+ warnings[0].message.should == 'User specifies attr_protected.'
35
+ end
36
+
37
+ it 'should accept an active record model that does specify attr_accessible' do
38
+ content = <<-END
39
+ class User < ActiveRecord::Base
40
+ attr_accessible :first_name
41
+ end
42
+ END
43
+ @excellent.check_content(content)
44
+ warnings = @excellent.warnings
45
+
46
+ warnings.should be_empty
47
+ end
48
+
49
+ it 'should accept an active record model that specifies neither attr_accessible not attr_protected' do
50
+ content = <<-END
51
+ class User < ActiveRecord::Base
52
+ end
53
+ END
54
+ @excellent.check_content(content)
55
+ warnings = @excellent.warnings
56
+
57
+ warnings.should be_empty
58
+ end
59
+
60
+ it 'should also work with namespaced models' do
61
+ content = <<-END
62
+ class Backend::User < ActiveRecord::Base
63
+ attr_protected :first_name
64
+ end
65
+ END
66
+ @excellent.check_content(content)
67
+ warnings = @excellent.warnings
68
+
69
+ warnings.should_not be_empty
70
+ warnings[0].info.should == { :class => 'Backend::User' }
71
+ warnings[0].line_number.should == 1
72
+ warnings[0].message.should == 'Backend::User specifies attr_protected.'
73
+ end
74
+
75
+ end
76
+
77
+ end