Skip to content

Commit

Permalink
Merge pull request #27 from nowsprinting/feature/gameobjectname_comparer
Browse files Browse the repository at this point in the history
Add GameObjectNameComparer
  • Loading branch information
nowsprinting committed Oct 28, 2023
2 parents 40ef007 + 42d690e commit 103dcb9
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 3 deletions.
32 changes: 29 additions & 3 deletions 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 @@ -193,10 +193,10 @@ using UnityEngine;
public class MyTestClass
{
[Test]
[LoadScene("Assets/MyTests/Scenes/Scene.unity")]
[LoadScene("Assets/MyTests/Scenes/TestScene.unity")]
public void MyTestMethod()
{
var cube = GameObject.Find("Cube");
var cube = GameObject.Find("Cube in TestScene");
Assert.That(cube, Is.Not.Null);
}
}
Expand All @@ -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 = GameObject.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.

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

using System;
using NUnit.Framework;
using UnityEngine;

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

Assert.That(actual, Is.EqualTo(new GameObject("test object")).Using(new GameObjectNameComparer()));
}

[Test]
public void UsingWithEqualTo_NotEqualName_Failure()
{
var actual = new GameObject("actual object");

Assert.That(() =>
{
Assert.That(actual, Is.EqualTo(new GameObject("expected object")).Using(new GameObjectNameComparer()));
}, Throws.TypeOf<AssertionException>().With.Message.EqualTo(
$" Expected: <expected object (UnityEngine.GameObject)>{Environment.NewLine} But was: <actual object (UnityEngine.GameObject)>{Environment.NewLine}"));
}

[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()));
}

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

Assert.That(() =>
{
Assert.That(actual, Does.Contain(new GameObject("test4")).Using(new GameObjectNameComparer()));
}, Throws.TypeOf<AssertionException>().With.Message.EqualTo(
$" Expected: collection containing <test4 (UnityEngine.GameObject)>{Environment.NewLine} But was: < <test1 (UnityEngine.GameObject)>, <test2 (UnityEngine.GameObject)>, <test3 (UnityEngine.GameObject)> >{Environment.NewLine}"));
}
}
}
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 103dcb9

Please sign in to comment.