Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 22, 2025
1 parent 12762d6 commit a0f0a31
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 25 deletions.
101 changes: 77 additions & 24 deletions src/Verify.Tests/Naming/NameForParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,83 @@
{
[Fact]
public Task Null() =>
Verify(VerifierSettings.GetNameForParameter(null));
Verify(VerifierSettings.GetNameForParameter(null, counter: Counter()));

[Fact]
public Task StringEmpty() =>
Verify(VerifierSettings.GetNameForParameter(""));
Verify(VerifierSettings.GetNameForParameter("", counter: Counter()));

[Fact]
public Task StringInvalidPathChar() =>
Verify(VerifierSettings.GetNameForParameter("a/a"));
Verify(VerifierSettings.GetNameForParameter("a/a", counter: Counter()));

[Fact]
public Task Int() =>
Verify(VerifierSettings.GetNameForParameter(10));
Verify(VerifierSettings.GetNameForParameter(10, counter: Counter()));

#if NET6_0_OR_GREATER
[Fact]
public Task Half() =>
Verify(VerifierSettings.GetNameForParameter((Half) 10));
Verify(
VerifierSettings.GetNameForParameter(
(Half) 10,
counter: Counter()));
#endif

#if NET6_0_OR_GREATER
[Fact]
public Task Date() =>
Verify(VerifierSettings.GetNameForParameter(new Date(2000, 10, 1)));
Verify(
VerifierSettings.GetNameForParameter(
new Date(2000, 10, 1),
counter: Counter()));

[Fact]
public Task Time() =>
Verify(VerifierSettings.GetNameForParameter(new Date(2000, 10, 1)));
Verify(
VerifierSettings.GetNameForParameter(
new Date(2000, 10, 1),
counter: Counter()));
#endif

[Fact]
public Task DateTimeLocal()
{
var date = new DateTime(2000, 10, 1, 0, 0, 0, DateTimeKind.Local);
return Verify(VerifierSettings.GetNameForParameter(date));
return Verify(
VerifierSettings.GetNameForParameter(
date,
counter: Counter()));
}

[Fact]
public Task DateTimeUnspecified()
{
var date = new DateTime(2000, 10, 1, 0, 0, 0);
return Verify(VerifierSettings.GetNameForParameter(date));
return Verify(
VerifierSettings.GetNameForParameter(
date,
counter: Counter()));
}

[Fact]
public Task DateTimeUtc()
{
var date = new DateTime(2000, 10, 1, 0, 0, 0, DateTimeKind.Utc);
return Verify(VerifierSettings.GetNameForParameter(date));
return Verify(
VerifierSettings.GetNameForParameter(
date,
counter: Counter()));
}

[Fact]
public Task DateTimeOffsetUtc()
{
var date = new DateTimeOffset(2000, 10, 1, 0, 0, 0, TimeSpan.Zero);
return Verify(VerifierSettings.GetNameForParameter(date));
return Verify(
VerifierSettings.GetNameForParameter(
date,
counter: Counter()));
}

[Fact]
Expand All @@ -66,16 +87,19 @@ public Task List() =>
new List<string>
{
"value"
}));
},
counter: Counter()));

[Fact]
public Task ListMultiple() =>
Verify(VerifierSettings.GetNameForParameter(
Verify(
VerifierSettings.GetNameForParameter(
new List<string>
{
"value1",
"value2"
}));
},
counter: Counter()));

[Fact]
public Task Nested() =>
Expand All @@ -93,11 +117,15 @@ public Task Nested() =>
}
},
"value4"
}));
},
counter: Counter()));

[Fact]
public Task EmptyList() =>
Verify(VerifierSettings.GetNameForParameter(new List<string>()));
Verify(
VerifierSettings.GetNameForParameter(
new List<string>(),
counter: Counter()));

[Fact]
public Task Dictionary() =>
Expand All @@ -107,7 +135,8 @@ public Task Dictionary() =>
{
"value", 10
}
}));
},
counter: Counter()));

[Fact]
public Task DictionaryMultiple() =>
Expand All @@ -120,23 +149,43 @@ public Task DictionaryMultiple() =>
{
"value2", 11
}
}));
},
counter: Counter()));

[Fact]
public Task EmptyDictionary() =>
Verify(VerifierSettings.GetNameForParameter(new Dictionary<string, int>()));
Verify(
VerifierSettings.GetNameForParameter(
new Dictionary<string, int>(),
counter: Counter()));

[Fact]
public Task EnumerableStaticEmpty() =>
Verify(VerifierSettings.GetNameForParameter(Enumerable.Empty<string>()));
Verify(
VerifierSettings.GetNameForParameter(
Enumerable.Empty<string>(),
counter: Counter()));

[Fact]
public Task Array() =>
Verify(VerifierSettings.GetNameForParameter(
Verify(
VerifierSettings.GetNameForParameter(
new[]
{
"value"
}));
},
counter: Counter()));

private static Counter Counter() =>
new(
true,
#if NET6_0_OR_GREATER
[],
[],
#endif
[],
[],
[]);

[Fact]
public Task ArrayMultiple() =>
Expand All @@ -145,9 +194,13 @@ public Task ArrayMultiple() =>
{
"value1",
"value2"
}));
},
counter: Counter()));

[Fact]
public Task ArrayEmpty() =>
Verify(VerifierSettings.GetNameForParameter(System.Array.Empty<string>()));
Verify(
VerifierSettings.GetNameForParameter(
System.Array.Empty<string>(),
counter: Counter()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content
72 changes: 71 additions & 1 deletion src/Verify/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,76 @@ public partial class Counter
bool dateCounting;
static AsyncLocal<Counter?> local = new();

internal bool TryGetNamed(object value, [NotNullWhen(true)] out string? result)
{
#if NET6_0_OR_GREATER

if (value is Date date)
{
if (namedDates.TryGetValue(date, out result) ||
globalNamedDates.TryGetValue(date, out result))
{
return true;
}

result = null;
return false;
}

if (value is Time time)
{
if (namedTimes.TryGetValue(time, out result) ||
globalNamedTimes.TryGetValue(time, out result))
{
return true;
}

result = null;
return false;
}

#endif

if (value is Guid guid)
{
if (namedGuids.TryGetValue(guid, out result) ||
globalNamedGuids.TryGetValue(guid, out result))
{
return true;
}

result = null;
return false;
}

if (value is DateTime dateTime)
{
if (namedDateTimes.TryGetValue(dateTime, out result) ||
globalNamedDateTimes.TryGetValue(dateTime, out result))
{
return true;
}

result = null;
return false;
}

if (value is DateTimeOffset dateTimeOffset)
{
if (namedDateTimeOffsets.TryGetValue(dateTimeOffset, out result) ||
globalNamedDateTimeOffsets.TryGetValue(dateTimeOffset, out result))
{
return true;
}

result = null;
return false;
}

result = null;
return false;
}

public static Counter Current
{
get
Expand All @@ -26,7 +96,7 @@ public static Counter Current
}
}

Counter(
public Counter(
bool dateCounting,
#if NET6_0_OR_GREATER
Dictionary<Date, string> namedDates,
Expand Down
6 changes: 6 additions & 0 deletions src/Verify/Naming/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ internal static void AppendParameter(object? parameter, StringBuilder builder, b
return;
}

if (counter.TryGetNamed(parameter, out var result))
{
builder.Append(result);
return;
}

if (parameter is string stringParameter)
{
if (pathFriendly)
Expand Down

0 comments on commit a0f0a31

Please sign in to comment.