File: test_singleton.py

package info (click to toggle)
backintime 1.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,632 kB
  • sloc: python: 23,454; sh: 859; makefile: 172; xml: 62
file content (61 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download
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
# SPDX-FileCopyrightText: © 2024 Christian BUHTZ <c.buhtz@posteo.jp>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of the program "Back In time" which is released under GNU
# General Public License v2 (GPLv2).
# See file LICENSE or go to <https://www.gnu.org/licenses/#GPL>.
"""Tests about singleton module."""
# pylint: disable=missing-class-docstring,too-few-public-methods
import unittest
import singleton


class Test(unittest.TestCase):
    class Foo(metaclass=singleton.Singleton):
        def __init__(self):
            self.value = 'Ogawa'

    class Bar(metaclass=singleton.Singleton):
        def __init__(self):
            self.value = 'Naomi'

    def setUp(self):
        # Clean up all instances
        singleton.Singleton._instances = {}  # pylint: disable=protected-access

    def test_twins(self):
        """Identical id and values."""
        a = self.Foo()
        b = self.Foo()

        self.assertEqual(id(a), id(b))
        self.assertEqual(a.value, b.value)

    def test_share_value(self):
        """Modify value"""
        a = self.Foo()
        b = self.Foo()
        a.value = 'foobar'

        self.assertEqual(a.value, 'foobar')
        self.assertEqual(a.value, b.value)

    def test_multi_class(self):
        """Two different singleton classes."""
        a = self.Foo()
        b = self.Foo()
        x = self.Bar()
        y = self.Bar()

        self.assertEqual(id(a), id(b))
        self.assertEqual(id(x), id(y))
        self.assertNotEqual(id(a), id(y))

        self.assertEqual(a.value, 'Ogawa')
        self.assertEqual(x.value, 'Naomi')

        a.value = 'who'
        self.assertEqual(b.value, 'who')
        self.assertEqual(x.value, 'Naomi')
        self.assertEqual(x.value, y.value)