File: test_tones.py

package info (click to toggle)
gpiozero 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,060 kB
  • sloc: python: 14,279; makefile: 4
file content (105 lines) | stat: -rw-r--r-- 2,901 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
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
# vim: set fileencoding=utf-8:
#
# GPIO Zero: a library for controlling the Raspberry Pi's GPIO pins
#
# Copyright (c) 2019-2021 Dave Jones <dave@waveform.org.uk>
#
# SPDX-License-Identifier: BSD-3-Clause

from __future__ import (
    unicode_literals,
    absolute_import,
    print_function,
    division,
)
str = type('')

import warnings
from fractions import Fraction

import pytest

from gpiozero.exc import AmbiguousTone
from gpiozero.tones import Tone


@pytest.fixture
def A4(request):
    return Tone.from_frequency(440.0)

def test_tone_init(A4):
    with warnings.catch_warnings(record=True) as w:
        warnings.resetwarnings()
        assert Tone(440) == A4
        assert Tone("A4") == A4
        assert Tone(Fraction(880, 2)) == A4
        assert len(w) == 0
        assert Tone(69) == A4
        assert len(w) == 1
        assert Tone(0) != A4
        assert len(w) == 1
        assert isinstance(w[0].message, AmbiguousTone)
    assert Tone(frequency=440) == A4
    assert Tone(note="A4") == A4
    assert Tone(midi=69) == A4
    with pytest.raises(TypeError):
        Tone()
    with pytest.raises(TypeError):
        Tone(foo=1)
    with pytest.raises(TypeError):
        Tone(frequency=440, midi=69)
    with pytest.raises(TypeError):
        Tone(440, midi=69)

def test_tone_str(A4):
    assert str(A4) == "A4"
    assert str(A4.up()) == "A#4"
    assert str(A4.down(12)) == "A3"
    assert repr(A4) == "<Tone %s midi=69 frequency=440.00Hz>" % ('note=%r' % 'A4')
    assert repr(Tone(13000)) == '<Tone frequency=13000.00Hz>'

def test_tone_from_frequency(A4):
    assert Tone.from_frequency(440) == A4
    assert Tone.from_frequency(440.0) == A4
    with pytest.raises(ValueError):
        Tone.from_frequency(-100.0)
    with pytest.raises(ValueError):
        Tone.from_frequency(30000.0)

def test_tone_from_note(A4):
    assert Tone.from_note(b"A4") == A4
    assert Tone.from_note("A4") == A4
    with pytest.raises(ValueError):
        Tone.from_note("a4")
    with pytest.raises(ValueError):
        Tone.from_note("foo")
    with pytest.raises(ValueError):
        Tone.from_note(0)

def test_tone_from_midi(A4):
    assert Tone.from_midi(69) == A4
    with pytest.raises(ValueError):
        Tone.from_midi(500)
    with pytest.raises(ValueError):
        Tone.from_midi(-1)

def test_tone_frequency(A4):
    assert A4.frequency == 440.0
    assert A4.up(12).frequency == 880.0
    assert A4.down(12).frequency == 220.0

def test_tone_midi(A4):
    assert A4.midi == 69
    assert A4.up().midi == 70
    assert A4.down().midi == 68
    with pytest.raises(ValueError):
        Tone.from_frequency(2).midi
    with pytest.raises(ValueError):
        Tone.from_frequency(15000).midi

def test_tone_note(A4):
    assert A4.note == "A4"
    assert A4.up().note == "A#4"
    assert A4.down().note == "G#4"
    with pytest.raises(ValueError):
        Tone.from_midi(8).note