Skip to content

Commit e628d5c

Browse files
committed
PathHelper relative null path exception fixed
AssertionExtensions custom error messages added
1 parent 4c28dc2 commit e628d5c

File tree

4 files changed

+78
-53
lines changed

4 files changed

+78
-53
lines changed

Core.Common.Standard/DataAccess/PathHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public string Parent(string path)
135135

136136
public string RelativeTo(string path, string to)
137137
{
138-
string[] pathChunks = this.Format(path).Split(Path.DirectorySeparatorChar);
139-
string[] toChunks = this.Format(to).Split(Path.DirectorySeparatorChar);
138+
string[] pathChunks = this.Format(path)?.Split(Path.DirectorySeparatorChar) ?? new string[0];
139+
string[] toChunks = this.Format(to)?.Split(Path.DirectorySeparatorChar) ?? new string[0];
140140
int sameChunks = 0;
141141
for (int index = 0; index < pathChunks.Length; index++)
142142
{

Core.Common.Standard/Extension/AssertionExtensions.cs

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,164 +2,188 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
// ReSharper disable PossibleMultipleEnumeration
6+
// ReSharper disable MemberCanBePrivate.Global
7+
// ReSharper disable UnusedMember.Global
8+
// ReSharper disable ParameterOnlyUsedForPreconditionCheck.Global
9+
// ReSharper disable UnusedMethodReturnValue.Global
510

611
namespace KY.Core
712
{
813
public static class AssertionExtensions
914
{
15+
private const string DefaultMessage = "[Message will be replaced in method body]";
16+
1017
[DebuggerHidden]
11-
public static double AssertIsPositive(this double self, string argumentName = "")
18+
public static double AssertIsPositive(this double self, string argumentName = "", string message = "Has to be positive")
1219
{
1320
if (self <= 0)
14-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be positive!");
21+
{
22+
throw new ArgumentOutOfRangeException(argumentName, self, message);
23+
}
1524

1625
return self;
1726
}
1827

1928
[DebuggerHidden]
20-
public static double AssertHasValueAndIsPositive(this double? self, string argumentName = "")
29+
public static double AssertIsPositive(this double? self, string argumentName = "", string message = "Has to be positive")
2130
{
22-
return self.AssertIsNotNull(argumentName).AssertIsPositive(argumentName);
31+
return self.AssertIsNotNull(argumentName, message).AssertIsPositive(argumentName, message);
2332
}
2433

2534
[DebuggerHidden]
26-
public static double? AssertIsNullOrPositive(this double? self, string argumentName = "")
35+
public static double? AssertIsNullOrPositive(this double? self, string argumentName = "", string message = "Has to be null or positive")
2736
{
28-
if (self.HasValue)
29-
self.Value.AssertIsPositive(argumentName);
30-
37+
self?.AssertIsPositive(argumentName, message);
3138
return self;
3239
}
3340

3441
[DebuggerHidden]
35-
public static int AssertIsNotNegative(this int self, string argumentName = "")
42+
public static int AssertIsNotNegative(this int self, string argumentName = "", string message = "Has to be positive or 0")
3643
{
3744
if (self < 0)
38-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be positive or 0!");
39-
45+
{
46+
throw new ArgumentOutOfRangeException(argumentName, self, message);
47+
}
4048
return self;
4149
}
4250

4351
[DebuggerHidden]
44-
public static int AssertIsPositive(this int self, string argumentName = "")
52+
public static int AssertIsPositive(this int self, string argumentName = "", string message = "Has to be positive")
4553
{
4654
if (self <= 0)
47-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be positive!");
48-
55+
{
56+
throw new ArgumentOutOfRangeException(argumentName, self, message);
57+
}
4958
return self;
5059
}
5160

5261
[DebuggerHidden]
53-
public static long AssertIsNotNegative(this long self, string argumentName = "")
62+
public static long AssertIsNotNegative(this long self, string argumentName = "", string message = "Has to be positive or 0")
5463
{
5564
if (self < 0)
56-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be positive or 0!");
57-
65+
{
66+
throw new ArgumentOutOfRangeException(argumentName, self, message);
67+
}
5868
return self;
5969
}
6070

6171
[DebuggerHidden]
62-
public static long AssertIsPositive(this long self, string argumentName = "")
72+
public static long AssertIsPositive(this long self, string argumentName = "", string message = "Has to be positive")
6373
{
6474
if (self <= 0)
65-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be positive!");
66-
75+
{
76+
throw new ArgumentOutOfRangeException(argumentName, self, message);
77+
}
6778
return self;
6879
}
6980

7081
[DebuggerHidden]
71-
public static long AssertHasValueAndIsPositive(this long? self, string argumentName = "")
82+
public static long AssertIsPositive(this long? self, string argumentName = "", string message = "Has to be positive")
7283
{
73-
return self.AssertIsNotNull(argumentName).AssertIsPositive(argumentName);
84+
return self.AssertIsNotNull(argumentName, message).AssertIsPositive(argumentName, message);
7485
}
7586

7687
[DebuggerHidden]
77-
public static long? AssertIsNullOrPositive(this long? self, string argumentName = "")
88+
public static long? AssertIsNullOrPositive(this long? self, string argumentName = "", string message = "Has to be null or positive")
7889
{
79-
if (self.HasValue)
80-
self.Value.AssertIsPositive(argumentName);
81-
90+
self?.AssertIsPositive(argumentName, message);
8291
return self;
8392
}
8493

8594
[DebuggerHidden]
86-
public static string AssertIsNotLongerThan(this string reference, int maxLength, string parameterName = null)
95+
public static string AssertIsNotLongerThan(this string reference, int maxLength, string parameterName = null, string message = DefaultMessage)
8796
{
8897
if (reference.Length > maxLength)
89-
throw new ArgumentException(parameterName, string.Format("String is too long. Max: {0} / Acctual: {1}", maxLength, reference.Length));
98+
{
99+
throw new ArgumentException(parameterName, message == DefaultMessage ? $"String is too long. Max: {maxLength} / Actual: {reference.Length}" : message);
100+
}
90101
return reference;
91102
}
92103

93104
[DebuggerHidden]
94-
public static string AssertIsNotNullOrEmpty(this string reference, string parameterName = null)
105+
public static string AssertIsNotNullOrEmpty(this string reference, string parameterName = null, string message = "Cannot be null or empty")
95106
{
96-
reference.AssertIsNotNull(parameterName);
97107
if (string.IsNullOrEmpty(reference))
98-
throw new ArgumentOutOfRangeException(parameterName, "Cannot be empty.");
108+
{
109+
throw new ArgumentOutOfRangeException(parameterName, message);
110+
}
99111
return reference;
100112
}
101113

102114
[DebuggerHidden]
103-
public static T AssertIsNotNull<T>(this T reference, string parameterName = null)
115+
public static T AssertIsNotNull<T>(this T reference, string parameterName = null, string message = "Cannot be null")
104116
where T : class
105117
{
106118
if (reference == null)
107-
throw new ArgumentNullException(parameterName ?? typeof(T).FullName, "Cannot be null");
119+
{
120+
throw new ArgumentNullException(parameterName ?? typeof(T).FullName, message);
121+
}
108122
return reference;
109123
}
110124

111125
[DebuggerHidden]
112-
public static T AssertIsNotNull<T>(this T? reference, string parameterName = null)
126+
public static T AssertIsNotNull<T>(this T? reference, string parameterName = null, string message = "Cannot be null")
113127
where T : struct
114128
{
115129
if (reference == null)
116-
throw new ArgumentNullException(parameterName ?? typeof(T).FullName, "Cannot be null");
130+
{
131+
throw new ArgumentNullException(parameterName ?? typeof(T).FullName, message);
132+
}
117133
return reference.Value;
118134
}
119135

120136
[DebuggerHidden]
121-
public static IEnumerable<long> AssertAllElementsArePositive(this IEnumerable<long> self, string parameterName = null)
137+
public static IEnumerable<long> AssertAllElementsArePositive(this IEnumerable<long> self, string parameterName = null, string message = "All Elements have to be positive")
122138
{
123139
if (self.Any(l => l <= 0))
124-
throw new ArgumentOutOfRangeException(parameterName, self, "All Elements have to be positive!");
125-
140+
{
141+
throw new ArgumentOutOfRangeException(parameterName, self, message);
142+
}
126143
return self;
127144
}
128145

129146
[DebuggerHidden]
130-
public static IEnumerable<T> AssertContains<T>(this IEnumerable<T> collection, T item, string argumentName = "")
147+
public static IEnumerable<T> AssertContains<T>(this IEnumerable<T> collection, T item, string argumentName = "", string message = DefaultMessage)
131148
{
132149
collection.AssertIsNotNull(argumentName);
133-
if (!collection.Contains(item))
134-
throw new InvalidOperationException(argumentName + " collection have to contains child");
150+
if (collection.Contains(item))
151+
{
152+
throw new InvalidOperationException(message == DefaultMessage ? argumentName + " collection have to contains child" : message);
153+
}
135154
return collection;
136155
}
137156

138157
[DebuggerHidden]
139-
public static IEnumerable<T> AssertIsNotNullOrEmpty<T>(this IEnumerable<T> collection, string argumentName = "")
158+
public static IEnumerable<T> AssertIsNotNullOrEmpty<T>(this IEnumerable<T> collection, string argumentName = "", string message = DefaultMessage)
140159
{
141160
collection.AssertIsNotNull(argumentName);
142-
if (!collection.Any())
143-
throw new InvalidOperationException(argumentName + " collection have to contain any child");
161+
if (collection.Any())
162+
{
163+
throw new InvalidOperationException(message == DefaultMessage ? argumentName + " collection have to contain any child" : message);
164+
}
144165
return collection;
145166
}
146167

147168
[DebuggerHidden]
148-
public static T AssertIs<T>(this T self, T expected, string argumentName = "")
169+
public static T AssertIs<T>(this T self, T expected, string argumentName = "", string message = DefaultMessage)
149170
{
150171
if (Equals(self, expected))
172+
{
151173
return self;
152-
153-
throw new ArgumentOutOfRangeException(argumentName, self, "Has to be " + expected);
174+
}
175+
throw new ArgumentOutOfRangeException(argumentName, self, message == DefaultMessage ? "Has to be " + expected : message);
154176
}
155177

156178
[DebuggerHidden]
157-
public static T AssertIsNot<T>(this T self, T notExpected, string argumentName = "")
179+
public static T AssertIsNot<T>(this T self, T notExpected, string argumentName = "", string message = DefaultMessage)
158180
{
159-
if (!Equals(self, notExpected))
181+
if (Equals(self, notExpected))
182+
{
160183
return self;
184+
}
161185

162-
throw new ArgumentOutOfRangeException(argumentName, self, "Mustn't be " + notExpected);
186+
throw new ArgumentOutOfRangeException(argumentName, self, message == DefaultMessage ? "Mustn't be " + notExpected : message);
163187
}
164188
}
165189
}

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.5.0</Version>
7+
<Version>4.6.0</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>

Core.Common.Standard/NugetPackageDependencyLoader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private static Assembly Resolve(object sender, ResolveEventArgs args)
2727
{
2828
return assembly;
2929
}
30+
Logger.Trace($"Try to find {args.Name}");
3031
Regex regex = new Regex(@"(?<name>[\w.]+),\sVersion=(?<version>[\d.]+),\sCulture=(?<culture>[\w-]+),\sPublicKeyToken=(?<token>\w+)");
3132
Match match = regex.Match(args.Name);
3233
if (match.Success)

0 commit comments

Comments
 (0)