Convertable severity

This commit is contained in:
Vitalii Elenhaupt 2019-04-13 21:16:59 +03:00
parent c95ea297bd
commit 0be42f94db
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
3 changed files with 129 additions and 1 deletions

View file

@ -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

View file

@ -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 %}

43
src/ameba/severity.cr Normal file
View file

@ -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