incompatibe methods
This commit is contained in:
parent
b646d2d267
commit
be563c26b9
30
debug.cr
30
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
|
||||
|
43
src/spec.cr
43
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
|
||||
|
Reference in New Issue
Block a user