2013-11-12 19:07:32 -06:00
|
|
|
import unittest
|
|
|
|
|
import hammer as h
|
|
|
|
|
|
|
|
|
|
class TestTokenParser(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_token("95\xa2")
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "95\xa2"), "95\xa2")
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_partial_fails(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "95"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser_int, "\xa2"), 0xa2)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa2"), ord("\xa2")) # TODO: interface change
|
2013-11-12 19:07:32 -06:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser_int, "\xa3"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser_chr, "\xa3"), 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):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), ord("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00\x00"), -0x200000000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xff\xff\xfe\x00\x00\x00"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00\x00"), -0x20000)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00"), 0x20000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xff\xfe\x00"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xfe\x00"), -0x200)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00"), 0x200)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\xfe"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x88"), -0x78)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00\x00"), 0x200000000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x00\x00\x02\x00\x00\x00"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00\x00"), 0x20000)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x00\x02\x00"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02\x00"), 0x200)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x02"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x78"), 0x78)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x05"), 5)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "\x0b"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), ord("a"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a"), ord("a"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a"), ord("a"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "\ta"), ord("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "_a"), 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):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), None) # empty string
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " "), None) # empty string
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " x"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a "), ord("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " a"), ord("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ba"), 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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, " a "), ord("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " a"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " b "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ba "), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, " ab"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
#@unittest.skip("Action not implemented yet")
|
2013-11-22 19:42:02 -06:00
|
|
|
class TestAction(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-23 16:40:57 -06:00
|
|
|
cls.parser = h.h_action(h.h_sequence__a([h.h_choice__a([h.h_ch("a"), h.h_ch("A")]),
|
|
|
|
|
h.h_choice__a([h.h_ch("b"), h.h_ch("B")])]),
|
|
|
|
|
lambda x: [chr(y).upper() for y in x])
|
2013-11-22 19:42:02 -06:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), ["A", "B"])
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "AB"), ["A", "B"])
|
2013-11-22 19:42:02 -06:00
|
|
|
def test_failure(self):
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "XX"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestIn(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_in("abc")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), ord("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestNotIn(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_not_in("abc")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "d"), ord("d"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestEndP(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_end_p()])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), tuple(ord(y) for y in ["a"]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa"), 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-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSequence(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_ch("b")])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), tuple(map(ord, "ab")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSequenceWhitespace(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), tuple(map(ord,"ab")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a b"), tuple(map(ord,"ab")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a b"), tuple(map(ord,"ab")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a c"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestChoice(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_choice__a([h.h_ch("a"), h.h_ch("b")])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), ord("a"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), ord("b"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "c"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestButNot(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), ord("a"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa"), ord("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), 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):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "4"), ord("4"))
|
2013-11-18 21:14:44 -06:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "6"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestDifference(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_difference(h.h_token("ab"), h.h_ch("a"))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), "ab")
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), 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):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "0"), ord("0"))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "9"), ord("9"))
|
2013-11-18 21:14:44 -06:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "5"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestMany(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_many(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), ())
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), tuple(map(ord, "a")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), tuple(map(ord, "b")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aabbaba"), tuple(map(ord, "aabbaba")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestMany1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), tuple(ord(y) for y in ["a"]))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "b"), tuple(ord(y) for y in ["b"]))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aabbaba"), tuple(ord(y) for y in ["a", "a", "b", "b", "a", "b", "a"]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "daabbabadef"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestRepeatN(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_repeat_n(h.h_choice__a([h.h_ch("a"), h.h_ch("b")]), 2)
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abdef"), (ord('a'), ord('b')))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "adef"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "dabdef"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestOptional(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_optional(h.h_choice__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abd"), (ord('a'),ord('b'),ord('d')))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "acd"), (ord('a'),ord('c'),ord('d')))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ad"), (ord('a'),None,ord('d')))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "aed"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "ac"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestIgnore(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "abc"), tuple(map(ord, "ac")))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ac"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestSepBy(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sepBy(h.h_choice__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,2,3"), tuple(map(ord, "123")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3,2"), tuple(map(ord, "132")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3"), tuple(map(ord, "13")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "3"), (ord('3'),))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), ())
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestSepBy1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sepBy1(h.h_choice__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,2,3"), tuple(map(ord, "123")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3,2"), tuple(map(ord, "132")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "1,3"), tuple(map(ord, "13")))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "3"), (ord('3'),))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, ""), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
2013-11-19 02:41:45 -06:00
|
|
|
### segfaults
|
2013-11-14 15:50:58 +01:00
|
|
|
class TestEpsilonP1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "ab"), tuple(ord(y) for y in ["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-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_epsilon_p(), h.h_ch("a")])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), tuple(ord(y) for y in ["a"]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestEpsilonP3(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_epsilon_p()])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), tuple(ord(y) for y in ["a"]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2013-11-19 02:41:45 -06:00
|
|
|
# class TestAttrBool(unittest.TestCase):
|
|
|
|
|
# @classmethod
|
|
|
|
|
# def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
# cls.parser = h.h_attr_bool(h.h_many1(h.h_choice__a([h.h_ch("a"), h.h_ch("b")])), lambda x: x[0] == x[1])
|
2013-11-19 02:41:45 -06:00
|
|
|
# def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
# self.assertEqual(h.h_parse(self.parser, "aa"), ["a", "a"])
|
|
|
|
|
# self.assertEqual(h.h_parse(self.parser, "bb"), ["b", "b"])
|
2013-11-19 02:41:45 -06:00
|
|
|
# def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
# self.assertEqual(h.h_parse(self.parser, "ab"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestAnd1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "0"), (0x30,))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestAnd2(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "0"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestAnd3(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "12"), (0x31,))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class TestNot1(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_ch("+"), h.h_token("++")]), h.h_ch("b")])
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a+b"), tuple(ord(y) for y in ["a", "+", "b"]))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a++b"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|
|
|
|
|
class TestNot2(unittest.TestCase):
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
2013-11-22 19:42:02 -06:00
|
|
|
cls.parser = h.h_sequence__a([h.h_ch("a"), h.h_choice__a([h.h_sequence__a([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-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a+b"), (ord('a'), (ord('+'),), ord('b')))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "a++b"), (ord('a'), "++", ord('b')))
|
2013-11-14 15:50:58 +01:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
|
|
|
|
|
2013-11-19 02:41:45 -06:00
|
|
|
# ### this is commented out for packrat in C ...
|
|
|
|
|
# #class TestLeftrec(unittest.TestCase):
|
|
|
|
|
# # @classmethod
|
|
|
|
|
# # def setUpClass(cls):
|
|
|
|
|
# # 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))
|
|
|
|
|
# # def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "a"), "a")
|
|
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "aa"), ["a", "a"])
|
|
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "aaa"), ["a", "a", "a"])
|
2013-11-19 02:41:45 -06:00
|
|
|
# # def test_failure(self):
|
|
|
|
|
# # pass
|
|
|
|
|
|
2013-11-19 19:00:58 -06:00
|
|
|
class TestRightrec(unittest.TestCase):
|
2013-11-19 17:26:01 -06:00
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
cls.parser = h.h_indirect()
|
|
|
|
|
a = h.h_ch("a")
|
2013-11-22 19:42:02 -06:00
|
|
|
h.h_bind_indirect(cls.parser, h.h_choice__a([h.h_sequence__a([a, cls.parser]), h.h_epsilon_p()]))
|
2013-11-19 17:26:01 -06:00
|
|
|
def test_success(self):
|
2013-11-23 16:40:57 -06:00
|
|
|
self.assertEqual(h.h_parse(self.parser, "a"), (ord('a'),))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aa"), (ord('a'), (ord('a'),)))
|
|
|
|
|
self.assertEqual(h.h_parse(self.parser, "aaa"), (ord('a'), (ord('a'), (ord('a'),))))
|
2013-11-19 17:26:01 -06:00
|
|
|
def test_failure(self):
|
|
|
|
|
pass
|
2013-11-19 02:41:45 -06:00
|
|
|
|
|
|
|
|
# ### this is just for GLR
|
|
|
|
|
# #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):
|
2013-11-23 16:40:57 -06:00
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "d"), ["d"])
|
|
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "d+d"), ["d", "+", "d"])
|
|
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "d+d+d"), ["d", "+", "d", "+", "d"])
|
2013-11-19 02:41:45 -06:00
|
|
|
# # def test_failure(self):
|
2013-11-22 19:42:02 -06:00
|
|
|
# # self.assertEqual(h.h_parse(self.parser, "d+"), None)
|
2013-11-14 15:50:58 +01:00
|
|
|
|