cuke_sniffer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,116 +1,143 @@
1
- require 'cuke_sniffer/constants'
2
- require 'cuke_sniffer/rule_config'
3
-
4
- module CukeSniffer
5
- class StepDefinition < RulesEvaluator
6
- include CukeSniffer::Constants
7
- include CukeSniffer::RuleConfig
8
-
9
- attr_accessor :start_line, :regex, :code, :parameters, :calls, :nested_steps
10
-
11
- def initialize(location, raw_code)
12
- super(location)
13
-
14
- @parameters = []
15
- @calls = {}
16
- @nested_steps = {}
17
- @start_line = location.match(/:(?<line>\d*)/)[:line].to_i
18
-
19
- end_match_index = (raw_code.size - 1) - raw_code.reverse.index("end")
20
- @code = raw_code[1...end_match_index]
21
-
22
- raw_code.each do |line|
23
- if line =~ STEP_DEFINITION_REGEX
24
- matches = STEP_DEFINITION_REGEX.match(line)
25
- @regex = Regexp.new(matches[:step])
26
- @parameters = matches[:parameters].split(",") unless matches[:parameters].nil?
27
- end
28
- end
29
-
30
- detect_nested_steps
31
- evaluate_score
32
- end
33
-
34
- def add_call(location, step_string)
35
- @calls[location] = step_string
36
- end
37
-
38
- def detect_nested_steps
39
- multi_line_step_flag = false
40
- counter = 1
41
- @code.each do |line|
42
- regex = nil
43
- case line
44
- when SIMPLE_NESTED_STEP_REGEX
45
- regex = SIMPLE_NESTED_STEP_REGEX
46
- when SAME_LINE_COMPLEX_STEP_REGEX
47
- regex = SAME_LINE_COMPLEX_STEP_REGEX
48
- when START_COMPLEX_WITH_STEP_REGEX
49
- multi_line_step_flag = true
50
- regex = START_COMPLEX_WITH_STEP_REGEX
51
- when START_COMPLEX_STEP_REGEX
52
- multi_line_step_flag = true
53
- when END_COMPLEX_WITH_STEP_REGEX
54
- regex = END_COMPLEX_WITH_STEP_REGEX
55
- multi_line_step_flag = false
56
- when STEP_REGEX
57
- regex = STEP_REGEX if multi_line_step_flag
58
- when END_COMPLEX_STEP_REGEX
59
- multi_line_step_flag = false
60
- else
61
- end
62
-
63
- if regex
64
- match = regex.match(line)
65
- nested_step_line = (@start_line + counter)
66
- @nested_steps[location.gsub(/:\d*/, ":" + nested_step_line.to_s)] = match[:step_string]
67
- end
68
- counter += 1
69
- end
70
- end
71
-
72
- def ==(comparison_object)
73
- super(comparison_object)
74
- comparison_object.regex == regex
75
- comparison_object.code == code
76
- comparison_object.parameters == parameters
77
- comparison_object.calls == calls
78
- comparison_object.nested_steps == nested_steps
79
- end
80
-
81
- def evaluate_score
82
- super
83
- rule_no_code
84
- rule_too_many_parameters
85
- rule_nested_steps
86
- rule_recursive_nested_step
87
- rule_commented_code
88
- end
89
-
90
- def rule_no_code
91
- store_rule(STEP_DEFINITION_RULES[:no_code]) if code.empty?
92
- end
93
-
94
- def rule_too_many_parameters
95
- rule = STEP_DEFINITION_RULES[:too_many_parameters]
96
- store_rule(rule) if parameters.size >= rule[:max]
97
- end
98
-
99
- def rule_nested_steps
100
- store_rule(STEP_DEFINITION_RULES[:nested_step]) unless nested_steps.empty?
101
- end
102
-
103
- def rule_recursive_nested_step
104
- nested_steps.each_value do |nested_step|
105
- store_rule(STEP_DEFINITION_RULES[:recursive_nested_step]) if nested_step =~ regex
106
- end
107
- end
108
-
109
- def rule_commented_code
110
- code.each do |line|
111
- store_rule(STEP_DEFINITION_RULES[:commented_code]) if is_comment?(line)
112
- end
113
- end
114
-
115
- end
116
- end
1
+ module CukeSniffer
2
+ class StepDefinition < RulesEvaluator
3
+ include CukeSniffer::Constants
4
+ include CukeSniffer::RuleConfig
5
+
6
+ SIMPLE_NESTED_STEP_REGEX = /steps\s"#{STEP_STYLES}(?<step_string>.*)"$/
7
+ SAME_LINE_COMPLEX_STEP_REGEX = /^steps\s%Q?{#{STEP_STYLES}(?<step_string>.*)}$/
8
+ START_COMPLEX_STEP_REGEX = /^steps\s%Q?\{\s*/
9
+ END_COMPLEX_STEP_REGEX = /}$/
10
+ START_COMPLEX_WITH_STEP_REGEX = /steps\s%Q?\{#{STEP_STYLES}(?<step_string>.*)$/
11
+ END_COMPLEX_WITH_STEP_REGEX = /#{STEP_STYLES}(?<step_string>.*)}$/
12
+ attr_accessor :start_line, :regex, :code, :parameters, :calls, :nested_steps
13
+
14
+ def initialize(location, raw_code)
15
+ super(location)
16
+
17
+ @parameters = []
18
+ @calls = {}
19
+ @nested_steps = {}
20
+ @start_line = location.match(/:(?<line>\d*)/)[:line].to_i
21
+
22
+ end_match_index = (raw_code.size - 1) - raw_code.reverse.index("end")
23
+ @code = raw_code[1...end_match_index]
24
+
25
+ raw_code.each do |line|
26
+ if line =~ STEP_DEFINITION_REGEX
27
+ matches = STEP_DEFINITION_REGEX.match(line)
28
+ @regex = Regexp.new(matches[:step])
29
+ @parameters = matches[:parameters].split(",") unless matches[:parameters].nil?
30
+ end
31
+ end
32
+
33
+ detect_nested_steps
34
+ evaluate_score
35
+ end
36
+
37
+ def add_call(location, step_string)
38
+ @calls[location] = step_string
39
+ end
40
+
41
+ def detect_nested_steps
42
+ multi_line_step_flag = false
43
+ counter = 1
44
+ @code.each do |line|
45
+ regex = nil
46
+ case line
47
+ when SIMPLE_NESTED_STEP_REGEX
48
+ regex = SIMPLE_NESTED_STEP_REGEX
49
+ when SAME_LINE_COMPLEX_STEP_REGEX
50
+ regex = SAME_LINE_COMPLEX_STEP_REGEX
51
+ when START_COMPLEX_WITH_STEP_REGEX
52
+ multi_line_step_flag = true
53
+ regex = START_COMPLEX_WITH_STEP_REGEX
54
+ when START_COMPLEX_STEP_REGEX
55
+ multi_line_step_flag = true
56
+ when END_COMPLEX_WITH_STEP_REGEX
57
+ if line =~ /[#]{.*}$/ && multi_line_step_flag
58
+ regex = STEP_REGEX
59
+ else
60
+ regex = END_COMPLEX_WITH_STEP_REGEX
61
+ multi_line_step_flag = false
62
+ end
63
+ when STEP_REGEX
64
+ regex = STEP_REGEX if multi_line_step_flag
65
+ when END_COMPLEX_STEP_REGEX
66
+ multi_line_step_flag = false
67
+ else
68
+ end
69
+
70
+ if regex
71
+ index = 0
72
+ while line.include?('#{') and index <= line.length
73
+ index = line.index('#{')
74
+ replace_string = ""
75
+ while index <= line.length and line[index - 1] != "}"
76
+ replace_string << line[index]
77
+ index += 1
78
+ end
79
+ line.gsub!(replace_string, "variable")
80
+ end
81
+
82
+ match = regex.match(line)
83
+ nested_step_line = (@start_line + counter)
84
+ @nested_steps[location.gsub(/:\d*/, ":" + nested_step_line.to_s)] = match[:step_string]
85
+ end
86
+ counter += 1
87
+ end
88
+ end
89
+
90
+ def condensed_call_list
91
+ condensed_list = {}
92
+ @calls.each{|call, step_string|
93
+ condensed_list[step_string] ||= []
94
+ condensed_list[step_string] << call
95
+ }
96
+ condensed_list
97
+ end
98
+
99
+ def ==(comparison_object)
100
+ super(comparison_object)
101
+ comparison_object.regex == regex
102
+ comparison_object.code == code
103
+ comparison_object.parameters == parameters
104
+ comparison_object.calls == calls
105
+ comparison_object.nested_steps == nested_steps
106
+ end
107
+
108
+ def evaluate_score
109
+ super
110
+ rule_no_code
111
+ rule_too_many_parameters
112
+ rule_nested_steps
113
+ rule_recursive_nested_step
114
+ rule_commented_code
115
+ end
116
+
117
+ def rule_no_code
118
+ store_rule(STEP_DEFINITION_RULES[:no_code]) if code.empty?
119
+ end
120
+
121
+ def rule_too_many_parameters
122
+ rule = STEP_DEFINITION_RULES[:too_many_parameters]
123
+ store_rule(rule) if parameters.size >= rule[:max]
124
+ end
125
+
126
+ def rule_nested_steps
127
+ store_rule(STEP_DEFINITION_RULES[:nested_step]) unless nested_steps.empty?
128
+ end
129
+
130
+ def rule_recursive_nested_step
131
+ nested_steps.each_value do |nested_step|
132
+ store_rule(STEP_DEFINITION_RULES[:recursive_nested_step]) if nested_step =~ regex
133
+ end
134
+ end
135
+
136
+ def rule_commented_code
137
+ code.each do |line|
138
+ store_rule(STEP_DEFINITION_RULES[:commented_code]) if is_comment?(line)
139
+ end
140
+ end
141
+
142
+ end
143
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuke_sniffer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,10 +11,10 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-15 00:00:00.000000000 Z
14
+ date: 2013-02-15 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: A ruby library used to root out smells in your cukes.
17
- email:
17
+ email: ''
18
18
  executables:
19
19
  - cuke_sniffer.rb
20
20
  extensions: []
@@ -43,15 +43,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ segments:
47
+ - 0
48
+ hash: -320967053
46
49
  required_rubygems_version: !ruby/object:Gem::Requirement
47
50
  none: false
48
51
  requirements:
49
52
  - - ! '>='
50
53
  - !ruby/object:Gem::Version
51
54
  version: '0'
55
+ segments:
56
+ - 0
57
+ hash: -320967053
52
58
  requirements: []
53
59
  rubyforge_project:
54
- rubygems_version: 1.8.23
60
+ rubygems_version: 1.8.24
55
61
  signing_key:
56
62
  specification_version: 3
57
63
  summary: CukeSniffer