blob: 1a74905730e6dd72e7913781ba7de45a101e29d4 [file] [log] [blame]
Akrone562ad62025-06-25 11:15:56 +02001package parser
2
3import (
4 "testing"
5
6 "github.com/KorAP/KoralPipe-TermMapper/ast"
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9)
10
Akron6b4c9eb2025-07-03 14:31:58 +020011// TestTitleAttributeParser_ParseTitleAttribute was removed as ParseTitleAttribute is no longer exported
12// The functionality is now only available through ParseTitleAttributesToTerms method
Akrone562ad62025-06-25 11:15:56 +020013
14func TestTitleAttributeParser_ParseTitleAttributesToTerms(t *testing.T) {
15 parser := NewTitleAttributeParser()
16
17 tests := []struct {
18 name string
19 input []string
20 expected []ast.Node
21 wantErr bool
22 }{
23 {
24 name: "Parse multiple title attributes",
Akron7de5b612025-06-26 16:15:03 +020025 input: []string{"corenlp/p:ART", "marmot/m:case:nom", "tt/l:die"},
Akrone562ad62025-06-25 11:15:56 +020026 expected: []ast.Node{
27 &ast.Term{
28 Foundry: "corenlp",
29 Layer: "p",
30 Key: "ART",
31 Value: "",
32 Match: ast.MatchEqual,
33 },
34 &ast.Term{
35 Foundry: "marmot",
36 Layer: "m",
37 Key: "case",
38 Value: "nom",
39 Match: ast.MatchEqual,
40 },
41 &ast.Term{
42 Foundry: "tt",
43 Layer: "l",
44 Key: "die",
45 Value: "",
46 Match: ast.MatchEqual,
47 },
48 },
49 wantErr: false,
50 },
51 {
52 name: "Empty input should return empty slice",
53 input: []string{},
54 expected: []ast.Node{},
55 wantErr: false,
56 },
57 {
58 name: "Invalid title should cause error",
59 input: []string{"corenlp/p:ART", "invalid_title", "tt/l:die"},
60 wantErr: true,
61 },
Akron6b4c9eb2025-07-03 14:31:58 +020062 // Additional tests to cover functionality from removed TestTitleAttributeParser_ParseTitleAttribute
63 {
64 name: "Parse simple title with key only",
65 input: []string{"corenlp/p:ART"},
66 expected: []ast.Node{
67 &ast.Term{
68 Foundry: "corenlp",
69 Layer: "p",
70 Key: "ART",
71 Value: "",
72 Match: ast.MatchEqual,
73 },
74 },
75 wantErr: false,
76 },
77 {
78 name: "Parse title with key and value",
79 input: []string{"marmot/m:case:nom"},
80 expected: []ast.Node{
81 &ast.Term{
82 Foundry: "marmot",
83 Layer: "m",
84 Key: "case",
85 Value: "nom",
86 Match: ast.MatchEqual,
87 },
88 },
89 wantErr: false,
90 },
91 {
92 name: "Parse title with colon separator for value",
93 input: []string{"marmot/m:gender:masc"},
94 expected: []ast.Node{
95 &ast.Term{
96 Foundry: "marmot",
97 Layer: "m",
98 Key: "gender",
99 Value: "masc",
100 Match: ast.MatchEqual,
101 },
102 },
103 wantErr: false,
104 },
105 {
106 name: "Parse title with equals separator for value",
107 input: []string{"marmot/m:degree:pos"},
108 expected: []ast.Node{
109 &ast.Term{
110 Foundry: "marmot",
111 Layer: "m",
112 Key: "degree",
113 Value: "pos",
114 Match: ast.MatchEqual,
115 },
116 },
117 wantErr: false,
118 },
119 {
120 name: "Parse title with lemma layer",
121 input: []string{"tt/l:die"},
122 expected: []ast.Node{
123 &ast.Term{
124 Foundry: "tt",
125 Layer: "l",
126 Key: "die",
127 Value: "",
128 Match: ast.MatchEqual,
129 },
130 },
131 wantErr: false,
132 },
133 {
134 name: "Parse title with special characters in value",
135 input: []string{"tt/l:@card@"},
136 expected: []ast.Node{
137 &ast.Term{
138 Foundry: "tt",
139 Layer: "l",
140 Key: "@card@",
141 Value: "",
142 Match: ast.MatchEqual,
143 },
144 },
145 wantErr: false,
146 },
147 {
148 name: "Parse complex key-value with colon",
149 input: []string{"opennlp/p:PronType:Ind"},
150 expected: []ast.Node{
151 &ast.Term{
152 Foundry: "opennlp",
153 Layer: "p",
154 Key: "PronType",
155 Value: "Ind",
156 Match: ast.MatchEqual,
157 },
158 },
159 wantErr: false,
160 },
161 {
162 name: "Parse complex key-value with equals",
163 input: []string{"opennlp/p:AdjType:Pdt"},
164 expected: []ast.Node{
165 &ast.Term{
166 Foundry: "opennlp",
167 Layer: "p",
168 Key: "AdjType",
169 Value: "Pdt",
170 Match: ast.MatchEqual,
171 },
172 },
173 wantErr: false,
174 },
175 {
176 name: "Parse complex nested pattern",
177 input: []string{"stts/p:ADJA"},
178 expected: []ast.Node{
179 &ast.Term{
180 Foundry: "stts",
181 Layer: "p",
182 Key: "ADJA",
183 Value: "",
184 Match: ast.MatchEqual,
185 },
186 },
187 wantErr: false,
188 },
189 // Error cases
190 {
191 name: "Empty title should fail",
192 input: []string{""},
193 wantErr: true,
194 },
195 {
196 name: "Missing foundry separator should fail",
197 input: []string{"corenlp_p:ART"},
198 wantErr: true,
199 },
200 {
201 name: "Missing layer separator should fail",
202 input: []string{"corenlp/p_ART"},
203 wantErr: true,
204 },
205 {
206 name: "Only foundry should fail",
207 input: []string{"corenlp"},
208 wantErr: true,
209 },
210 {
211 name: "Only foundry and layer should fail",
212 input: []string{"corenlp/p"},
213 wantErr: true,
214 },
215 {
216 name: "Missing key should fail",
217 input: []string{"corenlp/p:"},
218 wantErr: true,
219 },
Akrone562ad62025-06-25 11:15:56 +0200220 }
221
222 for _, tt := range tests {
223 t.Run(tt.name, func(t *testing.T) {
224 result, err := parser.ParseTitleAttributesToTerms(tt.input)
225
226 if tt.wantErr {
227 assert.Error(t, err)
228 } else {
229 require.NoError(t, err)
230 require.Len(t, result, len(tt.expected))
231
232 for i, expectedTerm := range tt.expected {
233 expectedTermNode := expectedTerm.(*ast.Term)
234 actualTermNode := result[i].(*ast.Term)
235
236 assert.Equal(t, expectedTermNode.Foundry, actualTermNode.Foundry)
237 assert.Equal(t, expectedTermNode.Layer, actualTermNode.Layer)
238 assert.Equal(t, expectedTermNode.Key, actualTermNode.Key)
239 assert.Equal(t, expectedTermNode.Value, actualTermNode.Value)
240 assert.Equal(t, expectedTermNode.Match, actualTermNode.Match)
241 }
242 }
243 })
244 }
245}
246
Akron6b4c9eb2025-07-03 14:31:58 +0200247// TestTitleAttribute_ToAST was removed as ToAST method is no longer available
248// TestTitleAttribute_String was removed as String method is no longer available
249// TestTitleAttributeParser_RealWorldExample was removed as it used the removed methods