Skip to content

Commit

Permalink
Add tests for TryCastArray methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Jan 11, 2024
1 parent 1a4f6fa commit 299c50e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public static void TryCastArray_CastIsInvalid_ExpectNull(
bool isSourceDefault)
{
var source = isSourceDefault ? default : new[] { PlusFifteen, Zero }.InitializeFlatArray();
var actual = source.TryCastArray<string>();

Assert.Null(actual);
}

[Theory]
[MemberData(nameof(TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState(
FlatArray<int> source, uint[]? expectedItems)
{
var actual = source.TryCastArray<uint>();

Assert.NotNull(actual);
actual.Value.VerifyTruncatedState(expectedItems);
}

[Theory]
[MemberData(nameof(TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState(
FlatArray<string?> source, object?[]? expectedItems)
{
var actual = source.TryCastArray<object?>();

Assert.NotNull(actual);
actual.Value.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<int>, uint[]?> TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource
=>
new()
{
{
default,
default
},
{
new int[] { PlusFifteen, Zero, One }.InitializeFlatArray(),
[ PlusFifteen, Zero, One ]
},
{
new int[] { int.MaxValue, One, PlusFifteen, Zero, MinusOne }.InitializeFlatArray(4),
[ int.MaxValue, One, PlusFifteen, Zero ]
}
};

public static TheoryData<FlatArray<string?>, object?[]?> TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource
=>
new()
{
{
default,
default
},
{
new string?[] { SomeString, null }.InitializeFlatArray(),
[ SomeString, null ]
},
{
new string?[] { null, AnotherString, EmptyString, UpperAnotherString, WhiteSpaceString }.InitializeFlatArray(3),
[ null, AnotherString, EmptyString ]
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

partial struct FlatArray<T>
{
// TODO: Add the tests and make public
internal FlatArray<TResult>? TryCastArray<TResult>()
public FlatArray<TResult>? TryCastArray<TResult>()
{
// Safe array cast: 'as' cast

Expand Down

0 comments on commit 299c50e

Please sign in to comment.