Add 'optional' matcher

This commit is contained in:
Bartłomiej Pluta
2019-07-13 23:08:17 +02:00
parent 44d234d36a
commit e70b5fa71a
2 changed files with 11 additions and 1 deletions

View File

@@ -12,6 +12,9 @@ class Signature:
def varargSignature(varargMatcher, *basicSignature, wrapVarargInValue=False):
def check(args):
if any([ matcher.optional for matcher in [ varargMatcher, *basicSignature ]]):
raise RuntimeError("Vararg signature can't have optional arguments")
if len(basicSignature) > len(args):
return doesNotMatchVararg(basicSignature)
@@ -38,7 +41,7 @@ def doesNotMatchVararg(basicSignature):
def signature(*signature):
def check(args):
if len(signature) != len(args):
if len(args) not in [len(signature), len([ matcher for matcher in signature if not matcher.optional ])]:
return doesNotMatch(signature)
for s, a in zip(signature, args):
@@ -52,6 +55,12 @@ def signature(*signature):
return Signature(check, string, signature)
def optional(matcher):
matcher.optional = True
matcher.string += "?"
return matcher
def doesNotMatch(sign):
return (False, *[None for n in sign])

View File

@@ -3,6 +3,7 @@ class Matcher:
self.type = objectType
self.matcher = matcher
self.string = string
self.optional = False
def match(self, value):
if self.type is not None and self.type != value.type: