Add parse tests

This commit is contained in:
Tony Garnock-Jones 2023-10-29 19:11:16 +01:00
parent 4869507b09
commit a14b2d49b7
1 changed files with 18 additions and 11 deletions

View File

@ -275,16 +275,23 @@ def install_test(d, variant, tName, binaryForm, annotatedTextForm):
if variant in ['normal', 'nondeterministic']:
add_method(d, tName, test_encode_ann)
def install_exn_test(d, tName, bs, check_proc):
def install_exn_test(d, tName, testLambda, check_proc):
def test_exn(self):
try:
self.D(bs)
testLambda(self)
except:
check_proc(self, sys.exc_info()[1])
return
self.fail('did not fail as expected')
add_method(d, tName, test_exn)
def expected_err(self, e):
self.assertIsInstance(e, DecodeError)
self.assertNotIsInstance(e, ShortPacket)
def expected_short(self, e):
self.assertIsInstance(e, ShortPacket)
class CommonTestSuite(PreservesTestCase):
TestCases = Record.makeConstructor('TestCases', 'cases')
@ -300,20 +307,20 @@ class CommonTestSuite(PreservesTestCase):
elif t.key == Symbol('DecodeTest'):
install_test(locals(), 'decode', tName, t[0].strip(), t[1])
elif t.key == Symbol('DecodeError'):
def expected_err(self, e):
self.assertIsInstance(e, DecodeError)
self.assertNotIsInstance(e, ShortPacket)
install_exn_test(locals(), tName, t[0].strip(), expected_err)
install_exn_test(locals(), tName, lambda self, t=t: self.D(t[0].strip()), expected_err)
elif t.key in [Symbol('DecodeShort'), Symbol('DecodeEOF')]:
def expected_short(self, e):
self.assertIsInstance(e, ShortPacket)
install_exn_test(locals(), tName, t[0].strip(), expected_short)
elif t.key in [Symbol('ParseError'), Symbol('ParseShort'), Symbol('ParseEOF')]:
# Skipped for now, until we have an implementation of text syntax
install_exn_test(locals(), tName, lambda self, t=t: self.D(t[0].strip()), expected_short)
elif t.key == Symbol('ParseError'):
install_exn_test(locals(), tName, lambda self, t=t: self.R(t[0].strip()), expected_err)
elif t.key in [Symbol('ParseShort'), Symbol('ParseEOF')]:
install_exn_test(locals(), tName, lambda self, t=t: self.R(t[0].strip()), expected_short)
pass
else:
raise Exception('Unsupported test kind', t.key)
def R(self, text):
return parse(text)
def DS(self, bs):
return decode(bs, decode_embedded=lambda x: x)