Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NullArgumentTest to cover GetShortestPath methods #475

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Source/SuperLinq/GetShortestPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ public partial class SuperEnumerable
break;

var newStates = getNeighbors(current, cost);
Guard.IsNotNull(newStates, $"{nameof(getNeighbors)}()");

foreach (var (s, p) in newStates)
{
if (!totalCost.TryGetValue(s, out _))
Expand Down Expand Up @@ -584,6 +586,8 @@ public partial class SuperEnumerable

var cost = from.cost;
var newStates = getNeighbors(current, cost);
Guard.IsNotNull(newStates, $"{nameof(getNeighbors)}()");

foreach (var (s, p) in newStates)
{
if (!totalCost.TryGetValue(s, out _))
Expand Down Expand Up @@ -767,6 +771,8 @@ public partial class SuperEnumerable

var cost = from.cost;
var newStates = getNeighbors(current, cost);
Guard.IsNotNull(newStates, $"{nameof(getNeighbors)}()");

foreach (var (s, p) in newStates)
{
if (!totalCost.TryGetValue(s, out _))
Expand Down Expand Up @@ -1083,6 +1089,8 @@ public partial class SuperEnumerable
break;

var newStates = getNeighbors(current, costs.traversed);
Guard.IsNotNull(newStates, $"{nameof(getNeighbors)}()");

foreach (var (s, p, h) in newStates)
queue.EnqueueMinimum(s, (h, p));
}
Expand Down Expand Up @@ -1410,6 +1418,8 @@ public partial class SuperEnumerable

var cost = from.traversed;
var newStates = getNeighbors(current, cost);
Guard.IsNotNull(newStates, $"{nameof(getNeighbors)}()");

foreach (var (s, p, h) in newStates)
queue.EnqueueMinimum(s, (current, h, p));
}
Expand Down
38 changes: 24 additions & 14 deletions Tests/SuperLinq.Test/NullArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,33 @@ public void CanBeNull(Action inlineData) =>
public static IEnumerable<object[]> GetNotNullInlineDatas() =>
GetInlineDatas(canBeNull: false, inlineDataFactory: (method, args, paramName) => () =>
{
Exception? e = null;
var tie = Assert.Throws<TargetInvocationException>(() =>
method.Invoke(null, args));

var e = tie.InnerException;
Assert.NotNull(e);

var ane = Assert.IsAssignableFrom<ArgumentNullException>(e);
Assert.Equal(paramName, ane.ParamName);
});

public static IEnumerable<object[]> GetCanBeNullInlineDatas() =>
GetInlineDatas(canBeNull: true, inlineDataFactory: (method, args, paramName) => () =>
{
try
{
_ = method.Invoke(null, args);
}
catch (TargetInvocationException tie)
{
e = tie.InnerException;
Assert.False(tie.InnerException != null
&& tie.InnerException is ArgumentNullException ane
&& ane.ParamName != paramName);
}

Assert.NotNull(e);
_ = Assert.IsAssignableFrom<ArgumentNullException>(e);
var ane = (ArgumentNullException)e!;
Assert.Equal(paramName, ane.ParamName);
});

public static IEnumerable<object[]> GetCanBeNullInlineDatas() =>
GetInlineDatas(canBeNull: true, inlineDataFactory: (method, args, _) => () => method.Invoke(null, args));

private static readonly string[] s_skipMethods =
{
nameof(SuperEnumerable.GetShortestPath),
nameof(SuperEnumerable.GetShortestPathCost),
nameof(SuperEnumerable.GetShortestPaths),
nameof(SuperEnumerable.CopyTo),
};

Expand Down Expand Up @@ -137,6 +139,7 @@ private static object CreateInstance(Type type)
if (type == typeof(JoinType)) return JoinType.Hash;
if (type == typeof(StringComparer)) return StringComparer.Ordinal;
if (type == typeof(TaskScheduler)) return TaskScheduler.Default;
if (type == typeof(IDisposable)) return new Disposable();
if (type.IsArray) return Array.CreateInstance(type.GetElementType()!, 0);
if (type.GetTypeInfo().IsValueType || HasDefaultConstructor(type)) return Activator.CreateInstance(type)!;
if (typeof(Delegate).IsAssignableFrom(type)) return CreateDelegateInstance(type);
Expand All @@ -155,7 +158,9 @@ private static Delegate CreateDelegateInstance(Type type)
{
var invoke = type.GetMethod("Invoke");
var parameters = invoke!.GetParameters().Select(p => Expression.Parameter(p.ParameterType, p.Name));
var body = Expression.Default(invoke.ReturnType); // requires >= .NET 4.0
Expression body = invoke.ReturnType == typeof(void)
? Expression.Default(invoke.ReturnType)
: Expression.Constant(CreateInstance(invoke.ReturnType)); // requires >= .NET 4.0
var lambda = Expression.Lambda(type, body, parameters);
return lambda.Compile();
}
Expand All @@ -169,6 +174,11 @@ private static object CreateGenericInterfaceInstance(TypeInfo type)
return Activator.CreateInstance(instantiation)!;
}

private class Disposable : IDisposable
{
public void Dispose() { }
}

private static class EmptyEnumerable
{
public static readonly IEnumerable Instance = new Enumerable();
Expand Down