-
Notifications
You must be signed in to change notification settings - Fork 166
/
test_main.py
117 lines (90 loc) · 3.93 KB
/
test_main.py
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
117
# Copyright 2015-2019 Thomas Schatz, Xuan Nga Cao, Mathieu Bernard
#
# This file is part of phonemizer: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Phonemizer 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with phonemizer. If not, see <http://www.gnu.org/licenses/>.
"""Test of the command line interface"""
import pytest
import tempfile
import shlex
import sys
from phonemizer import main, backend, logger
def _test(input, expected_output, args=''):
with tempfile.NamedTemporaryFile('w') as finput:
# python2 needs additional utf8 encoding
if sys.version_info[0] == 2:
input = input.encode('utf8')
finput.write(input)
finput.seek(0)
with tempfile.NamedTemporaryFile('w+') as foutput:
opts = '{} -o {} {}'.format(finput.name, foutput.name, args)
sys.argv = ['foo'] + shlex.split(opts)
main.main()
output = foutput.read()
# python2 needs additional utf8 decoding
if sys.version_info[0] == 2:
output = output.decode('utf8')
if expected_output == '':
assert output == ''
else:
assert output == expected_output + '\n'
def test_help():
sys.argv = ['foo', '-h']
with pytest.raises(SystemExit):
main.main()
def test_version():
sys.argv = ['foo', '--version']
main.main()
def test_readme():
_test(u'hello world', u'həloʊ wɜːld ')
_test(u'hello world', u'həloʊ wɜːld ', '--verbose')
_test(u'hello world', u'həloʊ wɜːld ', '--quiet')
if backend.EspeakBackend.is_espeak_ng():
_test(u'hello world', u'h@loU w3:ld ', '--sampa')
else: # sampa only supported by espeak-ng
with pytest.raises(SystemExit):
_test(u'hello world', u'h@loU w3:ld ', '--sampa')
_test(u'hello world', u'hhaxlow werld', '-b festival --strip')
_test(u'hello world', u'həloʊ wɜːld ', '-l en-us')
_test(u'bonjour le monde', u'bɔ̃ʒuʁ lə- mɔ̃d ', '-l fr-fr')
_test(u'bonjour le monde', u'b ɔ̃ ʒ u ʁ ;eword l ə- ;eword m ɔ̃ d ;eword ',
'-l fr-fr -p " " -w ";eword "')
@pytest.mark.skipif(
'2.1' in backend.FestivalBackend.version(),
reason='festival-2.1 gives different results than further versions '
'for syllable boundaries')
def test_readme_festival_syll():
_test(u'hello world',
u'hh ax ;esyll l ow ;esyll ;eword w er l d ;esyll ;eword ',
u"-p ' ' -s ';esyll ' -w ';eword ' -b festival -l en-us")
def test_njobs():
for njobs in range(1, 6):
_test(
u'hello world\ngoodbye\nthird line\nyet another',
u'h-ə-l-oʊ w-ɜː-l-d\nɡ-ʊ-d-b-aɪ\nθ-ɜː-d l-aɪ-n\nj-ɛ-t ɐ-n-ʌ-ð-ɚ',
u'--strip -j {} -l en-us -b espeak -p "-" -s "|" -w " "'
.format(njobs))
def test_unicode():
_test(u'untuʼule', u'untṵːle', '-l yucatec -b segments --strip')
_test(u'untuʼule', u'untṵːle ', '-l yucatec -b segments')
def test_language_switch():
_test("j'aime le football", "ʒɛm lə- (en)fʊtbɔːl(fr) ",
'-l fr-fr -b espeak')
_test("j'aime le football", "ʒɛm lə- (en)fʊtbɔːl(fr) ",
'-l fr-fr -b espeak --language-switch keep-flags')
_test("j'aime le football", "ʒɛm lə- fʊtbɔːl ",
'-l fr-fr -b espeak --language-switch remove-flags')
_test("j'aime le football", "",
'-l fr-fr -b espeak --language-switch remove-utterance')
def test_logger():
with pytest.raises(RuntimeError):
logger.get_logger(verbosity=1)