tailor 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,259 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'tailor/file_line'
3
+ require 'pathname'
4
+
5
+ include Tailor
6
+
7
+ def strip_regex regexp
8
+ original_regexp = regexp.source
9
+
10
+ case original_regexp
11
+ when /\\b\w+\\b/
12
+ return original_regexp.gsub!("\\b", '')
13
+ when /\w+{2,}/
14
+ return original_regexp.scan(/\w+{2,}/).first
15
+ when /\\\{\[/
16
+ return '{'
17
+ when /\*\\\}/
18
+ return '}'
19
+ when /\\\[\[/
20
+ return '['
21
+ when /\*\\\]/
22
+ return ']'
23
+ end
24
+ end
25
+
26
+ describe Tailor::Indentation do
27
+ include Tailor::Indentation
28
+
29
+ context "should return the number of leading spaces in a line" do
30
+ it "when the line is not indented" do
31
+ line = create_file_line "def do_something", __LINE__
32
+ line.indented_spaces.should == 0
33
+ end
34
+
35
+ it "when the line is indented 1 space" do
36
+ line = create_file_line " def do_something", __LINE__
37
+ line.indented_spaces.should == 1
38
+ end
39
+
40
+ it "when the line is indented 1 space and a hard tab" do
41
+ line = create_file_line " \tdef do_something", __LINE__
42
+ line.indented_spaces.should == 1
43
+ end
44
+ end
45
+
46
+ context "should know what level of indentation a line is at" do
47
+ context "for indent expressions" do
48
+ INDENT_EXPRESSIONS.each do |regexp|
49
+ expression = strip_regex(regexp)
50
+
51
+ it "when the '#{expression }' line is not indented" do
52
+ line = create_file_line "#{expression}", __LINE__
53
+ line.is_at_level.should == 0.0
54
+ end
55
+
56
+ it "when the '#{expression}' line is indented only 1 space" do
57
+ line = create_file_line " #{expression}", __LINE__
58
+ line.is_at_level.should == 0.5
59
+ end
60
+
61
+ it "when the '#{expression}' line is indented 2 spaces" do
62
+ line = create_file_line " #{expression}", __LINE__
63
+ line.is_at_level.should == 1.0
64
+ end
65
+ end
66
+ end
67
+
68
+ context "for outdent expressions" do
69
+ OUTDENT_EXPRESSIONS.each do |regexp|
70
+ expression = strip_regex(regexp)
71
+
72
+ it "when the '#{expression }' line is not indented" do
73
+ line = create_file_line "#{expression}", __LINE__
74
+ line.is_at_level.should == 0.0
75
+ end
76
+
77
+ it "when the '#{expression}' line is indented only 1 space" do
78
+ line = create_file_line " #{expression}", __LINE__
79
+ line.is_at_level.should == 0.5
80
+ end
81
+
82
+ it "when the '#{expression}' line is indented 2 spaces" do
83
+ line = create_file_line " #{expression}", __LINE__
84
+ line.is_at_level.should == 1.0
85
+ end
86
+ end
87
+ end
88
+
89
+ context "for end expressions" do
90
+ END_EXPRESSIONS.each do |regexp|
91
+ expression = strip_regex(regexp)
92
+
93
+ it "when the '#{expression }' line is not indented" do
94
+ line = create_file_line "#{expression}", __LINE__
95
+ line.is_at_level.should == 0.0
96
+ end
97
+
98
+ it "when the '#{expression}' line is indented only 1 space" do
99
+ line = create_file_line " #{expression}", __LINE__
100
+ line.is_at_level.should == 0.5
101
+ end
102
+
103
+ it "when the '#{expression}' line is indented 2 spaces" do
104
+ line = create_file_line " #{expression}", __LINE__
105
+ line.is_at_level.should == 1.0
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ context "#indent?" do
112
+ INDENT_EXPRESSIONS.each do |regexp|
113
+ expression = strip_regex(regexp)
114
+
115
+ it "should return true if the line contains #{expression}" do
116
+ line = create_file_line "#{expression}", __LINE__
117
+ line.indent?.should be_true
118
+ end
119
+ end
120
+ end
121
+
122
+ context "#outdent?" do
123
+ OUTDENT_EXPRESSIONS.each do |regexp|
124
+ expression = strip_regex(regexp)
125
+
126
+ it "should return true if the line contains #{expression}" do
127
+ line = create_file_line "#{expression}", __LINE__
128
+ line.outdent?.should be_true
129
+ end
130
+ end
131
+ end
132
+
133
+ context "#contains_end?" do
134
+ END_EXPRESSIONS.each do |regexp|
135
+ expression = strip_regex(regexp)
136
+
137
+ it "should return true if the line contains #{expression}" do
138
+ line = create_file_line "#{expression}", __LINE__
139
+ line.contains_end?.should be_true
140
+ end
141
+ end
142
+ end
143
+
144
+ context "#at_improper_level?" do
145
+ it "should return true if the line is at the wrong level" do
146
+ proper_level = 1.0
147
+ line = create_file_line "class SomeClass", __LINE__
148
+ line.at_improper_level?(proper_level).should be_true
149
+ end
150
+
151
+ it "should return false if the line is at the right level" do
152
+ proper_level = 0.0
153
+ line = create_file_line "class SomeClass", __LINE__
154
+ line.at_improper_level?(proper_level).should be_false
155
+ end
156
+ end
157
+
158
+ context "#ends_with_operator?" do
159
+ OPERATORS.each_pair do |op_family, op_values|
160
+ op_values.each do |op|
161
+ it "should return true if the line ends with a #{op}" do
162
+ line = create_file_line "1 #{op}", __LINE__
163
+ line.ends_with_operator?.should be_true
164
+ end
165
+
166
+ it "should return true if the line ends with a #{op} plus spaces" do
167
+ line = create_file_line "1 #{op} ", __LINE__
168
+ line.ends_with_operator?.should be_true
169
+ end
170
+
171
+ it "should return true if the line ends with a #{op} plus tabs" do
172
+ line = create_file_line "1 #{op}\t\t", __LINE__
173
+ line.ends_with_operator?.should be_true
174
+ end
175
+
176
+ it "should return true if the line only has spaces plus a #{op}" do
177
+ line = create_file_line " #{op}", __LINE__
178
+ line.ends_with_operator?.should be_true
179
+ end
180
+ end
181
+ end
182
+
183
+ it "should return false if the line doesn't contain an operator" do
184
+ line = create_file_line " def some_method(thing)", __LINE__
185
+ line.ends_with_operator?.should be_false
186
+ end
187
+ end
188
+
189
+ context "#ends_with_comma?" do
190
+ it "should return true if it ends with a ," do
191
+ line = create_file_line " def some_method(thing,", __LINE__
192
+ line.ends_with_comma?.should be_true
193
+ end
194
+
195
+ it "should return true if it ends with a , and spaces" do
196
+ line = create_file_line " def some_method(thing, ", __LINE__
197
+ line.ends_with_comma?.should be_true
198
+ end
199
+
200
+ it "should return true if it ends with a , and tabs" do
201
+ line = create_file_line " def some_method(thing,\t", __LINE__
202
+ line.ends_with_comma?.should be_true
203
+ end
204
+
205
+ it "should return false if it doesn't end with a ," do
206
+ line = create_file_line " def some_method(thing)", __LINE__
207
+ line.ends_with_comma?.should be_false
208
+ end
209
+
210
+ it "should return false if it has a , but doesn't end with one" do
211
+ line = create_file_line " def some_method(thing, other)", __LINE__
212
+ line.ends_with_comma?.should be_false
213
+ end
214
+ end
215
+
216
+ context "#ends_with_backslash?" do
217
+ it "should return true if it ends with a \\" do
218
+ line = create_file_line " def some_method(thing,\\", __LINE__
219
+ line.ends_with_backslash?.should be_true
220
+ end
221
+
222
+ it "should return true if it ends with a \\ and spaces" do
223
+ line = create_file_line " def some_method(thing,\\ ", __LINE__
224
+ line.ends_with_backslash?.should be_true
225
+ end
226
+
227
+ it "should return true if it ends with a \\ and tabs" do
228
+ line = create_file_line " def some_method(thing,\\\t", __LINE__
229
+ line.ends_with_backslash?.should be_true
230
+ end
231
+
232
+ it "should return false if it doesn't end with a \\" do
233
+ line = create_file_line " def some_method(thing)", __LINE__
234
+ line.ends_with_backslash?.should be_false
235
+ end
236
+ end
237
+
238
+ context "#unclosed_parenthesis?" do
239
+ it "should return true if it has a ( but no )" do
240
+ line = create_file_line " def some_method(thing,", __LINE__
241
+ line.unclosed_parenthesis?.should be_true
242
+ end
243
+
244
+ it "should return true if it has a ( but no ) and spaces" do
245
+ line = create_file_line " def some_method(thing, ", __LINE__
246
+ line.unclosed_parenthesis?.should be_true
247
+ end
248
+
249
+ it "should return true if it has a ( but no ) and tabs" do
250
+ line = create_file_line " def some_method(thing,\t\t", __LINE__
251
+ line.unclosed_parenthesis?.should be_true
252
+ end
253
+
254
+ it "should return false if it has a ( and a )" do
255
+ line = create_file_line " def some_method(thing)", __LINE__
256
+ line.unclosed_parenthesis?.should be_false
257
+ end
258
+ end
259
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+ require 'tailor/file_line'
3
+
4
+ include Tailor
5
+
6
+ describe Tailor::FileLine, "spacing around colons" do
7
+ context "in ternary statments" do
8
+ it "should be OK with 1 space around colon" do
9
+ line = create_file_line " bobo = true ? true : false", __LINE__
10
+ line.spacing_problems.should == 0
11
+ end
12
+
13
+ it "should detect 0 space after colon" do
14
+ line = create_file_line " bobo = true ? true :false", __LINE__
15
+ line.spacing_problems.should == 1
16
+ end
17
+
18
+ it "should detect 0 space before colon" do
19
+ line = create_file_line " bobo = true ? true: false", __LINE__
20
+ line.spacing_problems.should == 1
21
+ end
22
+
23
+ it "should detect 0 space before and after colon" do
24
+ line = create_file_line " bobo = true ? true:false", __LINE__
25
+ line.spacing_problems.should == 1
26
+ end
27
+
28
+ it "should detect 2 spaces after colon" do
29
+ line = create_file_line " bobo = true ? true : false", __LINE__
30
+ line.spacing_problems.should == 1
31
+ end
32
+
33
+ it "should detect 2 spaces before colon" do
34
+ line = create_file_line " bobo = true ? true : false", __LINE__
35
+ line.spacing_problems.should == 1
36
+ end
37
+
38
+ it "should detect 2 spaces before and after colon" do
39
+ line = create_file_line " bobo = true ? true : false", __LINE__
40
+ line.spacing_problems.should == 1
41
+ end
42
+ end
43
+
44
+ context "in symbols" do
45
+ it "should be OK with 1 space before" do
46
+ line = create_file_line " bobo = { :thing => :clown }", __LINE__
47
+ line.spacing_problems.should == 0
48
+ end
49
+
50
+ it "should be OK when Hash key, method with ? and symbol" do
51
+ line = create_file_line " bobo[:thing].eql? :clown", __LINE__
52
+ line.spacing_problems.should == 0
53
+ end
54
+ end
55
+
56
+ it "should be OK in namespace operators" do
57
+ line = create_file_line "bobo[:thing] == :dog ? bobo[:thing] : Class::String",
58
+ __LINE__
59
+ line.spacing_problems.should == 0
60
+ end
61
+
62
+ it "should be OK in Regexp classes" do
63
+ line = create_file_line "bobo[:thing].scan(/[:alpha:]/)", __LINE__
64
+ line.spacing_problems.should == 0
65
+ end
66
+
67
+ it "should be OK in setting the global load path" do
68
+ line = create_file_line "$:.unshift File.dirname(__FILE__)", __LINE__
69
+ line.spacing_problems.should == 0
70
+ end
71
+ end
@@ -0,0 +1,159 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+ require 'tailor/file_line'
3
+
4
+ include Tailor
5
+
6
+ describe Tailor::FileLine, "spacing around commas" do
7
+ it "should be OK when followed by a \\ to signify line-continue" do
8
+ line = create_file_line "string = 'One, two, three,'\\", __LINE__
9
+ line.spacing_problems.should == 0
10
+ end
11
+
12
+ context "in a method line" do
13
+ it "should be OK when no commas" do
14
+ line = create_file_line " def do_something this", __LINE__
15
+ line.spacing_problems.should == 0
16
+ end
17
+
18
+ it "should be OK when 0 spaces before and 1 space after a comma" do
19
+ line = create_file_line " def do_something this, that", __LINE__
20
+ line.spacing_problems.should == 0
21
+ end
22
+
23
+ it "should detect 2 spaces after a comma" do
24
+ line = create_file_line " def do_something this, that", __LINE__
25
+ line.spacing_problems.should == 1
26
+ end
27
+
28
+ it "should detect 0 spaces after a comma" do
29
+ line = create_file_line " def do_something this,that", __LINE__
30
+ line.spacing_problems.should == 1
31
+ end
32
+
33
+ it "should detect 1 space before a comma" do
34
+ line = create_file_line " def do_something this , that", __LINE__
35
+ line.spacing_problems.should == 1
36
+ end
37
+
38
+ it "should detect 1 space before a comma and 0 spaces after" do
39
+ line = create_file_line " def do_something this ,that", __LINE__
40
+ line.spacing_problems.should == 2
41
+ end
42
+ end
43
+
44
+ context "in a comment line" do
45
+ it "should be OK when no commas" do
46
+ line = create_file_line " # Comment line", __LINE__
47
+ line.spacing_problems.should == 0
48
+ end
49
+
50
+ it "should be OK when 1 space after a comma" do
51
+ line = create_file_line " # Comment line, and stuff", __LINE__
52
+ line.spacing_problems.should == 0
53
+ end
54
+
55
+ it "should detect 2 spaces after a comma" do
56
+ line = create_file_line " # Comment line, and stuff", __LINE__
57
+ line.spacing_problems.should == 1
58
+ end
59
+
60
+ it "should detect 0 spaces after a comma" do
61
+ line = create_file_line " # Comment line,and stuff", __LINE__
62
+ line.spacing_problems.should == 1
63
+ end
64
+
65
+ it "should detect 1 space before a comma" do
66
+ line = create_file_line " # Comment line , and stuff", __LINE__
67
+ line.spacing_problems.should == 1
68
+ end
69
+
70
+ it "should detect 1 space before a comma and 0 spaces after" do
71
+ line = create_file_line " # Comment line ,and stuff", __LINE__
72
+ line.spacing_problems.should == 2
73
+ end
74
+
75
+ it "should be OK when 0 spaces after a comma, but end of the line" do
76
+ line = create_file_line " # This is a comment that,\n", __LINE__
77
+ line.spacing_problems.should == 0
78
+ end
79
+
80
+ it "should detect 2 spaces after a comma when at the end of a line" do
81
+ line = create_file_line " # This is a comment that, \n", __LINE__
82
+ line.spacing_problems.should == 1 # 1 for whitespace
83
+ end
84
+
85
+ it "should detect 2 spaces after a comma" do
86
+ line = create_file_line " # This is a comment that, meows", __LINE__
87
+ line.spacing_problems.should == 1
88
+ end
89
+ end
90
+
91
+ context "in a statement with an Array" do
92
+ it "should be OK when no commas" do
93
+ line = create_file_line " bobo = ['hi']", __LINE__
94
+ line.spacing_problems.should == 0
95
+ end
96
+
97
+ it "should be OK when 1 space after a comma" do
98
+ line = create_file_line " bobo = ['hi', 'meow']", __LINE__
99
+ line.spacing_problems.should == 0
100
+ end
101
+
102
+ it "should detect 2 spaces after a comma" do
103
+ line = create_file_line " bobo = ['hi', 'meow']", __LINE__
104
+ line.spacing_problems.should == 1
105
+ end
106
+
107
+ it "should detect 0 spaces after a comma" do
108
+ line = create_file_line " bobo = ['hi','meow']", __LINE__
109
+ line.spacing_problems.should == 1
110
+ end
111
+
112
+ it "should detect 1 space before a comma" do
113
+ line = create_file_line " bobo = ['hi' , 'meow']", __LINE__
114
+ line.spacing_problems.should == 1
115
+ end
116
+
117
+ it "should detect 1 space before a comma and 0 spaces after" do
118
+ line = create_file_line " bobo = ['hi' ,'meow']", __LINE__
119
+ line.spacing_problems.should == 2
120
+ end
121
+ end
122
+
123
+ context "in a statement with a Hash" do
124
+ it "should be OK when no commas" do
125
+ line = create_file_line "bobo = { :hi => 'meow' }", __LINE__
126
+ line.spacing_problems.should == 0
127
+ end
128
+
129
+ it "should be OK when 1 space after a comma" do
130
+ line = create_file_line "bobo = { :hi => 'meow', :bye => 'meow' }",
131
+ __LINE__
132
+ line.spacing_problems.should == 0
133
+ end
134
+
135
+ it "should detect 2 spaces after a comma" do
136
+ line = create_file_line "bobo = { :hi => 'meow', :bye => 'meow' }",
137
+ __LINE__
138
+ line.spacing_problems.should == 1
139
+ end
140
+
141
+ it "should detect 0 spaces after a comma" do
142
+ line = create_file_line "bobo = { :hi => 'meow',:bye => 'meow' }",
143
+ __LINE__
144
+ line.spacing_problems.should == 1
145
+ end
146
+
147
+ it "should detect 1 space before a comma" do
148
+ line = create_file_line "bobo = { :hi => 'meow' , :bye => 'meow' }",
149
+ __LINE__
150
+ line.spacing_problems.should == 1
151
+ end
152
+
153
+ it "should detect 1 space before a comma and 0 spaces after" do
154
+ line = create_file_line "bobo = { :hi => 'meow' ,:bye => 'meow' }",
155
+ __LINE__
156
+ line.spacing_problems.should == 2
157
+ end
158
+ end
159
+ end