Refactor TODOFormatter spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-22 19:51:48 +01:00
parent 2af58cabd4
commit 9f670c09b5
2 changed files with 66 additions and 54 deletions

View file

@ -2,42 +2,51 @@ require "../../spec_helper"
require "file_utils" require "file_utils"
module Ameba module Ameba
private def with_formatter
io = IO::Memory.new
formatter = Formatter::TODOFormatter.new(io)
yield formatter, io
end
private def create_todo private def create_todo
formatter = Formatter::TODOFormatter.new IO::Memory.new with_formatter do |formatter|
s = Source.new "a = 1", "source.cr" s = Source.new "a = 1", "source.cr"
s.add_issue DummyRule.new, {1, 2}, "message" s.add_issue DummyRule.new, {1, 2}, "message"
file = formatter.finished([s]) file = formatter.finished([s])
file ? File.read(file.path) : "" file ? File.read(file.path) : ""
end
end end
describe Formatter::TODOFormatter do describe Formatter::TODOFormatter do
::Spec.after_each do ::Spec.after_each do
FileUtils.rm(Ameba::Config::PATH) if File.exists?(Ameba::Config::PATH) FileUtils.rm_rf(Ameba::Config::PATH)
end end
context "problems not found" do context "problems not found" do
it "does not create file" do it "does not create file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new with_formatter do |formatter|
file = formatter.finished [Source.new ""] file = formatter.finished [Source.new ""]
file.should be_nil file.should be_nil
end
end end
it "reports a message saying file is not created" do it "reports a message saying file is not created" do
io = IO::Memory.new with_formatter do |formatter, io|
formatter = Formatter::TODOFormatter.new io formatter.finished [Source.new ""]
formatter.finished [Source.new ""] io.to_s.should contain "No issues found. File is not generated"
io.to_s.should contain "No issues found. File is not generated" end
end end
end end
context "problems found" do context "problems found" do
it "prints a message saying file is created" do it "prints a message saying file is created" do
io = IO::Memory.new with_formatter do |formatter, io|
formatter = Formatter::TODOFormatter.new io s = Source.new "a = 1", "source.cr"
s = Source.new "a = 1", "source.cr" s.add_issue DummyRule.new, {1, 2}, "message"
s.add_issue DummyRule.new, {1, 2}, "message" formatter.finished([s])
formatter.finished([s]) io.to_s.should contain "Created .ameba.yml"
io.to_s.should contain "Created .ameba.yml" end
end end
it "creates a valid YAML document" do it "creates a valid YAML document" do
@ -77,49 +86,52 @@ module Ameba
end end
context "with multiple issues" do context "with multiple issues" do
formatter = Formatter::TODOFormatter.new IO::Memory.new it "does generate todo file" do
with_formatter do |formatter|
s1 = Source.new "a = 1", "source1.cr"
s2 = Source.new "a = 1", "source2.cr"
s1.add_issue DummyRule.new, {1, 2}, "message1"
s1.add_issue NamedRule.new, {1, 2}, "message1"
s1.add_issue DummyRule.new, {2, 2}, "message1"
s2.add_issue DummyRule.new, {2, 2}, "message2"
s1 = Source.new "a = 1", "source1.cr" file = formatter.finished([s1, s2]).should_not be_nil
s2 = Source.new "a = 1", "source2.cr" content = File.read(file.path)
s1.add_issue DummyRule.new, {1, 2}, "message1" content.should contain <<-CONTENT
s1.add_issue NamedRule.new, {1, 2}, "message1" # Problems found: 3
s1.add_issue DummyRule.new, {2, 2}, "message1" # Run `ameba --only Ameba/DummyRule` for details
s2.add_issue DummyRule.new, {2, 2}, "message2" Ameba/DummyRule:
Description: Dummy rule that does nothing.
file = formatter.finished([s1, s2]).should_not be_nil Excluded:
content = File.read(file.path) - source1.cr
content.should contain <<-CONTENT - source2.cr
# Problems found: 3 Enabled: true
# Run `ameba --only Ameba/DummyRule` for details Severity: Convention
Ameba/DummyRule: CONTENT
Description: Dummy rule that does nothing. end
Excluded: end
- source1.cr
- source2.cr
Enabled: true
Severity: Convention
CONTENT
end end
context "when invalid syntax" do context "when invalid syntax" do
it "does generate todo file" do it "does generate todo file" do
formatter = Formatter::TODOFormatter.new IO::Memory.new with_formatter do |formatter|
s = Source.new "def invalid_syntax" s = Source.new "def invalid_syntax"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message" s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
file = formatter.finished [s] file = formatter.finished [s]
file.should be_nil file.should be_nil
end
end end
it "prints an error message" do it "prints an error message" do
io = IO::Memory.new with_formatter do |formatter, io|
formatter = Formatter::TODOFormatter.new io s = Source.new "def invalid_syntax"
s = Source.new "def invalid_syntax" s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
s.add_issue Rule::Lint::Syntax.new, {1, 2}, "message"
formatter.finished [s] formatter.finished [s]
io.to_s.should contain "Unable to generate TODO file" io.to_s.should contain "Unable to generate TODO file"
io.to_s.should contain "Please fix syntax issues" io.to_s.should contain "Please fix syntax issues"
end
end end
end end
end end

View file

@ -74,14 +74,14 @@ module Ameba
# A rule that always raises an error # A rule that always raises an error
class RaiseRule < Rule::Base class RaiseRule < Rule::Base
property should_raise = false property? should_raise = false
properties do properties do
description "Internal rule that always raises" description "Internal rule that always raises"
end end
def test(source) def test(source)
should_raise && raise "something went wrong" should_raise? && raise "something went wrong"
end end
end end