-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_test.go
160 lines (145 loc) · 7.11 KB
/
word_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
package neng
import (
"slices"
"testing"
)
// Ensures that NewWord correctly parses the received line and returns an error
// if malformed input is encountered.
func TestNewWord(t *testing.T) {
type testCase struct {
good bool
line string
expected *Word
}
cases := []testCase{
{true, "0word", &Word{FT_REGULAR, nil, "word"}}, // Regular
{true, "1word,f2", &Word{FT_IRREGULAR, &[]string{"f2"}, "word"}}, // Irregular, one form
{true, "1word,f", &Word{FT_IRREGULAR, &[]string{"f"}, "word"}}, // One-letter irregular form
{true, "1word,f,f", &Word{FT_IRREGULAR, &[]string{"f", "f"}, "word"}}, // One-letter irregular forms
{true, "1word,f2,f3", &Word{FT_IRREGULAR, &[]string{"f2", "f3"}, "word"}}, // Irregular, two forms
{true, "1word,f1a b,f2", &Word{FT_IRREGULAR, &[]string{"f1a b", "f2"}, "word"}}, // Multi-word irregular
{true, "2word", &Word{FT_PLURAL_ONLY, nil, "word"}}, // Plural-only
{true, "3word", &Word{FT_SUFFIXED, nil, "word"}}, // Suffixed
{true, "4word", &Word{FT_NON_COMPARABLE, nil, "word"}}, // Non-comparable
{true, "5word", &Word{FT_UNCOUNTABLE, nil, "word"}}, // Uncountable
{false, "6word", nil}, // Error: Type value out of defined range for FormType
{false, "0word,f", nil}, // Error: Non-irregular with one irregular forms
{false, "0word,f,f", nil}, // Error: Non-irregular with two irregular forms
{false, "0word,", nil}, // Error: Non-irregular with comma at the end of the line
{false, "", nil}, // Error: empty line
{false, "0", nil}, // Error: FormType field only, regular
{false, "1", nil}, // Error: FormType field only, irregular
{false, "word", nil}, // Error: no FormType field at the beginning of the line
{false, "1,f1,f2", nil}, // Error: no word
{false, ",f1", nil}, // Error: no FormType, no word, just an irregular form
{false, "1word", nil}, // Error: irregular without irregular forms
{false, "1word,", nil}, // Error: one zero-length irregular form
{false, "1word,,", nil}, // Error: two zero-length irregular forms
{false, "1word,f2,f3,f4", nil}, // Error: too many irregular forms
}
for _, c := range cases {
out, err := NewWord(c.line)
if c.good {
switch true {
case err != nil:
t.Errorf("Failed for case %v: error returned: %v", c, err)
case out.word != c.expected.word:
t.Errorf("Failed for case %v: expected word '%s', got '%s'", c, c.expected.word, out.word)
case out.ft != c.expected.ft:
t.Errorf("Failed for case %v: expected FormType '%d', got '%d'", c, c.expected.ft, out.ft)
case out.ft == FT_IRREGULAR:
if out.irr == nil || !slices.Equal(*out.irr, *c.expected.irr) {
t.Errorf("Failed for case %v: slices are not equal, expected %v, got %v", c, c.expected.irr, out.irr)
}
}
} else {
if err == nil {
t.Errorf("Failed for case %v: no error returned, got %v", c, out)
}
}
}
}
// Tests whether NewWordFromParams enforces the designed limitations.
func TestNewWordFromParams(t *testing.T) {
type testCase struct {
good bool
word string
ft FormType
irr []string
expected *Word
}
w := "word"
cases := []testCase{
{true, w, FT_REGULAR, nil, &Word{FT_REGULAR, nil, w}}, // Regular
{true, w, FT_IRREGULAR, []string{"f1"}, &Word{FT_IRREGULAR, &[]string{"f1"}, w}}, // Irregular, one form
{true, w, FT_IRREGULAR, []string{"f1", "f2"}, &Word{FT_IRREGULAR, &[]string{"f1", "f2"}, w}}, // Irregular, two forms
{true, w, FT_PLURAL_ONLY, nil, &Word{FT_PLURAL_ONLY, nil, w}}, // Plural-only
{true, w, FT_SUFFIXED, nil, &Word{FT_SUFFIXED, nil, w}}, // Suffixed
{true, w, FT_NON_COMPARABLE, nil, &Word{FT_NON_COMPARABLE, nil, w}}, // Non-comparable
{true, w, FT_UNCOUNTABLE, nil, &Word{FT_UNCOUNTABLE, nil, w}}, // Uncountable
{false, w, FT_IRREGULAR, []string{"f1", "f2", "f3"}, &Word{FT_IRREGULAR, &[]string{"f1", "f2", "f3"}, w}}, // Error: too many forms
{false, w, FT_SUFFIXED, []string{"f1"}, &Word{FT_SUFFIXED, nil, w}}, // Error: irregular forms for non-irregular
{false, w, FT_IRREGULAR, []string{}, nil}, // Error: empty slice for irregular
{false, w, FT_IRREGULAR, nil, nil}, // Error: nil slice for irregular
{false, "", FT_REGULAR, nil, nil}, // Error: empty word
{false, w, 255, nil, nil}, // Error: undefined FormType
{false, w, FT_IRREGULAR, []string{""}, nil}, // Error: first irregular form empty
{false, w, FT_IRREGULAR, []string{w, ""}, nil}, // Error: second irregular form empty
}
for i, c := range cases {
out, err := NewWordFromParams(c.word, c.ft, c.irr)
if c.good {
switch true {
case err != nil:
t.Errorf("Failed for case %d: error returned: %v", i, err)
case out.word != c.expected.word:
t.Errorf("Failed for case %d: expected word '%s', got '%s'", i, c.expected.word, out.word)
case out.ft != c.expected.ft:
t.Errorf("Failed for case %d: expected FormType '%d', got '%d'", i, c.expected.ft, out.ft)
case out.ft == FT_IRREGULAR:
if out.irr == nil || !slices.Equal(*out.irr, *c.expected.irr) {
t.Errorf("Failed for case %d: slices are not equal, expected %v, got %v", i, c.expected.irr, out.irr)
}
case out.ft != FT_IRREGULAR:
if out.irr != nil {
t.Errorf("Failed for case %d: irregular slice assigned to non-irregular", i)
}
}
} else {
if err == nil {
t.Errorf("Failed for case %d: no error returned, got %v", i, out)
}
}
}
}
// Ensures that the Word.Irr method provides safety when working with
// irregular forms of the Word.
func TestWord_Irr(t *testing.T) {
type testCase struct {
good bool
word *Word
index int
expected string
}
regular, _ := NewWord("0word")
irregular, _ := NewWord("1good,better,best")
cases := []testCase{
{true, irregular, 0, "better"},
{true, irregular, 1, "best"},
{false, irregular, -1, ""},
{false, irregular, 2, ""},
{false, regular, 0, ""},
}
for i, c := range cases {
output, err := c.word.Irr(c.index)
if c.good {
if err != nil {
t.Errorf("Failed for case %d - error returned: %v", i, err)
} else if output != c.expected {
t.Errorf("Failed for case %d - expected %s, got %s", i, c.expected, output)
}
} else if err == nil {
t.Errorf("Failed for case %d - error not returned. Got: %s", i, output)
}
}
}