class TestMetaField(unittest.TestCase): def setUp(self): self.mf = MetaField("metafield", lambda context: 3) def test_trivial(self): pass def test_parse(self): self.assertEqual(self.mf.parse("abc"), "abc") def test_build(self): self.assertEqual(self.mf.build("abc"), "abc") def test_parse_too_short(self): self.assertRaises(FieldError, self.mf.parse, "ab") def test_build_too_short(self): self.assertRaises(FieldError, self.mf.build, "ab") def test_sizeof(self): self.assertEqual(self.mf.sizeof(), 3)
class TestMetaField(unittest.TestCase): def setUp(self): self.mf = MetaField("metafield", lambda context: 3) def test_trivial(self): pass def test_parse(self): self.assertEqual(self.mf.parse(b"abc"), b"abc") def test_build(self): self.assertEqual(self.mf.build(b"abc"), b"abc") def test_parse_too_short(self): self.assertRaises(FieldError, self.mf.parse, b"ab") def test_build_too_short(self): self.assertRaises(FieldError, self.mf.build, b"ab") def test_sizeof(self): self.assertEqual(self.mf.sizeof(), 3)
class TestMetaFieldStruct(unittest.TestCase): def setUp(self): self.mf = MetaField("data", lambda context: context["length"]) self.s = Struct("foo", Byte("length"), self.mf) def test_trivial(self): pass def test_parse(self): c = self.s.parse("\x03ABC") self.assertEqual(c.length, 3) self.assertEqual(c.data, "ABC") c = self.s.parse("\x04ABCD") self.assertEqual(c.length, 4) self.assertEqual(c.data, "ABCD") def test_sizeof_default(self): self.assertRaises(SizeofError, self.mf.sizeof) def test_sizeof(self): context = Container(length=4) self.assertEqual(self.mf.sizeof(context), 4)
class TestMetaFieldStruct(unittest.TestCase): def setUp(self): self.mf = MetaField("data", lambda context: context["length"]) self.s = Struct("foo", Byte("length"), self.mf) def test_trivial(self): pass def test_parse(self): c = self.s.parse(b"\x03ABC") self.assertEqual(c.length, 3) self.assertEqual(c.data, b"ABC") c = self.s.parse(b"\x04ABCD") self.assertEqual(c.length, 4) self.assertEqual(c.data, b"ABCD") def test_sizeof_default(self): self.assertRaises(SizeofError, self.mf.sizeof) def test_sizeof(self): context = Container(length=4) self.assertEqual(self.mf.sizeof(context), 4)