all repos

anpi @ main

yaml to anki importer

anpi/output_test.go (view raw)

Olexandr Smirnov Olexandr Smirnov
olexsmir@gmail.com
repurpose of the tool, 8 days ago
1
package main
2
3
import (
4
	"bytes"
5
	"strings"
6
	"testing"
7
8
	"olexsmir.xyz/x/is"
9
)
10
11
func TestToTSV_withExample(t *testing.T) {
12
	entry := &Entry{
13
		Word: "skip",
14
		POSBlocks: []POSBlock{
15
			{
16
				POS: "verb",
17
				IPA: "skɪp",
18
				Senses: []Sense{
19
					{
20
						Definition: "to move lightly and quickly",
21
						Examples:   []string{"she loves to skip down the hall"},
22
					},
23
				},
24
			},
25
		},
26
	}
27
	is.Equal(t, "she loves to <b>skip</b> down the hall\tto move lightly and quickly\t/skɪp/", toTSV(entry))
28
}
29
30
func TestToTSV_withoutExample(t *testing.T) {
31
	entry := &Entry{
32
		Word: "hello",
33
		POSBlocks: []POSBlock{
34
			{
35
				POS: "exclamation",
36
				IPA: "/heˈləʊ/",
37
				Senses: []Sense{
38
					{
39
						Definition: "used when meeting someone",
40
					},
41
				},
42
			},
43
		},
44
	}
45
	is.Equal(t, "<b>hello</b>\tused when meeting someone\t/heˈləʊ/", toTSV(entry))
46
47
	is.Equal(t, "", toTSV(&Entry{}))
48
49
	is.Equal(t, "", toTSV(&Entry{POSBlocks: []POSBlock{{}}}))
50
}
51
52
func TestToTSV_ipaWrapping(t *testing.T) {
53
	entry := &Entry{
54
		Word: "test",
55
		POSBlocks: []POSBlock{
56
			{
57
				IPA: "/test/",
58
				Senses: []Sense{
59
					{Definition: "a test"},
60
				},
61
			},
62
		},
63
	}
64
	is.Equal(t, "<b>test</b>\ta test\t/test/", toTSV(entry))
65
}
66
67
func TestToTSV_usIPA(t *testing.T) {
68
	entry := &Entry{
69
		Word: "water",
70
		POSBlocks: []POSBlock{
71
			{
72
				IPA: "ˈwɑː.t̬ɚ",
73
				Senses: []Sense{
74
					{Definition: "a clear liquid"},
75
				},
76
			},
77
		},
78
	}
79
	is.Equal(t, "<b>water</b>\ta clear liquid\t/ˈwɑː.t̬ɚ/", toTSV(entry))
80
}
81
82
func TestBoldWord_caseInsensitive(t *testing.T) {
83
	is.Equal(t, "she loves to <b>skip</b> down", boldWord("she loves to skip down", "skip"))
84
}
85
86
func TestBoldWord_inflectedExample(t *testing.T) {
87
	is.Equal(t, "she <b>skipped</b> down", boldWord("she skipped down", "skip"))
88
}
89
90
func TestBoldWord_mixedCase(t *testing.T) {
91
	is.Equal(t, "<b>Hello</b> there", boldWord("Hello there", "hello"))
92
}
93
94
func TestBoldWord_noMatch(t *testing.T) {
95
	is.Equal(t, "just passing through", boldWord("just passing through", "skip"))
96
}
97
98
func TestBoldWord_notSubstring(t *testing.T) {
99
	is.Equal(t, "<b>skipping</b> class", boldWord("skipping class", "skip"))
100
}
101
102
func TestBoldWord_matchesAllInstances(t *testing.T) {
103
	is.Equal(t, "<b>wait</b> <b>wait</b> <b>wait</b>", boldWord("wait wait wait", "wait"))
104
}
105
106
func TestBoldWord_notSubstringOfLargerWord(t *testing.T) {
107
	is.Equal(t, "<b>waiter</b>", boldWord("waiter", "wait"))
108
}
109
110
func TestBoldWord_inflectedLookup(t *testing.T) {
111
	is.Equal(t, "A suspect has been <b>detained</b> by the police", boldWord("A suspect has been detained by the police", "detaining"))
112
	is.Equal(t, "she <b>skipped</b> down the hall", boldWord("she skipped down the hall", "skipping"))
113
	is.Equal(t, "he <b>passes</b> the ball", boldWord("he passes the ball", "passing"))
114
	is.Equal(t, "she <b>liked</b> it", boldWord("she liked it", "liking"))
115
	is.Equal(t, "he <b>walks</b> fast", boldWord("he walks fast", "walking"))
116
	is.Equal(t, "we <b>loved</b> it", boldWord("we loved it", "loving"))
117
}
118
119
func TestWriteAll(t *testing.T) {
120
	entry := &Entry{
121
		Word: "skip",
122
		POSBlocks: []POSBlock{
123
			{
124
				POS: "verb",
125
				IPA: "skɪp",
126
				Senses: []Sense{
127
					{Definition: "move with quick light steps", Examples: []string{"she skipped down the path", "skip over the puddle"}},
128
					{Definition: "omit or pass over", Examples: []string{"skip the boring parts"}},
129
				},
130
			},
131
			{
132
				POS: "noun",
133
				IPA: "skɪp",
134
				Senses: []Sense{
135
					{Definition: "a light bouncing movement", Examples: []string{"a skip of excitement"}},
136
				},
137
			},
138
		},
139
	}
140
141
	var buf bytes.Buffer
142
	writeAll(entry, &buf)
143
144
	got := buf.String()
145
	is.Equal(t, true, strings.Contains(got, "[verb] /skɪp/"))
146
	is.Equal(t, true, strings.Contains(got, "[noun] /skɪp/"))
147
	is.Equal(t, true, strings.Contains(got, "1. move with quick light steps"))
148
	is.Equal(t, true, strings.Contains(got, "2. omit or pass over"))
149
	is.Equal(t, true, strings.Contains(got, "   - she skipped down the path"))
150
	is.Equal(t, true, strings.Contains(got, "   - skip over the puddle"))
151
	is.Equal(t, true, strings.Contains(got, "   - skip the boring parts"))
152
	is.Equal(t, true, strings.Contains(got, "1. a light bouncing movement"))
153
	is.Equal(t, true, strings.Contains(got, "   - a skip of excitement"))
154
}
155
156
func TestWriteAll_emptyEntry(t *testing.T) {
157
	var buf bytes.Buffer
158
	writeAll(&Entry{}, &buf)
159
	is.Equal(t, "", buf.String())
160
}
161
162
func TestWriteAll_noSenses(t *testing.T) {
163
	var buf bytes.Buffer
164
	writeAll(&Entry{POSBlocks: []POSBlock{{POS: "verb", IPA: "skɪp"}}}, &buf)
165
	is.Equal(t, "[verb] /skɪp/\n", buf.String())
166
}
167
168
func TestWriteAll_separatesBlocksWithBlankLine(t *testing.T) {
169
	entry := &Entry{
170
		Word: "test",
171
		POSBlocks: []POSBlock{
172
			{POS: "verb", IPA: "v", Senses: []Sense{{Definition: "def1"}}},
173
			{POS: "noun", IPA: "n", Senses: []Sense{{Definition: "def2"}}},
174
		},
175
	}
176
	var buf bytes.Buffer
177
	writeAll(entry, &buf)
178
	t.Logf("got:\n%s\n", buf.String())
179
	is.Equal(t, true, strings.Contains(buf.String(), "[verb] /v/\n  1. def1\n\n[noun] /n/"))
180
}