Ejemplo n.º 1
0
    def test_unpacking(self, value):
        """
        Test that signature unpacking works.
        """
        dbus_value = xformer("v")([value])[0]
        unpacked = signature(dbus_value, unpack=True)
        packed = signature(dbus_value)

        self.assertEqual(packed, "v")
        self.assertFalse(unpacked.startswith("v"))
Ejemplo n.º 2
0
def checked_property(value, sig):
    """
    Check the signature of a property.

    :param value: the property value
    :param str sig: the expected signature of the value
    :returns: value for convenience
    :raises ValueError: if the property type is incorrect
    """
    if signature(value, unpack=True) != sig:
        raise ValueError("property type does not match signature")
    return value
Ejemplo n.º 3
0
def checked_call(value, sig):
    """
    Call a method and check that the returned value has the expected signature.

    :param value: the value to check, an iterable
    :param str sig: the expected signature of the value
    :returns: value, for convenience
    :raises ValueError: if the result type is incorrect
    """
    if "".join(signature(x) for x in value) != sig:
        raise ValueError("result type does not match signature")
    return value
Ejemplo n.º 4
0
    def test_exceptions(self):
        """
        Test that exceptions are properly raised.
        """
        with self.assertRaises(IntoDPSignatureError):
            signature(
                dbus.Array(
                    [dbus.Boolean(False, variant_level=2),
                     dbus.Byte(0)],
                    signature="v"))

        with self.assertRaises(IntoDPSignatureError):
            signature("w")

        with self.assertRaises(IntoDPSignatureError):

            class TestObject:
                """
                A test  object that resembles a dbus-python object in having
                a variant_level field, but isn't actually a dbus-python
                object.
                """

                # pylint: disable=too-few-public-methods
                variant_level = 0

            signature(TestObject())
    def testParsing(self, a_signature):
        """
        Test that parsing is always succesful.

        Verify that the original signature corresponds to the signature
        returned by the parser and to the signature of the generated value.

        Verify that the variant levels always descend within the constructed
        value, always by single steps and that leaves of the value always
        have variant level of 0 or 1.
        """
        base_type_objects = [
           x.example() for x in \
              STRATEGY_GENERATOR.parseString(a_signature, parseAll=True)
        ]
        results = self._PARSER.PARSER.parseString(a_signature, parseAll=True)
        funcs = [f for (f, _) in results]
        sigs = [s for (_, s) in results]

        results = [f(x) for (f, x) in zip(funcs, base_type_objects)]
        values = [v for (v, _) in results]
        levels = [l for (_, l) in results]

        for sig_orig, (sig_synth, (level, value)) in \
           zip(dbus.Signature(a_signature), zip(sigs, zip(levels, values))):
            self.assertEqual(sig_orig, sig_synth)
            if 'v' not in sig_orig:
                self.assertEqual(level, 0)
            self.assertIsNotNone(_descending(value))
            self.assertEqual(signature(value), sig_orig)

        pairs = \
           zip(
              dbus.Signature(a_signature),
              xformer(a_signature)(base_type_objects)
           )
        # test equality of signatures, rather than results, since nan != nan
        for sig, value in pairs:
            self.assertEqual(sig, signature(value))
Ejemplo n.º 6
0
    def test_parsing(self, strat):
        """
        Test that parsing is always succesful.

        Verify that the original signature corresponds to the signature
        returned by the parser and to the signature of the generated value.

        Verify that the variant levels always descend within the constructed
        value.
        """
        (a_signature, base_type_object) = strat

        (func, sig_synth) = xformers(a_signature)[0]
        value = func(base_type_object)
        sig_orig = dbus.Signature(a_signature)

        self.assertEqual(sig_orig, sig_synth)

        self.assertEqual(signature(value), sig_orig)
Ejemplo n.º 7
0
 def test_int32(self, value):
     """
     Test that the signature of an int32 type is "i".
     """
     self.assertEqual(signature(value), "i")
Ejemplo n.º 8
0
 def test_uint32(self, value):
     """
     Test that the signature of a uint32 type is "u".
     """
     self.assertEqual(signature(value), "u")
Ejemplo n.º 9
0
 def test_uint64(self, value):
     """
     Test that the signature of a uint64 type is "t".
     """
     self.assertEqual(signature(value), "t")
Ejemplo n.º 10
0
 def test_string(self, value):
     """
     Test that the signature of a string type is "s".
     """
     self.assertEqual(signature(value), "s")
Ejemplo n.º 11
0
 def test_uint16(self, value):
     """
     Test that the signature of a uint16 type is "q".
     """
     self.assertEqual(signature(value), "q")
Ejemplo n.º 12
0
 def test_int64(self, value):
     """
     Test that the signature of an int64 type is "x".
     """
     self.assertEqual(signature(value), "x")
Ejemplo n.º 13
0
 def test_signature(self, value):
     """
     Test that the signature of a signature type is "g".
     """
     self.assertEqual(signature(value), "g")
Ejemplo n.º 14
0
 def test_object_path(self, value):
     """
     Test that the signature of an object path is "o".
     """
     self.assertEqual(signature(value), "o")
Ejemplo n.º 15
0
 def test_int16(self, value):
     """
     Test that the signature of an int16 type is "n".
     """
     self.assertEqual(signature(value), "n")
Ejemplo n.º 16
0
 def test_double(self, value):
     """
     Test that the signature of a double type is "d".
     """
     self.assertEqual(signature(value), "d")
Ejemplo n.º 17
0
 def test_byte(self, value):
     """
     Test that the signature of a byte type is "y".
     """
     self.assertEqual(signature(value), "y")
Ejemplo n.º 18
0
 def test_boolean(self, value):
     """
     Test that the signature of a boolean type is "b".
     """
     self.assertEqual(signature(value), "b")