From be563c26b9588901fad74f80475cfaa3f42cbc47 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Thu, 8 Aug 2019 01:02:02 +0200 Subject: [PATCH] incompatibe methods --- debug.cr | 30 ++++++++++++++++++++++++++++++ src/spec.cr | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/debug.cr b/debug.cr index bd4e7fd..158c0da 100644 --- a/debug.cr +++ b/debug.cr @@ -20,3 +20,33 @@ options["someoptionexample"] = "option" specs = Specs.parse recipe_file_name, options pp! specs + +# low level stuff + +sectioncontainer = Specs::SectionContainer.new "val" +begin + puts sectioncontainer.as_s + puts "(NOT OK) Specs::SectionContainer should not accept .as_s" +rescue e + puts "(OK) #{e}" +end +begin + pp! sectioncontainer.as_a_or_s + puts "(NOT OK) Specs::SectionContainer should not accept .as_a_or_s" +rescue e + puts "(OK) #{e}" +end + +arraycontainer = Specs::ArrayContainer.new Array(String).new.push "value" +begin + puts arraycontainer.as_s + puts "(NOT OK) Specs::ArrayContainer should not accept .as_s" +rescue e + puts "(OK) #{e}" +end +begin + pp! arraycontainer.as_a_or_s + puts "(OK) Specs::ArrayContainer should accept .as_a_or_s" +rescue e + puts "(NOT OK) #{e}" +end diff --git a/src/spec.cr b/src/spec.cr index d13e5e6..2520940 100644 --- a/src/spec.cr +++ b/src/spec.cr @@ -1,20 +1,58 @@ class Specs + + macro incompatible_methods(*names) + {% for name in names %} + {% if name.id == "as_s" %} + def {{name}} : String + raise "short string expected; which does not exist in #{self.class}" + end + {% elsif name.id == "as_a_or_s" %} + def {{name}} : Array(String) + raise "list or string expected; which does not exist in #{self.class}" + end + {% elsif name.id == "as_s_or_ls" %} + def {{name}} : String + raise "string or multiline text section expected; which does not exist in #{self.class}" + end + {% end %} + {% end %} + end + class StringContainer property value : String + def as_s : String + @value + end + + def as_a_or_s : Array(String) + # FIXME: We should probably be splitting the string around comas. + [@value] + end + + def as_s_or_ls : String + @value + end + def initialize(@value) end end class LongStringContainer + Specs.incompatible_methods as_s, as_a_or_s property value : String + def as_s_or_ls : String + @value + end + def initialize(@value) end end class SectionContainer + Specs.incompatible_methods as_s, as_a_or_s, as_s_or_ls property value : String def initialize(@value) @@ -22,8 +60,13 @@ class Specs end class ArrayContainer + Specs.incompatible_methods as_s, as_s_or_ls property value : Array(String) + def as_a_or_s : Array(String) + @value + end + def initialize(@value) end end