feat: add Check validation utility class with comprehensive unit tests#120
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Check argument-validation utility to the LuYao.Common library and adds a corresponding unit test suite, plus a small formatting-only change in StringExtensions.cs.
Changes:
- Added
Checkstatic utility class with validation helpers (null checks, string length checks, range/positive checks, etc.). - Added
CheckTestsunit tests covering majorCheckbehaviors. - Added a trailing blank line in
StringExtensions.cs(formatting).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| tests/LuYao.Common.UnitTests/CheckTests.cs | Adds unit tests for the new Check validation APIs. |
| src/LuYao.Common/StringExtensions.cs | Formatting-only: ensures file ends with a newline/blank line. |
| src/LuYao.Common/Check.cs | Introduces the new Check validation utility class. |
Comment on lines
+303
to
+316
| public static float Positive( | ||
| float value, | ||
| string parameterName) | ||
| { | ||
| if (value == 0) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is equal to zero"); | ||
| } | ||
| else if (value < 0) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is less than zero"); | ||
| } | ||
| return value; | ||
| } |
Comment on lines
+325
to
+338
| public static double Positive( | ||
| double value, | ||
| string parameterName) | ||
| { | ||
| if (value == 0) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is equal to zero"); | ||
| } | ||
| else if (value < 0) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is less than zero"); | ||
| } | ||
| return value; | ||
| } |
Comment on lines
+440
to
+451
| public static float Range( | ||
| float value, | ||
| string parameterName, | ||
| float minimumValue, | ||
| float maximumValue = float.MaxValue) | ||
| { | ||
| if (value < minimumValue || value > maximumValue) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); | ||
| } | ||
| return value; | ||
| } |
Comment on lines
+462
to
+474
| public static double Range( | ||
| double value, | ||
| string parameterName, | ||
| double minimumValue, | ||
| double maximumValue = double.MaxValue) | ||
| { | ||
| if (value < minimumValue || value > maximumValue) | ||
| { | ||
| throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}"); | ||
| } | ||
|
|
||
| return value; | ||
| } |
|
|
||
| if (!typeof(TBaseType).IsAssignableFrom(type)) | ||
| { | ||
| throw new ArgumentException($"{parameterName} (type of {type.AssemblyQualifiedName}) should be assignable to the {typeof(TBaseType).FullName}!"); |
Comment on lines
+691
to
+703
| [TestMethod] | ||
| public void Range_Double_WithValueInRange_ReturnsValue() | ||
| { | ||
| // Arrange | ||
| double value = 5.5; | ||
|
|
||
| // Act | ||
| var result = Check.Range(value, nameof(value), minimumValue: 0.0, maximumValue: 10.0); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(value, result); | ||
| } | ||
|
|
Comment on lines
+596
to
+621
| [TestMethod] | ||
| public void Positive_Double_WithPositiveValue_ReturnsValue() | ||
| { | ||
| // Arrange | ||
| double value = 3.14; | ||
|
|
||
| // Act | ||
| var result = Check.Positive(value, nameof(value)); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(value, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Positive_Decimal_WithPositiveValue_ReturnsValue() | ||
| { | ||
| // Arrange | ||
| decimal value = 99.99m; | ||
|
|
||
| // Act | ||
| var result = Check.Positive(value, nameof(value)); | ||
|
|
||
| // Assert | ||
| Assert.AreEqual(value, result); | ||
| } | ||
|
|
Comment on lines
170
to
174
| var len = endIndex - startIndex; | ||
| return str.Substring(startIndex, len); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request makes a minor formatting change to the
StringExtensions.csfile. An extra newline was added at the end of the file to ensure proper file formatting.