2013-11-12 19:07:32 -06:00
|
|
|
import unittest
|
|
|
|
|
import hammer as h
|
|
|
|
|
|
|
|
|
|
class TestTokenParser(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_token("95\xa2", 3)
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "95\xa2", 3).ast.token_data.bytes.token, "95\xa2")
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_partial_fails(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "95", 2), None)
|
2013-11-12 19:07:32 -06:00
|
|
|
|
|
|
|
|
class TestChParser(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser_int = h.h_ch(0xa2)
|
|
|
|
|
cls.parser_chr = h.h_ch("\xa2")
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser_int, "\xa2", 1).ast.token_data.uint, 0xa2)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa2", 1).ast.token_data.bytes, "\xa2")
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser_int, "\xa3", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa3", 1), None)
|
2013-11-12 19:07:32 -06:00
|
|
|
|
|
|
|
|
class TestChRange(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_ch_range("a", "c")
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestInt64(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_int64()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00\x00", 8).ast.token_data.sint, -0x200000000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00", 7), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestInt32(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_int32()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00\x00", 4).ast.token_data.sint, -0x20000)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.sint, 0x20000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00", 3), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestInt16(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_int16()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xfe\x00", 2).ast.token_data.sint, -0x200)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.sint, 0x200)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xfe", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestInt8(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_int8()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x88", 1).ast.token_data.sint, -0x78)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestUint64(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_uint64()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00\x00", 8).ast.token_data.uint, 0x200000000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00", 7), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestUint32(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_uint32()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00", 4).ast.token_data.uint, 0x20000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00", 3), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestUint16(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_uint16()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00", 2).ast.token_data.uint, 0x200)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestUint8(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_uint8()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x78", 1).ast.token_data.uint, 0x78)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
2013-11-12 19:07:32 -06:00
|
|
|
|
2013-11-14 15:50:58 +01:00
|
|
|
class TestIntRange(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_int_range(h.h_uint8(), 3, 10)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x05", 1).ast.token_data.uint, 5)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x0b", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestWhitespace(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_whitespace(h.h_ch("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.bytes, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a", 3).ast.token_data.bytes, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\ta", 2).ast.token_data.bytes, "a")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "_a", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestWhitespaceEnd(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_whitespace(h.h_end_p())
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0).ast, None) # empty string
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ", 2).ast, None) # empty string
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " x", 3), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestLeft(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_left(h.h_ch("a"), h.h_ch(" "))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a ", 2).ast.token_data.bytes, "a")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestRight(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_right(h.h_ch(" "), h.h_ch("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " a", 2).ast.token_data.bytes, "a")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ba", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestMiddle(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_middle(h.h_ch(" "), h.h_ch("a"), h.h_ch(" "))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " a ", 3).ast.token_data.bytes, "a")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a", 2), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a ", 2), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " b ", 3), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ba ", 3), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ab", 3), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestAction(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
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])
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["A", "B"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "AB", 2).ast.token_data.seq, ["A", "B"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "XX", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestIn(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_in("abc", 3)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b") # segfaulting when looking at bytes!
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestNotIn(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_not_in("abc", 3)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.bytes, "d") # segfaulting when looking at bytes!
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestEndP(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_end_p())
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestNothingP(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_nothing_p()
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
|
|
|
|
pass
|
|
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSequence(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_ch("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab").ast.token_data.seq, ["a", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSequenceWhitespace(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_whitespace(h.h_ch("b")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["a", "b"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a b", 3).ast.token_data.seq, ["a", "b"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a b", 4).ast.token_data.seq, ["a", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a c", 4), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestChoice(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_choice(h.h_ch("a"), h.h_ch("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.uint, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.bytes, "b")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "c", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestButNot(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_butnot(h.h_ch("a"), h.h_token("ab"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.bytes, "a")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
2013-11-18 21:14:44 -06:00
|
|
|
class TestButNotRange(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
cls.parser = h.h_butnot(h.h_ch_range("0", "9"), h.h_ch("6"))
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "4", 1).ast.token_data.bytes, "4")
|
|
|
|
|
def test_failure(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "6", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestDifference(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_difference(h.h_token("ab", 2), h.h_ch("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.bytes, "ab")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
2013-11-18 21:14:44 -06:00
|
|
|
class TestXor(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
cls.parser = h.h_xor(h.h_ch_range("0", "6"), h.h_ch_range("5", "9"))
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "0", 1).ast.token_data.bytes, "0")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "9", 1).ast.token_data.bytes, "9")
|
|
|
|
|
def test_failure(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "5", 1), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestMany(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_many(h.h_choice(h.h_ch("a"), h.h_ch("b")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.seq, ["b"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq, ["a", "a", "b", "b", "a", "b", "a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestMany1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_many1(h.h_choice(h.h_ch("a"), h.h_ch("b")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b", 1).ast.token_data.seq, ["b"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aabbaba", 7).ast.token_data.seq, ["a", "a", "b", "b", "a", "b", "a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "daabbabadef", 11), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestRepeatN(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_repeat_n(h.h_choice(h.h_ch("a"), h.h_ch("b")), 2)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abdef", 5).ast.token_data.seq, ["a", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "adef", 4), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "dabdef", 5), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestOptional(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
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"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abd", 3).ast.token_data.seq, ["a", "b", "d"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "acd", 3).ast.token_data.seq, ["a", "c", "d"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ad", 2).ast.token_data.seq, ["a", None, "d"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "aed", 3), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestIgnore(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_ignore(h.h_ch("b")), h.h_ch("c"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abc", 3).ast.token_data.seq, ["a", "c"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ac", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSepBy(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sepBy(h.h_choice(h.h_ch("1"), h.h_ch("2"), h.h_ch("3")), h.h_ch(","))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq, ["1", "2", "3"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq, ["1", "3", "2"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3", 3).ast.token_data.seq, ["1", "3"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "3", 1).ast.token_data.seq, ["3"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0).ast.token_data.seq, [])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestSepBy1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sepBy1(h.h_choice(h.h_ch("1"), h.h_ch("2"), h.h_ch("3")), h.h_ch(","))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,2,3", 5).ast.token_data.seq, ["1", "2", "3"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3,2", 5).ast.token_data.seq, ["1", "3", "2"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3", 3).ast.token_data.seq, ["1", "3"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "3", 1).ast.token_data.seq, ["3"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "", 0), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestEpsilonP1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_epsilon_p(), h.h_ch("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2).ast.token_data.seq, ["a", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestEpsilonP2(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_epsilon_p(), h.h_ch("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestEpsilonP3(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_epsilon_p())
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2013-11-18 21:14:44 -06:00
|
|
|
class TestAttrBool(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
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])
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "bb", 2).ast.token_data.seq, ["b", "b"])
|
|
|
|
|
def test_failure(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestAnd1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_and(h.h_ch("0")), h.h_ch("0"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "0", 1).ast.token_data.seq, ["0"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestAnd2(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_and(h.h_ch("0")), h.h_ch("1"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
|
|
|
|
pass
|
|
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "0", 1), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestAnd3(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("1"), h.h_and(h.h_ch("2")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "12", 2).ast.token_data.seq, ["1"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestNot1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_sequence(h.h_ch("a"), h.h_choice(h.h_ch("+"), h.h_token("++")), h.h_ch("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", "+", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a++b", 4), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestNot2(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
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"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a+b", 3).ast.token_data.seq, ["a", ["+"], "b"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a++b", 4).ast.token_data.seq, ["a", "++", "b"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestLeftrec(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_indirect()
|
|
|
|
|
a = h.h_ch("a")
|
|
|
|
|
h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, a), a))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.bytes, "a")
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", "a"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", "a", "a"])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestRightrec(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-18 21:14:44 -06:00
|
|
|
cls.parser = h.h_indirect()
|
|
|
|
|
a = h.h_ch("a")
|
|
|
|
|
h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(a, cls.parser), h.h_epsilon_p()))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-18 21:14:44 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a", 1).ast.token_data.seq, ["a"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa", 2).ast.token_data.seq, ["a", ["a"]])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aaa", 3).ast.token_data.seq, ["a", ["a", ["a"]]])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2013-11-18 21:14:44 -06:00
|
|
|
class TestAmbiguous(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
cls.parser = h.h_indirect()
|
|
|
|
|
d = h.h_ch("d")
|
|
|
|
|
p = h.h_ch("+")
|
|
|
|
|
h.h_bind_indirect(cls.parser, h.h_choice(h.h_sequence(cls.parser, p, cls.parser), d))
|
|
|
|
|
# this is supposed to be flattened
|
|
|
|
|
def test_success(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "d", 1).ast.token_data.seq, ["d"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "d+d", 3).ast.token_data.seq, ["d", "+", "d"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "d+d+d", 5).ast.token_data.seq, ["d", "+", "d", "+", "d"])
|
|
|
|
|
def test_failure(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "d+", 2), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|