Skip to content

Commit

Permalink
Fix the minLength/maxLength constraint checks on Darwin for nullables. (
Browse files Browse the repository at this point in the history
#27355)

This is the darwin-framework-tool equivalent of
#27312

Also fixes a similar issue for minValue/maxValue.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Nov 23, 2023
1 parent eb0920f commit 37b2431
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions examples/darwin-framework-tool/commands/tests/TestCommandBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,25 +396,37 @@ class TestCommandBridge : public CHIPCommandBridge,

using ConstraintsChecker::CheckConstraintMinLength;

bool CheckConstraintMinLength(const char * _Nonnull itemName, NSString * _Nonnull current, uint64_t expected)
bool CheckConstraintMinLength(const char * _Nonnull itemName, NSString * _Nullable current, uint64_t expected)
{
if (current == nil) {
return true;
}
return CheckConstraintMinLength(itemName, [current length], expected);
}

bool CheckConstraintMinLength(const char * _Nonnull itemName, NSArray * _Nonnull current, uint64_t expected)
bool CheckConstraintMinLength(const char * _Nonnull itemName, NSArray * _Nullable current, uint64_t expected)
{
if (current == nil) {
return true;
}
return CheckConstraintMinLength(itemName, [current count], expected);
}

using ConstraintsChecker::CheckConstraintMaxLength;

bool CheckConstraintMaxLength(const char * _Nonnull itemName, NSString * _Nonnull current, uint64_t expected)
bool CheckConstraintMaxLength(const char * _Nonnull itemName, NSString * _Nullable current, uint64_t expected)
{
if (current == nil) {
return true;
}
return CheckConstraintMaxLength(itemName, [current length], expected);
}

bool CheckConstraintMaxLength(const char * _Nonnull itemName, NSArray * _Nonnull current, uint64_t expected)
bool CheckConstraintMaxLength(const char * _Nonnull itemName, NSArray * _Nullable current, uint64_t expected)
{
if (current == nil) {
return true;
}
return CheckConstraintMaxLength(itemName, [current count], expected);
}

Expand All @@ -423,20 +435,29 @@ class TestCommandBridge : public CHIPCommandBridge,
// Used when the minValue is a saved variable, since ConstraintsChecker does
// not expect Core Foundation types.
template <typename T, std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, int> = 0>
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMinValue(itemName, current, [expected longLongValue]);
}

template <typename T, std::enable_if_t<std::is_integral<T>::value && !std::is_signed<T>::value, int> = 0>
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMinValue(itemName, current, [expected unsignedLongLongValue]);
}

template <typename T, std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMinValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMinValue(itemName, current, [expected doubleValue]);
}

Expand All @@ -445,20 +466,29 @@ class TestCommandBridge : public CHIPCommandBridge,
// Used when the maxValue is a saved variable, since ConstraintsChecker does
// not expect Core Foundation types.
template <typename T, std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, int> = 0>
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMaxValue(itemName, current, [expected longLongValue]);
}

template <typename T, std::enable_if_t<std::is_integral<T>::value && !std::is_signed<T>::value, int> = 0>
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMaxValue(itemName, current, [expected unsignedLongLongValue]);
}

template <typename T, std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nonnull expected)
bool CheckConstraintMaxValue(const char * _Nonnull itemName, T current, const NSNumber * _Nullable expected)
{
if (expected == nil) {
return true;
}
return ConstraintsChecker::CheckConstraintMaxValue(itemName, current, [expected doubleValue]);
}

Expand Down

0 comments on commit 37b2431

Please sign in to comment.