~cnx/palace

ref: 708f23b35a7e232273ba0a63a65b3ef778a84540 palace/tests/unit/test_context.py -rw-r--r-- 4.1 KiB
708f23b3 — Ngô Ngọc Đức Huy Write tutorial for source effect 1 year, 8 months ago
                                                                                
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
107
108
109
110
111
112
113
114
115
116
# Context pytest module
# Copyright (C) 2020  Ngô Ngọc Đức Huy
# Copyright (C) 2020  Nguyễn Gia Phong
# Copyright (C) 2020  Ngô Xuân Minh
#
# This file is part of palace.
#
# palace is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# palace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with palace.  If not, see <https://www.gnu.org/licenses/>.

"""This pytest module tries to test the correctness of the class Context."""

from palace import current_context, distance_models, Context, MessageHandler
from pytest import raises

from math import inf


def test_comparison(device):
    """Test basic comparisons."""
    with Context(device) as c0, Context(device) as c1, Context(device) as c2:
        assert c0 != c1
        contexts = [c1, c1, c0, c2]
        contexts.sort()
        contexts.remove(c2)
        contexts.remove(c0)
        assert contexts[0] == contexts[1]


def test_bool(device):
    """Test boolean value."""
    with Context(device) as context: assert context
    assert not context


def test_batch_control(device):
    """Test calls of start_batch and end_batch."""
    with Context(device) as context:
        # At the moment these are no-op.
        context.start_batch()
        context.end_batch()


def test_message_handler(device):
    """Test read-write property MessageHandler."""
    context = Context(device)
    assert type(context.message_handler) is MessageHandler
    message_handler_test = type('MessageHandlerTest', (MessageHandler,), {})()
    context.message_handler = message_handler_test
    assert context.message_handler is message_handler_test
    with context:
        assert current_context().message_handler is context.message_handler


def test_async_wake_interval(device):
    """Test read-write property async_wake_interval."""
    with Context(device) as context:
        context.async_wake_interval = 42
        assert context.async_wake_interval == 42


def test_format_support(device):
    """Test method is_supported."""
    with Context(device) as context:
        assert isinstance(context.is_supported('Rear', '32-bit float'), bool)
        with raises(ValueError): context.is_supported('Shu', 'Mulaw')
        with raises(ValueError): context.is_supported('Stereo', 'Type')


def test_default_resampler_index(device):
    """Test read-only property default_resampler_index."""
    with Context(device) as context:
        index = context.default_resampler_index
        assert index >= 0
        assert len(context.available_resamplers) > index
        with raises(AttributeError): context.available_resamplers = 0


def test_doppler_factor(device):
    """Test write-only property doppler_factor."""
    with Context(device) as context:
        context.doppler_factor = 4/9
        context.doppler_factor = 9/4
        context.doppler_factor = 0
        context.doppler_factor = inf
        with raises(ValueError): context.doppler_factor = -1
        with raises(AttributeError): context.doppler_factor


def test_speed_of_sound(device):
    """Test write-only property speed_of_sound."""
    with Context(device) as context:
        context.speed_of_sound = 5/7
        context.speed_of_sound = 7/5
        with raises(ValueError): context.speed_of_sound = 0
        context.speed_of_sound = inf
        with raises(ValueError): context.speed_of_sound = -1
        with raises(AttributeError): context.speed_of_sound


def test_distance_model(device):
    """Test write-only distance_model."""
    with Context(device) as context:
        for model in distance_models: context.distance_model = model
        with raises(ValueError): context.distance_model = 'EYYYYLMAO'
        with raises(AttributeError): context.distance_model