summaryrefslogtreecommitdiff
path: root/tests/glibext/configuration.py
blob: 880b445614e9910a4cde69f6c5842c141d18101e (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

import gi
gi.require_version('Gdk', '3.0')
from gi.repository import Gdk

from chrysacase import ChrysalideTestCase
from pychrysalide.glibext import ConfigParam, GenConfig


class TestConfiguration(ChrysalideTestCase):
    """TestCase for configuration related items.*"""


    def testCfgParamValues(self):
        """Set and unset configuration parameter values."""

        color = Gdk.RGBA()
        color.parse('#3465A4')

        param = ConfigParam('config.color', ConfigParam.ConfigParamType.COLOR, color)

        self.assertEqual(param.value, color)

        param.make_empty()

        void = Gdk.RGBA(red=0, green=0, blue=0, alpha=0)
        self.assertEqual(param.value, void)

        param.value = color

        self.assertEqual(param.value, color)


    def testCfgParamStates(self):
        """Validate all states of an evolving parameter."""

        param = ConfigParam('config.int', ConfigParam.ConfigParamType.INTEGER)

        self.assertEqual(param.state, ConfigParam.ConfigParamState.EMPTY | ConfigParam.ConfigParamState.DEFAULT)

        param.make_empty()

        self.assertEqual(param.state, ConfigParam.ConfigParamState.EMPTY | ConfigParam.ConfigParamState.DEFAULT)

        param = ConfigParam('config.int', ConfigParam.ConfigParamType.INTEGER, 0x123)

        self.assertEqual(param.value, 0x123)

        self.assertEqual(param.state, ConfigParam.ConfigParamState.DEFAULT)

        param.make_empty()

        self.assertEqual(param.state, ConfigParam.ConfigParamState.EMPTY | ConfigParam.ConfigParamState.CHANGED)

        param.value = 0x1

        self.assertEqual(param.state, ConfigParam.ConfigParamState.CHANGED)

        param.reset()

        self.assertEqual(param.state, ConfigParam.ConfigParamState.DEFAULT)


    def testCfgParamDesc(self):
        """Export types and states as strings when needed."""

        param = ConfigParam('config.int', ConfigParam.ConfigParamType.INTEGER)

        self.assertTrue('|' in str(param.state))

        self.assertTrue('.' in str(param.type))


    def testConfiguration(self):
        """Feed and browse a basic configuration."""

        config = GenConfig()
        self.assertIsNotNone(config)
        self.assertIsNone(config.filename)

        for i in range(5):
            param = ConfigParam('config.int.%u' % i, ConfigParam.ConfigParamType.INTEGER, i)
            config.add(param)

        chain = ''

        for p in config.params:
            chain += '%u' % p.value

        self.assertTrue(chain == ''.join([ '%u' % i for i in range(5) ]))

        found = config.search('config.int.3')
        self.assertTrue(found.value == 3)

        found = config.search('config.int.33')
        self.assertIsNone(found)

        for p in config.params:
            p.value *= 10

        chain = ''

        for p in config.params:
            chain += '%u' % p.value

        self.assertTrue(chain == ''.join([ '%u' % (i * 10) for i in range(5) ]))