|
9 | 9 | package goipp |
10 | 10 |
|
11 | 11 | import ( |
| 12 | + "bytes" |
12 | 13 | "reflect" |
13 | 14 | "testing" |
14 | 15 | ) |
@@ -348,3 +349,155 @@ func TestMessageAttrGroups(t *testing.T) { |
348 | 349 | ) |
349 | 350 | } |
350 | 351 | } |
| 352 | + |
| 353 | +// TestMessageEqualSimilar tests Message.Equal and Message.Similar functions. |
| 354 | +func TestMessageEqualSimilar(t *testing.T) { |
| 355 | + type testData struct { |
| 356 | + m1, m2 Message // Input messages |
| 357 | + equal bool // Expected Message.Equal output |
| 358 | + similar bool // Expected Message.Similar output |
| 359 | + } |
| 360 | + |
| 361 | + uri := "ipp://192/168.0.1/ipp/print" |
| 362 | + |
| 363 | + tests := []testData{ |
| 364 | + // Empty messages are equal and similar |
| 365 | + { |
| 366 | + m1: Message{}, |
| 367 | + m2: Message{}, |
| 368 | + equal: true, |
| 369 | + similar: true, |
| 370 | + }, |
| 371 | + |
| 372 | + // Messages with different Version/Code/RequestID are |
| 373 | + // neither equal or similar |
| 374 | + { |
| 375 | + m1: Message{}, |
| 376 | + m2: Message{Version: 1}, |
| 377 | + equal: false, |
| 378 | + similar: false, |
| 379 | + }, |
| 380 | + |
| 381 | + { |
| 382 | + m1: Message{}, |
| 383 | + m2: Message{Code: 1}, |
| 384 | + equal: false, |
| 385 | + similar: false, |
| 386 | + }, |
| 387 | + |
| 388 | + { |
| 389 | + m1: Message{}, |
| 390 | + m2: Message{RequestID: 1}, |
| 391 | + equal: false, |
| 392 | + similar: false, |
| 393 | + }, |
| 394 | + |
| 395 | + // If the same attributes represented as Message.Groups in one |
| 396 | + // message and via Message.Operation/Job/Printer etc in the |
| 397 | + // another message, these messages are equal and similar |
| 398 | + { |
| 399 | + m1: Message{ |
| 400 | + Groups: Groups{ |
| 401 | + Group{ |
| 402 | + Tag: TagOperationGroup, |
| 403 | + Attrs: Attributes{ |
| 404 | + MakeAttr("attributes-charset", |
| 405 | + TagCharset, String("utf-8")), |
| 406 | + MakeAttr("attributes-natural-language", |
| 407 | + TagLanguage, String("en-US")), |
| 408 | + MakeAttr("printer-uri", |
| 409 | + TagURI, String(uri)), |
| 410 | + }, |
| 411 | + }, |
| 412 | + Group{ |
| 413 | + Tag: TagJobGroup, |
| 414 | + Attrs: Attributes{ |
| 415 | + MakeAttr("copies", TagInteger, Integer(1)), |
| 416 | + }, |
| 417 | + }, |
| 418 | + }, |
| 419 | + }, |
| 420 | + |
| 421 | + m2: Message{ |
| 422 | + Operation: Attributes{ |
| 423 | + MakeAttr("attributes-charset", |
| 424 | + TagCharset, String("utf-8")), |
| 425 | + MakeAttr("attributes-natural-language", |
| 426 | + TagLanguage, String("en-US")), |
| 427 | + MakeAttr("printer-uri", |
| 428 | + TagURI, String(uri)), |
| 429 | + }, |
| 430 | + |
| 431 | + Job: Attributes{ |
| 432 | + MakeAttr("copies", TagInteger, Integer(1)), |
| 433 | + }, |
| 434 | + }, |
| 435 | + |
| 436 | + equal: true, |
| 437 | + similar: true, |
| 438 | + }, |
| 439 | + |
| 440 | + // Messages with the different order of the same set of attributes |
| 441 | + // are similar but not equal. |
| 442 | + { |
| 443 | + m1: Message{ |
| 444 | + Operation: Attributes{ |
| 445 | + MakeAttr("attributes-charset", |
| 446 | + TagCharset, String("utf-8")), |
| 447 | + MakeAttr("attributes-natural-language", |
| 448 | + TagLanguage, String("en-US")), |
| 449 | + MakeAttr("printer-uri", |
| 450 | + TagURI, String(uri)), |
| 451 | + }, |
| 452 | + }, |
| 453 | + |
| 454 | + m2: Message{ |
| 455 | + Operation: Attributes{ |
| 456 | + MakeAttr("attributes-charset", |
| 457 | + TagCharset, String("utf-8")), |
| 458 | + MakeAttr("printer-uri", |
| 459 | + TagURI, String(uri)), |
| 460 | + MakeAttr("attributes-natural-language", |
| 461 | + TagLanguage, String("en-US")), |
| 462 | + }, |
| 463 | + }, |
| 464 | + |
| 465 | + equal: false, |
| 466 | + similar: true, |
| 467 | + }, |
| 468 | + } |
| 469 | + |
| 470 | + for _, test := range tests { |
| 471 | + equal := test.m1.Equal(test.m2) |
| 472 | + if equal != test.equal { |
| 473 | + var buf1, buf2 bytes.Buffer |
| 474 | + test.m1.Print(&buf1, true) |
| 475 | + test.m2.Print(&buf1, true) |
| 476 | + |
| 477 | + t.Errorf("testing Message.Equal:\n"+ |
| 478 | + "message 1: %s\n"+ |
| 479 | + "message 2: %s\n"+ |
| 480 | + "expected: %v\n"+ |
| 481 | + "present: %v\n", |
| 482 | + &buf1, &buf2, |
| 483 | + test.equal, equal, |
| 484 | + ) |
| 485 | + } |
| 486 | + |
| 487 | + similar := test.m1.Similar(test.m2) |
| 488 | + if similar != test.similar { |
| 489 | + var buf1, buf2 bytes.Buffer |
| 490 | + test.m1.Print(&buf1, true) |
| 491 | + test.m2.Print(&buf1, true) |
| 492 | + |
| 493 | + t.Errorf("testing Message.Similar:\n"+ |
| 494 | + "message 1: %s\n"+ |
| 495 | + "message 2: %s\n"+ |
| 496 | + "expected: %v\n"+ |
| 497 | + "present: %v\n", |
| 498 | + &buf1, &buf2, |
| 499 | + test.similar, similar, |
| 500 | + ) |
| 501 | + } |
| 502 | + } |
| 503 | +} |
0 commit comments