Add support for showing end location marker (#200)

* Add support for showing end location marker

* Cleanup Reportable method definitions

There’s no need for double splats, since they mess up method resolution, and obscure the actual - single (!) - argument - `status`, so… be gone

Also, all of the helpers return the constructed `Issue` like a behaving good methods.

* Refactor Util#affected_code

* Increase max length of trimmed lines to 120 characters

* Refactor Issue to use enum instead of a symbol for #status

* Optimize Reportable#valid?

* Add spec coverage for newly added Util methods

* Refactor DotFormatter a bit

Make text format moar in line with Crystal spec runner.

* Update README.md
This commit is contained in:
Sijawusz Pur Rahnama 2021-01-26 07:38:19 +01:00 committed by GitHub
parent 8b52dc4b1d
commit ea98554191
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 225 additions and 100 deletions

View file

@ -8,7 +8,7 @@ module Ameba::Formatter
describe "#started" do
it "writes started message" do
subject.started [Source.new ""]
output.to_s.should eq "Inspecting 1 file.\n\n"
output.to_s.should eq "Inspecting 1 file\n\n"
end
end
@ -29,7 +29,7 @@ module Ameba::Formatter
describe "#finished" do
it "writes a final message" do
subject.finished [Source.new ""]
output.to_s.should contain "1 inspected, 0 failures."
output.to_s.should contain "1 inspected, 0 failures"
end
it "writes the elapsed time" do
@ -45,7 +45,7 @@ module Ameba::Formatter
end
subject.finished [s]
log = output.to_s
log.should contain "1 inspected, 2 failures."
log.should contain "1 inspected, 2 failures"
log.should contain "DummyRuleError"
log.should contain "NamedRuleError"
end
@ -60,7 +60,7 @@ module Ameba::Formatter
end
subject.finished [s]
log = output.to_s
log.should contain "> a = 22"
log.should contain "> \e[97ma = 22"
log.should contain " \e[33m^\e[0m"
end
@ -99,7 +99,7 @@ module Ameba::Formatter
s.add_issue(DummyRule.new, location: {1, 1},
message: "DummyRuleError", status: :disabled)
subject.finished [s]
output.to_s.should contain "1 inspected, 0 failures."
output.to_s.should contain "1 inspected, 0 failures"
end
end
end

View file

@ -8,6 +8,65 @@ module Ameba::Formatter
subject = Subject.new
describe Util do
describe "#deansify" do
it "returns given string without ANSI codes" do
str = String.build do |io|
io << "foo".colorize.green.underline
io << '-'
io << "bar".colorize.red.underline
end
subject.deansify("foo-bar").should eq "foo-bar"
subject.deansify(str).should eq "foo-bar"
end
end
describe "#trim" do
it "trims string longer than :max_length" do
subject.trim(("+" * 300), 1).should eq "+"
subject.trim(("+" * 300), 3).should eq "+++"
subject.trim(("+" * 300), 5).should eq "+ ..."
subject.trim(("+" * 300), 7).should eq "+++ ..."
end
it "leaves intact string shorter than :max_length" do
subject.trim(("+" * 3), 100).should eq "+++"
end
it "allows to use custom ellipsis" do
subject.trim(("+" * 300), 3, "").should eq "++…"
end
end
describe "#context" do
it "returns correct pre/post context lines" do
source = Source.new <<-EOF
# pre:1
# pre:2
# pre:3
# pre:4
# pre:5
a = 1
# post:1
# post:2
# post:3
# post:4
# post:5
EOF
subject.context(source.lines, lineno: 6, context_lines: 3)
.should eq({<<-PRE.lines, <<-POST.lines
# pre:3
# pre:4
# pre:5
PRE
# post:1
# post:2
# post:3
POST
})
end
end
describe "#affected_code" do
it "returns nil if there is no such a line number" do
source = Source.new %(
@ -23,7 +82,7 @@ module Ameba::Formatter
)
location = Crystal::Location.new("filename", 1, 1)
subject.deansify(subject.affected_code(source, location))
.should eq "> a = 1\n ^"
.should eq "> a = 1\n ^\n"
end
it "returns correct line if it is found" do