Skip to content

Commit

Permalink
Refactor linters using base linter
Browse files Browse the repository at this point in the history
Updated the existing linters so that they use the base linter class.
  • Loading branch information
enkessler committed May 9, 2019
1 parent b00ffa2 commit 746dd4a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 58 deletions.
20 changes: 7 additions & 13 deletions lib/cuke_linter/linters/example_without_name_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ module CukeLinter

# A linter that detects unnamed example groups

class ExampleWithoutNameLinter
class ExampleWithoutNameLinter < Linter

# Returns the name of the linter
def name
'ExampleWithoutNameLinter'
end
def rule(model)
return false unless model.is_a?(CukeModeler::Example)

# Lints the given model and returns linting data about said model
def lint(model)
return [] unless model.is_a?(CukeModeler::Example)
model.name.nil? || model.name.empty?
end

if model.name.nil? || model.name.empty?
[{ problem: 'Example has no name', location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
else
[]
end
def message
'Example has no name'
end

end
Expand Down
20 changes: 7 additions & 13 deletions lib/cuke_linter/linters/feature_without_scenarios_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ module CukeLinter

# A linter that detects empty features

class FeatureWithoutScenariosLinter
class FeatureWithoutScenariosLinter < Linter

# Returns the name of the linter
def name
'FeatureWithoutScenariosLinter'
end
def rule(model)
return false unless model.is_a?(CukeModeler::Feature)

# Lints the given model and returns linting data about said model
def lint(model)
return [] unless model.is_a?(CukeModeler::Feature)
model.tests.nil? || model.tests.empty?
end

if model.tests.nil? || model.tests.empty?
[{ problem: 'Feature has no scenarios', location: "#{model.parent_model.path}:#{model.source_line}" }]
else
[]
end
def message
'Feature has no scenarios'
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/cuke_linter/linters/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def initialize(name: nil, message: nil, rule: nil)
@rule = rule
end

# Returns the name of the linter
def name
@name
end

# Lints the given model and returns linting data about said model
def lint(model)
raise 'No linting rule provided!' unless @rule || respond_to?(:rule)

Expand All @@ -19,7 +21,7 @@ def lint(model)
if problem_found
problem_message = respond_to?(:message) ? message : @message

[{ problem: problem_message, location: "#{model.parent_model.path}:#{model.source_line}" }]
[{ problem: problem_message, location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
else
[]
end
Expand Down
24 changes: 9 additions & 15 deletions lib/cuke_linter/linters/outline_with_single_example_row_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ module CukeLinter

# A linter that detects outlines that don't have multiple example rows

class OutlineWithSingleExampleRowLinter
class OutlineWithSingleExampleRowLinter < Linter

# Returns the name of the linter
def name
'OutlineWithSingleExampleRowLinter'
end

# Lints the given model and returns linting data about said model
def lint(model)
return [] unless model.is_a?(CukeModeler::Outline)
return [] if model.examples.nil?
def rule(model)
return false unless model.is_a?(CukeModeler::Outline)
return false if model.examples.nil?

examples_rows = model.examples.collect(&:argument_rows).flatten

if examples_rows.count == 1
[{ problem: 'Outline has only one example row', location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
else
[]
end
examples_rows.count == 1
end

def message
'Outline has only one example row'
end

end
Expand Down
26 changes: 10 additions & 16 deletions lib/cuke_linter/linters/test_with_too_many_steps_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,24 @@ module CukeLinter

# A linter that detects scenarios and outlines that have too many steps

class TestWithTooManyStepsLinter

# Returns the name of the linter
def name
'TestWithTooManyStepsLinter'
end
class TestWithTooManyStepsLinter < Linter

# Changes the linting settings on the linter using the provided configuration
def configure(options)
@step_threshold = options['StepThreshold'] if options['StepThreshold']
end

# Lints the given model and returns linting data about said model
def lint(model)
return [] unless model.is_a?(CukeModeler::Scenario) || model.is_a?(CukeModeler::Outline)
def rule(model)
return false unless model.is_a?(CukeModeler::Scenario) || model.is_a?(CukeModeler::Outline)

step_count = model.steps.nil? ? 0 : model.steps.count
step_threshold = @step_threshold || 10
@linted_step_count = model.steps.nil? ? 0 : model.steps.count
@linted_step_threshold = @step_threshold || 10

@linted_step_count > @linted_step_threshold
end

if step_count > step_threshold
[{ problem: "Test has too many steps. #{step_count} steps found (max #{step_threshold})", location: "#{model.get_ancestor(:feature_file).path}:#{model.source_line}" }]
else
[]
end
def message
"Test has too many steps. #{@linted_step_count} steps found (max #{@linted_step_threshold})"
end

end
Expand Down

0 comments on commit 746dd4a

Please sign in to comment.