summaryrefslogtreecommitdiff
path: root/tests/helpers/test_stubs.py
blob: 0b0f61119b7fdaf346e06d25ed6491d97c3bb083 (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
# SPDX-FileCopyrightText: Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Test test stubs."""

from unittest import mock

import pytest


@pytest.fixture
def timer(stubs):
    return stubs.FakeTimer()


def test_timeout(timer):
    """Test whether timeout calls the functions."""
    func = mock.Mock()
    func2 = mock.Mock()
    timer.timeout.connect(func)
    timer.timeout.connect(func2)
    func.assert_not_called()
    func2.assert_not_called()
    timer.timeout.emit()
    func.assert_called_once_with()
    func2.assert_called_once_with()


def test_disconnect_all(timer):
    """Test disconnect without arguments."""
    func = mock.Mock()
    timer.timeout.connect(func)
    timer.timeout.disconnect()
    timer.timeout.emit()
    func.assert_not_called()


def test_disconnect_one(timer):
    """Test disconnect with a single argument."""
    func = mock.Mock()
    timer.timeout.connect(func)
    timer.timeout.disconnect(func)
    timer.timeout.emit()
    func.assert_not_called()


def test_disconnect_all_invalid(timer):
    """Test disconnecting with no connections."""
    with pytest.raises(TypeError):
        timer.timeout.disconnect()


def test_disconnect_one_invalid(timer):
    """Test disconnecting with an invalid connection."""
    func1 = mock.Mock()
    func2 = mock.Mock()
    timer.timeout.connect(func1)
    with pytest.raises(TypeError):
        timer.timeout.disconnect(func2)
    func1.assert_not_called()
    func2.assert_not_called()
    timer.timeout.emit()
    func1.assert_called_once_with()


def test_singleshot(timer):
    """Test setting singleShot."""
    assert not timer.isSingleShot()
    timer.setSingleShot(True)
    assert timer.isSingleShot()
    timer.start()
    assert timer.isActive()
    timer.timeout.emit()
    assert not timer.isActive()


def test_active(timer):
    """Test isActive."""
    assert not timer.isActive()
    timer.start()
    assert timer.isActive()
    timer.stop()
    assert not timer.isActive()


def test_interval(timer):
    """Test setting an interval."""
    assert timer.interval() == 0
    timer.setInterval(1000)
    assert timer.interval() == 1000