2019-04-13 18:16:59 +00:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
module Ameba
|
|
|
|
describe Severity do
|
2019-04-14 13:45:31 +00:00
|
|
|
describe "#symbol" do
|
|
|
|
it "returns the symbol for each severity in the enum" do
|
2021-01-25 01:34:33 +00:00
|
|
|
Severity.values.each(&.symbol.should_not(be_nil))
|
2019-04-14 13:45:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns symbol for Error" do
|
|
|
|
Severity::Error.symbol.should eq 'E'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns symbol for Warning" do
|
|
|
|
Severity::Warning.symbol.should eq 'W'
|
|
|
|
end
|
|
|
|
|
2019-05-11 18:17:18 +00:00
|
|
|
it "returns symbol for Convention" do
|
|
|
|
Severity::Convention.symbol.should eq 'C'
|
2019-04-14 13:45:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-26 15:15:36 +00:00
|
|
|
describe ".parse" do
|
2019-04-13 18:16:59 +00:00
|
|
|
it "creates error severity by name" do
|
2019-04-26 15:15:36 +00:00
|
|
|
Severity.parse("Error").should eq Severity::Error
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates warning severity by name" do
|
2019-04-26 15:15:36 +00:00
|
|
|
Severity.parse("Warning").should eq Severity::Warning
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
|
2019-05-11 18:17:18 +00:00
|
|
|
it "creates convention severity by name" do
|
|
|
|
Severity.parse("Convention").should eq Severity::Convention
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises when name is incorrect" do
|
2023-02-19 15:39:25 +00:00
|
|
|
expect_raises(Exception, "Incorrect severity name BadName. Try one of: Error, Warning, Convention") do
|
2019-04-26 15:15:36 +00:00
|
|
|
Severity.parse("BadName")
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-16 15:55:32 +00:00
|
|
|
struct SeverityConvertible
|
2020-06-15 11:19:23 +00:00
|
|
|
include YAML::Serializable
|
|
|
|
|
|
|
|
@[YAML::Field(converter: Ameba::SeverityYamlConverter)]
|
2023-04-05 09:20:44 +00:00
|
|
|
property severity : Severity?
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe SeverityYamlConverter do
|
|
|
|
describe ".from_yaml" do
|
|
|
|
it "converts from yaml to Severity::Error" do
|
2019-04-13 19:38:37 +00:00
|
|
|
yaml = {severity: "error"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml)
|
2019-04-13 18:16:59 +00:00
|
|
|
converted.severity.should eq Severity::Error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts from yaml to Severity::Warning" do
|
2019-04-13 19:38:37 +00:00
|
|
|
yaml = {severity: "warning"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml)
|
2019-04-13 18:16:59 +00:00
|
|
|
converted.severity.should eq Severity::Warning
|
|
|
|
end
|
|
|
|
|
2019-05-11 18:17:18 +00:00
|
|
|
it "converts from yaml to Severity::Convention" do
|
|
|
|
yaml = {severity: "convention"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml)
|
2019-05-11 18:17:18 +00:00
|
|
|
converted.severity.should eq Severity::Convention
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
2019-04-13 19:38:37 +00:00
|
|
|
|
|
|
|
it "raises if severity is not a scalar" do
|
2019-05-11 18:17:18 +00:00
|
|
|
yaml = {severity: {convention: true}}.to_yaml
|
2019-04-13 19:38:37 +00:00
|
|
|
expect_raises(Exception, "Severity must be a scalar") do
|
2022-11-16 15:55:32 +00:00
|
|
|
SeverityConvertible.from_yaml(yaml)
|
2019-04-13 19:38:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if severity has a wrong type" do
|
|
|
|
yaml = {severity: [1, 2, 3]}.to_yaml
|
|
|
|
expect_raises(Exception, "Severity must be a scalar") do
|
2022-11-16 15:55:32 +00:00
|
|
|
SeverityConvertible.from_yaml(yaml)
|
2019-04-13 19:38:37 +00:00
|
|
|
end
|
|
|
|
end
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".to_yaml" do
|
|
|
|
it "converts Severity::Error to yaml" do
|
2019-04-13 19:38:37 +00:00
|
|
|
yaml = {severity: "error"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml).to_yaml
|
2019-04-13 18:16:59 +00:00
|
|
|
converted.should eq "---\nseverity: Error\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts Severity::Warning to yaml" do
|
2019-04-13 19:38:37 +00:00
|
|
|
yaml = {severity: "warning"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml).to_yaml
|
2019-04-13 18:16:59 +00:00
|
|
|
converted.should eq "---\nseverity: Warning\n"
|
|
|
|
end
|
|
|
|
|
2019-05-11 18:17:18 +00:00
|
|
|
it "converts Severity::Convention to yaml" do
|
|
|
|
yaml = {severity: "convention"}.to_yaml
|
2022-11-16 15:55:32 +00:00
|
|
|
converted = SeverityConvertible.from_yaml(yaml).to_yaml
|
2019-05-11 18:17:18 +00:00
|
|
|
converted.should eq "---\nseverity: Convention\n"
|
2019-04-13 18:16:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|