Normalize sources for tests

This commit is contained in:
Vitalii Elenhaupt 2018-09-07 15:07:03 +03:00
parent cb5f802012
commit d7b0e10d1e
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
27 changed files with 89 additions and 65 deletions

View file

@ -7,7 +7,7 @@ module Ameba
# ameba:disable #{NamedRule.name}
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "Error!")
s.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
s.should be_valid
end
@ -15,7 +15,7 @@ module Ameba
s = Source.new %Q(
Time.epoch(1483859302) # ameba:disable #{NamedRule.name}
)
s.add_issue(NamedRule.new, location: {2, 12}, message: "Error!")
s.add_issue(NamedRule.new, location: {1, 12}, message: "Error!")
s.should be_valid
end
@ -24,7 +24,7 @@ module Ameba
# ameba:disable WrongName
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "Error!")
s.add_issue(NamedRule.new, location: {2, 12}, message: "Error!")
s.should_not be_valid
end
@ -33,7 +33,7 @@ module Ameba
# ameba:disable SomeRule LargeNumbers #{NamedRule.name} SomeOtherRule
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should be_valid
end
@ -42,7 +42,7 @@ module Ameba
# ameba:disable SomeRule, LargeNumbers, #{NamedRule.name}, SomeOtherRule
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should be_valid
end
@ -51,7 +51,7 @@ module Ameba
# ameba:disable SomeRule, SomeOtherRule LargeNumbers
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.add_issue(NamedRule.new, location: {2, 12}, message: "")
s.should_not be_valid
end
@ -61,7 +61,7 @@ module Ameba
#
Time.epoch(1483859302)
)
s.add_issue(NamedRule.new, location: {4, 12}, message: "")
s.add_issue(NamedRule.new, location: {3, 12}, message: "")
s.should_not be_valid
end
@ -106,7 +106,7 @@ module Ameba
s = Source.new %Q(
a = 1 # ameba:disable #{DummyRule.rule_name}
)
s.add_issue(DummyRule.new, location: {2, 12}, message: "")
s.add_issue(DummyRule.new, location: {1, 12}, message: "")
s.should be_valid
end
@ -122,7 +122,7 @@ module Ameba
s = Source.new %Q(
a = 1 # ameba:disable #{DummyRule.group_name}
)
s.add_issue(DummyRule.new, location: {2, 12}, message: "")
s.add_issue(DummyRule.new, location: {1, 12}, message: "")
s.should be_valid
end

View file

@ -10,7 +10,7 @@ module Ameba::Rule::Layout
end
it "fails if there is a blank line at the end of a source" do
source = Source.new "a = 1\n \n "
source = Source.new "a = 1\n \n ", normalize: false
subject.catch(source).should_not be_valid
end
@ -20,12 +20,12 @@ module Ameba::Rule::Layout
end
it "passes if last line is not blank" do
source = Source.new "\n\n\n puts 22"
source = Source.new "\n\n\n puts 22", normalize: false
subject.catch(source).should be_valid
end
it "reports rule, pos and message" do
source = Source.new "a = 1\n\n ", "source.cr"
source = Source.new "a = 1\n\n ", "source.cr", normalize: false
subject.catch(source).should_not be_valid
issue = source.issues.first

View file

@ -61,7 +61,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:11"
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq "Empty `ensure` block detected"
end
end

View file

@ -103,7 +103,7 @@ module Ameba
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:12"
issue.location.to_s.should eq "source.cr:1:4"
issue.message.should eq "Avoid empty expressions"
end
end

View file

@ -34,7 +34,7 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:13"
issue.location.to_s.should eq "source.cr:1:5"
issue.message.should eq %(Duplicated keys in hash literal: "a")
end

View file

@ -65,7 +65,7 @@ module Ameba::Rule::Lint
s.issues.size.should eq 1
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Literal value found in conditional"
end
end

View file

@ -46,7 +46,7 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Symbols `,:` may be unwanted in %i array literals"
)
@ -60,7 +60,7 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Symbols `,\"` may be unwanted in %w array literals"
)

View file

@ -158,7 +158,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:11"
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq "Argument `bar` is assigned before it is used"
end
end

View file

@ -166,7 +166,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:11"
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq(
"Exception handler has shadowed exceptions: IndexError"
)

View file

@ -160,7 +160,7 @@ module Ameba::Rule::Lint
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:20"
issue.location.to_s.should eq "source.cr:2:12"
issue.message.should eq "Shadowing outer local variable `foo`"
end
end

View file

@ -23,7 +23,7 @@ module Ameba::Rule::Lint
# ameba:disable #{NamedRule.name}
a = 1
)
s.add_issue NamedRule.new, location: {3, 9},
s.add_issue NamedRule.new, location: {2, 1},
message: "Useless assignment", status: :disabled
subject.catch(s).should be_valid
end
@ -32,7 +32,7 @@ module Ameba::Rule::Lint
s = Source.new %Q(
a = 1 # ameba:disable #{NamedRule.name}
)
s.add_issue NamedRule.new, location: {2, 1},
s.add_issue NamedRule.new, location: {1, 1},
message: "Alarm!", status: :disabled
subject.catch(s).should be_valid
end
@ -95,7 +95,7 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Unnecessary disabling of Rule1, Rule2"
end
end

View file

@ -178,7 +178,7 @@ module Ameba::Rule::Lint
issue.rule.should_not be_nil
issue.message.should eq "Unused argument `a`. If it's necessary, use `_a` " \
"as an argument name to indicate that it won't be used."
issue.location.to_s.should eq "source.cr:2:22"
issue.location.to_s.should eq "source.cr:1:12"
end
end

View file

@ -77,7 +77,7 @@ module Ameba::Rule::Lint
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:11"
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq "Useless assignment to variable `a`"
end
@ -137,7 +137,7 @@ module Ameba::Rule::Lint
end
)
subject.catch(s).should_not be_valid
s.issues.first.location.to_s.should eq ":3:11"
s.issues.first.location.to_s.should eq ":2:3"
end
it "reports if variable reassigned and not used" do
@ -316,7 +316,7 @@ module Ameba::Rule::Lint
issue = s.issues.last
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:5:13"
issue.location.to_s.should eq "source.cr:4:3"
issue.message.should eq "Useless assignment to variable `a`"
end
end
@ -341,7 +341,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.location.to_s.should eq ":3:16"
issue.location.to_s.should eq ":2:6"
issue.message.should eq "Useless assignment to variable `b`"
end
@ -364,11 +364,11 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.location.to_s.should eq ":3:13"
issue.location.to_s.should eq ":2:3"
issue.message.should eq "Useless assignment to variable `a`"
issue = s.issues.last
issue.location.to_s.should eq ":3:16"
issue.location.to_s.should eq ":2:6"
issue.message.should eq "Useless assignment to variable `b`"
end
end
@ -381,8 +381,8 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 2
s.issues.first.location.to_s.should eq ":2:11"
s.issues.last.location.to_s.should eq ":3:11"
s.issues.first.location.to_s.should eq ":1:1"
s.issues.last.location.to_s.should eq ":2:1"
end
it "doesn't report if assignments are referenced" do
@ -445,7 +445,7 @@ module Ameba::Rule::Lint
s.issues.size.should eq 1
issue = s.issues.first
issue.location.to_s.should eq "source.cr:2:11"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Useless assignment to variable `foo`"
end
end
@ -517,7 +517,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":5:17"
s.issues.first.location.to_s.should eq ":4:5"
end
it "does not report of assignments are referenced in all branches" do
@ -586,7 +586,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":5:17"
s.issues.first.location.to_s.should eq ":4:5"
end
end
@ -619,8 +619,8 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 2
s.issues.first.location.to_s.should eq ":5:17"
s.issues.last.location.to_s.should eq ":7:17"
s.issues.first.location.to_s.should eq ":4:5"
s.issues.last.location.to_s.should eq ":6:5"
end
it "doesn't report if assignment is referenced in cond" do
@ -656,7 +656,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":3:27"
s.issues.first.location.to_s.should eq ":2:15"
end
end
@ -683,7 +683,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":4:17"
s.issues.first.location.to_s.should eq ":3:5"
end
it "does not report if assignment is referenced in a loop" do
@ -778,7 +778,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":4:17"
s.issues.first.location.to_s.should eq ":3:5"
end
end
@ -826,7 +826,7 @@ module Ameba::Rule::Lint
)
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
s.issues.first.location.to_s.should eq ":4:15"
s.issues.first.location.to_s.should eq ":3:3"
end
end
end

View file

@ -38,7 +38,7 @@ module Ameba::Rule::Lint
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:6:23"
issue.location.to_s.should eq "source.cr:5:15"
issue.message.should eq "Useless condition in when detected"
end
end

View file

@ -56,8 +56,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:44"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:36"
issue.message.should eq "Use `any? {...}` instead of `reject {...}.any?`"
end
end

View file

@ -71,8 +71,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:45"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:37"
issue.message.should eq "Use `find {...}` instead of `select {...}.first`"
end
@ -86,8 +86,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:46"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:38"
issue.message.should eq "Use `find {...}` instead of `select {...}.first?`"
end
@ -101,8 +101,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:44"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:36"
issue.message.should eq "Use `reverse_each.find {...}` instead of `select {...}.last`"
end
@ -116,8 +116,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:19"
issue.end_location.to_s.should eq "source.cr:2:45"
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:37"
issue.message.should eq "Use `reverse_each.find {...}` instead of `select {...}.last?`"
end

View file

@ -57,8 +57,8 @@ module Ameba::Rule::Performance
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:4"
issue.end_location.to_s.should eq "source.cr:2:25"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:25"
issue.message.should eq "Use `count {...}` instead of `reject {...}.size`."
end
end

View file

@ -42,7 +42,7 @@ module Ameba
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Constant name should be screaming-cased: CONST, not Const"
)

View file

@ -112,12 +112,12 @@ module Ameba
it "reports rule, pos and message" do
s = Source.new %q(
1200000
1200000
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:10"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should match /1_200_000/
end

View file

@ -46,7 +46,7 @@ module Ameba
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Method name should be underscore-cased: bad_name, not bad_Name"
)

View file

@ -40,7 +40,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:3:11"
issue.location.to_s.should eq "source.cr:2:3"
issue.message.should eq(
"Favour method name 'picture?' over 'has_picture?'")
end

View file

@ -206,7 +206,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Redundant `begin` block detected"
end
end

View file

@ -51,7 +51,7 @@ module Ameba
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Type name should be camelcased: MyClass, but it was My_class"
)

View file

@ -37,7 +37,7 @@ module Ameba::Rule::Style
issue = s.issues.first
issue.should_not be_nil
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Favour if over unless with else"
end
end

View file

@ -52,7 +52,7 @@ module Ameba
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:9"
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq(
"Var name should be underscore-cased: bad_name, not badName"
)

View file

@ -4,7 +4,7 @@ module Ameba
private def it_tokenizes(str, expected)
it "tokenizes #{str}" do
([] of Symbol).tap do |token_types|
Tokenizer.new(Source.new str)
Tokenizer.new(Source.new str, normalize: false)
.run { |token| token_types << token.type }
.should be_true
end.should eq expected

View file

@ -11,6 +11,30 @@ module Ameba
end
end
class Source
def initialize(code : String, @path = "", normalize = true)
@code = normalize ? normalize_source(code) : code
end
private def normalize_source(code, separator = "\n")
lines = code.split(separator)
# remove unneeded first and last blank lines if any
lines.shift if lines[0].blank? && lines.size > 1
lines.pop if lines[-1].blank? && lines.size > 1
# find the minimum indentation
min_indent = lines.min_of do |line|
line.blank? ? code.size : line.size - line.lstrip.size
end
# remove the width of minimum indentation in each line
lines
.map! { |line| line.blank? ? line : line[min_indent..-1] }
.join(separator)
end
end
struct NamedRule < Rule::Base
properties do
description : String = "A rule with a custom name."