Skip to content

Commit

Permalink
Add GameObjectNameComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Oct 28, 2023
1 parent 40ef007 commit 4e59d33
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Test](https://github.com/nowsprinting/test-helper/actions/workflows/test.yml/badge.svg)](https://github.com/nowsprinting/test-helper/actions/workflows/test.yml)
[![openupm](https://img.shields.io/npm/v/com.nowsprinting.test-helper?label=openupm&registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.nowsprinting.test-helper/)

Provides attributes and constraints useful for testing.
Provides custom attributes, comparers, and constraints useful for testing with Unity Test Framework.

Required Unity 2019 LTS or later.

Expand Down Expand Up @@ -207,6 +207,32 @@ public class MyTestClass
> - Scene file path is starts with `Assets/` or `Packages/`. And package name using `name` instead of `displayName`, when scenes in the package. (e.g., `Packages/com.nowsprinting.test-helper/Tests/Scenes/Scene.unity`)

### Comparers

#### GameObjectNameComparer

`GameObjectNameComparer` is an NUnit test comparer class to compare GameObjects by name.

Usage:

```csharp
using NUnit.Framework;
using TestHelper.Comparers;
using UnityEngine;

[TestFixture]
public class GameObjectNameComparerTest
{
[Test]
public void UsingGameObjectNameComparer_CompareGameObjectsByName()
{
var actual = Object.FindObjectsOfType<GameObject>();
Assert.That(actual, Does.Contain(new GameObject("test")).Using(new GameObjectNameComparer()));
}
}
```


### Constraints

#### Destroyed
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Comparers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Runtime/Comparers/GameObjectNameComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UnityEngine;

namespace TestHelper.Comparers
{
/// <summary>
/// Compare GameObjects by name.
/// </summary>
public class GameObjectNameComparer : IComparer<GameObject>
{
/// <inheritdoc/>
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
public int Compare(GameObject x, GameObject y)
{
return string.Compare(x.name, y.name, StringComparison.Ordinal);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Comparers/GameObjectNameComparer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tests/Runtime/Comparers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Tests/Runtime/Comparers/GameObjectNameComparerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.

using NUnit.Framework;
using UnityEngine;

namespace TestHelper.Comparers
{
[TestFixture]
public class GameObjectNameComparerTest
{
[Test]
public void UsingWithEqualTo_CompareGameObjectsByName()
{
var actual = new GameObject("test");

Assert.That(actual, Is.EqualTo(new GameObject("test")).Using(new GameObjectNameComparer()));
// Message if failure:
// Expected: <test1 (UnityEngine.GameObject)>
// But was: <test (UnityEngine.GameObject)>
}

[Test]
public void UsingWithCollection_CompareGameObjectsByName()
{
var actual = new []
{
new GameObject("test1"), new GameObject("test2"), new GameObject("test3"),
};

Assert.That(actual, Does.Contain(new GameObject("test3")).Using(new GameObjectNameComparer()));
// Message if failure:
// Expected: collection containing <test4 (UnityEngine.GameObject)>
// But was: < <test1 (UnityEngine.GameObject)>, <test2 (UnityEngine.GameObject)>, <test3 (UnityEngine.GameObject)> >
}
}
}
3 changes: 3 additions & 0 deletions Tests/Runtime/Comparers/GameObjectNameComparerTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e59d33

Please sign in to comment.