Skip to content

Commit

Permalink
GD-8: Add generic VectorAssert to support all Godot Vector types
Browse files Browse the repository at this point in the history
# Why
We want to have one assert for all Godot vector types, actual we have only covered Vector2 and Vector3

# What
- Removed the interfaces and implementations for Vector2 and Vector3
- Introduced IVectorAssert<T> and implementation
- make the old asserts as deprecated.
- removed the custom float and vector format
- set explicit the culture to "en-us" to get expected failure messages in english notation
- fix test name generation to use culture to "en-us" for parameter formattings
  • Loading branch information
Nullpointer committed Feb 29, 2024
1 parent bd8e300 commit c6deba8
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 511 deletions.
39 changes: 34 additions & 5 deletions api/src/Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

namespace GdUnit4
{
using System.Diagnostics.CodeAnalysis;
using Asserts;

/// <summary>
Expand Down Expand Up @@ -56,19 +55,29 @@ public sealed class Assertions
/// <returns></returns>
public static IEnumerableAssert AssertArray(IEnumerable? current) => new EnumerableAssert(current);

/// <summary>
/// An assertion method for all Godot vector types.
/// </summary>
/// <typeparam name="T">The type of Godot vector.</typeparam>
/// <param name="vector">The vector value to verify.</param>
/// <returns>An instance of IVectorAssert for further assertions.</returns>
public static IVectorAssert<T> AssertVector<T>(T vector) where T : notnull, IEquatable<T> => new VectorAssert<T>(vector);

/// <summary>
/// An Assertion to verify Godot.Vector2 values
/// </summary>
/// <param name="current">The current vector2 value to verify</param>
/// <returns></returns>
public static IVector2Assert AssertVec2(Godot.Vector2 current) => new Vector2Assert(current);
[Obsolete("AssertVec2 is deprecated, please use AssertVector instead.")]
public static IVectorAssert<Godot.Vector2> AssertVec2(Godot.Vector2 current) => AssertVector<Godot.Vector2>(current);

/// <summary>
/// An Assertion to verify Godot.Vector3 values
/// </summary>
/// <param name="current">The current vector3 value to verify</param>
/// <returns></returns>
public static IVector3Assert AssertVec3(Godot.Vector3 current) => new Vector3Assert(current);
[Obsolete("AssertVec3 is deprecated, please use AssertVector instead.")]
public static IVectorAssert<Godot.Vector3> AssertVec3(Godot.Vector3 current) => AssertVector<Godot.Vector3>(current);

/// <summary>
/// An Assertion used by test generation to notify the test is not yet implemented
Expand Down Expand Up @@ -103,6 +112,7 @@ public sealed class Assertions
public static INumberAssert<double> AssertThat(double current) => new NumberAssert<double>(current);
public static INumberAssert<decimal> AssertThat(decimal current) => new NumberAssert<decimal>(current);


public static IDictionaryAssert<K, V> AssertThat<K, V>(IDictionary<K, V>? current) where K : notnull
=> new DictionaryAssert<K, V>(current?.ToDictionary(e => e.Key, e => e.Value));

Expand All @@ -112,8 +122,18 @@ public sealed class Assertions
public static IDictionaryAssert<TKey, TValue> AssertThat<[Godot.MustBeVariant] TKey, [Godot.MustBeVariant] TValue>(Godot.Collections.Dictionary<TKey, TValue>? current) where TKey : notnull
=> new DictionaryAssert<TKey, TValue>(current);

public static IVector2Assert AssertThat(Godot.Vector2 current) => new Vector2Assert(current);
public static IVector3Assert AssertThat(Godot.Vector3 current) => new Vector3Assert(current);

/// <summary>
/// The dynamic assertions for all Godot vector types.
/// </summary>
/// <param name="current">The vector value to verify.</param>
/// <returns>An instance of IVectorAssert for further assertions.</returns>
public static IVectorAssert<Godot.Vector2> AssertThat(Godot.Vector2 current) => new VectorAssert<Godot.Vector2>(current);
public static IVectorAssert<Godot.Vector2I> AssertThat(Godot.Vector2I current) => new VectorAssert<Godot.Vector2I>(current);
public static IVectorAssert<Godot.Vector3> AssertThat(Godot.Vector3 current) => new VectorAssert<Godot.Vector3>(current);
public static IVectorAssert<Godot.Vector3I> AssertThat(Godot.Vector3I current) => new VectorAssert<Godot.Vector3I>(current);
public static IVectorAssert<Godot.Vector4> AssertThat(Godot.Vector4 current) => new VectorAssert<Godot.Vector4>(current);
public static IVectorAssert<Godot.Vector4I> AssertThat(Godot.Vector4I current) => new VectorAssert<Godot.Vector4I>(current);


/// <summary>
Expand Down Expand Up @@ -233,5 +253,14 @@ public static dynamic AssertThat<T>(T? current)
/// Builds an extractor by given method name and optional arguments
/// </summary>
public static IValueExtractor Extr(string methodName, params object[] args) => new ValueExtractor(methodName, args);


/// <summary>
/// Provides the expected line number via compile state.
/// Is primary designed to use on internal test coverage to validate the reported error line is correct.
/// </summary>
/// <param name="lineNumber"></param>
/// <returns></returns>
internal static int ExpectedLineNumber([System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) => lineNumber - 1;
}
}
79 changes: 0 additions & 79 deletions api/src/IVector2Assert.cs

This file was deleted.

79 changes: 0 additions & 79 deletions api/src/IVector3Assert.cs

This file was deleted.

79 changes: 79 additions & 0 deletions api/src/IVectorAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace GdUnit4.Asserts;
using System;

/// <summary> An Assertion tool to verify Godot.Vector values </summary>
public interface IVectorAssert<T> : IAssertBase<T> where T : IEquatable<T>
{
/// <summary>
/// Verifies that the current value is equal to expected one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public new IVectorAssert<T> IsEqual(T expected);

/// <summary>
/// Verifies that the current value is not equal to expected one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public new IVectorAssert<T> IsNotEqual(T expected);

/// <summary>
/// Verifies that the current and expected value are approximately equal.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="approx">The approximal value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsEqualApprox(T expected, T approx);

/// <summary>
/// Verifies that the current value is less than the given one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsLess(T expected);

/// <summary>
/// Verifies that the current value is less than or equal the given one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsLessEqual(T expected);

/// <summary>
/// Verifies that the current value is greater than the given one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsGreater(T expected);

/// <summary>
/// Verifies that the current value is greater than or equal the given one.
/// </summary>
/// <param name="expected">The expected value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsGreaterEqual(T expected);

/// <summary>
/// Verifies that the current value is between the given boundaries (inclusive).
/// </summary>
/// <param name="min">The minimal value</param>
/// <param name="max">The maximal value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsBetween(T min, T max);

/// <summary>
/// Verifies that the current value is not between the given boundaries (inclusive).
/// </summary>
/// <param name="min">The minimal value</param>
/// <param name="max">The maximal value</param>
/// <returns>IVectorAssert</returns>
public IVectorAssert<T> IsNotBetween(T min, T max);

/// <summary>
/// Overrides the default failure message by given custom message.
/// </summary>
/// <param name="message">The message to replace the default message</param>
/// <returns>IVectorAssert</returns>
new IVectorAssert<T> OverrideFailureMessage(string message);
}
62 changes: 0 additions & 62 deletions api/src/asserts/Vector2Assert.cs

This file was deleted.

Loading

0 comments on commit c6deba8

Please sign in to comment.