tailor 0.0.3 → 0.1.0

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 (44) hide show
  1. data/History.txt +8 -0
  2. data/Manifest.txt +22 -2
  3. data/PostInstall.txt +4 -0
  4. data/README.rdoc +7 -6
  5. data/Rakefile +7 -1
  6. data/bin/tailor +21 -5
  7. data/features/indentation.feature +22 -0
  8. data/features/spacing.feature +13 -18
  9. data/features/spacing/commas.feature +44 -0
  10. data/features/step_definitions/indentation_steps.rb +139 -0
  11. data/features/step_definitions/spacing/commas_steps.rb +14 -0
  12. data/features/step_definitions/spacing_steps.rb +1 -36
  13. data/features/support/1_file_with_bad_comma_spacing/bad_comma_spacing.rb +43 -5
  14. data/features/support/1_file_with_bad_curly_brace_spacing/bad_curly_brace_spacing.rb +60 -0
  15. data/features/support/1_file_with_bad_operator_spacing/bad_op_spacing.rb +31 -0
  16. data/features/support/1_file_with_bad_parenthesis/bad_parenthesis.rb +1 -3
  17. data/features/support/1_file_with_bad_square_brackets/bad_square_brackets.rb +62 -0
  18. data/features/support/1_file_with_bad_ternary_colon_spacing/bad_ternary_colon_spacing.rb +31 -0
  19. data/features/support/1_good_simple_file/simple_project.rb +5 -0
  20. data/features/support/1_long_file_with_indentation/my_project.rb +56 -0
  21. data/features/support/common.rb +74 -1
  22. data/features/support/env.rb +3 -1
  23. data/features/support/world.rb +0 -52
  24. data/lib/tailor.rb +132 -41
  25. data/lib/tailor/file_line.rb +66 -177
  26. data/lib/tailor/indentation.rb +251 -0
  27. data/lib/tailor/spacing.rb +243 -0
  28. data/lib/tasks/metrics.rake +23 -0
  29. data/lib/tasks/stats.rake +11 -0
  30. data/logic.txt +30 -0
  31. data/ruby-style-checker.rb +77 -46
  32. data/spec/file_line_spec.rb +18 -193
  33. data/spec/indentation_spec.rb +259 -0
  34. data/spec/spacing/colon_spacing_spec.rb +71 -0
  35. data/spec/spacing/comma_spacing_spec.rb +159 -0
  36. data/spec/spacing/curly_brace_spacing_spec.rb +258 -0
  37. data/spec/spacing/parentheses_spacing_spec.rb +28 -0
  38. data/spec/spacing/square_bracket_spacing_spec.rb +116 -0
  39. data/spec/spacing_spec.rb +167 -0
  40. data/spec/spec_helper.rb +4 -0
  41. data/spec/tailor_spec.rb +2 -2
  42. metadata +73 -38
  43. data/features/support/1_good_simple_file/my_project.rb +0 -7
  44. data/lib/tailor/indentation_checker.rb +0 -27
@@ -0,0 +1,14 @@
1
+ Given /^a file with (\d+) spaces? after a comma in a:$/ do |arg1, table|
2
+ # table is a Cucumber::Ast::Table
3
+ pending # express the regexp above with the code you wish you had
4
+ end
5
+
6
+ Given /^a file with more than (\d+) spaces before a comma in a:$/ do |arg1, table|
7
+ # table is a Cucumber::Ast::Table
8
+ pending # express the regexp above with the code you wish you had
9
+ end
10
+
11
+ Given /^a file with (\d+) spaces before a comma in a:$/ do |arg1, table|
12
+ # table is a Cucumber::Ast::Table
13
+ pending # express the regexp above with the code you wish you had
14
+ end
@@ -1,18 +1,5 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../../lib')
2
- require 'tailor/indentation_checker'
3
1
  require 'tailor/file_line'
4
2
 
5
- include Tailor
6
- include Tailor::IndentationChecker
7
-
8
- def check_file
9
- ruby_source = File.open(@file_list[0], 'r')
10
-
11
- ruby_source.each_line do |line|
12
- yield(line)
13
- end
14
- end
15
-
16
3
  def check_spacing method_name, line_type
17
4
  is_line_type = false
18
5
  bad_spacing = false
@@ -56,17 +43,6 @@ Given /^that file contains lines with hard tabs$/ do
56
43
  contains_hard_tabs.should be_true
57
44
  end
58
45
 
59
- Given /^that file does not contain any "([^\"]*)" statements$/ do |keyword|
60
- ruby_source = File.open(@file_list[0], 'r')
61
-
62
- count = count_keywords(ruby_source, keyword)
63
- count.should == 0
64
- end
65
-
66
- Given /^that file is indented properly$/ do
67
- pending
68
- end
69
-
70
46
  Given /^that file contains lines with trailing whitespace$/ do
71
47
  line_number = 1
72
48
 
@@ -128,13 +104,6 @@ Given /^that file contains a "([^\"]*)" line with spaces after an open parenthes
128
104
  check_spacing("space_before_closed_parenthesis?", line_type)
129
105
  end
130
106
 
131
- #-----------------------------------------------------------------------------
132
- # "When" statements
133
- #-----------------------------------------------------------------------------
134
- When "I run the checker on the project" do
135
- @result = `#{@tailor} #{@project_dir}`
136
- end
137
-
138
107
  #-----------------------------------------------------------------------------
139
108
  # "Then" statements
140
109
  #-----------------------------------------------------------------------------
@@ -142,16 +111,12 @@ Then /^the checker should tell me each line that has a hard tab$/ do
142
111
  @result.should include("Line contains hard tabs")
143
112
  end
144
113
 
145
- Then "the checker should tell me my indentation is OK" do
146
- pending
147
- end
148
-
149
114
  Then /^the checker should tell me each line has trailing whitespace$/ do
150
115
  @result.should include("Line contains #{@whitespace_count} trailing whitespace(s)")
151
116
  end
152
117
 
153
118
  Then /^the checker should tell me each line is too long$/ do
154
- message = "Line is greater than #{Tailor::FileLine::LINE_LENGTH_MAX} characters"
119
+ message = "Line is >#{Tailor::FileLine::LINE_LENGTH_MAX} characters"
155
120
  @result.should include(message)
156
121
  end
157
122
 
@@ -1,5 +1,43 @@
1
- # This is a sample which ,when checked,should show bad-comma errors. Comma errors,
2
- # and stuff, make source difficult to read!
3
- def do_something this ,that, other
4
- puts "We ,I mean I,am doing, er, something!"
5
- end
1
+ # Perfect spacing after a comma, in comment and method
2
+ def do_something this, that, other
3
+ puts "We, I mean I, am doing, er, something!"
4
+ end
5
+
6
+ # More than one space, after a comma, in a comment & method
7
+ def do_something this, that, other
8
+ puts "We, I mean I, am doing, er, something!"
9
+ end
10
+
11
+ # No space,after a comma,in a comment & method
12
+ def do_something this,that,other
13
+ puts "We,I mean I ,am doing,er ,something!"
14
+ end
15
+
16
+ # Spaces , before a comma ,in a comment & method
17
+ def do_something this , that ,other
18
+ puts "We , I mean I ,am doing ,er , something!"
19
+ end
20
+
21
+ # Perfect spacing around a comma, in an array
22
+ thing = [ 1, 2, 3 ]
23
+
24
+ # More than one space, after elements in an array
25
+ thing = [ 1, 2, 3 ]
26
+
27
+ # No space, after elements in an array
28
+ thing = [ 1,2,3 ]
29
+
30
+ # Spaces, before elements in an array
31
+ thing = [ 1 , 2 ,3 ]
32
+
33
+ # Perfect spacing around a comma, in a hash
34
+ thing = { :one => 1, :two => 2, :three => 3 }
35
+
36
+ # More than one space, after elements in a hash
37
+ thing = { :one => 1, :two => 2, :three => 3 }
38
+
39
+ # No space, after elements in an array
40
+ thing = { :one => 1,:two => 2,:three => 3 }
41
+
42
+ # Spaces, before elements in an array
43
+ thing = { :one => 1 , :two => 2 , :three => 3 }
@@ -0,0 +1,60 @@
1
+ # Blocks...
2
+ # No space after {
3
+ 1..10.times {|number| puts number }
4
+
5
+ # No space before {
6
+ 1..10.times{ |number| puts number }
7
+
8
+ # No space before or after {
9
+ 1..10.times{|number| puts number }
10
+
11
+ # No space before }
12
+ 1..10.times { |number| puts number}
13
+
14
+ # No space before or after { and }
15
+ 1..10.times{|number| puts number}
16
+
17
+ # >1 space before {
18
+ 1..10.times { |number| puts number }
19
+
20
+ # >1 space after {
21
+ 1..10.times { |number| puts number }
22
+
23
+ # >1 space before, no spaces after {
24
+ 1..10.times {|number| puts number }
25
+
26
+ # >1 space after, no spaces before {
27
+ 1..10.times{ |number| puts number }
28
+
29
+ # >1 space before }
30
+ 1..10.times { |number| puts number }
31
+
32
+ # Perfect
33
+ 1..10.times { |number| puts number }
34
+
35
+
36
+ # Hashes...
37
+ # No space after {
38
+ thing = {:one => 1 }
39
+
40
+ # No space before {
41
+ thing ={ :one => 1 }
42
+
43
+ # No space before or after {
44
+ thing ={:one => 1 }
45
+
46
+ # No space before }
47
+ thing = { :one => 1}
48
+
49
+ # No space before or after { and }
50
+ thing ={:one => 1}
51
+
52
+ # Perfect
53
+ thing = { :one => 1 }
54
+
55
+ # Skip on default params in methods...
56
+ def a_method; one={}; end;
57
+
58
+ # Skip on strings...
59
+ a_string = "This is a #{thing}..."
60
+ b_string = "This has #{Class.methods}"
@@ -0,0 +1,31 @@
1
+ # Comment with a +
2
+ # Comment with a -
3
+ # Comment with a *
4
+ # Comment with a /
5
+ # Comment with a =
6
+ # Comment with a +=
7
+ # Comment with a -=
8
+ # Comment with a *=
9
+ # Comment with a /=
10
+ # Comment with a %=
11
+ # Comment with a **=
12
+ # Comment with a ||=
13
+ # Comment with a &&=
14
+ # Comment with a ==
15
+ # Comment with a ===
16
+ # Comment with a !=
17
+ # Comment with a >
18
+ # Comment with a <
19
+ # Comment with a >=
20
+ # Comment with a <=
21
+ # Comment with a <=>
22
+ # Comment with a &&
23
+ # Comment with a ||
24
+ # Comment with a ?
25
+ # Comment with a :
26
+ # Comment with a =~
27
+
28
+ class BadOps
29
+ def +@
30
+ end
31
+ end
@@ -1,6 +1,4 @@
1
- # This is a comment; this should get caught: ( me, you )
2
- # This is a comment; this should also get caught: [ us, them ]
1
+ # Spaces after open and before closed parentheses in a comment: ( me, you )
3
2
  def do_something( hello, and_hi=[ "me", "you" ] )
4
- test = [ "one", "two" ]
5
3
  File.expand_path( File.dirname (__FILE__) )
6
4
  end
@@ -0,0 +1,62 @@
1
+ # Arrays...
2
+ # Perfect
3
+ thing = []
4
+
5
+ # 1 space after [
6
+ thing = [ ]
7
+
8
+ # Perfect with element
9
+ one = 'won'
10
+ thing = [one]
11
+
12
+ # 1 space after [ with element
13
+ thing = [ one]
14
+
15
+ # 1 space before ] with element
16
+ thing = [one ]
17
+
18
+ # 1 space before and after [ and ]
19
+ thing = [ one ]
20
+
21
+ # Perfect element reference
22
+ thing[0]
23
+
24
+ # 1 space after [ with element reference
25
+ thing[ 0]
26
+
27
+ # 1 space before [ with element reference
28
+ thing [0]
29
+
30
+ # 1 space before ] with element reference
31
+ thing[0 ]
32
+
33
+ # Pefect multi-line
34
+ two = 'too'
35
+ thing = [
36
+ one,
37
+ two
38
+ ]
39
+
40
+ # Perfect multi-line, indented
41
+ def thing
42
+ a_thing = [
43
+ one,
44
+ two
45
+ ]
46
+ end
47
+
48
+
49
+ # Hash references...
50
+ thing = { :one => 1 }
51
+
52
+ # Perfect element reference
53
+ thing[:one]
54
+
55
+ # 1 space after [ with element reference
56
+ thing[ :one]
57
+
58
+ # 1 space before [ with element reference
59
+ thing [:one]
60
+
61
+ # 1 space before ] with element reference
62
+ thing[:one ]
@@ -0,0 +1,31 @@
1
+ # Perfect spacing around ternary colon
2
+ bobo = true ? true : false
3
+
4
+ # No space after ternary colon
5
+ bobo = true ? true :false
6
+
7
+ # No space before ternary colon
8
+ bobo = true ? true: false
9
+
10
+ # No space before or after ternary colon
11
+ bobo = true ? true:false
12
+
13
+ # 2 spaces after ternary colon
14
+ bobo = true ? true : false
15
+
16
+ # 2 spaces before ternary colon
17
+ bobo = true ? true : false
18
+
19
+ # Skip when colon is part of a symbol or namespace operator
20
+ bobo = { :thing => ":clown" }
21
+ bobo[:thing] == :dog ? bobo[:thing] : Math::PI
22
+
23
+ # Skip when colon is part of Regexp class
24
+ bobo[:thing].scan(/[:alpha:]/)
25
+
26
+ # Skip when setting load path
27
+ $:.unshift File.dirname(__FILE__)
28
+
29
+ # Skip when question mark method is followed by a symbol
30
+ if bobo[:thing].eql? :clown
31
+ end
@@ -0,0 +1,5 @@
1
+ class Something
2
+ def a_method
3
+ puts "hi"
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ # This file contains 1 method, no class, and is properly indented
2
+ # in order to test a related scenario.
3
+ module MyModule
4
+
5
+ # This is a class!
6
+ class AnotherThing
7
+
8
+ # This is a method!
9
+ def a_method
10
+ case
11
+ when 1
12
+ 1..10.each { |num| puts num }
13
+ when 2
14
+ else
15
+ while false
16
+ # Don't do anything
17
+ # And stuff
18
+ "meow".scan(/woem/)
19
+ array = [1 ,2 ,3]
20
+ other_thing = [
21
+ 4,
22
+ 5,
23
+ 6
24
+ ]
25
+ end
26
+ end
27
+
28
+ an_array = Array.new
29
+ a_hash = Hash.new
30
+
31
+ # This is another comment
32
+ an_array = [1, 2, 3]
33
+ a_hash = {
34
+ :one => 1,
35
+ :two => 2
36
+ }
37
+
38
+ if true
39
+ # Let's return!
40
+ return true
41
+ elsif false
42
+ return false
43
+ else
44
+ return nil
45
+ end
46
+
47
+ # Now how about a block...
48
+ 1..10.each do |number|
49
+ begin
50
+ rescue
51
+ ensure
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
2
+
3
+ require 'tailor'
4
+
5
+ include Tailor
6
+
1
7
  module CommonHelpers
2
8
  def in_tmp_folder(&block)
3
9
  FileUtils.chdir(@tmp_root, &block)
@@ -26,4 +32,71 @@ module CommonHelpers
26
32
  end
27
33
  end
28
34
 
29
- World(CommonHelpers)
35
+ World(CommonHelpers)
36
+
37
+ # Counts keywords in the file provided.
38
+ #
39
+ # @param [String] file Path to the file to check
40
+ # @param [String] keyword Keyword to count
41
+ # @return [Number] Number of keywords counted
42
+ def count_keywords file, keyword
43
+ ruby_source = File.open(file, 'r')
44
+
45
+ count = 0
46
+ ruby_source.each_line do |line|
47
+ if line =~ /^#{keyword}/
48
+ count =+ 1
49
+ end
50
+ end
51
+ ruby_source.close
52
+ count
53
+ end
54
+
55
+ def check_file
56
+ ruby_source = File.open(@file_list[0], 'r')
57
+
58
+ ruby_source.each_line do |line|
59
+ yield(line)
60
+ end
61
+ end
62
+
63
+ # Prep for the testing
64
+ Before do
65
+ @tailor = "#{File.dirname(__FILE__)}/../../bin/tailor"
66
+ end
67
+
68
+ #-----------------------------------------------------------------------------
69
+ # "Given" statements
70
+ #-----------------------------------------------------------------------------
71
+ Given /^I have a project directory "([^\"]*)"$/ do |project_dir|
72
+ project_dir = "#{@features_path}/support/#{project_dir}"
73
+ File.exists?(project_dir).should be_true
74
+ File.directory?(project_dir).should be_true
75
+ @project_dir = project_dir
76
+ end
77
+
78
+ Given /^the file contains only "([^\"]*)" "([^\"]*)" statement$/ do
79
+ |count_in_spec, keyword|
80
+ #count_in_file = count_keywords(@ruby_source, keyword)
81
+ count_in_file = count_keywords(@file_list[0], keyword)
82
+ count_in_file.should == count_in_spec.to_i
83
+ end
84
+
85
+ Given /^I have 1 file in my project$/ do
86
+ @file_list = Dir.glob("#{@project_dir}/*")
87
+ @file_list.length.should == 1
88
+ end
89
+
90
+ Given /^that file does not contain any "([^\"]*)" statements$/ do |keyword|
91
+ ruby_source = File.open(@file_list[0], 'r')
92
+
93
+ count = count_keywords(ruby_source, keyword)
94
+ count.should == 0
95
+ end
96
+
97
+ #-----------------------------------------------------------------------------
98
+ # "When" statements
99
+ #-----------------------------------------------------------------------------
100
+ When "I run the checker on the project" do
101
+ @result = `#{@tailor} #{@project_dir}`
102
+ end