Skip to content

Conversation

@feO2x
Copy link
Owner

@feO2x feO2x commented Jul 21, 2025

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 (MustHaveMaximumLength and updates to MustHaveLengthIn and MustNotContain) to improve functionality and ensure proper handling of default instances of ImmutableArray<T>. Comprehensive unit tests have been added to validate these changes.

New Assertions for ImmutableArray<T>:

  • MustHaveMaximumLength Implementation:
    • Added two overloads to ensure that an 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.
    • Default instances of ImmutableArray<T> are handled gracefully and do not throw exceptions when the maximum length is negative.

Enhancements to Existing Assertions:

  • MustHaveLengthIn Updates:

    • Modified the assertion to handle default instances of ImmutableArray<T> by treating their length as 0. This prevents errors when checking ranges for default instances. [1] [2]
    • Updated the exception message to clarify that default instances have no length.
  • MustNotContain Updates:

    • Enhanced the assertion to skip checks for default instances of ImmutableArray<T>, as they cannot contain any items. [1] [2]
    • Added remarks to the documentation to explain behavior for default instances. [1] [2]

Unit Tests:

  • New Tests for MustHaveMaximumLength:

    • Created a dedicated test file MustHaveMaximumLengthTests.cs to validate the new assertion. Tests cover scenarios for exceeding maximum length, custom exceptions, and handling of default instances.
  • Additional Tests for MustHaveLengthIn:

    • Added cases to ensure proper handling of default instances and custom exception scenarios.
  • Additional Tests for MustNotContain:

    • Verified behavior for default instances and custom exception handling.

Documentation:

  • Issue Documentation:
    • Added a markdown file detailing the implementation of MustHaveMaximumLength for ImmutableArray<T>. This includes context, tasks, and notes for contributors.

feO2x added 4 commits July 21, 2025 06:58
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>
@feO2x feO2x self-assigned this Jul 21, 2025
@feO2x feO2x requested a review from Copilot July 21, 2025 06:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MustHaveMaximumLength assertion with two overloads (default and custom exception)
  • Updates MustHaveLengthIn and MustNotContain assertions to handle default ImmutableArray<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)
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Suggested change
if (parameter.IsDefault && length < 0 || parameter.Length > length)
if ((parameter.IsDefault && length < 0) || (!parameter.IsDefault && parameter.Length > length))

Copilot uses AI. Check for mistakes.
…ect, adjusted default exception message

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
@feO2x feO2x merged commit a9529b8 into dev Jul 21, 2025
1 check passed
@feO2x feO2x deleted the issue-120-must-have-maximum-length-for-immutable-array branch July 21, 2025 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MustHaveMaximumLength for ImmutableArray

2 participants