-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_test.go
225 lines (207 loc) · 4.54 KB
/
engine_test.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package picogo
import (
"errors"
"runtime"
"testing"
"time"
)
const (
langDirInternal = "../picopi/pico/lang/"
)
func Test_NewEngine(t *testing.T) {
tests := []struct {
name string
lang, dir string
wantErr error
}{
{
name: "Check no language directory",
lang: LangDefault,
dir: "./not-existed",
wantErr: ErrBadDirectory,
},
{
name: "Check en-GB is supported",
lang: "en-GB",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check en-US is supported",
lang: "en-US",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check de-DE is supported",
lang: "de-DE",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check es-ES is supported",
lang: "es-ES",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check fr-FR is supported",
lang: "fr-FR",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check it-IT is supported",
lang: "it-IT",
dir: langDirInternal,
wantErr: nil,
},
{
name: "Check zu-ZU is not supported",
lang: "zu-ZU",
dir: langDirInternal,
wantErr: ErrBadLanguage,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e, err := NewEngine(tt.lang, tt.dir)
if err != nil {
if tt.wantErr != errors.Unwrap(err) {
t.Errorf("want %v error, but got %v", tt.wantErr, err)
}
} else if e == nil {
t.Errorf("e == nil")
} else if e.tts == nil {
t.Errorf("e.tts == nil")
}
})
}
// wait finalizer to be called
runtime.GC()
time.Sleep(400 * time.Millisecond)
}
func Test_Speak(t *testing.T) {
tests := []struct {
lang string
text string
want int
}{
{
lang: "en-US",
text: "Hello",
want: 20224,
},
{
lang: "en-GB",
text: "Hello",
want: 19968,
},
{
lang: "en-GB",
text: ".............................................................",
want: 1085312,
},
}
for _, tt := range tests {
t.Run(tt.lang, func(t *testing.T) {
e := newTestEngine(tt.lang)
pcm, err := e.Speak(tt.text)
if err != nil {
t.Fatalf("error Speak: %s", err)
}
if len(pcm) != tt.want {
t.Fatalf("want %d, got %d length", tt.want, len(pcm))
}
})
}
}
func newTestEngine(lang string) *Engine {
e, err := NewEngine(lang, langDirInternal)
if err != nil {
panic(err)
}
return e
}
func Test_Rate(t *testing.T) {
e := newTestEngine("en-US")
if e.Rate() != RateDefault {
t.Errorf("e.Rate() != RateDefault: %d", e.Rate())
}
e.SetRate(RateMin)
if e.Rate() != RateMin {
t.Errorf("e.Rate() != RateMin: %d", e.Rate())
}
e.SetRate(RateMax)
if e.Rate() != RateMax {
t.Errorf("e.Rate() != RateMax: %d", e.Rate())
}
}
func Test_Pitch(t *testing.T) {
e := newTestEngine("en-US")
if e.Pitch() != PitchDefault {
t.Errorf("e.Pitch() != PitchDefault: %d", e.Pitch())
}
e.SetPitch(PitchMin)
if e.Pitch() != PitchMin {
t.Errorf("e.Pitch() != PitchMin: %d", e.Pitch())
}
e.SetPitch(PitchMax)
if e.Pitch() != PitchMax {
t.Errorf("e.Pitch() != PitchMax: %d", e.Pitch())
}
}
func Test_Volume(t *testing.T) {
e := newTestEngine("en-US")
if e.Volume() != VolumeDefault {
t.Errorf("e.Volume() != VolumeDefault: %d", e.Volume())
}
e.SetVolume(VolumeMin)
if e.Volume() != VolumeMin {
t.Errorf("e.Volume() != VolumeMin: %d", e.Volume())
}
e.SetVolume(VolumeMax)
if e.Volume() != VolumeMax {
t.Errorf("e.Volume() != VolumeMax: %d", e.Volume())
}
}
func Test_AbortSpeakCB(t *testing.T) {
e := newTestEngine("it-IT")
// text must be large enough to callback three times:
// 1st call: abort synth
// 2nd call: receive remaining buffer
// 3d call: not happen
text := "Hello World, abort the engine, text must be larger enough to callback three times"
var i int
err := e.SpeakCB(text, func(pcm []byte, final bool) bool {
i++
return true
})
if i < 3 {
t.Fatalf("text is not large enough to continue: %d", i)
}
if err != nil {
t.Fatalf("[1] e.SpeakCB: %v", err)
}
i = 0
err = e.SpeakCB(text, func(pcm []byte, final bool) bool {
i++
return false
})
if i != 2 {
t.Errorf("e.SpeakCB is not stopped by callback return value: %d", i)
} else if errors.Unwrap(err) != ErrSpeak {
t.Errorf("e.SpeakCB stopped by callback returns unexpected error value: %v", err)
}
i = 0
err = e.SpeakCB(text, func(pcm []byte, final bool) bool {
i++
e.Abort()
return true
})
if i != 2 {
t.Errorf("e.SpeakCB is not stopped by Abort() call: %d", i)
} else if errors.Unwrap(err) != ErrSpeak {
t.Errorf("e.SpeakCB stopped by Abort() call returns unexpected error value: %v", err)
}
}