https://github.com/annulusgames/chainingassertion-unity
Chaining Assertion for Unity Test Framework
https://github.com/annulusgames/chainingassertion-unity
assertions testing unity
Last synced: over 1 year ago
JSON representation
Chaining Assertion for Unity Test Framework
- Host: GitHub
- URL: https://github.com/annulusgames/chainingassertion-unity
- Owner: annulusgames
- License: mit
- Created: 2024-01-05T15:37:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-08T00:52:37.000Z (over 2 years ago)
- Last Synced: 2025-03-02T03:24:15.663Z (over 1 year ago)
- Topics: assertions, testing, unity
- Language: C#
- Homepage:
- Size: 50.8 KB
- Stars: 14
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ChainingAssertion for Unity
Chaining Assertion for Unity Test Framework
[](LICENSE)
[日本語版READMEはこちら](README_JA.md)
ChainingAssertion for Unity is a method chaining based assertion library for Unity Test Framework.
ChainingAssertion for Unity adds extension methods such as `Is()` and `AsDynamic()` from the original [ChainingAssertion](https://github.com/neuecc/ChainingAssertion) to Unity Test Framework.
Additionally, there are additional functions such as `IsApproximately()`, which performs comparisons that take floating-point errors into account for float, Vector3, and Quaternion, and `AsPrivateObject()`, which supports access to private members using reflection for testing in IL2CPP (AOT environment).
## Setup
### Requirements
* Unity 2019.2 or later
* Test Framework 1.0.0 or later
### Installation
1. Open the Package Manager from Window > Package Manager.
2. Click on the "+" button > Add package from git URL.
3. Enter the following URL:
```
https://github.com/AnnulusGames/ChainingAssertion-Unity.git?path=Assets/ChainingAssertion
```
Alternatively, open Packages/manifest.json and add the following to the dependencies block:
```json
{
"dependencies": {
"com.annulusgames.chaining-assertion": "https://github.com/AnnulusGames/ChainingAssertion-Unity.git?path=Assets/ChainingAssertion"
}
}
```
## Is / IsNot
You can intuitively perform assertions using the `Is()`/`IsNot()` extension methods.
```csharp
using System;
using System.Linq;
using NUnit.Framework;
using ChainingAssertion;
public class Example
{
[Test]
public void Test_Is()
{
Math.Pow(5, 2).Is(25); // Assert.Equals
Math.Abs(-1).IsNot(-1); // Assert.NotEquals
}
[Test]
public void Test_Is_Lambda()
{
// Assert.IsTrue(x => ...);
"ChainingAssertion".Is(s => s.StartsWith("Chaining") && s.EndsWith("Assertion"));
}
[Test]
public void Test_Is_Collection()
{
Enumerable.Range(0, 5).Is(0, 1, 2, 3, 4); // CollectionAssert.AreEqual
Enumerable.Range(0, 5).IsNot(0, 1, 2, 3, 4, 5); // CollectionAssert.AreNotEqual
}
}
```
## Collection Assertions
Combining LINQ to Objects with Chaining Assertion allows expressing assertions similar to CollectionAssert.
```csharp
var array = new[] { 1, 3, 7, 8 };
array.Contains(8).IsTrue(); // CollectionAssert.Contains
array.Any().IsTrue(); // CollectionAssert.IsNotEmpty
new int[] { }.Any().IsFalse(); // CollectionAssert.IsEmpty
array.OrderBy(x => x).Is(array); // CollectionAssert.IsOrdered
```
## IsApproximately
For floating-point types like `float`, `Vector3`, `Quaternion`, etc., you can perform comparisons considering errors using `IsApproximately()`.
```csharp
transform.position.IsApproximately(Vector3.one);
transform.localEulerAngles.IsApproximately(new Vector3(0f, 0f, 90f), 0.001f);
```
## Other Assertions
```csharp
using System;
using NUnit.Framework;
using ChainingAssertion;
public class Example
{
[Test]
public void Test_IsNull()
{
object target = null;
target.IsNull(); // Assert.IsNull
new object().IsNotNull(); // Assert.IsNull
}
[Test]
public void Test_IsSameReferenceAs()
{
var tuple = Tuple.Create("foo");
tuple.IsSameReferenceAs(tuple); // Assert.AreSame
tuple.IsNotSameReferenceAs(Tuple.Create("foo")); // Assert.AreNotSame
}
[Test]
public void Test_IsInstanceOf()
{
"foo".IsInstanceOf(); // Assert.IsInstanceOfType
999.IsNotInstanceOf(); // Assert.IsNotInstanceOfType
}
}
```
## AsDynamic
Using `AsDynamic()` converts the target to a dynamic type, enabling dynamic access to private members.
> [!WARNING]
> `AsDynamic()` does not work in an IL2CPP environment. Therefore, it's recommended to use `AsPrivateObject()` instead for PlayMode tests.
```csharp
public class TestClass
{
public TestClass(string str)
{
_privateField = str;
}
private string _privateField;
private string PrivateProperty
{
get => _privateField;
set => _privateField = value;
}
private string PrivateMethod()
{
return _privateField;
}
}
var actual = new TestClass("foo");
Assert.AreEqual(actual.AsDynamic()._privateField, "foo");
Assert.AreEqual(actual.AsDynamic().PrivateProperty, "foo");
Assert.AreEqual(actual.AsDynamic().PrivateMethod(), "foo");
actual.AsDynamic().PrivateProperty = "bar";
Assert.AreEqual(actual.AsDynamic()._privateField, "bar");
```
## AsPrivateObject
Using `AsPrivateObject()` converts the target to a `PrivateObject`, enabling access to private members using reflection. Use this instead of `AsDynamic()` when executing tests in an IL2CPP environment.
```csharp
var actual = new TestClass("foo");
Assert.AreEqual(actual.AsPrivateObject().GetField("_privateField"), "foo");
Assert.AreEqual(actual.AsPrivateObject().GetProperty("PrivateProperty"), "foo");
Assert.AreEqual(actual.AsPrivateObject().Invoke("PrivateMethod"), "foo");
actual.AsPrivateObject().SetFieldOrProperty("PrivateProperty", "bar");
Assert.AreEqual(actual.AsPrivateObject().GetFieldOrProperty("_privateField"), "bar");
```
## License
[MIT License](LICENSE)