summaryrefslogtreecommitdiff
path: root/tests/arch/operand.py
blob: e8df8b560d0a36037d8bb359ee072ec18c2ae376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

import pychrysalide

from chrysacase import ChrysalideTestCase
from pychrysalide.arch import ArchOperand
from pychrysalide.glibext import HashableObject, StringBuilder


class TestOperand(ChrysalideTestCase):
    """TestCase for arch.ArchOperand."""


    def testAbstractClass(self):
        """Forbid operand class instance."""

        with self.assertRaisesRegex(RuntimeError, 'pychrysalide.arch.ArchOperand is an abstract class'):
            pc = ArchOperand()


    def testStringBuilderMethodsOverriding(self):
        """Override the StringBuilder interface provided by native implementations."""

        class MyOperand(ArchOperand, StringBuilder):

            def __init__(self):
                super().__init__(self)

            def _to_string(self, flags=0):
                return 'my-op'

        op = MyOperand()

        self.assertEqual(op.to_string(), 'my-op')
        self.assertEqual(str(op), 'my-op')
        self.assertEqual(f'{op}', 'my-op')


    def testHashableObjectMethods(self):
        """Test the HashableObject methods implemantation for operands."""

        # Pas d'implementation de particulière de HashableObject,
        # c'est donc la définition d'implémentation d'ArchOperand
        # qui s'applique.

        # Spécificité de l'implémentation GLib : hash 32 bits

        class DefaultHashableOperand(ArchOperand):
            pass

        def_op = DefaultHashableOperand()

        h = hash(def_op)

        self.assertEqual(0, h & ~0xffffffff)


        # Définition particulière de l'opérande, sur la base de
        # l'implémentation parente.

        class CustomHashableOperand(ArchOperand, HashableObject):

            def _hash(self):
                h = self.parent_hash()
                h &= ~0xffff
                return h

        cust_op = CustomHashableOperand()

        h = hash(cust_op)

        self.assertEqual(0, h & 0xffff)
        self.assertEqual(0, h & ~0xffffffff)