python tests baked into scons; h_ch needs the first char of its input string as its input

This commit is contained in:
Meredith L. Patterson 2013-11-18 21:14:44 -06:00
parent 6746df20db
commit b979f66c21
3 changed files with 226 additions and 228 deletions

View file

@ -13,4 +13,7 @@ pythonenv.Command("hammer.i", "../swig/hammer.i", Copy("$TARGET", "$SOURCE"))
swig = ['hammer.i'] swig = ['hammer.i']
libhammer_python = pythonenv.SharedLibrary('hammer', swig) libhammer_python = pythonenv.SharedLibrary('hammer', swig, SHLIBPREFIX='_')
pytestenv = pythonenv.Clone()
pytestenv.Command(None, 'hammer_tests.py', "nosetests $SOURCE")

View file

@ -4,503 +4,495 @@ import hammer as h
class TestTokenParser(unittest.TestCase): class TestTokenParser(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.token("95\xa2") cls.parser = h.h_token("95\xa2", 3)
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("95\xa2"), "95\xa2") self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes.token, "95\xa2")
def test_partial_fails(self): def test_partial_fails(self):
self.assertEqual(self.parser.parse("95"), None) self.assertEqual(h.h_parse(self.parser, "95", 2), None)
class TestChParser(unittest.TestCase): class TestChParser(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser_int = h.ch(0xa2) cls.parser_int = h.h_ch(0xa2)
cls.parser_chr = h.ch("\xa2") cls.parser_chr = h.h_ch("\xa2")
def test_success(self): def test_success(self):
self.assertEqual(self.parser_int.parse("\xa2"), 0xa2) self.assertEqual(h.h_parse(self.parser_int, "\xa2", 1).ast.token_data.uint, 0xa2)
self.assertEqual(self.parser_chr.parse("\xa2"), "\xa2") self.assertEqual(h.h_parse(self.parser_chr, "\xa2", 1).ast.token_data.bytes, "\xa2")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser_int.parse("\xa3"), None) self.assertEqual(h.h_parse(self.parser_int, "\xa3", 1), None)
self.assertEqual(self.parser_chr.parse("\xa3"), None) self.assertEqual(h.h_parse(self.parser_chr, "\xa3", 1), None)
class TestChRange(unittest.TestCase): class TestChRange(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.ch_range("a", "c") cls.parser = h.h_ch_range("a", "c")
### this segfaults def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b")
# self.assertEqual(self.parser.parse("b"), "b")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("d"), None) self.assertEqual(h.h_parse(self.parser, "d", 1), None)
class TestInt64(unittest.TestCase): class TestInt64(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.int64() cls.parser = h.h_int64()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\xff\xff\xff\xfe\x00\x00\x00\x00"), -0x200000000) self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00\x00", 8).ast.token_data.sint, -0x200000000)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\xff\xff\xff\xfe\x00\x00\x00"), None) self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00", 7), None)
class TestInt32(unittest.TestCase): class TestInt32(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.int32() cls.parser = h.h_int32()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\xff\xfe\x00\x00"), -0x20000) self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00\x00", 4).ast.token_data.sint, -0x20000)
self.assertEqual(self.parser.parse("\x00\x02\x00\x00"), 0x20000) self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.sint, 0x20000)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\xff\xfe\x00"), None) self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00", 3), None)
self.assertEqual(self.parser.parse("\x00\x02\x00"), None) self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
class TestInt16(unittest.TestCase): class TestInt16(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.int16() cls.parser = h.h_int16()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\xfe\x00"), -0x200) self.assertEqual(h.h_parse(self.parser, "\xfe\x00", 2).ast.token_data.sint, -0x200)
self.assertEqual(self.parser.parse("\x02\x00"), 0x200) self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.sint, 0x200)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\xfe"), None) self.assertEqual(h.h_parse(self.parser, "\xfe", 1), None)
self.assertEqual(self.parser.parse("\x02"), None) self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
class TestInt8(unittest.TestCase): class TestInt8(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.int8() cls.parser = h.h_int8()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x88"), -0x78) self.assertEqual(h.h_parse(self.parser, "\x88", 1).ast.token_data.sint, -0x78)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse(""), None) self.assertEqual(h.h_parse(self.parser, "", 0), None)
class TestUint64(unittest.TestCase): class TestUint64(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.uint64() cls.parser = h.h_uint64()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x00\x00\x00\x02\x00\x00\x00\x00"), 0x200000000) self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00\x00", 8).ast.token_data.uint, 0x200000000)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\x00\x00\x00\x02\x00\x00\x00"), None) self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00", 7), None)
class TestUint32(unittest.TestCase): class TestUint32(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.uint32() cls.parser = h.h_uint32()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x00\x02\x00\x00"), 0x20000) self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.uint, 0x20000)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\x00\x02\x00"), None) self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
class TestUint16(unittest.TestCase): class TestUint16(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.uint16() cls.parser = h.h_uint16()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x02\x00"), 0x200) self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.uint, 0x200)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\x02"), None) self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
class TestUint8(unittest.TestCase): class TestUint8(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.uint8() cls.parser = h.h_uint8()
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x78"), 0x78) self.assertEqual(h.h_parse(self.parser, "\x78", 1).ast.token_data.uint, 0x78)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse(""), None) self.assertEqual(h.h_parse(self.parser, "", 0), None)
class TestIntRange(unittest.TestCase): class TestIntRange(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.int_range(h.uint8(), 3, 10) cls.parser = h.h_int_range(h.h_uint8(), 3, 10)
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("\x05"), 5) self.assertEqual(h.h_parse(self.parser, "\x05", 1).ast.token_data.uint, 5)
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("\x0b"), None) self.assertEqual(h.h_parse(self.parser, "\x0b", 1), None)
class TestWhitespace(unittest.TestCase): class TestWhitespace(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.whitespace(h.ch("a")) cls.parser = h.h_whitespace(h.h_ch("a"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), "a") self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
self.assertEqual(self.parser.parse(" a"), "a") self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.bytes, "a")
self.assertEqual(self.parser.parse(" a"), "a") self.assertEqual(h.h_parse(self.parser, " a", 3).ast.token_data.bytes, "a")
self.assertEqual(self.parser.parse("\ta"), "a") self.assertEqual(h.h_parse(self.parser, "\ta", 2).ast.token_data.bytes, "a")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("_a"), None) self.assertEqual(h.h_parse(self.parser, "_a", 2), None)
class TestWhitespaceEnd(unittest.TestCase): class TestWhitespaceEnd(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.whitespace(h.end_p()) cls.parser = h.h_whitespace(h.h_end_p())
### this segfaults def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "", 0).ast, None) # empty string
# self.assertEqual(self.parser.parse(""), "") self.assertEqual(h.h_parse(self.parser, " ", 2).ast, None) # empty string
# self.assertEqual(self.parser.parse(" "), "")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse(" x"), None) self.assertEqual(h.h_parse(self.parser, " x", 3), None)
class TestLeft(unittest.TestCase): class TestLeft(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.left(h.ch("a"), h.ch(" ")) cls.parser = h.h_left(h.h_ch("a"), h.h_ch(" "))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a "), "a") self.assertEqual(h.h_parse(self.parser, "a ", 2).ast.token_data.bytes, "a")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
self.assertEqual(self.parser.parse(" "), None) self.assertEqual(h.h_parse(self.parser, " ", 1), None)
self.assertEqual(self.parser.parse("ab"), None) self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
class TestRight(unittest.TestCase): class TestRight(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.right(h.ch(" "), h.ch("a")) cls.parser = h.h_right(h.h_ch(" "), h.h_ch("a"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse(" a"), "a") self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.bytes, "a")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
self.assertEqual(self.parser.parse(" "), None) self.assertEqual(h.h_parse(self.parser, " ", 1), None)
self.assertEqual(self.parser.parse("ba"), None) self.assertEqual(h.h_parse(self.parser, "ba", 2), None)
class TestMiddle(unittest.TestCase): class TestMiddle(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.middle(h.ch(" "), h.ch("a"), h.ch(" ")) cls.parser = h.h_middle(h.h_ch(" "), h.h_ch("a"), h.h_ch(" "))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse(" a "), "a") self.assertEqual(h.h_parse(self.parser, " a ", 3).ast.token_data.bytes, "a")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
self.assertEqual(self.parser.parse(" "), None) self.assertEqual(h.h_parse(self.parser, " ", 1), None)
self.assertEqual(self.parser.parse(" a"), None) self.assertEqual(h.h_parse(self.parser, " a", 2), None)
self.assertEqual(self.parser.parse("a "), None) self.assertEqual(h.h_parse(self.parser, "a ", 2), None)
self.assertEqual(self.parser.parse(" b "), None) self.assertEqual(h.h_parse(self.parser, " b ", 3), None)
self.assertEqual(self.parser.parse("ba "), None) self.assertEqual(h.h_parse(self.parser, "ba ", 3), None)
self.assertEqual(self.parser.parse(" ab"), None) self.assertEqual(h.h_parse(self.parser, " ab", 3), None)
class TestAction(unittest.TestCase): class TestAction(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.action(h.sequence(h.choice(h.ch("a"), h.ch("A")), h.choice(h.ch("b"), h.ch("B"))), lambda x: [y.upper() for y in x]) cls.parser = h.h_action(h.h_sequence(h.h_choice(h.h_ch("a"), h.h_ch("A")), h.h_choice(h.h_ch("b"), h.h_ch("B"))), lambda x: [y.upper() for y in x])
### fails with "corrupted double-linked list" def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["A", "B"])
# self.assertEqual(self.parser.parse("ab"), ["A", "B"]) self.assertEqual(h.h_parse(self.parser, "AB", 2).ast.token_data.seq, ["A", "B"])
# self.assertEqual(self.parser.parse("AB"), ["A", "B"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("XX"), None) self.assertEqual(h.h_parse(self.parser, "XX", 2), None)
class TestIn(unittest.TestCase): class TestIn(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.in_("abc") cls.parser = h.h_in("abc", 3)
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("b"), "b") self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b") # segfaulting when looking at bytes!
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("d"), None) self.assertEqual(h.h_parse(self.parser, "d", 1), None)
class TestNotIn(unittest.TestCase): class TestNotIn(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.not_in("abc") cls.parser = h.h_not_in("abc", 3)
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("d"), "d") self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.bytes, "d") # segfaulting when looking at bytes!
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
class TestEndP(unittest.TestCase): class TestEndP(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.end_p()) cls.parser = h.h_sequence(h.h_ch("a"), h.h_end_p())
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("aa"), None) self.assertEqual(h.h_parse(self.parser, "aa", 2), None)
class TestNothingP(unittest.TestCase): class TestNothingP(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.nothing_p() cls.parser = h.h_nothing_p()
def test_success(self): def test_success(self):
pass pass
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
class TestSequence(unittest.TestCase): class TestSequence(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.ch("b")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_ch("b"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("ab"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "ab").ast.token_data.seq, ["a", "b"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
self.assertEqual(self.parser.parse("b"), None) self.assertEqual(h.h_parse(self.parser, "b", 1), None)
class TestSequenceWhitespace(unittest.TestCase): class TestSequenceWhitespace(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.whitespace(h.ch("b"))) cls.parser = h.h_sequence(h.h_ch("a"), h.h_whitespace(h.h_ch("b")))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("ab"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["a", "b"])
self.assertEqual(self.parser.parse("a b"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "a b", 3).ast.token_data.seq, ["a", "b"])
self.assertEqual(self.parser.parse("a b"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "a b", 4).ast.token_data.seq, ["a", "b"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a c"), None) self.assertEqual(h.h_parse(self.parser, "a c", 4), None)
class TestChoice(unittest.TestCase): class TestChoice(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.choice(h.ch("a"), h.ch("b")) cls.parser = h.h_choice(h.h_ch("a"), h.h_ch("b"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), "a") self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.uint, "a")
self.assertEqual(self.parser.parse("b"), "b") self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("c"), None) self.assertEqual(h.h_parse(self.parser, "c", 1), None)
class TestButNot(unittest.TestCase): class TestButNot(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.butnot(h.ch("a"), h.token("ab")) cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), "a") self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
self.assertEqual(self.parser.parse("aa"), "a") self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.bytes, "a")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("ab"), None) self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
### fails with malloc() memory corruption class TestButNotRange(unittest.TestCase):
#class TestButNotRange(unittest.TestCase): @classmethod
# @classmethod def setUpClass(cls):
# def setUpClass(cls): cls.parser = h.h_butnot(h.h_ch_range("0", "9"), h.h_ch("6"))
# cls.parser = h.butnot(h.ch_range("0", "9"), h.ch("6")) def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "4", 1).ast.token_data.bytes, "4")
# self.assertEqual(self.parser.parse("4"), "4") def test_failure(self):
### this segfaults self.assertEqual(h.h_parse(self.parser, "6", 1), None)
# def test_failure(self):
# self.assertEqual(self.parser.parse("6"), None)
class TestDifference(unittest.TestCase): class TestDifference(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.difference(h.token("ab"), h.ch("a")) cls.parser = h.h_difference(h.h_token("ab", 2), h.h_ch("a"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("ab"), "ab") self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.bytes, "ab")
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a"), None) self.assertEqual(h.h_parse(self.parser, "a", 1), None)
#class TestXor(unittest.TestCase): class TestXor(unittest.TestCase):
# @classmethod @classmethod
# def setUpClass(cls): def setUpClass(cls):
# cls.parser = h.xor(h.ch_range("0", "6"), h.ch_range("5", "9")) cls.parser = h.h_xor(h.h_ch_range("0", "6"), h.h_ch_range("5", "9"))
### this segfaults def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "0", 1).ast.token_data.bytes, "0")
# self.assertEqual(self.parser.parse("0"), "0") self.assertEqual(h.h_parse(self.parser, "9", 1).ast.token_data.bytes, "9")
# self.assertEqual(self.parser.parse("9"), "9") def test_failure(self):
### fails with "malloc(): smallbin double linked list corrupted" self.assertEqual(h.h_parse(self.parser, "5", 1), None)
# def test_failure(self): self.assertEqual(h.h_parse(self.parser, "a", 1), None)
# self.assertEqual(self.parser.parse("5"), None)
# self.assertEqual(self.parser.parse("a"), None)
class TestMany(unittest.TestCase): class TestMany(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.many(h.choice(h.ch("a"), h.ch("b"))) cls.parser = h.h_many(h.h_choice(h.h_ch("a"), h.h_ch("b")))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse(""), []) self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
self.assertEqual(self.parser.parse("b"), ["b"]) self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.seq, ["b"])
self.assertEqual(self.parser.parse("aabbaba"), ["a", "a", "b", "b", "a", "b", "a"]) self.assertEqual(h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq, ["a", "a", "b", "b", "a", "b", "a"])
def test_failure(self): def test_failure(self):
pass pass
class TestMany1(unittest.TestCase): class TestMany1(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.many1(h.choice(h.ch("a"), h.ch("b"))) cls.parser = h.h_many1(h.h_choice(h.h_ch("a"), h.h_ch("b")))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
self.assertEqual(self.parser.parse("b"), ["b"]) self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.seq, ["b"])
self.assertEqual(self.parser.parse("aabbaba"), ["a", "a", "b", "b", "a", "b", "a"]) self.assertEqual(h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq, ["a", "a", "b", "b", "a", "b", "a"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse(""), None) self.assertEqual(h.h_parse(self.parser, "", 0), None)
self.assertEqual(self.parser.parse("daabbabadef"), None) self.assertEqual(h.h_parse(self.parser, "daabbabadef", 11), None)
class TestRepeatN(unittest.TestCase): class TestRepeatN(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.repeat_n(h.choice(h.ch("a"), h.ch("b")), 2) cls.parser = h.h_repeat_n(h.h_choice(h.h_ch("a"), h.h_ch("b")), 2)
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("abdef"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "abdef", 5).ast.token_data.seq, ["a", "b"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("adef"), None) self.assertEqual(h.h_parse(self.parser, "adef", 4), None)
self.assertEqual(self.parser.parse("dabdef"), None) self.assertEqual(h.h_parse(self.parser, "dabdef", 5), None)
class TestOptional(unittest.TestCase): class TestOptional(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.optional(h.choice(h.ch("b"), h.ch("c"))), h.ch("d")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_optional(h.h_choice(h.h_ch("b"), h.h_ch("c"))), h.h_ch("d"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("abd"), ["a", "b", "d"]) self.assertEqual(h.h_parse(self.parser, "abd", 3).ast.token_data.seq, ["a", "b", "d"])
self.assertEqual(self.parser.parse("acd"), ["a", "c", "d"]) self.assertEqual(h.h_parse(self.parser, "acd", 3).ast.token_data.seq, ["a", "c", "d"])
self.assertEqual(self.parser.parse("ad"), ["a", None, "d"]) self.assertEqual(h.h_parse(self.parser, "ad", 2).ast.token_data.seq, ["a", None, "d"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("aed"), None) self.assertEqual(h.h_parse(self.parser, "aed", 3), None)
self.assertEqual(self.parser.parse("ab"), None) self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
self.assertEqual(self.parser.parse("ac"), None) self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
class TestIgnore(unittest.TestCase): class TestIgnore(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.ignore(h.ch("b")), h.ch("c")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_ignore(h.h_ch("b")), h.h_ch("c"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("abc"), ["a", "c"]) self.assertEqual(h.h_parse(self.parser, "abc", 3).ast.token_data.seq, ["a", "c"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("ac"), None) self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
class TestSepBy(unittest.TestCase): class TestSepBy(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sepBy(h.choice(h.ch("1"), h.ch("2"), h.ch("3")), h.ch(",")) cls.parser = h.h_sepBy(h.h_choice(h.h_ch("1"), h.h_ch("2"), h.h_ch("3")), h.h_ch(","))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("1,2,3"), ["1", "2", "3"]) self.assertEqual(h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq, ["1", "2", "3"])
self.assertEqual(self.parser.parse("1,3,2"), ["1", "3", "2"]) self.assertEqual(h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq, ["1", "3", "2"])
self.assertEqual(self.parser.parse("1,3"), ["1", "3"]) self.assertEqual(h.h_parse(self.parser, "1,3", 3).ast.token_data.seq, ["1", "3"])
self.assertEqual(self.parser.parse("3"), ["3"]) self.assertEqual(h.h_parse(self.parser, "3", 1).ast.token_data.seq, ["3"])
self.assertEqual(self.parser.parse(""), []) self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
def test_failure(self): def test_failure(self):
pass pass
class TestSepBy1(unittest.TestCase): class TestSepBy1(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sepBy1(h.choice(h.ch("1"), h.ch("2"), h.ch("3")), h.ch(",")) cls.parser = h.h_sepBy1(h.h_choice(h.h_ch("1"), h.h_ch("2"), h.h_ch("3")), h.h_ch(","))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("1,2,3"), ["1", "2", "3"]) self.assertEqual(h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq, ["1", "2", "3"])
self.assertEqual(self.parser.parse("1,3,2"), ["1", "3", "2"]) self.assertEqual(h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq, ["1", "3", "2"])
self.assertEqual(self.parser.parse("1,3"), ["1", "3"]) self.assertEqual(h.h_parse(self.parser, "1,3", 3).ast.token_data.seq, ["1", "3"])
self.assertEqual(self.parser.parse("3"), ["3"]) self.assertEqual(h.h_parse(self.parser, "3", 1).ast.token_data.seq, ["3"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse(""), None) self.assertEqual(h.h_parse(self.parser, "", 0), None)
class TestEpsilonP1(unittest.TestCase): class TestEpsilonP1(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.epsilon_p(), h.ch("b")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_epsilon_p(), h.h_ch("b"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("ab"), ["a", "b"]) self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["a", "b"])
def test_failure(self): def test_failure(self):
pass pass
class TestEpsilonP2(unittest.TestCase): class TestEpsilonP2(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.epsilon_p(), h.ch("a")) cls.parser = h.h_sequence(h.h_epsilon_p(), h.h_ch("a"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
def test_failure(self): def test_failure(self):
pass pass
class TestEpsilonP3(unittest.TestCase): class TestEpsilonP3(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.epsilon_p()) cls.parser = h.h_sequence(h.h_ch("a"), h.h_epsilon_p())
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
def test_failure(self): def test_failure(self):
pass pass
# this has a double-free problem class TestAttrBool(unittest.TestCase):
#class TestAttrBool(unittest.TestCase): @classmethod
# @classmethod def setUpClass(cls):
# def setUpClass(cls): cls.parser = h.h_attr_bool(h.h_many1(h.h_choice(h.h_ch("a"), h.h_ch("b"))), lambda x: x[0] == x[1])
# cls.parser = h.attr_bool(h.many1(h.choice(h.ch("a"), h.ch("b"))), lambda x: x[0] == x[1]) def test_success(self):
# def test_success(self): self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
# self.assertEqual(self.parser.parse("aa"), ["a", "a"]) self.assertEqual(h.h_parse(self.parser, "bb", 2).ast.token_data.seq, ["b", "b"])
# self.assertEqual(self.parser.parse("bb"), ["b", "b"]) def test_failure(self):
# def test_failure(self): self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
# self.assertEqual(self.parser.parse("ab"), None)
class TestAnd1(unittest.TestCase): class TestAnd1(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.and_(h.ch("0")), h.ch("0")) cls.parser = h.h_sequence(h.h_and(h.h_ch("0")), h.h_ch("0"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("0"), ["0"]) self.assertEqual(h.h_parse(self.parser, "0", 1).ast.token_data.seq, ["0"])
def test_failure(self): def test_failure(self):
pass pass
class TestAnd2(unittest.TestCase): class TestAnd2(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.and_(h.ch("0")), h.ch("1")) cls.parser = h.h_sequence(h.h_and(h.h_ch("0")), h.h_ch("1"))
def test_success(self): def test_success(self):
pass pass
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("0"), None) self.assertEqual(h.h_parse(self.parser, "0", 1), None)
class TestAnd3(unittest.TestCase): class TestAnd3(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("1"), h.and_(h.ch("2"))) cls.parser = h.h_sequence(h.h_ch("1"), h.h_and(h.h_ch("2")))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("12"), ["1"]) self.assertEqual(h.h_parse(self.parser, "12", 2).ast.token_data.seq, ["1"])
def test_failure(self): def test_failure(self):
pass pass
class TestNot1(unittest.TestCase): class TestNot1(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.choice(h.ch("+"), h.token("++")), h.ch("b")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++")), h.h_ch("b"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a+b"), ["a", "+", "b"]) self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", "+", "b"])
def test_failure(self): def test_failure(self):
self.assertEqual(self.parser.parse("a++b"), None) self.assertEqual(h.h_parse(self.parser, "a++b", 4), None)
class TestNot2(unittest.TestCase): class TestNot2(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.sequence(h.ch("a"), h.choice(h.sequence(h.ch("+"), h.not_(h.ch("+"))), h.token("++")), h.ch("b")) cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_sequence(h.h_ch("+"), h.h_not(h.h_ch("+"))), h.h_token("++")), h.h_ch("b"))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a+b"), ["a", ["+"], "b"]) self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", ["+"], "b"])
self.assertEqual(self.parser.parse("a++b"), ["a", "++", "b"]) self.assertEqual(h.h_parse(self.parser, "a++b", 4).ast.token_data.seq, ["a", "++", "b"])
def test_failure(self): def test_failure(self):
pass pass
class TestLeftrec(unittest.TestCase): class TestLeftrec(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.indirect() cls.parser = h.h_indirect()
a = h.ch("a") a = h.h_ch("a")
h.bind_indirect(cls.parser, h.choice(h.sequence(cls.parser, a), a)) h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, a), a))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), "a") self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
self.assertEqual(self.parser.parse("aa"), ["a", "a"]) self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
self.assertEqual(self.parser.parse("aaa"), ["a", "a", "a"]) self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", "a", "a"])
def test_failure(self): def test_failure(self):
pass pass
class TestRightrec(unittest.TestCase): class TestRightrec(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.parser = h.indirect() cls.parser = h.h_indirect()
a = h.ch("a") a = h.h_ch("a")
h.bind_indirect(cls.parser, h.choice(h.sequence(a, cls.parser), h.epsilon_p())) h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(a, cls.parser), h.h_epsilon_p()))
def test_success(self): def test_success(self):
self.assertEqual(self.parser.parse("a"), ["a"]) self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
self.assertEqual(self.parser.parse("aa"), ["a", ["a"]]) self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", ["a"]])
self.assertEqual(self.parser.parse("aaa"), ["a", ["a", ["a"]]]) self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", ["a", ["a"]]])
def test_failure(self): def test_failure(self):
pass pass
#class TestAmbiguous(unittest.TestCase): class TestAmbiguous(unittest.TestCase):
# @classmethod @classmethod
# def setUpClass(cls): def setUpClass(cls):
# cls.parser = h.indirect() cls.parser = h.h_indirect()
# d = h.ch("d") d = h.h_ch("d")
# p = h.ch("+") p = h.h_ch("+")
# h.bind_indirect(cls.parser, h.choice(h.sequence(cls.parser, p, cls.parser), d)) h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, p, cls.parser), d))
# # this is supposed to be flattened # this is supposed to be flattened
# def test_success(self): def test_success(self):
# self.assertEqual(self.parser.parse("d"), ["d"]) self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.seq, ["d"])
# self.assertEqual(self.parser.parse("d+d"), ["d", "+", "d"]) self.assertEqual(h.h_parse(self.parser, "d+d", 3).ast.token_data.seq, ["d", "+", "d"])
# self.assertEqual(self.parser.parse("d+d+d"), ["d", "+", "d", "+", "d"]) self.assertEqual(h.h_parse(self.parser, "d+d+d", 5).ast.token_data.seq, ["d", "+", "d", "+", "d"])
# def test_failure(self): def test_failure(self):
# self.assertEqual(self.parser.parse("d+"), None) self.assertEqual(h.h_parse(self.parser, "d+", 2), None)

View file

@ -8,8 +8,11 @@
%typemap(in) uint8_t* { %typemap(in) uint8_t* {
$1 = (uint8_t*)PyString_AsString($input); $1 = (uint8_t*)PyString_AsString($input);
} }
%typemap(out) uint8_t* {
$result = PyString_FromString((char*)$1);
}
#else #else
#warning no "in" typemap defined #warning no uint8_t* typemaps defined
#endif #endif
// All the include paths are relative to the build, i.e., ../../. If you need to build these manually (i.e., not with scons), keep that in mind. // All the include paths are relative to the build, i.e., ../../. If you need to build these manually (i.e., not with scons), keep that in mind.