Conversation
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
…rrays Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
There was a problem hiding this comment.
Pull Request Overview
This pull request implements the MustHaveMaximumLength assertion for ImmutableArray<T> and enhances existing assertions to handle default instances properly. The changes avoid boxing performance issues by providing dedicated methods for ImmutableArray<T> rather than relying on generic IEnumerable<T> extensions.
- Adds new
MustHaveMaximumLengthassertion with two overloads (default and custom exception) - Updates
MustHaveLengthInandMustNotContainassertions to handle defaultImmutableArray<T>instances - Comprehensive unit tests covering all scenarios including default instance handling
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| issue-120-must-have-maximum-length-for-immutable-array.md | Documentation file outlining implementation requirements |
| Check.MustHaveMaximumLength.cs | New file implementing the core MustHaveMaximumLength assertions |
| Check.MustHaveLengthIn.cs | Updated to treat default instances as having length 0 |
| Check.MustNotContain.cs | Enhanced to skip checks for default instances |
| Throw.ImmutableArrayLengthNotInRange.cs | Updated exception message for default instances |
| MustHaveMaximumLengthTests.cs | Comprehensive test suite for the new assertion |
| MustHaveLengthInTests.cs | Additional tests for default instance handling |
| MustNotContainTests.cs | Additional tests for default instance behavior |
| string? message = null | ||
| ) | ||
| { | ||
| if (parameter.IsDefault && length < 0 || parameter.Length > length) |
There was a problem hiding this comment.
The condition logic is incorrect. It should throw when parameter.IsDefault AND length < 0, OR when parameter.Length > length. However, the current condition will throw for default instances when length < 0, but default instances should be allowed for any non-negative maximum length. The condition should be: if ((parameter.IsDefault && length < 0) || (!parameter.IsDefault && parameter.Length > length))
| if (parameter.IsDefault && length < 0 || parameter.Length > length) | |
| if ((parameter.IsDefault && length < 0) || (!parameter.IsDefault && parameter.Length > length)) |
…ect, adjusted default exception message Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Closes #120
This pull request introduces enhancements and new features for handling
ImmutableArray<T>in the Light.GuardClauses library. Key changes include adding new assertions (MustHaveMaximumLengthand updates toMustHaveLengthInandMustNotContain) to improve functionality and ensure proper handling of default instances ofImmutableArray<T>. Comprehensive unit tests have been added to validate these changes.New Assertions for
ImmutableArray<T>:MustHaveMaximumLengthImplementation:ImmutableArray<T>does not exceed a specified maximum length. One overload uses a default exception (InvalidCollectionCountException), while the other allows custom exceptions via a delegate.ImmutableArray<T>are handled gracefully and do not throw exceptions when the maximum length is negative.Enhancements to Existing Assertions:
MustHaveLengthInUpdates:ImmutableArray<T>by treating their length as0. This prevents errors when checking ranges for default instances. [1] [2]MustNotContainUpdates:ImmutableArray<T>, as they cannot contain any items. [1] [2]Unit Tests:
New Tests for
MustHaveMaximumLength:MustHaveMaximumLengthTests.csto validate the new assertion. Tests cover scenarios for exceeding maximum length, custom exceptions, and handling of default instances.Additional Tests for
MustHaveLengthIn:Additional Tests for
MustNotContain:Documentation:
MustHaveMaximumLengthforImmutableArray<T>. This includes context, tasks, and notes for contributors.