Let ameba explain the issue at the specified location (#86)

This commit is contained in:
Vitalii Elenhaupt 2018-12-27 23:34:10 +02:00 committed by GitHub
parent 4e19571fb3
commit c91da1aa08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 359 additions and 22 deletions

View file

@ -0,0 +1,70 @@
require "../../spec_helper"
module Ameba
def explanation(source)
output = IO::Memory.new
ErrorRule.new.catch(source)
location = {file: "source.cr", line: 1, column: 1}
Formatter::ExplainFormatter.new(output, location).finished([source])
output.to_s
end
describe Formatter::ExplainFormatter do
describe "#location" do
it "returns crystal location" do
location = Formatter::ExplainFormatter
.new(STDOUT, {file: "compiler.cr", line: 3, column: 8}).location
location.is_a?(Crystal::Location).should be_true
location.filename.should eq "compiler.cr"
location.line_number.should eq 3
location.column_number.should eq 8
end
end
describe "#output" do
it "returns io" do
output = Formatter::ExplainFormatter
.new(STDOUT, {file: "compiler.cr", line: 3, column: 8}).output
output.should eq STDOUT
end
end
describe "#finished" do
it "writes issue info" do
source = Source.new "a = 42", "source.cr"
output = explanation(source)
output.should contain "ISSUE INFO"
output.should contain "This rule always adds an error"
output.should contain "source.cr:1:1"
end
it "writes affected code" do
source = Source.new "a = 42", "source.cr"
output = explanation(source)
output.should contain "AFFECTED CODE"
output.should contain "a = 42"
end
it "writes rule info" do
source = Source.new "a = 42", "source.cr"
output = explanation(source)
output.should contain "RULE INFO"
output.should contain "Ameba/ErrorRule"
output.should contain "Always adds an error at 1:1"
end
it "writes detailed description" do
source = Source.new "a = 42", "source.cr"
output = explanation(source)
output.should contain "DETAILED DESCRIPTION"
output.should contain "TO BE DONE..."
end
it "writes nothing if location not found" do
source = Source.new "a = 42", "another_source.cr"
explanation(source).should be_empty
end
end
end
end

View file

@ -0,0 +1,29 @@
require "../../spec_helper"
module Ameba::Formatter
class Subject
include Util
end
subject = Subject.new
describe Util do
describe "#affected_code" do
it "returns nil if there is no such a line number" do
source = Source.new %(
a = 1
)
location = Crystal::Location.new("filename", 2, 1)
subject.affected_code(source, location).should be_nil
end
it "returns correct line if it is found" do
source = Source.new %(
a = 1
)
location = Crystal::Location.new("filename", 1, 1)
subject.affected_code(source, location).should eq "> a = 1\n \e[33m^\e[0m"
end
end
end
end