summaryrefslogtreecommitdiff
path: root/main_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'main_test.go')
-rw-r--r--main_test.go172
1 files changed, 172 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go
new file mode 100644
index 0000000..09ce825
--- /dev/null
+++ b/main_test.go
@@ -0,0 +1,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)
+ }
+}