3
0

incompatibe methods

This commit is contained in:
Philippe PITTOLI 2019-08-08 01:02:02 +02:00
parent b646d2d267
commit be563c26b9
2 changed files with 73 additions and 0 deletions

View File

@ -20,3 +20,33 @@ options["someoptionexample"] = "option"
specs = Specs.parse recipe_file_name, options specs = Specs.parse recipe_file_name, options
pp! specs 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

View File

@ -1,20 +1,58 @@
class Specs 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 class StringContainer
property value : String 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) def initialize(@value)
end end
end end
class LongStringContainer class LongStringContainer
Specs.incompatible_methods as_s, as_a_or_s
property value : String property value : String
def as_s_or_ls : String
@value
end
def initialize(@value) def initialize(@value)
end end
end end
class SectionContainer class SectionContainer
Specs.incompatible_methods as_s, as_a_or_s, as_s_or_ls
property value : String property value : String
def initialize(@value) def initialize(@value)
@ -22,8 +60,13 @@ class Specs
end end
class ArrayContainer class ArrayContainer
Specs.incompatible_methods as_s, as_s_or_ls
property value : Array(String) property value : Array(String)
def as_a_or_s : Array(String)
@value
end
def initialize(@value) def initialize(@value)
end end
end end