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,43 +2,52 @@ 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
YAML.parse(create_todo).should_not be_nil YAML.parse(create_todo).should_not be_nil
@ -77,8 +86,8 @@ 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" s1 = Source.new "a = 1", "source1.cr"
s2 = Source.new "a = 1", "source2.cr" s2 = Source.new "a = 1", "source2.cr"
s1.add_issue DummyRule.new, {1, 2}, "message1" s1.add_issue DummyRule.new, {1, 2}, "message1"
@ -100,20 +109,22 @@ module Ameba
Severity: Convention Severity: Convention
CONTENT CONTENT
end end
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"
@ -124,4 +135,5 @@ module Ameba
end end
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