diff --git a/spec/ameba/severity_spec.cr b/spec/ameba/severity_spec.cr new file mode 100644 index 00000000..275d5059 --- /dev/null +++ b/spec/ameba/severity_spec.cr @@ -0,0 +1,79 @@ +require "../spec_helper" + +module Ameba + describe Severity do + describe ".default" do + it "returns default severity" do + Severity.default.should eq Severity::Refactoring + end + end + + describe ".from_name" do + it "creates error severity by name" do + Severity.from_name("Error").should eq Severity::Error + end + + it "creates warning severity by name" do + Severity.from_name("Warning").should eq Severity::Warning + end + + it "creates refactoring severity by name" do + Severity.from_name("Refactoring").should eq Severity::Refactoring + end + + it "raises when name is incorrect" do + expect_raises(Exception, "Incorrect severity name BadName. Try one of [Error, Warning, Refactoring]") do + Severity.from_name("BadName") + end + end + end + end + + struct SeverityConvertable + YAML.mapping( + severity: { type: Severity, converter: SeverityYamlConverter } + ) + end + + describe SeverityYamlConverter do + describe ".from_yaml" do + it "converts from yaml to Severity::Error" do + yaml = { severity: "error" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml) + converted.severity.should eq Severity::Error + end + + it "converts from yaml to Severity::Warning" do + yaml = { severity: "warning" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml) + converted.severity.should eq Severity::Warning + end + + it "converts from yaml to Severity::Refactoring" do + yaml = { severity: "refactoring" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml) + converted.severity.should eq Severity::Refactoring + end + end + + describe ".to_yaml" do + it "converts Severity::Error to yaml" do + yaml = { severity: "error" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml).to_yaml + converted.should eq "---\nseverity: Error\n" + end + + it "converts Severity::Warning to yaml" do + yaml = { severity: "warning" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml).to_yaml + converted.should eq "---\nseverity: Warning\n" + end + + it "converts Severity::Refactoring to yaml" do + yaml = { severity: "refactoring" }.to_yaml + converted = SeverityConvertable.from_yaml(yaml).to_yaml + converted.should eq "---\nseverity: Refactoring\n" + end + end + end +end diff --git a/src/ameba/config.cr b/src/ameba/config.cr index c204acd2..e8fdac23 100644 --- a/src/ameba/config.cr +++ b/src/ameba/config.cr @@ -193,6 +193,12 @@ class Ameba::Config {% key = name.camelcase.stringify %} {% value = df[:value] %} {% type = df[:type] %} + {% converter = nil %} + + {% if key == "Severity" %} + {% type = Severity %} + {% converter = SeverityYamlConverter %} + {% end %} {% if type == nil %} {% if value.is_a? BoolLiteral %} @@ -214,7 +220,7 @@ class Ameba::Config {% type = Nil if type == nil %} {% end %} - {% properties[name] = {key: key, default: value, type: type} %} + {% properties[name] = {key: key, default: value, type: type, converter: converter} %} {% end %} {% if properties["enabled".id] == nil %} diff --git a/src/ameba/severity.cr b/src/ameba/severity.cr new file mode 100644 index 00000000..66de0763 --- /dev/null +++ b/src/ameba/severity.cr @@ -0,0 +1,43 @@ +module Ameba + enum Severity + Error + Warning + Refactoring + + def self.default + Refactoring + end + + def self.from_name(name : String) + case name.downcase + when "error" + Error + when "warning" + Warning + when "refactoring" + Refactoring + else + raise "Incorrect severity name #{name}. Try one of #{Severity.values}" + end + end + end + + class SeverityYamlConverter + def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) + unless node.is_a?(YAML::Nodes::Scalar) + raise "Severity must be a scalar, not #{node.class}" + end + + case value = node.value + when String then Severity.from_name(value) + when Nil then nil + else + raise "Incorrect severity: #{value}" + end + end + + def self.to_yaml(value : Severity, yaml : YAML::Nodes::Builder) + yaml.scalar value.to_s + end + end +end