Skip to content

Commit 2aae7d6

Browse files
author
Balint Nagy
committed
Added strong naming.
Fixed Guard.NotNull usage validation when type is not provided.
1 parent 755f860 commit 2aae7d6

File tree

6 files changed

+705
-30
lines changed

6 files changed

+705
-30
lines changed

LICENSE

Lines changed: 674 additions & 21 deletions
Large diffs are not rendered by default.

YAGuard/YAGuard.Test/GuardTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public void AgainstNull_ShouldFail_WhenIncorrectlyInvoked2()
3838
Guard.AgainstNull(new { one = "anonymous argument", two = "has too many properties" });
3939
}
4040

41+
[TestMethod]
42+
[DataRow("")]
43+
[DataRow(" ")]
44+
[DataRow("blah")]
45+
public void AgainstNullT_ShouldSucceed_EvenIfTypeIsString(string goodValue)
46+
{
47+
Guard.AgainstNull(new { goodValue });
48+
}
49+
4150
[TestMethod]
4251
[DataRow("")]
4352
[DataRow(" ")]

YAGuard/YAGuard/ArgHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static void AssertType(object arg, Type expectedType)
3838
internal static TValue GetProp0Value<TValue>(object arg)
3939
{
4040
Guard.AgainstNull(arg, nameof(arg));
41-
AssertType(arg, typeof(TValue));
41+
//AssertType(arg, typeof(TValue));
4242
return (TValue)arg.GetType().GetProperties()[0].GetValue(arg);
4343
}
4444

@@ -61,8 +61,8 @@ internal static void CheckUsage<TValue>(object arg)
6161
throw new ArgumentException($"Invalid use of SafeAssign.Assign(). The anonymous object argument should have a single property, containing the argument to test and return. Usage example:\n{usage}");
6262

6363
// Check that the single property of arg is of type TValue
64-
if (props[0].PropertyType != typeof(TValue))
65-
throw new ArgumentException($"Invalid use of SafeAssign.Assign(). The anonymous object argument type {props[0].PropertyType.Name} doesn't match the expected result type {nameof(TValue)}. Usage example:\n{usage}");
64+
//if (props[0].PropertyType != typeof(TValue))
65+
// throw new ArgumentException($"Invalid use of SafeAssign.Assign(). The anonymous object argument type {props[0].PropertyType.Name} doesn't match the expected result type {nameof(TValue)}. Usage example:\n{usage}");
6666
}
6767
}
6868
}

YAGuard/YAGuard/Guard.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public static class Guard
1212
{
1313
#region Generic Checks
1414

15-
//public static void AgainstCondition(bool condition, string argName = null, string message = null)
16-
//{
17-
// if (condition)
18-
// throw new ArgumentException(message ?? $"Argument {argName ?? ""} did not satisfy condition.");
19-
//}
15+
public static void AgainstCondition(bool condition, string argName = null, string message = null)
16+
{
17+
if (condition)
18+
throw new ArgumentException(message ?? $"Argument {argName ?? ""} did not satisfy condition.");
19+
}
2020

2121
//public static void AgainstCondition(Func<bool> condition, string argName = null, string message = null)
2222
//{
596 Bytes
Binary file not shown.

YAGuard/YAGuard/YAGuard.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,20 @@ The alternative where you provide the argument nameis fast. The one without is c
2222
<RepositoryType>github</RepositoryType>
2323
<AssemblyVersion>0.0.0.0</AssemblyVersion>
2424
<FileVersion>0.0.0.0</FileVersion>
25-
<Version>0.0.0</Version>
25+
<Version>0.0.4</Version>
26+
<PackageReleaseNotes>Yet Another Guard argument validation package.
27+
The need to provide the name of the argument was driving me mad - here we go, without that.
28+
Usage:
29+
public void MyFunc(string myArg)
30+
{
31+
Guard.AgainstNull(myArg, nameof(myArg));
32+
// OR
33+
Guard.AgainstNull(new {myArg});
34+
}
35+
In case myArg is null, this will throw an ArgumentNullException with the correct argument name.
36+
The alternative where you provide the argument nameis fast. The one without is convenient (it uses reflection, so it's slower). You can decide which one you need in any case.</PackageReleaseNotes>
37+
<SignAssembly>true</SignAssembly>
38+
<AssemblyOriginatorKeyFile>StrongNamingKey.snk</AssemblyOriginatorKeyFile>
2639
</PropertyGroup>
2740

2841
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp2.2|AnyCPU'">

0 commit comments

Comments
 (0)