def test_override_on_inner_field(self): self.assertEqual( 3, ConstantField(0xf1, base_field=IntegerField(length=1, override=3)).override) self.assertEqual( 3, ConstantField(0xf1, base_field=IntegerField(length=1), override=3).override)
def test_based_on_other_field(self): self.assertFieldStreamEqual( b"\xf1", 0xf1, ConstantField(0xf1, base_field=IntegerField(length=1))) with self.assertRaises(WrongMagicError): self.call_field_from_stream( ConstantField(0xf1, base_field=IntegerField(length=1)), b"\xf2") with self.assertRaises(WriteError): self.call_field_to_stream( ConstantField(0xf1, base_field=IntegerField(length=1)), 12)
def test_encode_on_inner_field(self): self.assertFieldStreamEqual( b'\x24', 0x12, ConstantField(0x12, base_field=IntegerField(length=1, encoder=lambda x: x + 0x12, decoder=lambda x: x - 0x12)))
def test_wrong_constant_value(self): with self.assertRaises(DefinitionError): ConstantField(0xf1)
def test_default_is_set(self): self.assertEqual(True, ConstantField(b"hello").has_default) self.assertEqual(b"hello", ConstantField(b"hello").default) self.assertEqual(True, ConstantField(b"hello", default=12).has_default) self.assertEqual(12, ConstantField(b"hello", default=12).default)
def test_wrong_write(self): with self.assertRaises(WriteError): self.call_field_to_stream(ConstantField(b"hello"), b"derp2")
def test_wrong_read(self): with self.assertRaises(WrongMagicError): self.call_field_from_stream(ConstantField(b"hello"), b"derp2")
def test_simple(self): self.assertFieldStreamEqual(b"hello", b"hello", ConstantField(b"hello"))
class ConditionalStructure(Structure): condition = ByteField() value = ConditionalField(ConstantField(1, IntegerField(1)), condition='condition')