guideline 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,17 +21,18 @@ $ gem install guideline
21
21
  ```
22
22
  $ guideline --help
23
23
  Usage: guideline [options]
24
- --no-abc-complexity (default: true) check method ABC complexity
25
- --no-hard-tab-indent (default: true) check hard tab indent
26
- --no-hash-comma (default: true) check last comma in Hash literal
27
- --no-long-line (default: true) check line length
28
- --no-long-method (default: true) check method height
29
- --no-trailing-whitespace (default: true) check trailing whitespace
30
- --no-unused-method (default: true) check unused method
31
- --abc-complexity= (default: 15) threshold of ABC complexity
32
- --long-line= (default: 80) threshold of long line
33
- --long-method= (default: 10) threshold of long method
34
- --path= (default: ./) checked file or dir or glob pattern
24
+ --no-abc-complexity (default: false) check method ABC complexity
25
+ --no-hard-tab-indent (default: false) check hard tab indent
26
+ --no-hash-comma (default: false) check last comma in Hash literal
27
+ --no-long-line (default: false) check line length
28
+ --no-long-method (default: false) check method height
29
+ --no-trailing-whitespace (default: false) check trailing whitespace
30
+ --no-unused-method (default: false) check unused method
31
+ --no-detail (default: false) only render summary
32
+ --abc-complexity= (default: 15) threshold of ABC complexity
33
+ --long-line= (default: 80) threshold of long line
34
+ --long-method= (default: 10) threshold of long method
35
+ --path= (default: ./) checked file or dir or glob pattern
35
36
  ```
36
37
 
37
38
  ```
@@ -1,6 +1,7 @@
1
1
  require "guideline/version"
2
2
  require "guideline/path_finder"
3
3
  require "guideline/visitor"
4
+ require "guideline/renderer"
4
5
  require "guideline/error"
5
6
  require "guideline/option_parser"
6
7
  require "guideline/runner"
@@ -10,14 +10,21 @@ module Guideline
10
10
 
11
11
  def report(options)
12
12
  errors << Error.new(
13
+ :line => options[:line],
13
14
  :message => options[:message],
14
15
  :path => options[:path],
15
- :line => options[:line]
16
+ :name => name
16
17
  )
17
18
  end
18
19
 
19
20
  def has_error?
20
21
  !errors.empty?
21
22
  end
23
+
24
+ private
25
+
26
+ def name
27
+ @name ||= self.class.to_s.split("::").last
28
+ end
22
29
  end
23
30
  end
@@ -1,9 +1,16 @@
1
- require "ostruct"
2
-
3
1
  module Guideline
4
- class Error < OpenStruct
2
+ class Error
3
+ attr_reader :line, :message, :path, :name
4
+
5
+ def initialize(options)
6
+ @line = options[:line]
7
+ @message = options[:message]
8
+ @path = options[:path]
9
+ @name = options[:name]
10
+ end
11
+
5
12
  def render
6
- puts "%4d: %s" % [line, message]
13
+ "%4d: %s" % [line, message]
7
14
  end
8
15
  end
9
16
  end
@@ -3,17 +3,18 @@ require "optparse"
3
3
  module Guideline
4
4
  class OptionParser < ::OptionParser
5
5
  OPTIONS = [
6
- "--no-abc-complexity", "(default: true) check method ABC complexity",
7
- "--no-hard-tab-indent", "(default: true) check hard tab indent",
8
- "--no-hash-comma", "(default: true) check last comma in Hash literal",
9
- "--no-long-line", "(default: true) check line length",
10
- "--no-long-method", "(default: true) check method height",
11
- "--no-trailing-whitespace", "(default: true) check trailing whitespace",
12
- "--no-unused-method", "(default: true) check unused method",
13
- "--abc-complexity=", "(default: 15) threshold of ABC complexity",
14
- "--long-line=", "(default: 80) threshold of long line",
15
- "--long-method=", "(default: 10) threshold of long method",
16
- "--path=", "(default: ./) checked file or dir or glob pattern",
6
+ "--no-abc-complexity", "(default: false) check method ABC complexity",
7
+ "--no-hard-tab-indent", "(default: false) check hard tab indent",
8
+ "--no-hash-comma", "(default: false) check last comma in Hash literal",
9
+ "--no-long-line", "(default: false) check line length",
10
+ "--no-long-method", "(default: false) check method height",
11
+ "--no-trailing-whitespace", "(default: false) check trailing whitespace",
12
+ "--no-unused-method", "(default: false) check unused method",
13
+ "--no-detail", "(default: false) only render summary",
14
+ "--abc-complexity=", "(default: 15) threshold of ABC complexity",
15
+ "--long-line=", "(default: 80) threshold of long line",
16
+ "--long-method=", "(default: 10) threshold of long method",
17
+ "--path=", "(default: ./) checked file or dir or glob pattern",
17
18
  ]
18
19
 
19
20
  def self.parse(argv = ARGV)
@@ -0,0 +1,47 @@
1
+ require "pathname"
2
+
3
+ module Guideline
4
+ class Renderer
5
+ def initialize(errors)
6
+ @errors = errors
7
+ end
8
+
9
+ def detail
10
+ "".tap do |result|
11
+ errors_by_path.each do |path, errors|
12
+ result << "\n#{path}\n"
13
+ errors.sort_by(&:line).each {|error| result << "#{error.render}\n" }
14
+ end
15
+ end
16
+ end
17
+
18
+ def summary
19
+ "".tap do |result|
20
+ error_summary.each do |name, count|
21
+ result << "#{name}: #{count}\n"
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def errors_by_path
29
+ errors.group_by(&:path)
30
+ end
31
+
32
+ def error_summary
33
+ errors.inject(summary_hash) do |hash, error|
34
+ hash[error.name] += 1
35
+ hash
36
+ end
37
+ end
38
+
39
+ def errors
40
+ @errors
41
+ end
42
+
43
+ def summary_hash
44
+ Hash.new {|hash, key| hash[key] = 0 }
45
+ end
46
+ end
47
+ end
@@ -5,17 +5,27 @@ module Guideline
5
5
  end
6
6
 
7
7
  def run
8
- visitor.prepare
9
- visitor.visit
10
- visitor.render
8
+ puts renderer.detail if options[:detail] != false
9
+ puts
10
+ puts renderer.summary
11
11
  end
12
12
 
13
13
  private
14
14
 
15
+ def renderer
16
+ @renderer ||= Renderer.new(errors)
17
+ end
18
+
15
19
  def visitor
16
20
  @visitor ||= Visitor.new(options[:path], enable_checkers)
17
21
  end
18
22
 
23
+ def errors
24
+ visitor.prepare
25
+ visitor.visit
26
+ visitor.errors
27
+ end
28
+
19
29
  def enable_checkers
20
30
  array = []
21
31
  array << abc_complexity_checker if options[:abc_complexity] != false
@@ -1,3 +1,3 @@
1
1
  module Guideline
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -25,12 +25,8 @@ module Guideline
25
25
  end
26
26
  end
27
27
 
28
- def render
29
- errors.group_by(&:path).each do |path, errors|
30
- puts path
31
- errors.sort_by(&:line).each(&:render)
32
- puts
33
- end
28
+ def errors
29
+ @errors ||= checkers.select(&:has_error?).map(&:errors).inject([], &:+)
34
30
  end
35
31
 
36
32
  private
@@ -46,9 +42,5 @@ module Guideline
46
42
  def pattern
47
43
  @pattern
48
44
  end
49
-
50
- def errors
51
- @errors ||= checkers.select(&:has_error?).map(&:errors).inject([], &:+)
52
- end
53
45
  end
54
46
  end
@@ -3,14 +3,17 @@ require "spec_helper"
3
3
  module Guideline
4
4
  describe Error do
5
5
  let(:error) do
6
- described_class.new(:line => 1, :message => "message")
6
+ described_class.new(:line => 1, :type => :type, :message => "message")
7
7
  end
8
8
 
9
9
  describe "#render" do
10
- it "renders its line and message" do
11
- error.should_receive(:puts).with(" 1: message")
10
+ subject do
12
11
  error.render
13
12
  end
13
+
14
+ it "return string including its line and message" do
15
+ should == " 1: message"
16
+ end
14
17
  end
15
18
  end
16
19
  end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe Renderer do
5
+ let(:renderer) do
6
+ described_class.new(errors)
7
+ end
8
+
9
+ let(:errors) do
10
+ [error]
11
+ end
12
+
13
+ let(:error) do
14
+ mock(
15
+ :line => 1,
16
+ :path => "path",
17
+ :render => "rendered",
18
+ :name => "name"
19
+ )
20
+ end
21
+
22
+ describe "#detail" do
23
+ subject do
24
+ renderer.detail
25
+ end
26
+
27
+ it { should == "\npath\nrendered\n" }
28
+ end
29
+
30
+ describe "#summary" do
31
+ subject do
32
+ renderer.summary
33
+ end
34
+
35
+ it { should == "name: 1\n" }
36
+ end
37
+ end
38
+ end
@@ -56,25 +56,5 @@ module Guideline
56
56
  end
57
57
  end
58
58
  end
59
-
60
- describe "#render" do
61
- before do
62
- visitor.instance_variable_set(:@errors, errors)
63
- visitor.stub(:puts)
64
- end
65
-
66
- let(:errors) do
67
- [error]
68
- end
69
-
70
- let(:error) do
71
- mock(:path => "path", :line => 1)
72
- end
73
-
74
- it "calls #render of each error" do
75
- error.should_receive(:render)
76
- visitor.render
77
- end
78
- end
79
59
  end
80
60
  end
@@ -3,3 +3,9 @@ SimpleCov.start
3
3
 
4
4
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
5
  require "guideline"
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guideline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -104,6 +104,7 @@ files:
104
104
  - lib/guideline/option_parser.rb
105
105
  - lib/guideline/parser/moduleable.rb
106
106
  - lib/guideline/path_finder.rb
107
+ - lib/guideline/renderer.rb
107
108
  - lib/guideline/runner.rb
108
109
  - lib/guideline/version.rb
109
110
  - lib/guideline/visitor.rb
@@ -118,6 +119,7 @@ files:
118
119
  - spec/guideline/checkers/unused_method_checker_spec.rb
119
120
  - spec/guideline/error_spec.rb
120
121
  - spec/guideline/option_parser_spec.rb
122
+ - spec/guideline/renderer_spec.rb
121
123
  - spec/guideline/runner_spec.rb
122
124
  - spec/guideline/visitor_spec.rb
123
125
  - spec/spec_helper.rb
@@ -157,6 +159,7 @@ test_files:
157
159
  - spec/guideline/checkers/unused_method_checker_spec.rb
158
160
  - spec/guideline/error_spec.rb
159
161
  - spec/guideline/option_parser_spec.rb
162
+ - spec/guideline/renderer_spec.rb
160
163
  - spec/guideline/runner_spec.rb
161
164
  - spec/guideline/visitor_spec.rb
162
165
  - spec/spec_helper.rb