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

Guard APIs #3131

Merged
merged 128 commits into from
Mar 15, 2020
Merged
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
1c79157
Initial guard APIs added
Sergio0694 Feb 13, 2020
0554d2f
Added EqualTo and NotEqualTo APIs
Sergio0694 Feb 13, 2020
6dc701f
Merge branch 'master' into feature/guard-apis
Sergio0694 Feb 13, 2020
018e5c4
Added more Guard APIs
Sergio0694 Feb 13, 2020
8d78ba3
New interval Guard APIs added
Sergio0694 Feb 13, 2020
0b76fb9
Added Guard APIs for IEnumerable<T> values
Sergio0694 Feb 13, 2020
8028741
Renamed APIs
Sergio0694 Feb 13, 2020
cd23275
Added Guard APIs for Stream values
Sergio0694 Feb 13, 2020
a54441c
Added Guard APIs for ReadOnlySpan<T> values
Sergio0694 Feb 13, 2020
8dee59a
Added Guard APIs for ReadOnlyMemory<T> values
Sergio0694 Feb 13, 2020
2d32333
Code refactoring
Sergio0694 Feb 13, 2020
19e3bcc
Minor code refactoring
Sergio0694 Feb 13, 2020
0ff1f00
Added Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
9dd8a80
Added Guard.IsNull API
Sergio0694 Feb 13, 2020
f2a4500
Code refactoring and optimizations
Sergio0694 Feb 13, 2020
b79daa7
Code refactoring
Sergio0694 Feb 13, 2020
d44e633
Added Guard.HasSizeNotEqualTo<T> API
Sergio0694 Feb 13, 2020
0292f9d
Added Guard size API overloads for the string type
Sergio0694 Feb 13, 2020
073a205
Added Guard size API overloads for T[] arrays
Sergio0694 Feb 13, 2020
5edef16
Code refactoring
Sergio0694 Feb 13, 2020
1a19fea
Removed unnecessary using directives
Sergio0694 Feb 13, 2020
d38ad13
Added Guard reference check APIs
Sergio0694 Feb 13, 2020
e9108fc
Added new Guaard APIs for string values
Sergio0694 Feb 13, 2020
18bb2d4
Improved Guard.IsBitwiseEqualTo<T> API
Sergio0694 Feb 13, 2020
2d9536f
Improved Guard.IsNull APIs
Sergio0694 Feb 13, 2020
a927dad
Added new size APIs for copy operations
Sergio0694 Feb 13, 2020
56a3a18
Added new type test APIs, minor code tweaks
Sergio0694 Feb 13, 2020
bbd10a8
Minor bug fixes
Sergio0694 Feb 13, 2020
fbb5801
Added Guard APIs for Task values
Sergio0694 Feb 14, 2020
eba232e
Minor code tweak
Sergio0694 Feb 14, 2020
9071e12
Added new Guard.IsEmpty APIs
Sergio0694 Feb 14, 2020
edb1d41
Refactored code to remove unsafe requirement
Sergio0694 Feb 14, 2020
d9c83f4
Added Guard.IsInRange<T> APIs
Sergio0694 Feb 14, 2020
e63402b
Added missing type checks in Guard.IsBitwiseEqualTo API
Sergio0694 Feb 17, 2020
87056c9
Added ValueTypeExtensions.ToHexString API
Sergio0694 Feb 18, 2020
0d34c0a
Code refactoring
Sergio0694 Feb 18, 2020
497d149
Added ValueTypeExtensions tests
Sergio0694 Feb 18, 2020
bc0fc1b
Added general Guard tests
Sergio0694 Feb 18, 2020
606cbde
Bug fixes in the Guard class
Sergio0694 Feb 18, 2020
f29cd93
Minor speed improvements
Sergio0694 Feb 18, 2020
79d2dd4
Code refactoring
Sergio0694 Feb 18, 2020
793c769
Removed unnecessary using directives
Sergio0694 Feb 18, 2020
8e2f2d8
More speed improvements and API refactoring
Sergio0694 Feb 18, 2020
5b58e5e
Refactored/fixed some Guard APIs
Sergio0694 Feb 18, 2020
b664c90
More bug fixes
Sergio0694 Feb 18, 2020
7f65184
Added Guard tests for array APIs
Sergio0694 Feb 18, 2020
4304edd
Fixed the Guard.IsNotEmpty<T> array API
Sergio0694 Feb 18, 2020
4802936
Moved exception throwers to separate class
Sergio0694 Feb 23, 2020
ed5315a
Moved general Guard throwers to separate class
Sergio0694 Feb 23, 2020
1772a0f
Disabled warning for XML overload resolution
Sergio0694 Feb 23, 2020
9814d91
Moved array Guard throwers to separate class
Sergio0694 Feb 23, 2020
1b78344
Moved stream Guard throwers to separate class
Sergio0694 Feb 23, 2020
bc0dc18
Fixed some XML docs
Sergio0694 Feb 23, 2020
e000fe0
Moved enumerable Guard throwers to separate class
Sergio0694 Feb 23, 2020
21cf16a
Moved task Guard throwers to separate class
Sergio0694 Feb 23, 2020
c1b9c8b
Added new Guard APIs for tasks
Sergio0694 Feb 23, 2020
c784d4e
Moved string Guard throwers to separate class
Sergio0694 Feb 23, 2020
8cf87c3
Moved ReadOnlySpan<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
24e03f5
Added Span<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
6f1679b
Moved ReadOnlyMemory<T> Guard throwers to separate class
Sergio0694 Feb 23, 2020
7e80e43
Added Memory<T> APIs to the Guard class
Sergio0694 Feb 23, 2020
662bb83
Minor code refactoring
Sergio0694 Feb 23, 2020
1c334df
Update file headers
Sergio0694 Feb 23, 2020
eccc2a6
Removed unnecessary methods
Sergio0694 Feb 23, 2020
15ef6a3
Added TypeExtensions.ToTypeString extension
Sergio0694 Feb 24, 2020
317fc99
Improved error messages for general Guard APIs
Sergio0694 Feb 24, 2020
cb1f3b0
Improved error messages for Enumerable Guard APIs
Sergio0694 Feb 24, 2020
23f1504
Improved error messages for stream Guard APIs
Sergio0694 Feb 24, 2020
920e69f
Improved error messages for array Guard APIs
Sergio0694 Feb 24, 2020
18e661f
Improved error messages for string Guard APIs
Sergio0694 Feb 24, 2020
d00e0e3
Improved error messages for task Guard APIs
Sergio0694 Feb 24, 2020
6aeedaa
Improved error messages for span Guard APIs
Sergio0694 Feb 24, 2020
acafd8a
Improved error messages for memory Guard APIs
Sergio0694 Feb 24, 2020
8c7757a
Improved type string for nullable types
Sergio0694 Feb 24, 2020
c2fd97c
Improved type string for value tuple types
Sergio0694 Feb 24, 2020
8facb7e
Minor performance improvement
Sergio0694 Feb 24, 2020
2952e23
Added numeric comparison T4 file
Sergio0694 Feb 29, 2020
4991a5c
Updated .gitignore to skip generated files
Sergio0694 Feb 29, 2020
714cf31
Moved comparison Guard APIs to separate file
Sergio0694 Feb 29, 2020
fa8cc56
Renamed template file, fixed incorrect XML doc
Sergio0694 Feb 29, 2020
effb2ed
Fixed missing blank line
Sergio0694 Mar 1, 2020
e2e5171
Removed unnecessary type parameters
Sergio0694 Mar 1, 2020
4482217
Moved enumerable Guard APIs to separate file
Sergio0694 Mar 1, 2020
94872b9
Code refactoring in the enumerable T4 template
Sergio0694 Mar 1, 2020
756c842
Renamed a file
Sergio0694 Mar 1, 2020
0ecef12
Added empty template for ThrowHelper.Collection
Sergio0694 Mar 1, 2020
e093427
Code refactoring in the T4 templates
Sergio0694 Mar 1, 2020
9db9cd0
Switched collection throw helpers to T4 generation
Sergio0694 Mar 1, 2020
f6e3248
Code refactoring
Sergio0694 Mar 1, 2020
deb302b
Merge pull request #7 from Sergio0694/feature/guard-apis-T4
Sergio0694 Mar 1, 2020
fcc3c05
Removed incorrect file header
Sergio0694 Mar 1, 2020
760972f
Added new Guard.IsInRangeFor API
Sergio0694 Mar 1, 2020
8d6cc8a
Updated .gitignore, added generated files to source control
Sergio0694 Mar 3, 2020
e3d245f
Added tests for Guard.IsInRangeFor
Sergio0694 Mar 3, 2020
d367add
Improved formatting of values in error messages
Sergio0694 Mar 4, 2020
6d97f33
Improved error messages
Sergio0694 Mar 4, 2020
ec67152
More improvements to the error messages
Sergio0694 Mar 4, 2020
b2c3acb
Minor code tweaks
Sergio0694 Mar 5, 2020
43b22f0
Added Guard.IsNotOfType APIs
Sergio0694 Mar 5, 2020
c8af095
Added Guard.IsNotAssignableToType APIs
Sergio0694 Mar 5, 2020
7adfa79
Added Guard.IsDefault<T> APIs
Sergio0694 Mar 8, 2020
95b9fd1
Merge branch 'master' into feature/guard-apis
Sergio0694 Mar 9, 2020
d4c2d05
Fixed some XML docs
Sergio0694 Mar 11, 2020
afd973e
Added missing [Pure] attribute
Sergio0694 Mar 11, 2020
96ce238
Fixed typo in a comment
Sergio0694 Mar 11, 2020
cb98a02
Removed unnecessary comment after code changes
Sergio0694 Mar 11, 2020
5f25e18
Added XML comment for the T4 service
Sergio0694 Mar 11, 2020
cf5785c
Added more comments to ToHexString
Sergio0694 Mar 11, 2020
801dc93
Suppressed an incorrect code style warning
Sergio0694 Mar 11, 2020
b840201
Added more tests for Guard.IsInRange
Sergio0694 Mar 11, 2020
818c1eb
Added Guard.IsNotInRangeFor APIs
Sergio0694 Mar 11, 2020
caad2f7
Added readme file for the T4 templates
Sergio0694 Mar 11, 2020
7134f98
Changed generated files extension to .g.cs to avoid conflicts
Sergio0694 Mar 11, 2020
01a7257
Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs
Sergio0694 Mar 11, 2020
f33c984
Added tests for IsCloseTo
Sergio0694 Mar 11, 2020
1b0f2c9
Updated ".g.cs" extension for generated files
Sergio0694 Mar 11, 2020
ef189ba
Removed extra space (typo)
Sergio0694 Mar 11, 2020
ac44dad
More tweakes to the Guard.md file
Sergio0694 Mar 11, 2020
21115f6
Excluded TypeInfo.g.cs file from .ttinclude from checkout
Sergio0694 Mar 11, 2020
67fcd47
Added more info about target ranges for some APIs
Sergio0694 Mar 11, 2020
41fea84
Added IsCloseTo overloads for long type
Sergio0694 Mar 11, 2020
132d240
Minor code tweaks
Sergio0694 Mar 11, 2020
f17aacd
Added comment to describe (uint) cast range check trick
Sergio0694 Mar 11, 2020
949134b
Added a nore about the .g.cs files being checked in
Sergio0694 Mar 13, 2020
c9283d1
Merge branch 'master' into feature/guard-apis
michael-hawker Mar 13, 2020
de701b6
Merge branch 'master' into feature/guard-apis
Sergio0694 Mar 13, 2020
51b45e2
Fixed IsCloseTo tests
Sergio0694 Mar 13, 2020
244da99
Fixed IsNull tests
Sergio0694 Mar 13, 2020
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
Prev Previous commit
Next Next commit
Added tests for IsCloseTo
  • Loading branch information
Sergio0694 committed Mar 11, 2020
commit f33c98496d7338e42f692ac2517150887538c20d
100 changes: 100 additions & 0 deletions UnitTests/Diagnostics/Test_Guard.Comparable.Numeric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Toolkit.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests.Diagnostics
{
public partial class Test_Guard
{
[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsCloseToInt_Ok()
{
Guard.IsCloseTo(0, 5, 10, nameof(Test_Guard_IsCloseToInt_Ok));
Guard.IsCloseTo(0, 5, 5, nameof(Test_Guard_IsCloseToInt_Ok));
Guard.IsCloseTo(0, int.MaxValue, int.MaxValue, nameof(Test_Guard_IsCloseToInt_Ok));
Guard.IsCloseTo(-500, -530, 50, nameof(Test_Guard_IsCloseToInt_Ok));
Guard.IsCloseTo(1000, 800, 200, nameof(Test_Guard_IsCloseToInt_Ok));
Guard.IsCloseTo(int.MaxValue, int.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToInt_Ok));
}

[TestCategory("Guard")]
[TestMethod]
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
public void Test_Guard_IsCloseToInt_Fail()
{
foreach (var item in new (int Value, int Target, int Delta)[]
{
(0, 20, 10),
(0, 6, 5),
(0, int.MaxValue, 500),
(-500, -530, 10),
(1000, 800, 100),
(int.MaxValue, int.MaxValue - 10, 7),
(int.MinValue, int.MaxValue, int.MaxValue)
})
{
bool fail = false;

try
{
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToInt_Fail));
}
catch (ArgumentException)
{
fail = true;
}

Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
}
}

[TestCategory("Guard")]
[TestMethod]
public void Test_Guard_IsCloseToFloat_Ok()
{
Guard.IsCloseTo(0f, 5, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
Guard.IsCloseTo(0f, 5, 5, nameof(Test_Guard_IsCloseToFloat_Ok));
Guard.IsCloseTo(0f, float.MaxValue, float.MaxValue, nameof(Test_Guard_IsCloseToFloat_Ok));
Guard.IsCloseTo(-500f, -530, 50, nameof(Test_Guard_IsCloseToFloat_Ok));
Guard.IsCloseTo(1000f, 800, 200, nameof(Test_Guard_IsCloseToFloat_Ok));
Guard.IsCloseTo(float.MaxValue, float.MaxValue - 10, 10, nameof(Test_Guard_IsCloseToFloat_Ok));
}

[TestCategory("Guard")]
[TestMethod]
[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1000", Justification = "Value tuple")]
public void Test_Guard_IsCloseToFloat_Fail()
{
foreach (var item in new (float Value, float Target, float Delta)[]
{
(0, 20, 10),
(0, 6, 5),
(0, float.MaxValue, 500),
(-500, -530, 10),
(1000, 800, 100),
(float.MaxValue, float.MaxValue - 10, 7),
(float.MinValue, float.MaxValue, float.MaxValue)
})
{
bool fail = false;

try
{
Guard.IsCloseTo(item.Value, item.Target, item.Delta, nameof(Test_Guard_IsCloseToFloat_Fail));
}
catch (ArgumentException)
{
fail = true;
}

Assert.IsTrue(fail, $"IsCloseTo didn't fail with {item}");
}
}
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -132,6 +132,7 @@
<Compile Include="Converters\Test_EmptyCollectionToObjectConverter.cs" />
<Compile Include="Converters\Test_EmptyStringToObjectConverter.cs" />
<Compile Include="Converters\Test_StringFormatConverter.cs" />
<Compile Include="Diagnostics\Test_Guard.Comparable.Numeric.cs" />
<Compile Include="Diagnostics\Test_Guard.Array.cs" />
<Compile Include="Diagnostics\Test_Guard.cs" />
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />