< Summary

Class:Microsoft.Azure.Search.Tests.ModelComparerTests
Assembly:Search.Management.Tests
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Management.Search\tests\ModelComparer\ModelComparerTests.cs
Covered lines:0
Uncovered lines:254
Coverable lines:254
Total lines:488
Line coverage:0% (0 of 254)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CanComparePrimitives()-0%100%
CanCompareEnums()-0%100%
CanCompareNulls()-0%100%
CanCompareExtensibleEnums()-0%100%
CanCompareNullables()-0%100%
CanCompareCollections()-0%100%
CanCompareDictionaries()-0%100%
IEquatableTakesPrecedenceOverIEnumerable()-0%100%
CanComparePolymorphicObjects()-0%100%
CanCompareDtos()-0%100%
ComparingMarkerClassesAlwaysReturnsTrue()-0%100%
CanCompareDifferentEnumerableTypes()-0%100%
IntegerComparisonsUseWidestType()-0%100%
CanCompareComplexModels()-0%100%
.ctor()-0%100%
.ctor(...)-0%100%
get_Name()-0%100%
get_Age()-0%100%
.cctor()-0%100%
.ctor(...)-0%0%
op_Equality(...)-0%100%
op_Inequality(...)-0%100%
Equals(...)-0%100%
Equals(...)-0%0%
GetHashCode()-0%100%
ToString()-0%100%
.ctor(...)-0%0%
.ctor(...)-0%0%
Equals(...)-0%0%
GetEnumerator()-0%100%
System.Collections.IEnumerable.GetEnumerator()-0%100%
.ctor(...)-0%100%
get_Name()-0%100%
.ctor(...)-0%100%
.ctor(...)-0%100%
get_Noise()-0%0%
Equals(...)-0%0%
.ctor(...)-0%100%
.ctor(...)-0%100%
get_Noise()-0%100%
Equals(...)-0%0%
.ctor()-0%100%
.ctor(...)-0%100%
get_Name()-0%100%
get_Aliases()-0%100%
get_Age()-0%100%
get_Address()-0%100%
get_FirstName()-0%100%
get_LastName()-0%100%
get_Directions()-0%100%
get_Occupations()-0%100%
Clone()-0%100%
get_Street()-0%100%
get_City()-0%100%
.ctor()-0%100%
.ctor(...)-0%100%
get_State()-0%100%
get_ZipCode()-0%100%
Clone()-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Management.Search\tests\ModelComparer\ModelComparerTests.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using Microsoft.Azure.Search.Tests.Utilities;
 6using System;
 7using System.Collections;
 8using System.Collections.Generic;
 9using System.Linq;
 10using Xunit;
 11
 12namespace Microsoft.Azure.Search.Tests
 13{
 14    public sealed class ModelComparerTests
 15    {
 16        private enum Direction
 17        {
 18            Right,
 19            Left
 20        }
 21
 22        [Fact]
 23        public void CanComparePrimitives()
 24        {
 025            Assert.Equal(5, 5, new ModelComparer<int>());
 026            Assert.Equal(3.14, 3.14, new ModelComparer<double>());
 027            Assert.Equal('c', 'c', new ModelComparer<char>());
 028            Assert.Equal(5, 5L, new ModelComparer<object>());
 29
 030            Assert.NotEqual(5, 1, new ModelComparer<int>());
 031            Assert.NotEqual(3.14, -2.7, new ModelComparer<double>());
 032            Assert.NotEqual('c', 'z', new ModelComparer<char>());
 033            Assert.NotEqual(3, 3.0, new ModelComparer<object>());
 034        }
 35
 36        [Fact]
 37        public void CanCompareEnums()
 38        {
 039            Assert.Equal(Direction.Left, Direction.Left, new ModelComparer<Direction>());
 040            Assert.NotEqual(Direction.Left, Direction.Right, new ModelComparer<Direction>());
 041        }
 42
 43        [Fact]
 44        public void CanCompareNulls()
 45        {
 046            Model nullModel = null;
 47
 048            Assert.Equal(nullModel, nullModel, new ModelComparer<Model>());
 049            Assert.NotEqual(new Model(), nullModel, new ModelComparer<Model>());
 050            Assert.NotEqual(nullModel, new Model(), new ModelComparer<Model>());
 51
 052            IEnumerable<int> nullCollection = null;
 53
 054            Assert.True(new ModelComparer<IEnumerable<int>>().Equals(nullCollection, nullCollection));
 055            Assert.False(new ModelComparer<IEnumerable<int>>().Equals(new[] { 1 }, nullCollection));
 056            Assert.False(new ModelComparer<IEnumerable<int>>().Equals(nullCollection, new[] { 4, 5 }));
 057        }
 58
 59        [Fact]
 60        public void CanCompareExtensibleEnums()
 61        {
 062            Assert.Equal(FancyDirection.Left, FancyDirection.Left, new ModelComparer<FancyDirection>());
 063            Assert.NotEqual(FancyDirection.Left, FancyDirection.Right, new ModelComparer<FancyDirection>());
 064        }
 65
 66        [Fact]
 67        public void CanCompareNullables()
 68        {
 069            int? maybeSeven = 7;
 070            int? maybeFive = 5;
 071            double? maybePi = 3.14;
 072            double? maybeE = 2.718;
 073            bool? maybeTrue = true;
 074            bool? maybeFalse = false;
 75
 076            Assert.Equal(maybeSeven, maybeSeven, new ModelComparer<int?>());
 077            Assert.Equal(maybePi, maybePi, new ModelComparer<double?>());
 078            Assert.Equal(maybeTrue, maybeTrue, new ModelComparer<bool?>());
 79
 080            Assert.NotEqual(maybeSeven, maybeFive, new ModelComparer<int?>());
 081            Assert.NotEqual(maybePi, maybeE, new ModelComparer<double?>());
 082            Assert.NotEqual(maybeTrue, maybeFalse, new ModelComparer<bool?>());
 83
 84#pragma warning disable xUnit2003 // Do not use equality check to test for null value
 085            Assert.NotEqual(null, maybeSeven, new ModelComparer<int?>());
 086            Assert.NotEqual(null, maybePi, new ModelComparer<double?>());
 087            Assert.NotEqual(null, maybeTrue, new ModelComparer<bool?>());
 88#pragma warning restore xUnit2003 // Do not use equality check to test for null value
 89
 90#pragma warning disable xUnit2000 // Literal or constant should be the first argument to Assert.NotEqual
 091            Assert.NotEqual(maybeSeven, null, new ModelComparer<int?>());
 092            Assert.NotEqual(maybePi, null, new ModelComparer<double?>());
 093            Assert.NotEqual(maybeTrue, null, new ModelComparer<bool?>());
 94#pragma warning restore xUnit2000 // Literal or constant should be the first argument to Assert.NotEqual
 095        }
 96
 97        [Fact]
 98        public void CanCompareCollections()
 99        {
 0100            var ints = new[] { 1, 2, 3 };
 0101            var otherInts = new[] { 4, 5, 6 };
 0102            var longerInts = new[] { 1, 2, 3, 4 };
 0103            var strings = new[] { "hello", "bonjour", "guten tag", "你好", "aloha" };
 0104            var otherStrings = new[] { "good bye", "au revoir", "auf wiedersehen", "再見", "aloha" };
 105
 0106            var intComparer = new ModelComparer<int[]>();
 0107            var stringComparer = new ModelComparer<string[]>();
 108
 109            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelCompa
 110            // Also, we use deep copies for some comparisons to make sure the ModelComparer isn't falling back on refere
 0111            Assert.True(intComparer.Equals(ints, ints));
 0112            Assert.True(stringComparer.Equals(strings, strings));
 113
 0114            Assert.True(intComparer.Equals(ints, (int[])ints.Clone()));
 0115            Assert.True(stringComparer.Equals((string[])strings.Clone(), strings));
 116
 0117            Assert.False(intComparer.Equals(ints, otherInts));
 0118            Assert.False(stringComparer.Equals(otherStrings, strings));
 119
 0120            Assert.False(intComparer.Equals(ints, new[] { 7 }));
 0121            Assert.False(stringComparer.Equals(new[] { "who?" }, strings));
 122
 123            // Test collections of varying lengths.
 0124            Assert.False(intComparer.Equals(ints, longerInts));
 0125            Assert.False(intComparer.Equals(longerInts, ints));
 0126        }
 127
 128        [Fact]
 129        public void CanCompareDictionaries()
 130        {
 0131            var x = new Dictionary<string, double>() { { "pi", 3.14 }, { "e", 2.718 } };
 0132            var notX = new Dictionary<string, double>() { { "m", 13.3 }, { "v", 77 } };
 133
 0134            var y = new Dictionary<string, object>() { { "a", "test" }, { "b", "test2" } };
 0135            var notY = new Dictionary<string, object>() { { "w", "other" }, { "u", "side" } };
 136
 0137            var doubleComparer = new ModelComparer<Dictionary<string, double>>();
 0138            var stringComparer = new ModelComparer<Dictionary<string, object>>();
 139
 140            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelCompa
 141            // Also, we use deep copies for some comparisons to make sure the ModelComparer isn't falling back on refere
 0142            Assert.True(doubleComparer.Equals(x, x));
 0143            Assert.True(stringComparer.Equals(y, y));
 144
 0145            Assert.True(doubleComparer.Equals(x, new Dictionary<string, double>(x)));
 0146            Assert.True(stringComparer.Equals(new Dictionary<string, object>(y), y));
 147
 0148            Assert.False(doubleComparer.Equals(x, notX));
 0149            Assert.False(stringComparer.Equals(notY, y));
 150
 0151            Assert.False(doubleComparer.Equals(x, new Dictionary<string, double>() { { "r", 12.0 } }));
 0152            Assert.False(stringComparer.Equals(new Dictionary<string, object>() { { "c", "other" } }, y));
 0153        }
 154
 155        [Fact]
 156        public void IEquatableTakesPrecedenceOverIEnumerable()
 157        {
 0158            var ints = new BiasedList(1, 2, 3);
 0159            var odds = new BiasedList(1, 3, 5);
 0160            var evens = new BiasedList(2, 4, 6);
 161
 0162            var comparer = new ModelComparer<BiasedList>();
 163
 0164            Assert.True(comparer.Equals(ints, ints));
 0165            Assert.True(comparer.Equals(ints, new BiasedList(ints)));
 166
 167            // ints and odds should compare as equal because BiasedList only looks at the first element.
 0168            Assert.True(comparer.Equals(odds, ints));
 0169            Assert.True(comparer.Equals(new BiasedList(odds), ints));
 170
 0171            Assert.False(comparer.Equals(ints, evens));
 0172            Assert.False(comparer.Equals(ints, new BiasedList(evens)));
 0173        }
 174
 175        [Fact]
 176        public void CanComparePolymorphicObjects()
 177        {
 0178            var cat = new Cat("Gizmo");
 0179            var otherCat = new Cat("Ness");
 0180            var dog = new Dog("Mara");
 0181            var otherDog = new Dog("Daisy");
 182
 0183            var comparer = new ModelComparer<Animal>();
 184
 0185            Assert.Equal(dog, dog, comparer);
 0186            Assert.Equal(dog, new Dog(dog), comparer);
 0187            Assert.Equal(cat, cat, comparer);
 0188            Assert.Equal(cat, new Cat(cat), comparer);
 189
 0190            Assert.NotEqual(dog, otherDog, comparer);
 0191            Assert.NotEqual(cat, otherCat, comparer);
 0192            Assert.NotEqual(dog, cat, comparer);
 0193        }
 194
 195        [Fact]
 196        public void CanCompareDtos()
 197        {
 0198            var model = new Model() { Name = "Magical Trevor", Age = 11 };
 0199            var sameModel = new Model(model);
 0200            var differentModel = new Model() { Name = "Mr. Stabby", Age = 35 };
 201
 0202            var comparer = new ModelComparer<Model>();
 203
 0204            Assert.Equal(model, model, comparer);
 0205            Assert.Equal(model, sameModel, comparer);
 0206            Assert.NotEqual(model, differentModel, comparer);
 0207        }
 208
 209        [Fact]
 210        public void ComparingMarkerClassesAlwaysReturnsTrue()
 211        {
 0212            var marker = new Empty();
 0213            var otherMarker = new Empty();
 214
 0215            Assert.Equal(marker, marker, new ModelComparer<Empty>());
 0216            Assert.Equal(marker, otherMarker, new ModelComparer<Empty>());
 0217        }
 218
 219        [Fact]
 220        public void CanCompareDifferentEnumerableTypes()
 221        {
 0222            var intArray = new[] { 1, 2, 3 };
 0223            var intList = new List<int>() { 1, 2, 3 };
 224
 0225            var intComparer = new ModelComparer<IEnumerable<int>>();
 226
 227            // We have to use Assert.True/False instead of .Equal/.NotEqual or we won't actually exercise the ModelCompa
 0228            Assert.True(intComparer.Equals(intArray, intList));
 0229        }
 230
 231        [Fact]
 232        public void IntegerComparisonsUseWidestType()
 233        {
 0234            byte b = 10;
 0235            short s = 10;
 0236            int i = 10;
 0237            long l = 10;
 238
 0239            var comparer = new ModelComparer<object>();
 240
 0241            Assert.Equal(b, s, comparer);
 0242            Assert.Equal(b, i, comparer);
 0243            Assert.Equal(b, l, comparer);
 0244            Assert.Equal(s, b, comparer);
 0245            Assert.Equal(s, i, comparer);
 0246            Assert.Equal(s, l, comparer);
 0247            Assert.Equal(i, b, comparer);
 0248            Assert.Equal(i, s, comparer);
 0249            Assert.Equal(i, l, comparer);
 0250            Assert.Equal(l, b, comparer);
 0251            Assert.Equal(l, s, comparer);
 0252            Assert.Equal(l, i, comparer);
 0253        }
 254
 255        [Fact]
 256        public void CanCompareComplexModels()
 257        {
 0258            var homeAddress = new USAddress()
 0259            {
 0260                Street = "308 Negra Arroyo Lane",
 0261                City = "Albuquerque",
 0262                State = "New Mexico",
 0263                ZipCode = 87104
 0264            };
 265
 0266            var alternateAddress = new USAddress()
 0267            {
 0268                Street = "Los Pollos Hermanos, 123 Central Ave Southeast",
 0269                City = "Albuquerque",
 0270                State = "New Mexico",
 0271                ZipCode = 87108
 0272            };
 273
 0274            var theOneWhoKnocks = new Customer()
 0275            {
 0276                FirstName = "Walter",
 0277                LastName = "White",
 0278                Aliases = new[] { "Heisenberg", "The One Who Knocks" },
 0279                Age = 51,
 0280                Address = homeAddress,
 0281                Directions = new List<Direction?>() { null, Direction.Left, null, Direction.Right },
 0282                Occupations = new Dictionary<string, Address>()
 0283                {
 0284                    { "Husband and father", homeAddress },
 0285                    { "Career criminal", alternateAddress }
 0286                }
 0287            };
 288
 0289            var walterWhite = theOneWhoKnocks.Clone();
 290
 0291            var gustavoFring = new Customer()
 0292            {
 0293                FirstName = "Gustavo",
 0294                LastName = "Fring",
 0295                Age = 49,
 0296                Address = alternateAddress
 0297            };
 298
 0299            var comparer = new ModelComparer<Customer>();
 300
 0301            Assert.Equal(theOneWhoKnocks, theOneWhoKnocks, comparer);
 0302            Assert.Equal(walterWhite, theOneWhoKnocks, comparer);
 0303            Assert.NotEqual(gustavoFring, theOneWhoKnocks, comparer);
 0304        }
 305
 306        private class Empty { }
 307
 308        private class Model
 309        {
 0310            public Model() { }
 311
 0312            public Model(Model other)
 313            {
 0314                Name = other.Name;
 0315                Age = other.Age;
 0316            }
 317
 0318            public string Name { get; set; }
 319
 0320            public int Age { get; set; }
 321        }
 322
 323        private struct FancyDirection : IEquatable<FancyDirection>
 324        {
 325            private readonly string _value;
 326
 0327            public static readonly FancyDirection Left = new FancyDirection("left");
 328
 0329            public static readonly FancyDirection Right = new FancyDirection("right");
 330
 331            private FancyDirection(string name)
 332            {
 0333                _value = name ?? throw new ArgumentNullException(nameof(name));
 0334            }
 335
 0336            public static bool operator ==(FancyDirection lhs, FancyDirection rhs) => Equals(lhs, rhs);
 337
 0338            public static bool operator !=(FancyDirection lhs, FancyDirection rhs) => !Equals(lhs, rhs);
 339
 0340            public bool Equals(FancyDirection other) => _value == other._value;
 341
 0342            public override bool Equals(object obj) => obj is FancyDirection ? Equals((FancyDirection)obj) : false;
 343
 0344            public override int GetHashCode() => _value.GetHashCode();
 345
 0346            public override string ToString() => _value;
 347        }
 348
 349        // Biased lists only care about the first element when it comes to equality comparison.
 350        private class BiasedList : IEnumerable<int>, IEquatable<BiasedList>
 351        {
 352            private readonly int[] _values;
 353
 0354            public BiasedList(params int[] values)
 355            {
 0356                if (values == null)
 357                {
 0358                    throw new ArgumentNullException(nameof(values));
 359                }
 360
 0361                if (values.Length == 0)
 362                {
 0363                    throw new ArgumentOutOfRangeException(nameof(values), "List must be non-empty");
 364                }
 365
 0366                _values = values;
 0367            }
 368
 0369            public BiasedList(BiasedList other)
 370            {
 0371                if (other == null)
 372                {
 0373                    throw new ArgumentNullException(nameof(other));
 374                }
 375
 0376                _values = (int[])other._values.Clone();
 0377            }
 378
 0379            public bool Equals(BiasedList other) => other != null && this.First() == other.First();
 380
 0381            public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)_values).GetEnumerator();
 382
 0383            IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();
 384        }
 385
 386        private abstract class Animal
 387        {
 0388            protected Animal(string name)
 389            {
 0390                Name = name;
 0391            }
 392
 393            public abstract string Noise { get; }
 394
 0395            public string Name { get; }
 396        }
 397
 398        private sealed class Dog : Animal, IEquatable<Dog>
 399        {
 400            private readonly Random _random;
 401
 0402            public Dog(string name) : base(name)
 403            {
 0404                _random = new Random();
 0405            }
 406
 0407            public Dog(Dog dog) : this(dog.Name) { }
 408
 409            // Add some randomness to ensure that IEquatable always takes precedence.
 0410            public override string Noise => _random.Next() % 2 == 0 ? "woof!" : "bark!";
 411
 0412            public bool Equals(Dog other) => other != null && this.Name == other.Name;
 413        }
 414
 415        private sealed class Cat : Animal, IEquatable<Cat>
 416        {
 0417            public Cat(string name) : base(name) { }
 418
 0419            public Cat(Cat cat) : this(cat.Name) { }
 420
 0421            public override string Noise => "meow!";
 422
 0423            public bool Equals(Cat other) => other != null && this.Name == other.Name;
 424        }
 425
 426        private class Customer
 427        {
 0428            public Customer() { }
 429
 0430            public Customer(Customer other)
 431            {
 0432                Aliases = other.Aliases.ToArray();
 0433                Age = other.Age;
 0434                Address = other.Address.Clone();
 0435                FirstName = other.FirstName;
 0436                LastName = other.LastName;
 0437                Directions = other.Directions.ToList();
 0438                Occupations = other.Occupations.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Clone());
 0439            }
 440
 0441            public string Name => FirstName + " " + LastName;
 442
 0443            public IList<string> Aliases { get; set; }
 444
 0445            public int? Age { get; set; }
 446
 0447            public Address Address { get; set; }
 448
 0449            public string FirstName { get; set; }
 450
 0451            public string LastName { get; set; }
 452
 0453            public List<Direction?> Directions { get; set; }
 454
 0455            public Dictionary<string, Address> Occupations { get; set; }
 456
 0457            public Customer Clone() => new Customer(this);
 458        }
 459
 460        private abstract class Address
 461        {
 0462            public string Street { get; set; }
 463
 0464            public string City { get; set; }
 465
 466            public abstract Address Clone();
 467        }
 468
 469        private sealed class USAddress : Address
 470        {
 0471            public USAddress() { }
 472
 0473            public USAddress(USAddress other)
 474            {
 0475                Street = other.Street;
 0476                City = other.City;
 0477                State = other.State;
 0478                ZipCode = other.ZipCode;
 0479            }
 480
 0481            public string State { get; set; }
 482
 0483            public int ZipCode { get; set; }
 484
 0485            public override Address Clone() => new USAddress(this);
 486        }
 487    }
 488}