Add expect_correction and expect_no_corrections

This commit is contained in:
fn ⌃ ⌥ 2021-10-24 11:56:46 -07:00
parent c1b4add094
commit d3d3ccd7e3
2 changed files with 39 additions and 2 deletions

View file

@ -15,7 +15,8 @@ class Ameba::Spec::AnnotatedSource
def self.parse(annotated_code) def self.parse(annotated_code)
lines = [] of String lines = [] of String
annotations = [] of {Int32, String, String} annotations = [] of {Int32, String, String}
annotated_code.each_line do |code_line| code_lines = annotated_code.split('\n') # must preserve trailing newline
code_lines.each do |code_line|
if (annotation_match = ANNOTATION_PATTERN_1.match(code_line)) if (annotation_match = ANNOTATION_PATTERN_1.match(code_line))
message_index = annotation_match.end message_index = annotation_match.end
prefix = code_line[0...message_index] prefix = code_line[0...message_index]

View file

@ -47,6 +47,8 @@ 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 = "",
@ -79,6 +81,39 @@ module Ameba::Spec::ExpectIssue
end end
end end
def expect_correction(correction, *, file = __FILE__, line = __LINE__)
source = ExpectIssue.source
raise "`expect_correction` must follow `expect_issue`" unless source
corrected_code = Source::Corrector.correct(source) # TODO: recursive
raise "Use `expect_no_corrections` if the code will not change" unless corrected_code
return if correction == corrected_code
fail <<-MSG, file, line
Expected correction:
#{correction}
Got:
#{corrected_code}
MSG
end
def expect_no_corrections(*, file = __FILE__, line = __LINE__)
source = ExpectIssue.source
raise "`expect_no_corrections` must follow `expect_offense`" unless source
corrected_code = Source::Corrector.correct(source)
return unless corrected_code
fail <<-MSG, file, line
Expected no corrections, but got:
#{corrected_code}
MSG
end
def expect_no_issues(rules : Rule::Base | Enumerable(Rule::Base), def expect_no_issues(rules : Rule::Base | Enumerable(Rule::Base),
code : String, code : String,
path = "", path = "",
@ -87,7 +122,7 @@ module Ameba::Spec::ExpectIssue
file = __FILE__, file = __FILE__,
line = __LINE__) line = __LINE__)
code = normalize_code(code) if normalize code = normalize_code(code) if normalize
lines = code.lines 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
@ -100,6 +135,7 @@ 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