Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
21 changes: 21 additions & 0 deletions src/StaticSettingsTests/RegisterFileConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class RegisterFileConverterTests :
BaseTest
{
[Fact]
public void ObjectThrows()
{
var exception = Assert.Throws<Exception>(
() => VerifierSettings.RegisterFileConverter<object>(
(_, _) => new(null, "txt", "value")));
Assert.Contains("too greedy", exception.Message);
}

[Fact]
public void ObjectAsyncThrows()
{
var exception = Assert.Throws<Exception>(
() => VerifierSettings.RegisterFileConverter<object>(
(_, _) => Task.FromResult<ConversionResult>(new(null, "txt", "value"))));
Assert.Contains("too greedy", exception.Message);
}
}
10 changes: 10 additions & 0 deletions src/Verify/Splitters/Settings_Typed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void RegisterFileConverter<T>(
CanConvert<T>? canConvert = null)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
ThrowIfObjectConverter<T>();
RegisterFileConverter(
(target, context) => Task.FromResult(conversion(target, context)),
canConvert);
Expand All @@ -36,10 +37,19 @@ public static void RegisterFileConverter<T>(
CanConvert<T>? canConvert = null)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
ThrowIfObjectConverter<T>();
var converter = new TypeConverter((target, context) => conversion((T) target, context), DefaultCanConvert(canConvert));
typedConverters.Add(converter);
}

static void ThrowIfObjectConverter<T>()
{
if (typeof(T) == typeof(object))
{
throw new("RegisterFileConverter<object> is too greedy since object matches all verified values. Instead use a more specific type, or use the non-generic RegisterFileConverter overload that takes an explicit `canConvert`.");
}
}

public static void RegisterFileConverter(
Conversion conversion,
CanConvert canConvert)
Expand Down
Loading