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
|
package main
import (
"testing"
"time"
)
// collect returns the phrases published by one run of presenter.update.
func collect(t *testing.T, p *presenter, b *broker, u string, endpoint bool) []string {
t.Helper()
ch, _, unsub := b.subscribe()
defer unsub()
p.update(u, time.Now().UnixMilli(), endpoint)
var got []string
for {
select {
case s := <-ch:
got = append(got, s.Text)
default:
return got
}
}
}
func TestShortSentenceIsNotCutIntoAStub(t *testing.T) {
b := newBroker()
p := &presenter{max: 9, preview: 3, b: b}
// 10 chars with a full stop: used to end up on screen as "了".
got := collect(t, p, b, "我自学中文已经五年了。", true)
want := []string{"我自学中文已经五年了"}
if len(got) != 1 || got[0] != want[0] {
t.Fatalf("got %q, want %q", got, want)
}
}
func TestLongRunIsCutButNeverLeavesAStub(t *testing.T) {
b := newBroker()
p := &presenter{max: 9, preview: 3, b: b}
u := "今天我想跟大家讲一讲我自己学习中文的一些经历"
got := collect(t, p, b, u, false)
if len(got) == 0 {
t.Fatal("nothing published")
}
last := []rune(got[len(got)-1])
if len(last) < 3 {
t.Fatalf("last phrase %q is a stub (all: %q)", string(last), got)
}
// Slides stay around the requested size (last one may be the shorter tail).
for i, s := range got {
n := len([]rune(s))
if i < len(got)-1 && (n < 3 || n > 9) {
t.Fatalf("phrase %d = %q (%d runes), want 3..9", i, s, n)
}
}
}
func TestPunctuationBreaksSlides(t *testing.T) {
b := newBroker()
p := &presenter{max: 9, preview: 3, b: b}
got := collect(t, p, b, "第一,第二,第三。", true)
want := []string{"第一", "第二", "第三"}
if len(got) != len(want) {
t.Fatalf("got %q, want %q", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("got %q, want %q", got, want)
}
}
}
func TestEnglishLineIsStickyAcrossPhrases(t *testing.T) {
b := newBroker()
p := &presenter{max: 9, preview: 3, b: b}
collect(t, p, b, "第一", false)
b.publishEN("First")
// The next phrase is a different sentence, but the last translation stays up
// until its replacement arrives.
got := []Slide{}
ch, _, unsub := b.subscribe()
defer unsub()
p.update("第二,", time.Now().UnixMilli(), false)
for {
select {
case s := <-ch:
got = append(got, s)
default:
if len(got) == 0 {
t.Fatal("nothing published")
}
if got[len(got)-1].EN != "First" {
t.Fatalf("EN = %q, want the previous translation to persist", got[len(got)-1].EN)
}
return
}
}
}
func TestColourAlternatesPerSentence(t *testing.T) {
b := newBroker()
p := &presenter{max: 9, preview: 3, b: b}
first := []Slide{}
ch, _, unsub := b.subscribe()
p.update("你好啊", time.Now().UnixMilli(), true) // sentence ends -> colour flips
for {
select {
case s := <-ch:
first = append(first, s)
default:
goto next
}
}
next:
unsub()
ch2, _, unsub2 := b.subscribe()
defer unsub2()
p.update("再见了", time.Now().UnixMilli(), false)
for {
select {
case s := <-ch2:
if !s.Invert {
t.Fatalf("second sentence should be white on black, got %+v", s)
}
return
default:
time.Sleep(5 * time.Millisecond)
}
}
}
func TestCutBlanksTheScreen(t *testing.T) {
b := newBroker()
b.publish("你好", time.Now().UnixMilli())
ch, _, unsub := b.subscribe()
defer unsub()
b.cutNow()
s := <-ch
if !s.Cut || s.Text != "" || s.EN != "" {
t.Fatalf("cut slide = %+v, want empty red screen", s)
}
// The next phrase leaves the red screen behind.
p := &presenter{max: 9, preview: 3, b: b}
p.update("重来说吧", time.Now().UnixMilli(), false)
for {
select {
case s := <-ch:
if s.Cut {
t.Fatalf("still red after new phrase: %+v", s)
}
return
default:
time.Sleep(5 * time.Millisecond)
}
}
}
func TestContextWindowPrefersClauseBoundary(t *testing.T) {
got := contextWindow("第一句话在这里,第二句话在这里,第三句话", 12)
if []rune(got)[0] == ',' {
t.Fatalf("window starts on punctuation: %q", got)
}
if n := len([]rune(got)); n > 12 {
t.Fatalf("window %q is %d runes, want <= 12", got, n)
}
if got != "第三句话" {
t.Logf("window = %q", got)
}
}
|