|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.Configuration.Assemblies; |
| 7 | +using System.Globalization; |
6 | 8 | using Microsoft.Build.BackEnd;
|
7 | 9 | using System.IO;
|
| 10 | +using System.Reflection; |
| 11 | +using Shouldly; |
8 | 12 | using Xunit;
|
9 | 13 |
|
10 | 14 | namespace Microsoft.Build.UnitTests.BackEnd
|
@@ -431,6 +435,163 @@ public void TestSerializeDictionaryStringTNoComparerNull()
|
431 | 435 | Assert.Equal(value, deserializedValue);
|
432 | 436 | }
|
433 | 437 |
|
| 438 | + [Theory] |
| 439 | + [InlineData("en")] |
| 440 | + [InlineData("en-US")] |
| 441 | + [InlineData("en-CA")] |
| 442 | + [InlineData("zh-HK")] |
| 443 | + [InlineData("sr-Cyrl-CS")] |
| 444 | + public void CultureInfo(string name) |
| 445 | + { |
| 446 | + CultureInfo value = new CultureInfo(name); |
| 447 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 448 | + |
| 449 | + CultureInfo deserializedValue = null; |
| 450 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 451 | + |
| 452 | + deserializedValue.ShouldBe(value); |
| 453 | + } |
| 454 | + |
| 455 | + [Fact] |
| 456 | + public void CultureInfoAsNull() |
| 457 | + { |
| 458 | + CultureInfo value = null; |
| 459 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 460 | + |
| 461 | + CultureInfo deserializedValue = null; |
| 462 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 463 | + |
| 464 | + deserializedValue.ShouldBeNull(); |
| 465 | + } |
| 466 | + |
| 467 | + [Theory] |
| 468 | + [InlineData("1.2")] |
| 469 | + [InlineData("1.2.3")] |
| 470 | + [InlineData("1.2.3.4")] |
| 471 | + public void Version(string version) |
| 472 | + { |
| 473 | + Version value = new Version(version); |
| 474 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 475 | + |
| 476 | + Version deserializedValue = null; |
| 477 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 478 | + |
| 479 | + deserializedValue.ShouldBe(value); |
| 480 | + } |
| 481 | + |
| 482 | + [Fact] |
| 483 | + public void VersionAsNull() |
| 484 | + { |
| 485 | + Version value = null; |
| 486 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 487 | + |
| 488 | + Version deserializedValue = null; |
| 489 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 490 | + |
| 491 | + deserializedValue.ShouldBeNull(); |
| 492 | + } |
| 493 | + |
| 494 | + [Fact] |
| 495 | + public void HashSetOfT() |
| 496 | + { |
| 497 | + HashSet<BaseClass> values = new() |
| 498 | + { |
| 499 | + new BaseClass(1), |
| 500 | + new BaseClass(2), |
| 501 | + null |
| 502 | + }; |
| 503 | + TranslationHelpers.GetWriteTranslator().TranslateHashSet(ref values, BaseClass.FactoryForDeserialization, capacity => new()); |
| 504 | + |
| 505 | + HashSet<BaseClass> deserializedValues = null; |
| 506 | + TranslationHelpers.GetReadTranslator().TranslateHashSet(ref deserializedValues, BaseClass.FactoryForDeserialization, capacity => new()); |
| 507 | + |
| 508 | + deserializedValues.ShouldBe(values, ignoreOrder: true); |
| 509 | + } |
| 510 | + |
| 511 | + [Fact] |
| 512 | + public void HashSetOfTAsNull() |
| 513 | + { |
| 514 | + HashSet<BaseClass> value = null; |
| 515 | + TranslationHelpers.GetWriteTranslator().TranslateHashSet(ref value, BaseClass.FactoryForDeserialization, capacity => new()); |
| 516 | + |
| 517 | + HashSet<BaseClass> deserializedValue = null; |
| 518 | + TranslationHelpers.GetReadTranslator().TranslateHashSet(ref deserializedValue, BaseClass.FactoryForDeserialization, capacity => new()); |
| 519 | + |
| 520 | + deserializedValue.ShouldBeNull(); |
| 521 | + } |
| 522 | + |
| 523 | + [Fact] |
| 524 | + public void AssemblyNameAsNull() |
| 525 | + { |
| 526 | + AssemblyName value = null; |
| 527 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 528 | + |
| 529 | + AssemblyName deserializedValue = null; |
| 530 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 531 | + |
| 532 | + deserializedValue.ShouldBeNull(); |
| 533 | + } |
| 534 | + |
| 535 | + [Fact] |
| 536 | + public void AssemblyNameWithAllFields() |
| 537 | + { |
| 538 | + AssemblyName value = new() |
| 539 | + { |
| 540 | + Name = "a", |
| 541 | + Version = new Version(1, 2, 3), |
| 542 | + Flags = AssemblyNameFlags.PublicKey, |
| 543 | + ProcessorArchitecture = ProcessorArchitecture.X86, |
| 544 | + CultureInfo = new CultureInfo("zh-HK"), |
| 545 | + HashAlgorithm = System.Configuration.Assemblies.AssemblyHashAlgorithm.SHA256, |
| 546 | + VersionCompatibility = AssemblyVersionCompatibility.SameMachine, |
| 547 | + CodeBase = "C:\\src", |
| 548 | + KeyPair = new StrongNameKeyPair(new byte[] { 4, 3, 2, 1 }), |
| 549 | + ContentType = AssemblyContentType.WindowsRuntime, |
| 550 | + CultureName = "zh-HK", |
| 551 | + }; |
| 552 | + value.SetPublicKey(new byte[]{ 3, 2, 1}); |
| 553 | + value.SetPublicKeyToken(new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }); |
| 554 | + |
| 555 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 556 | + |
| 557 | + AssemblyName deserializedValue = null; |
| 558 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 559 | + |
| 560 | + HelperAssertAssemblyNameEqual(value, deserializedValue); |
| 561 | + } |
| 562 | + |
| 563 | + [Fact] |
| 564 | + public void AssemblyNameWithMinimalFields() |
| 565 | + { |
| 566 | + AssemblyName value = new(); |
| 567 | + |
| 568 | + TranslationHelpers.GetWriteTranslator().Translate(ref value); |
| 569 | + |
| 570 | + AssemblyName deserializedValue = null; |
| 571 | + TranslationHelpers.GetReadTranslator().Translate(ref deserializedValue); |
| 572 | + |
| 573 | + HelperAssertAssemblyNameEqual(value, deserializedValue); |
| 574 | + } |
| 575 | + |
| 576 | + /// <summary> |
| 577 | + /// Assert two AssemblyName objects values are same. |
| 578 | + /// Ignoring KeyPair, ContentType, CultureName as those are not serialized |
| 579 | + /// </summary> |
| 580 | + private static void HelperAssertAssemblyNameEqual(AssemblyName expected, AssemblyName actual) |
| 581 | + { |
| 582 | + actual.Name.ShouldBe(expected.Name); |
| 583 | + actual.Version.ShouldBe(expected.Version); |
| 584 | + actual.Flags.ShouldBe(expected.Flags); |
| 585 | + actual.ProcessorArchitecture.ShouldBe(expected.ProcessorArchitecture); |
| 586 | + actual.CultureInfo.ShouldBe(expected.CultureInfo); |
| 587 | + actual.HashAlgorithm.ShouldBe(expected.HashAlgorithm); |
| 588 | + actual.VersionCompatibility.ShouldBe(expected.VersionCompatibility); |
| 589 | + actual.CodeBase.ShouldBe(expected.CodeBase); |
| 590 | + |
| 591 | + actual.GetPublicKey().ShouldBe(expected.GetPublicKey()); |
| 592 | + actual.GetPublicKeyToken().ShouldBe(expected.GetPublicKeyToken()); |
| 593 | + } |
| 594 | + |
434 | 595 | /// <summary>
|
435 | 596 | /// Helper for bool serialization.
|
436 | 597 | /// </summary>
|
@@ -610,6 +771,24 @@ protected BaseClass()
|
610 | 771 | {
|
611 | 772 | }
|
612 | 773 |
|
| 774 | + protected bool Equals(BaseClass other) |
| 775 | + { |
| 776 | + return _baseValue == other._baseValue; |
| 777 | + } |
| 778 | + |
| 779 | + public override bool Equals(object obj) |
| 780 | + { |
| 781 | + if (ReferenceEquals(null, obj)) return false; |
| 782 | + if (ReferenceEquals(this, obj)) return true; |
| 783 | + if (obj.GetType() != this.GetType()) return false; |
| 784 | + return Equals((BaseClass) obj); |
| 785 | + } |
| 786 | + |
| 787 | + public override int GetHashCode() |
| 788 | + { |
| 789 | + return _baseValue; |
| 790 | + } |
| 791 | + |
613 | 792 | /// <summary>
|
614 | 793 | /// Gets a comparer.
|
615 | 794 | /// </summary>
|
|
0 commit comments