mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Return source
from expect_issue
This commit is contained in:
parent
470e41cb7b
commit
c2aa2fedb6
3 changed files with 15 additions and 22 deletions
|
@ -13,18 +13,18 @@ module Ameba::Rule::Layout
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there is no blank lines at the end" do
|
it "fails if there is no blank lines at the end" do
|
||||||
expect_issue subject, "no-blankline # error: Trailing newline missing"
|
s = expect_issue subject, "no-blankline # error: Trailing newline missing"
|
||||||
expect_correction "no-blankline\n"
|
expect_correction s, "no-blankline\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if there more then one blank line at the end of a source" do
|
it "fails if there more then one blank line at the end of a source" do
|
||||||
expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", normalize: false
|
s = expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", normalize: false
|
||||||
expect_no_corrections
|
expect_no_corrections s
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if last line is not blank" do
|
it "fails if last line is not blank" do
|
||||||
expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing", normalize: false
|
s = expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing", normalize: false
|
||||||
expect_correction "\n\n\n puts 22\n"
|
expect_correction s, "\n\n\n puts 22\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when unnecessary blank line has been detected" do
|
context "when unnecessary blank line has been detected" do
|
||||||
|
|
|
@ -7,12 +7,12 @@ module Ameba
|
||||||
it "transforms large number #{number}" do
|
it "transforms large number #{number}" do
|
||||||
rule = Rule::Style::LargeNumbers.new
|
rule = Rule::Style::LargeNumbers.new
|
||||||
|
|
||||||
expect_issue rule, <<-CRYSTAL, number: number
|
s = expect_issue rule, <<-CRYSTAL, number: number
|
||||||
number = %{number}
|
number = %{number}
|
||||||
# ^{number} error: Large numbers should be written with underscores: #{expected}
|
# ^{number} error: Large numbers should be written with underscores: #{expected}
|
||||||
CRYSTAL
|
CRYSTAL
|
||||||
|
|
||||||
expect_correction <<-CRYSTAL
|
expect_correction s, <<-CRYSTAL
|
||||||
number = #{expected}
|
number = #{expected}
|
||||||
CRYSTAL
|
CRYSTAL
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,8 +47,6 @@ require "./util"
|
||||||
module Ameba::Spec::ExpectIssue
|
module Ameba::Spec::ExpectIssue
|
||||||
include Spec::Util
|
include Spec::Util
|
||||||
|
|
||||||
class_property source : Source?
|
|
||||||
|
|
||||||
def expect_issue(rules : Rule::Base | Enumerable(Rule::Base),
|
def expect_issue(rules : Rule::Base | Enumerable(Rule::Base),
|
||||||
annotated_code : String,
|
annotated_code : String,
|
||||||
path = "",
|
path = "",
|
||||||
|
@ -67,7 +65,7 @@ module Ameba::Spec::ExpectIssue
|
||||||
raise "Use `report_no_issues` to assert that no issues are found"
|
raise "Use `report_no_issues` to assert that no issues are found"
|
||||||
end
|
end
|
||||||
|
|
||||||
actual_annotations = actual_annotations(rules, code, path, lines)
|
source, actual_annotations = actual_annotations(rules, code, path, lines)
|
||||||
unless actual_annotations == expected_annotations
|
unless actual_annotations == expected_annotations
|
||||||
fail <<-MSG, file, line
|
fail <<-MSG, file, line
|
||||||
Expected:
|
Expected:
|
||||||
|
@ -79,12 +77,11 @@ module Ameba::Spec::ExpectIssue
|
||||||
#{actual_annotations}
|
#{actual_annotations}
|
||||||
MSG
|
MSG
|
||||||
end
|
end
|
||||||
|
|
||||||
|
source
|
||||||
end
|
end
|
||||||
|
|
||||||
def expect_correction(correction, *, file = __FILE__, line = __LINE__)
|
def expect_correction(source, correction, *, file = __FILE__, line = __LINE__)
|
||||||
source = ExpectIssue.source
|
|
||||||
raise "`expect_correction` must follow `expect_issue`" unless source
|
|
||||||
|
|
||||||
raise "Use `expect_no_corrections` if the code will not change" unless source.correct
|
raise "Use `expect_no_corrections` if the code will not change" unless source.correct
|
||||||
return if correction == source.code
|
return if correction == source.code
|
||||||
|
|
||||||
|
@ -99,10 +96,7 @@ module Ameba::Spec::ExpectIssue
|
||||||
MSG
|
MSG
|
||||||
end
|
end
|
||||||
|
|
||||||
def expect_no_corrections(*, file = __FILE__, line = __LINE__)
|
def expect_no_corrections(source, *, file = __FILE__, line = __LINE__)
|
||||||
source = ExpectIssue.source
|
|
||||||
raise "`expect_no_corrections` must follow `expect_offense`" unless source
|
|
||||||
|
|
||||||
return unless source.correct
|
return unless source.correct
|
||||||
|
|
||||||
fail <<-MSG, file, line
|
fail <<-MSG, file, line
|
||||||
|
@ -121,7 +115,7 @@ module Ameba::Spec::ExpectIssue
|
||||||
line = __LINE__)
|
line = __LINE__)
|
||||||
code = normalize_code(code) if normalize
|
code = normalize_code(code) if normalize
|
||||||
lines = code.split('\n') # must preserve trailing newline
|
lines = code.split('\n') # must preserve trailing newline
|
||||||
actual_annotations = actual_annotations(rules, code, path, lines)
|
_, actual_annotations = actual_annotations(rules, code, path, lines)
|
||||||
unless actual_annotations.to_s == code
|
unless actual_annotations.to_s == code
|
||||||
fail <<-MSG, file, line
|
fail <<-MSG, file, line
|
||||||
Expected no issues, but got:
|
Expected no issues, but got:
|
||||||
|
@ -133,13 +127,12 @@ module Ameba::Spec::ExpectIssue
|
||||||
|
|
||||||
private def actual_annotations(rules, code, path, lines)
|
private def actual_annotations(rules, code, path, lines)
|
||||||
source = Source.new(code, path, normalize: false) # already normalized
|
source = Source.new(code, path, normalize: false) # already normalized
|
||||||
ExpectIssue.source = source
|
|
||||||
if rules.is_a?(Enumerable)
|
if rules.is_a?(Enumerable)
|
||||||
rules.each(&.catch(source))
|
rules.each(&.catch(source))
|
||||||
else
|
else
|
||||||
rules.catch(source)
|
rules.catch(source)
|
||||||
end
|
end
|
||||||
AnnotatedSource.new(lines, source.issues)
|
{source, AnnotatedSource.new(lines, source.issues)}
|
||||||
end
|
end
|
||||||
|
|
||||||
private def format_issue(code, **replacements)
|
private def format_issue(code, **replacements)
|
||||||
|
|
Loading…
Reference in a new issue