Use macros to create methods for all watched operators

This commit is contained in:
Michael Miller 2019-01-23 21:02:32 -07:00
parent d02e5d33b4
commit 73c4334246

View file

@ -1,30 +1,27 @@
# Example system to test that doubles as a spy. # Example system to test that doubles as a spy.
# This class tracks calls made to it. # This class tracks calls made to it.
class SpySUT class SpySUT
# Number of times the `#==` method was called. {% for item in [
getter eq_call_count = 0 {"==", "eq"},
{"!=", "ne"},
{"<", "lt"},
{"<=", "le"},
{">", "gt"},
{">=", "ge"},
{"===", "case_eq"},
{"=~", "match"}
] %}
{% operator = item[0].id %}
{% name = item[1].id %}
# Number of times the `#===` method was called. # Number of times the `#{{operator}}` method was called.
getter case_eq_call_count = 0 getter {{name}}_call_count = 0
# Number of times the `#=~` method was called. # Returns true and increments `#{{name}}_call_count`.
getter match_call_count = 0 def {{operator}}(other : T) forall T
@{{name}}_call_count += 1
# Returns true and increments `#eq_call_count`.
def ==(other : T) forall T
@eq_call_count += 1
true true
end end
# Returns true and increments `#case_eq_call_count`. {% end %}
def ===(other : T) forall T
@case_eq_call_count += 1
true
end
# Returns true and increments `#match_eq_call_count`.
def =~(other : T) forall T
@match_call_count += 1
true
end
end end