Skip to content

Commit

Permalink
add tests for remaining testable utils methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dit-zy committed Jul 28, 2024
1 parent 49fca69 commit 3d04ca1
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
46 changes: 44 additions & 2 deletions ScoutHelperTests/TestUtils/FsCheck/Arbs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Collections.Immutable;
using System.Numerics;
using CSharpFunctionalExtensions;
using FsCheck;
using ScoutHelper;
Expand All @@ -20,6 +21,26 @@ public static Arbitrary<string> NonEmptyString() =>
.Where(s => !string.IsNullOrEmpty(s))
.ToArbitrary();

public static Arbitrary<float> Float() => Arb.Default.Float32();

public static Arbitrary<Vector2> Vector2() =>
Float()
.Choose2()
.Select(vals => new Vector2(vals.Item1, vals.Item2))
.ToArbitrary();

public static Arbitrary<Vector3> Vector3() =>
Float()
.Choose3()
.Select(vals => new Vector3(vals.Item1, vals.Item2, vals.Item3))
.ToArbitrary();

public static Arbitrary<Vector4> Vector4() =>
Float()
.Choose4()
.Select(vals => new Vector4(vals.Item1, vals.Item2, vals.Item3, vals.Item4))
.ToArbitrary();

public static Arbitrary<IDictionary<K, V>> DictOf<K, V>(Gen<K> keyGen, Gen<V> valueGen) where K : notnull =>
FsCheckUtils.Zip(keyGen, valueGen)
.ListOf()
Expand Down Expand Up @@ -227,6 +248,27 @@ public static Gen<U> SelectMany<T, U>(this Arbitrary<T> arb, Func<T, Gen<U>> sel
public static Gen<T> Where<T>(this Arbitrary<T> arb, Func<T, bool> predicate) => arb.Generator.Where(predicate);

public static Gen<A> KeepFirst<A, B>(this Gen<(A, B)> source) => source.Select(pair => pair.Item1);

public static Gen<B> KeepSecond<A, B>(this Gen<(A, B)> source) => source.Select(pair => pair.Item2);

public static Arbitrary<(A a, A b)> Choose2<A>(this Arbitrary<A> source) =>
source
.Generator
.Two()
.Select(vals => (vals.Item1, vals.Item2))
.ToArbitrary();

public static Arbitrary<(A a, A b, A c)> Choose3<A>(this Arbitrary<A> source) =>
source
.Generator
.Three()
.Select(vals => (vals.Item1, vals.Item2, vals.Item3))
.ToArbitrary();

public static Arbitrary<(A a, A b, A c, A d)> Choose4<A>(this Arbitrary<A> source) =>
source
.Generator
.Four()
.Select(vals => (vals.Item1, vals.Item2, vals.Item3, vals.Item4))
.ToArbitrary();
}
65 changes: 65 additions & 0 deletions ScoutHelperTests/Utils/UtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Dalamud.Game.ClientState.Objects.SubKinds;
using Dalamud.Plugin.Services;
using FluentAssertions;
Expand All @@ -17,6 +18,70 @@ public UtilsTests(TestFixture fixture) {
_fixture = fixture;
}

[Property]
public Property V2_Single() => FsCheckUtils.ForAll(
Arbs.Float(),
xy =>
ScoutHelper.Utils.Utils.V2(xy).Should().Be(new Vector2(xy, xy))
);

[Property]
public Property V2() => FsCheckUtils.ForAll(
Arbs.Vector2(),
expected =>
ScoutHelper.Utils.Utils.V2(expected.X, expected.Y).Should().Be(expected)
);

[Property]
public Property V4() => FsCheckUtils.ForAll(
Arbs.Vector4(),
expected =>
ScoutHelper.Utils.Utils.V4(expected.X, expected.Y, expected.Z, expected.W).Should().Be(expected)
);

[Property]
public Property Color() => FsCheckUtils.ForAll(
Arbs.Vector4(),
expected =>
ScoutHelper.Utils.Utils.Color(expected.X, expected.Y, expected.Z, expected.W).Should().Be(expected)
);

[Property]
public Property Color_3() => FsCheckUtils.ForAll(
Arbs.Vector3(),
expected =>
ScoutHelper.Utils.Utils.Color(expected.X, expected.Y, expected.Z)
.Should().Be(new Vector4(expected.X, expected.Y, expected.Z, 1f))
);

[Property]
public Property Color_Uint() => FsCheckUtils.ForAll(
Arb.Default.UInt32().Choose4(),
components =>
ScoutHelper.Utils.Utils.Color(components.a, components.b, components.c, components.d)
.Should().Be(new Vector4(components.a / 256f, components.b / 256f, components.c / 256f, components.d / 256f))
);

[Property]
public Property Color_Uint3() => FsCheckUtils.ForAll(
Arb.Default.UInt32().Choose3(),
components =>
ScoutHelper.Utils.Utils.Color(components.a, components.b, components.c)
.Should().Be(new Vector4(components.a / 256f, components.b / 256f, components.c / 256f, 1f))
);

[Fact]
public void GetEnumValues() {
// DATA
var expected = new[] { TestEnum.A, TestEnum.B, TestEnum.C, TestEnum.D, TestEnum.E };

// WHEN
var actual = GetEnumValues<TestEnum>();

// THEN
actual.Should().Equal(expected);
}

[Fact]
public void WorldName_NoPlayer() {
// DATA
Expand Down

0 comments on commit 3d04ca1

Please sign in to comment.