Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Collections/OrderedHashSetTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

97 lines
2.5 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using NUnit.Framework;
using Umbraco.Cms.Core.Collections;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Collections
{
[TestFixture]
public class OrderedHashSetTests
{
[Test]
public void Keeps_Last()
{
var list = new OrderedHashSet<MyClass>(keepOldest: false);
MyClass[] items = new[] { new MyClass("test"), new MyClass("test"), new MyClass("test") };
foreach (MyClass item in items)
{
list.Add(item);
}
Assert.AreEqual(1, list.Count);
Assert.AreEqual(items[2].Id, list[0].Id);
Assert.AreNotEqual(items[0].Id, list[0].Id);
}
[Test]
public void Keeps_First()
{
var list = new OrderedHashSet<MyClass>(keepOldest: true);
MyClass[] items = new[] { new MyClass("test"), new MyClass("test"), new MyClass("test") };
foreach (MyClass item in items)
{
list.Add(item);
}
Assert.AreEqual(1, list.Count);
Assert.AreEqual(items[0].Id, list[0].Id);
}
private class MyClass : IEquatable<MyClass>
{
public MyClass(string name)
{
Name = name;
Id = Guid.NewGuid();
}
public string Name { get; }
public Guid Id { get; }
public bool Equals(MyClass other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((MyClass)obj);
}
public override int GetHashCode() => Name.GetHashCode();
public static bool operator ==(MyClass left, MyClass right) => Equals(left, right);
public static bool operator !=(MyClass left, MyClass right) => Equals(left, right) == false;
}
}
}