| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/gob" |
| 6 |
"fmt" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-ap/errors" |
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
"golang.org/x/text/language" |
| 14 |
) |
| 15 |
|
| 16 |
func TestNaturalLanguageValue_MarshalJSON(t *testing.T) { |
| 17 |
p := NaturalLanguageValues{ |
| 18 |
English: Content("the test"), |
| 19 |
French: Content("le test"), |
| 20 |
} |
| 21 |
js1 := `{"en":"the test","fr":"le test"}` |
| 22 |
js2 := `{"fr":"le test","en":"the test"}` |
| 23 |
out, err := p.MarshalJSON() |
| 24 |
if err != nil { |
| 25 |
t.Errorf("Error: '%s'", err) |
| 26 |
} |
| 27 |
if js1 != string(out) && js2 != string(out) { |
| 28 |
t.Errorf("Different marshal result '%s', instead of '%s' or '%s'", out, js1, js2) |
| 29 |
} |
| 30 |
p1 := NaturalLanguageValues{ |
| 31 |
English: Content("the test"), |
| 32 |
} |
| 33 |
|
| 34 |
out1, err1 := p1.MarshalJSON() |
| 35 |
|
| 36 |
if err1 != nil { |
| 37 |
t.Errorf("Error: '%s'", err1) |
| 38 |
} |
| 39 |
txt := `"the test"` |
| 40 |
if txt != string(out1) { |
| 41 |
t.Errorf("Different marshal result '%s', instead of '%s'", out1, txt) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestLangRefValue_MarshalJSON(t *testing.T) { |
| 46 |
{ |
| 47 |
tst := LangRefValue{ |
| 48 |
Ref: NilLangRef, |
| 49 |
Value: Content("test"), |
| 50 |
} |
| 51 |
j, err := tst.MarshalJSON() |
| 52 |
if err != nil { |
| 53 |
t.Errorf("Error: %s", err) |
| 54 |
} |
| 55 |
expected := `"test"` |
| 56 |
if string(j) != expected { |
| 57 |
t.Errorf("Different marshal result '%s', expected '%s'", j, expected) |
| 58 |
} |
| 59 |
} |
| 60 |
{ |
| 61 |
tst := LangRefValue{ |
| 62 |
Ref: MakeRef([]byte("en")), |
| 63 |
Value: Content("test"), |
| 64 |
} |
| 65 |
j, err := tst.MarshalJSON() |
| 66 |
if err != nil { |
| 67 |
t.Errorf("Error: %s", err) |
| 68 |
} |
| 69 |
expected := `"en":"test"` |
| 70 |
if string(j) != expected { |
| 71 |
t.Errorf("Different marshal result '%s', expected '%s'", j, expected) |
| 72 |
} |
| 73 |
} |
| 74 |
{ |
| 75 |
tst := LangRefValue{ |
| 76 |
Ref: MakeRef([]byte("en")), |
| 77 |
Value: Content("test\nwith characters\tneeding escaping\r\n"), |
| 78 |
} |
| 79 |
j, err := tst.MarshalJSON() |
| 80 |
if err != nil { |
| 81 |
t.Errorf("Error: %s", err) |
| 82 |
} |
| 83 |
expected := `"en":"test\nwith characters\tneeding escaping\r\n"` |
| 84 |
if string(j) != expected { |
| 85 |
t.Errorf("Different marshal result '%s', expected '%s'", j, expected) |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestLangRefValue_MarshalText(t *testing.T) { |
| 91 |
{ |
| 92 |
tst := LangRefValue{ |
| 93 |
Ref: NilLangRef, |
| 94 |
Value: Content("test"), |
| 95 |
} |
| 96 |
j, err := tst.MarshalText() |
| 97 |
if err != nil { |
| 98 |
t.Errorf("Error: %s", err) |
| 99 |
} |
| 100 |
expected := "test" |
| 101 |
if string(j) != expected { |
| 102 |
t.Errorf("Different marshal result '%s', expected '%s'", j, expected) |
| 103 |
} |
| 104 |
} |
| 105 |
{ |
| 106 |
tst := LangRefValue{ |
| 107 |
Ref: MakeRef([]byte("en")), |
| 108 |
Value: Content("test"), |
| 109 |
} |
| 110 |
j, err := tst.MarshalText() |
| 111 |
if err != nil { |
| 112 |
t.Errorf("Error: %s", err) |
| 113 |
} |
| 114 |
expected := "test[en]" |
| 115 |
if string(j) != expected { |
| 116 |
t.Errorf("Different marshal result '%s', expected '%s'", j, expected) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestNaturalLanguageValue_Get(t *testing.T) { |
| 122 |
testVal := Content("test") |
| 123 |
a := NaturalLanguageValues{NilLangRef: testVal} |
| 124 |
if !a.Get(NilLangRef).Equal(testVal) { |
| 125 |
t.Errorf("Invalid Get result. Expected %s received %s", testVal, a.Get(NilLangRef)) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestNaturalLanguageValue_Append(t *testing.T) { |
| 130 |
a := make(NaturalLanguageValues) |
| 131 |
|
| 132 |
if len(a) != 0 { |
| 133 |
t.Errorf("Invalid initialization of %T. Size %d > 0 ", a, len(a)) |
| 134 |
} |
| 135 |
langEn := English |
| 136 |
valEn := Content("random value") |
| 137 |
|
| 138 |
_ = a.Append(langEn, valEn) |
| 139 |
if len(a) != 1 { |
| 140 |
t.Errorf("Invalid append of one element to %T. Size %d != 1", a, len(a)) |
| 141 |
} |
| 142 |
if !a.Get(langEn).Equal(valEn) { |
| 143 |
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langEn, valEn, a.Get(langEn)) |
| 144 |
} |
| 145 |
langDe := MakeRef([]byte("de")) |
| 146 |
valDe := Content("randomisch") |
| 147 |
_ = a.Append(langDe, valDe) |
| 148 |
|
| 149 |
if len(a) != 2 { |
| 150 |
t.Errorf("Invalid append of one element to %T. Size %d != 2", a, len(a)) |
| 151 |
} |
| 152 |
if !a.Get(langEn).Equal(valEn) { |
| 153 |
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langEn, valEn, a.Get(langEn)) |
| 154 |
} |
| 155 |
if !a.Get(langDe).Equal(valDe) { |
| 156 |
t.Errorf("Invalid append of one element to %T. Value of %q not equal to %q, but %q", a, langDe, valDe, a.Get(langDe)) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestNaturalLanguageValue_UnmarshalFullObjectJSON(t *testing.T) { |
| 161 |
langEn := []byte(AmericanEnglish.String()) |
| 162 |
valEn := Content("random") |
| 163 |
langDe := []byte(German.String()) |
| 164 |
valDe := Content("zufällig\n") |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
rawJson := `{ |
| 171 |
"` + string(langEn) + `": "` + valEn.String() + `", |
| 172 |
"` + string(langDe) + `": "` + valDe.String() + `" |
| 173 |
}` |
| 174 |
|
| 175 |
a := make(NaturalLanguageValues) |
| 176 |
_ = a.Append(AmericanEnglish, valEn) |
| 177 |
_ = a.Append(German, valDe) |
| 178 |
err := a.UnmarshalJSON([]byte(rawJson)) |
| 179 |
if err != nil { |
| 180 |
t.Error(err) |
| 181 |
} |
| 182 |
for lang, val := range a { |
| 183 |
if lang != AmericanEnglish && lang != German { |
| 184 |
t.Errorf("Invalid json unmarshal for %T. Expected lang %q or %q, found %q", a, langEn, langDe, lang) |
| 185 |
} |
| 186 |
|
| 187 |
if lang == AmericanEnglish && !val.Equal(valEn) { |
| 188 |
t.Errorf("Invalid json unmarshal for %T. Expected value %q, found %q", a, valEn, val) |
| 189 |
} |
| 190 |
if lang == German && !val.Equal(valDe) { |
| 191 |
t.Errorf("Invalid json unmarshal for %T. Expected value %q, found %q", a, valDe, val) |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestNaturalLanguageValue_UnmarshalJSON(t *testing.T) { |
| 197 |
l := NilLangRef |
| 198 |
dataEmpty := []byte("") |
| 199 |
|
| 200 |
_ = l.UnmarshalJSON(dataEmpty) |
| 201 |
if l != NilLangRef { |
| 202 |
t.Errorf("Unmarshaled object %T should be an empty string, received %q", l, l) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func TestNaturalLanguageValue_UnmarshalText(t *testing.T) { |
| 207 |
l := NilLangRef |
| 208 |
dataEmpty := []byte("") |
| 209 |
|
| 210 |
_ = l.UnmarshalText(dataEmpty) |
| 211 |
if l != NilLangRef { |
| 212 |
t.Errorf("Unmarshaled object %T should be an empty string, received %q", l, l) |
| 213 |
} |
| 214 |
tests := []struct { |
| 215 |
name string |
| 216 |
data []byte |
| 217 |
want NaturalLanguageValues |
| 218 |
wantErr error |
| 219 |
}{ |
| 220 |
{ |
| 221 |
name: "empty string", |
| 222 |
data: []byte(""), |
| 223 |
want: nil, |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "string w/ quotes", |
| 227 |
data: []byte(`"ana"`), |
| 228 |
want: NaturalLanguageValues{und: Content(`"ana"`)}, |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "string w/o quotes", |
| 232 |
data: []byte(`foo-bar`), |
| 233 |
want: NaturalLanguageValues{und: Content("foo-bar")}, |
| 234 |
}, |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
} |
| 242 |
for _, tt := range tests { |
| 243 |
t.Run(tt.name, func(t *testing.T) { |
| 244 |
c := make(NaturalLanguageValues) |
| 245 |
err := c.UnmarshalText(tt.data) |
| 246 |
if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 247 |
t.Errorf("UnmarshalText() error = %s", cmp.Diff(tt.wantErr, err, EquateWeakErrors)) |
| 248 |
} |
| 249 |
if tt.wantErr != nil { |
| 250 |
return |
| 251 |
} |
| 252 |
if !cmp.Equal(c, tt.want) { |
| 253 |
t.Errorf("UnmarshalText() got = %s", cmp.Diff(tt.want, c, EquateWeakErrors)) |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func TestNaturalLanguageValue_First(t *testing.T) { |
| 260 |
t.Skipf("TODO") |
| 261 |
} |
| 262 |
|
| 263 |
func TestNaturalLanguageValueNew(t *testing.T) { |
| 264 |
n := NaturalLanguageValuesNew() |
| 265 |
|
| 266 |
if len(n) != 0 { |
| 267 |
t.Errorf("Initial %T should have length 0, received %d", n, len(n)) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
func TestNaturalLanguageValue_MarshalText(t *testing.T) { |
| 272 |
val := Content("test") |
| 273 |
tst := NaturalLanguageValues{English: val} |
| 274 |
j, err := tst.MarshalText() |
| 275 |
if err != nil { |
| 276 |
t.Errorf("Error marshaling: %s", err) |
| 277 |
} |
| 278 |
if j == nil { |
| 279 |
t.Errorf("Error marshaling: nil value returned") |
| 280 |
} |
| 281 |
expected := fmt.Sprintf("%s[%s]", val, English) |
| 282 |
if string(j) != expected { |
| 283 |
t.Errorf("Wrong value: %s, expected %s", j, expected) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
func TestNaturalLanguageValues_Append(t *testing.T) { |
| 288 |
t.Skipf("TODO") |
| 289 |
} |
| 290 |
|
| 291 |
func TestNaturalLanguageValues_First(t *testing.T) { |
| 292 |
t.Skipf("TODO") |
| 293 |
} |
| 294 |
|
| 295 |
func TestNaturalLanguageValues_Get(t *testing.T) { |
| 296 |
t.Skipf("TODO") |
| 297 |
} |
| 298 |
|
| 299 |
func TestNaturalLanguageValues_MarshalJSON(t *testing.T) { |
| 300 |
{ |
| 301 |
m := NaturalLanguageValues{ |
| 302 |
English: Content("test"), |
| 303 |
German: Content("test"), |
| 304 |
} |
| 305 |
result, err := m.MarshalJSON() |
| 306 |
if err != nil { |
| 307 |
t.Errorf("Failed marshaling '%v'", err) |
| 308 |
} |
| 309 |
mRes1 := `{"en":"test","de":"test"}` |
| 310 |
mRes2 := `{"de":"test","en":"test"}` |
| 311 |
if string(result) != mRes1 && string(result) != mRes2 { |
| 312 |
t.Errorf("Different results '%v' vs. '%v' or '%v'", string(result), mRes1, mRes2) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
s := make(map[LangRef]string) |
| 318 |
s[LangRef(language.English)] = "test" |
| 319 |
n1 := NaturalLanguageValues{ |
| 320 |
English: Content("test"), |
| 321 |
} |
| 322 |
result1, err1 := n1.MarshalJSON() |
| 323 |
if err1 != nil { |
| 324 |
t.Errorf("Failed marshaling '%v'", err1) |
| 325 |
} |
| 326 |
mRes3 := `"test"` |
| 327 |
if string(result1) != mRes3 { |
| 328 |
t.Errorf("Different results '%v' vs. '%v'", string(result1), mRes3) |
| 329 |
} |
| 330 |
} |
| 331 |
{ |
| 332 |
val := Content("test") |
| 333 |
tst := NaturalLanguageValues{NilLangRef: val} |
| 334 |
j, err := tst.MarshalJSON() |
| 335 |
if err != nil { |
| 336 |
t.Errorf("Error marshaling: %s", err) |
| 337 |
} |
| 338 |
if j == nil { |
| 339 |
t.Errorf("Error marshaling: nil value returned") |
| 340 |
} |
| 341 |
expected := fmt.Sprintf("\"%s\"", val) |
| 342 |
if string(j) != expected { |
| 343 |
t.Errorf("Wrong value: %s, expected %s", j, expected) |
| 344 |
} |
| 345 |
} |
| 346 |
{ |
| 347 |
val := Content("test") |
| 348 |
tst := NaturalLanguageValues{English: val} |
| 349 |
j, err := tst.MarshalJSON() |
| 350 |
if err != nil { |
| 351 |
t.Errorf("Error marshaling: %s", err) |
| 352 |
} |
| 353 |
if j == nil { |
| 354 |
t.Errorf("Error marshaling: nil value returned") |
| 355 |
} |
| 356 |
expected := fmt.Sprintf("\"%s\"", val) |
| 357 |
if string(j) != expected { |
| 358 |
t.Errorf("Wrong value: %s, expected %s", j, expected) |
| 359 |
} |
| 360 |
} |
| 361 |
{ |
| 362 |
valEn := Content("test") |
| 363 |
valFr := Content("teste") |
| 364 |
tst := NaturalLanguageValues{English: valEn, French: valFr} |
| 365 |
j, err := tst.MarshalJSON() |
| 366 |
if err != nil { |
| 367 |
t.Errorf("Error marshaling: %s", err) |
| 368 |
} |
| 369 |
if j == nil { |
| 370 |
t.Errorf("Error marshaling: nil value returned") |
| 371 |
} |
| 372 |
expected1 := fmt.Sprintf(`{"%s":"%s","%s":"%s"}`, English, valEn, French, valFr) |
| 373 |
expected2 := fmt.Sprintf(`{"%s":"%s","%s":"%s"}`, French, valFr, English, valEn) |
| 374 |
if string(j) != expected1 && string(j) != expected2 { |
| 375 |
t.Errorf("Wrong value: '%s', expected '%s' or '%s'", j, expected1, expected2) |
| 376 |
} |
| 377 |
} |
| 378 |
{ |
| 379 |
valEn := Content("test\nwith new line") |
| 380 |
valFr := Content("teste\navec une ligne nouvelle") |
| 381 |
tst := NaturalLanguageValues{English: valEn, French: valFr} |
| 382 |
j, err := tst.MarshalJSON() |
| 383 |
if err != nil { |
| 384 |
t.Errorf("Error marshaling: %s", err) |
| 385 |
} |
| 386 |
if j == nil { |
| 387 |
t.Errorf("Error marshaling: nil value returned") |
| 388 |
} |
| 389 |
expected1 := fmt.Sprintf(`{"%s":%s,"%s":%s}`, English, strconv.Quote(valEn.String()), French, strconv.Quote(valFr.String())) |
| 390 |
expected2 := fmt.Sprintf(`{"%s":%s,"%s":%s}`, French, strconv.Quote(valFr.String()), English, strconv.Quote(valEn.String())) |
| 391 |
if string(j) != expected1 && string(j) != expected2 { |
| 392 |
t.Errorf("Wrong value: '%s', expected '%s' or '%s'", j, expected1, expected2) |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
func TestNaturalLanguageValues_MarshalText(t *testing.T) { |
| 398 |
t.Skipf("TODO") |
| 399 |
} |
| 400 |
|
| 401 |
func TestNaturalLanguageValues_Set(t *testing.T) { |
| 402 |
t.Skipf("TODO") |
| 403 |
} |
| 404 |
|
| 405 |
func TestNaturalLanguageValues_UnmarshalJSON(t *testing.T) { |
| 406 |
tests := []struct { |
| 407 |
name string |
| 408 |
data []byte |
| 409 |
want NaturalLanguageValues |
| 410 |
wantErr error |
| 411 |
}{ |
| 412 |
{ |
| 413 |
name: "empty string", |
| 414 |
data: []byte(""), |
| 415 |
want: nil, |
| 416 |
}, |
| 417 |
{ |
| 418 |
name: "string w/ quotes", |
| 419 |
data: []byte(`"ana"`), |
| 420 |
want: NaturalLanguageValues{und: Content("ana")}, |
| 421 |
}, |
| 422 |
{ |
| 423 |
name: "string w/o quotes", |
| 424 |
data: []byte(`foo-bar`), |
| 425 |
wantErr: errors.Newf("unquoted string value when unmarshaling Content type"), |
| 426 |
}, |
| 427 |
{ |
| 428 |
name: "en:ana are mere", |
| 429 |
data: []byte(`{"en":"ana are mere"}`), |
| 430 |
want: NaturalLanguageValues{English: Content("ana are mere")}, |
| 431 |
}, |
| 432 |
} |
| 433 |
for _, tt := range tests { |
| 434 |
t.Run(tt.name, func(t *testing.T) { |
| 435 |
c := make(NaturalLanguageValues) |
| 436 |
err := c.UnmarshalJSON(tt.data) |
| 437 |
if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 438 |
t.Errorf("UnmarshalJSON() error = %s", cmp.Diff(tt.wantErr, err, EquateWeakErrors)) |
| 439 |
} |
| 440 |
if tt.wantErr != nil { |
| 441 |
return |
| 442 |
} |
| 443 |
if !cmp.Equal(c, tt.want) { |
| 444 |
t.Errorf("UnmarshalJSON() got = %s", cmp.Diff(tt.want, c, EquateWeakErrors)) |
| 445 |
} |
| 446 |
}) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
func TestNaturalLanguageValues_UnmarshalText(t *testing.T) { |
| 451 |
t.Skipf("TODO") |
| 452 |
} |
| 453 |
|
| 454 |
func TestNaturalLanguageValuesNew(t *testing.T) { |
| 455 |
t.Skipf("TODO") |
| 456 |
} |
| 457 |
|
| 458 |
func TestNaturalLanguageValues_String(t *testing.T) { |
| 459 |
t.Skipf("TODO") |
| 460 |
} |
| 461 |
|
| 462 |
func TestNaturalLanguageValues_Count(t *testing.T) { |
| 463 |
t.Skipf("TODO") |
| 464 |
} |
| 465 |
|
| 466 |
func TestNaturalLanguageValues_Equals(t *testing.T) { |
| 467 |
type args struct { |
| 468 |
with NaturalLanguageValues |
| 469 |
} |
| 470 |
tests := []struct { |
| 471 |
name string |
| 472 |
n NaturalLanguageValues |
| 473 |
args args |
| 474 |
want bool |
| 475 |
}{ |
| 476 |
{ |
| 477 |
name: "equal-key-value", |
| 478 |
n: NaturalLanguageValues{ |
| 479 |
English: Content("test123#"), |
| 480 |
}, |
| 481 |
args: args{ |
| 482 |
with: NaturalLanguageValues{ |
| 483 |
English: Content("test123#"), |
| 484 |
}, |
| 485 |
}, |
| 486 |
want: true, |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "not-equal-key", |
| 490 |
n: NaturalLanguageValues{ |
| 491 |
English: Content("test123#"), |
| 492 |
}, |
| 493 |
args: args{ |
| 494 |
with: NaturalLanguageValues{ |
| 495 |
French: Content("test123#"), |
| 496 |
}, |
| 497 |
}, |
| 498 |
want: false, |
| 499 |
}, |
| 500 |
{ |
| 501 |
name: "not-equal-value", |
| 502 |
n: NaturalLanguageValues{ |
| 503 |
English: Content("test123#"), |
| 504 |
}, |
| 505 |
args: args{ |
| 506 |
with: NaturalLanguageValues{ |
| 507 |
English: Content("test123"), |
| 508 |
}, |
| 509 |
}, |
| 510 |
want: false, |
| 511 |
}, |
| 512 |
} |
| 513 |
for _, tt := range tests { |
| 514 |
t.Run(tt.name, func(t *testing.T) { |
| 515 |
if got := tt.n.Equal(tt.args.with); got != tt.want { |
| 516 |
t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 517 |
} |
| 518 |
}) |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
func TestContent_String(t *testing.T) { |
| 523 |
t.Skip("TODO") |
| 524 |
} |
| 525 |
|
| 526 |
func TestContent_UnmarshalJSON(t *testing.T) { |
| 527 |
tests := []struct { |
| 528 |
name string |
| 529 |
data []byte |
| 530 |
want Content |
| 531 |
wantErr error |
| 532 |
}{ |
| 533 |
{ |
| 534 |
name: "empty string", |
| 535 |
data: []byte(""), |
| 536 |
want: nil, |
| 537 |
}, |
| 538 |
{ |
| 539 |
name: "string w/ quotes", |
| 540 |
data: []byte(`"ana"`), |
| 541 |
want: Content(`ana`), |
| 542 |
}, |
| 543 |
{ |
| 544 |
name: "string w/o quotes", |
| 545 |
data: []byte(`foo-bar`), |
| 546 |
wantErr: errors.Newf("unquoted string value when unmarshaling Content type"), |
| 547 |
}, |
| 548 |
} |
| 549 |
for _, tt := range tests { |
| 550 |
t.Run(tt.name, func(t *testing.T) { |
| 551 |
c := Content{} |
| 552 |
err := c.UnmarshalJSON(tt.data) |
| 553 |
if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 554 |
t.Errorf("UnmarshalJSON() error = %s", cmp.Diff(tt.wantErr, err, EquateWeakErrors)) |
| 555 |
} |
| 556 |
if tt.wantErr != nil { |
| 557 |
return |
| 558 |
} |
| 559 |
if !cmp.Equal(c, tt.want) { |
| 560 |
t.Errorf("UnmarshalJSON() got = %s", cmp.Diff(tt.want, c)) |
| 561 |
} |
| 562 |
}) |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
func TestContent_UnmarshalText(t *testing.T) { |
| 567 |
tests := []struct { |
| 568 |
name string |
| 569 |
data []byte |
| 570 |
want Content |
| 571 |
wantErr error |
| 572 |
}{ |
| 573 |
{ |
| 574 |
name: "empty string", |
| 575 |
data: []byte(""), |
| 576 |
want: nil, |
| 577 |
}, |
| 578 |
{ |
| 579 |
name: "string w/ quotes", |
| 580 |
data: []byte(`"ana"`), |
| 581 |
want: Content(`"ana"`), |
| 582 |
}, |
| 583 |
{ |
| 584 |
name: "string w/o quotes", |
| 585 |
data: []byte(`foo-bar`), |
| 586 |
want: Content(`foo-bar`), |
| 587 |
}, |
| 588 |
} |
| 589 |
for _, tt := range tests { |
| 590 |
t.Run(tt.name, func(t *testing.T) { |
| 591 |
c := Content{} |
| 592 |
err := c.UnmarshalText(tt.data) |
| 593 |
if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 594 |
t.Errorf("UnmarshalText() error = %s", cmp.Diff(tt.wantErr, err, EquateWeakErrors)) |
| 595 |
} |
| 596 |
if tt.wantErr != nil { |
| 597 |
return |
| 598 |
} |
| 599 |
if !cmp.Equal(c, tt.want) { |
| 600 |
t.Errorf("UnmarshalText() got = %s", cmp.Diff(tt.want, c)) |
| 601 |
} |
| 602 |
}) |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func gobValue(a interface{}) []byte { |
| 607 |
b := bytes.Buffer{} |
| 608 |
gg := gob.NewEncoder(&b) |
| 609 |
_ = gg.Encode(a) |
| 610 |
return b.Bytes() |
| 611 |
} |
| 612 |
|
| 613 |
func TestContent_GobEncode(t *testing.T) { |
| 614 |
tests := []struct { |
| 615 |
name string |
| 616 |
c Content |
| 617 |
want []byte |
| 618 |
wantErr bool |
| 619 |
}{ |
| 620 |
{ |
| 621 |
name: "empty", |
| 622 |
c: Content{}, |
| 623 |
want: []byte{}, |
| 624 |
wantErr: false, |
| 625 |
}, |
| 626 |
{ |
| 627 |
name: "empty value", |
| 628 |
c: Content{'0'}, |
| 629 |
want: gobValue([]byte{'0'}), |
| 630 |
wantErr: false, |
| 631 |
}, |
| 632 |
{ |
| 633 |
name: "some text", |
| 634 |
c: Content{'a', 'n', 'a', ' ', 'a', 'r', 'e'}, |
| 635 |
want: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), |
| 636 |
wantErr: false, |
| 637 |
}, |
| 638 |
} |
| 639 |
for _, tt := range tests { |
| 640 |
t.Run(tt.name, func(t *testing.T) { |
| 641 |
got, err := tt.c.GobEncode() |
| 642 |
if (err != nil) != tt.wantErr { |
| 643 |
t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) |
| 644 |
return |
| 645 |
} |
| 646 |
if !reflect.DeepEqual(got, tt.want) { |
| 647 |
t.Errorf("GobEncode() got = %v, want %v", got, tt.want) |
| 648 |
} |
| 649 |
}) |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
func TestContent_GobDecode(t *testing.T) { |
| 654 |
tests := []struct { |
| 655 |
name string |
| 656 |
c Content |
| 657 |
data []byte |
| 658 |
wantErr bool |
| 659 |
}{ |
| 660 |
{ |
| 661 |
name: "empty", |
| 662 |
c: Content{}, |
| 663 |
data: []byte{}, |
| 664 |
wantErr: false, |
| 665 |
}, |
| 666 |
{ |
| 667 |
name: "empty value", |
| 668 |
c: Content{'0'}, |
| 669 |
data: gobValue([]byte{'0'}), |
| 670 |
wantErr: false, |
| 671 |
}, |
| 672 |
{ |
| 673 |
name: "some text", |
| 674 |
c: Content{'a', 'n', 'a', ' ', 'a', 'r', 'e'}, |
| 675 |
data: gobValue([]byte{'a', 'n', 'a', ' ', 'a', 'r', 'e'}), |
| 676 |
wantErr: false, |
| 677 |
}, |
| 678 |
} |
| 679 |
for _, tt := range tests { |
| 680 |
t.Run(tt.name, func(t *testing.T) { |
| 681 |
if err := tt.c.GobDecode(tt.data); (err != nil) != tt.wantErr { |
| 682 |
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) |
| 683 |
} |
| 684 |
}) |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
func TestLangRef_GobDecode(t *testing.T) { |
| 689 |
tests := []struct { |
| 690 |
name string |
| 691 |
l LangRef |
| 692 |
data []byte |
| 693 |
wantErr bool |
| 694 |
}{ |
| 695 |
{ |
| 696 |
name: "empty", |
| 697 |
l: NilLangRef, |
| 698 |
data: []byte{}, |
| 699 |
wantErr: false, |
| 700 |
}, |
| 701 |
{ |
| 702 |
name: "some text", |
| 703 |
l: MakeRef([]byte("ana are")), |
| 704 |
data: gobValue([]byte("ana are")), |
| 705 |
wantErr: false, |
| 706 |
}, |
| 707 |
} |
| 708 |
for _, tt := range tests { |
| 709 |
t.Run(tt.name, func(t *testing.T) { |
| 710 |
if err := tt.l.GobDecode(tt.data); (err != nil) != tt.wantErr { |
| 711 |
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) |
| 712 |
} |
| 713 |
}) |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
func TestLangRefValue_GobDecode(t *testing.T) { |
| 718 |
type fields struct { |
| 719 |
Ref LangRef |
| 720 |
Value Content |
| 721 |
} |
| 722 |
tests := []struct { |
| 723 |
name string |
| 724 |
fields fields |
| 725 |
data []byte |
| 726 |
wantErr bool |
| 727 |
}{ |
| 728 |
{ |
| 729 |
name: "empty", |
| 730 |
fields: fields{}, |
| 731 |
data: gobValue(kv{}), |
| 732 |
wantErr: false, |
| 733 |
}, |
| 734 |
{ |
| 735 |
name: "some values", |
| 736 |
fields: fields{ |
| 737 |
Ref: MakeRef([]byte("ana")), |
| 738 |
Value: Content("are mere"), |
| 739 |
}, |
| 740 |
data: gobValue(kv{K: []byte("ana"), V: []byte("are mere")}), |
| 741 |
wantErr: false, |
| 742 |
}, |
| 743 |
} |
| 744 |
for _, tt := range tests { |
| 745 |
t.Run(tt.name, func(t *testing.T) { |
| 746 |
l := &LangRefValue{ |
| 747 |
Ref: tt.fields.Ref, |
| 748 |
Value: tt.fields.Value, |
| 749 |
} |
| 750 |
if err := l.GobDecode(tt.data); (err != nil) != tt.wantErr { |
| 751 |
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) |
| 752 |
} |
| 753 |
}) |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
func TestNaturalLanguageValues_GobEncode(t *testing.T) { |
| 758 |
tests := []struct { |
| 759 |
name string |
| 760 |
n NaturalLanguageValues |
| 761 |
want []byte |
| 762 |
wantErr bool |
| 763 |
}{ |
| 764 |
{ |
| 765 |
name: "empty", |
| 766 |
n: NaturalLanguageValues{}, |
| 767 |
want: []byte{}, |
| 768 |
wantErr: false, |
| 769 |
}, |
| 770 |
{ |
| 771 |
name: "some values", |
| 772 |
n: NaturalLanguageValues{ |
| 773 |
Und: []byte("are mere"), |
| 774 |
}, |
| 775 |
want: gobValue([]kv{{K: []byte("und"), V: []byte("are mere")}}), |
| 776 |
wantErr: false, |
| 777 |
}, |
| 778 |
} |
| 779 |
for _, tt := range tests { |
| 780 |
t.Run(tt.name, func(t *testing.T) { |
| 781 |
got, err := tt.n.GobEncode() |
| 782 |
if (err != nil) != tt.wantErr { |
| 783 |
t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) |
| 784 |
return |
| 785 |
} |
| 786 |
if !reflect.DeepEqual(got, tt.want) { |
| 787 |
t.Errorf("GobEncode() got = %v, want %v", got, tt.want) |
| 788 |
} |
| 789 |
}) |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
func TestNaturalLanguageValues_GobDecode(t *testing.T) { |
| 794 |
tests := []struct { |
| 795 |
name string |
| 796 |
n NaturalLanguageValues |
| 797 |
data []byte |
| 798 |
wantErr bool |
| 799 |
}{ |
| 800 |
{ |
| 801 |
name: "empty", |
| 802 |
n: NaturalLanguageValues{}, |
| 803 |
data: []byte{}, |
| 804 |
wantErr: false, |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "some values", |
| 808 |
n: NaturalLanguageValues{ |
| 809 |
Und: []byte("are mere"), |
| 810 |
}, |
| 811 |
data: gobValue([]kv{{K: []byte("ana"), V: []byte("are mere")}}), |
| 812 |
wantErr: false, |
| 813 |
}, |
| 814 |
} |
| 815 |
for _, tt := range tests { |
| 816 |
t.Run(tt.name, func(t *testing.T) { |
| 817 |
if err := tt.n.GobDecode(tt.data); (err != nil) != tt.wantErr { |
| 818 |
t.Errorf("GobDecode() error = %v, wantErr %v", err, tt.wantErr) |
| 819 |
} |
| 820 |
}) |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
func TestLangRefValue_String(t *testing.T) { |
| 825 |
type fields struct { |
| 826 |
Ref LangRef |
| 827 |
Value Content |
| 828 |
} |
| 829 |
tests := []struct { |
| 830 |
name string |
| 831 |
fields fields |
| 832 |
want string |
| 833 |
}{ |
| 834 |
{ |
| 835 |
name: "empty", |
| 836 |
fields: fields{}, |
| 837 |
want: "", |
| 838 |
}, |
| 839 |
{ |
| 840 |
name: "empty LangRef, not empty Value", |
| 841 |
fields: fields{ |
| 842 |
Value: Content("not empty"), |
| 843 |
}, |
| 844 |
want: "not empty", |
| 845 |
}, |
| 846 |
{ |
| 847 |
name: "nil LangRef, empty Value", |
| 848 |
fields: fields{ |
| 849 |
Ref: NilLangRef, |
| 850 |
Value: Content(""), |
| 851 |
}, |
| 852 |
want: "", |
| 853 |
}, |
| 854 |
{ |
| 855 |
name: "nil LangRef, non empty Value", |
| 856 |
fields: fields{ |
| 857 |
Ref: NilLangRef, |
| 858 |
Value: Content("test"), |
| 859 |
}, |
| 860 |
want: "test", |
| 861 |
}, |
| 862 |
{ |
| 863 |
name: "ro-example", |
| 864 |
fields: fields{ |
| 865 |
Ref: MakeRef([]byte("ro")), |
| 866 |
Value: Content("example"), |
| 867 |
}, |
| 868 |
want: "example[ro]", |
| 869 |
}, |
| 870 |
} |
| 871 |
for _, tt := range tests { |
| 872 |
t.Run(tt.name, func(t *testing.T) { |
| 873 |
l := LangRefValue{ |
| 874 |
Ref: tt.fields.Ref, |
| 875 |
Value: tt.fields.Value, |
| 876 |
} |
| 877 |
if got := l.String(); got != tt.want { |
| 878 |
t.Errorf("String() = %v, want %v", got, tt.want) |
| 879 |
} |
| 880 |
}) |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
func TestLangRefValue_UnmarshalJSON(t *testing.T) { |
| 885 |
tests := []struct { |
| 886 |
name string |
| 887 |
wantErr bool |
| 888 |
data []byte |
| 889 |
want LangRefValue |
| 890 |
}{ |
| 891 |
{ |
| 892 |
name: "empty", |
| 893 |
data: []byte(""), |
| 894 |
want: LangRefValue{ |
| 895 |
Ref: NilLangRef, |
| 896 |
}, |
| 897 |
}, |
| 898 |
{ |
| 899 |
name: "empty LangRef, not empty Value", |
| 900 |
want: LangRefValue{ |
| 901 |
Ref: NilLangRef, |
| 902 |
Value: Content("not empty"), |
| 903 |
}, |
| 904 |
data: []byte("not empty"), |
| 905 |
}, |
| 906 |
{ |
| 907 |
name: "nil LangRef, non empty Value", |
| 908 |
want: LangRefValue{ |
| 909 |
Ref: NilLangRef, |
| 910 |
Value: Content("test"), |
| 911 |
}, |
| 912 |
data: []byte("test"), |
| 913 |
}, |
| 914 |
{ |
| 915 |
name: "ro-example", |
| 916 |
data: []byte(`{"ro":"example"}`), |
| 917 |
want: LangRefValue{ |
| 918 |
Ref: MakeRef([]byte("ro")), |
| 919 |
Value: Content("example"), |
| 920 |
}, |
| 921 |
}, |
| 922 |
} |
| 923 |
for _, tt := range tests { |
| 924 |
t.Run(tt.name, func(t *testing.T) { |
| 925 |
l := LangRefValue{} |
| 926 |
if _ = l.UnmarshalJSON(tt.data); !reflect.DeepEqual(l, tt.want) { |
| 927 |
t.Errorf("UnmarshalJSON() got = %+v, want %+v", l, tt.want) |
| 928 |
} |
| 929 |
}) |
| 930 |
} |
| 931 |
} |