Skip to content

Commit

Permalink
Add new function to deal with Nullable not value comparison (#17363)
Browse files Browse the repository at this point in the history
* Add new function to deal with Nullable negative comparison

* Generated Code.
  • Loading branch information
krypton36 authored May 11, 2022
1 parent 2ae1d5a commit ba0692c
Show file tree
Hide file tree
Showing 8 changed files with 5,217 additions and 4,730 deletions.
9 changes: 8 additions & 1 deletion examples/chip-tool-darwin/commands/tests/TestCommandBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,15 @@ class TestCommandBridge : public CHIPCommandBridge,
return ConstraintsChecker::CheckConstraintNotValue(itemName, currentValue, expectedValue);
}

bool CheckConstraintNotValue(const char * _Nonnull itemName, const NSNumber * _Nonnull current, NSNumber * _Nonnull expected)
bool CheckConstraintNotValue(const char * _Nonnull itemName, const NSNumber * _Nullable current, NSNumber * _Nullable expected)
{
if (current == nil && expected == nil) {
Exit(std::string(itemName) + " got unexpected value. Both values are nil.");
return false;
}
if ((current == nil) != (expected == nil)) {
return true;
}
if ([current isEqualToNumber:expected]) {
Exit(std::string(itemName) + " got unexpected value: " + std::string([[current stringValue] UTF8String]));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ class {{filename}}: public TestCommandBridge

{{#chip_tests_item_responses}}
{{#if error}}
VerifyOrReturn(CheckValue("status", err, {{error}}));
VerifyOrReturn(CheckValue("status", err ? err.code : 0, {{error}}));
NextTest();
{{else}}
VerifyOrReturn(CheckValue("status", err, 0));
VerifyOrReturn(CheckValue("status", err ? err.code : 0, 0));
{{#unless isSubscribeAttribute}}

{{#chip_tests_item_response_parameters}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
{{#*inline "data"}}
{{#if isNullable}}{{asPropertyValue}}.Value().data(){{else}}{{asPropertyValue}}.data(){{/if}}
{{/inline}}
{{#*inline "size"}}
{{#if isNullable}}{{asPropertyValue}}.Value().size(){{else}}{{asPropertyValue}}.size(){{/if}}
{{/inline}}
{{#if saveAs}}
{{#if (isString type)}}
{{#if isNullable}}
if ({{asPropertyValue}}.IsNull()){
{{saveAs}}.SetNull();
}
else {
{{/if}}
if ({{saveAs}}Buffer != nullptr)
{
chip::Platform::MemoryFree({{saveAs}}Buffer);
}
{{saveAs}}Buffer = static_cast<{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} *>(chip::Platform::MemoryAlloc({{asPropertyValue}}.size()));
memcpy({{saveAs}}Buffer, {{asPropertyValue}}.data(), {{asPropertyValue}}.size());
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{asPropertyValue}}.size());
{{saveAs}}Buffer = static_cast<{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} *>(chip::Platform::MemoryAlloc({{>size}}));
memcpy({{saveAs}}Buffer, {{>data}}, {{>size}});
{{#if isNullable}}
{{saveAs}}.SetNonNull({{saveAs}}Buffer, {{>size}});
}
{{else}}
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{>size}});
{{/if}}
{{else}}
{{saveAs}} = {{asPropertyValue}};
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
else
{
VerifyOrReturn(CheckValueNonNull("{{label}}", {{actual}}));
{{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=(concat expected ".Value()") isNullable=false depth=(incrementDepth depth)}}
{{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=(concat expected ".Value()") isNullable=false keepAsExpected=true depth=(incrementDepth depth)}}
}
{{else}}
VerifyOrReturn(CheckValueNonNull("{{label}}", {{actual}}));
Expand Down Expand Up @@ -51,6 +51,7 @@
{{else}}
VerifyOrReturn(CheckValue{{#if (isString type)}}AsString{{/if}}("{{label}}", {{actual}},
{{~#if (chip_tests_variables_has expected)}}{{expected}}
{{else if keepAsExpected}}{{expected}}
{{else if (isOctetString type)}}chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}})
{{else if (isCharString type)}}chip::CharSpan("{{expected}}", {{utf8StringLength expected}})
{{else if (chip_tests_config_has expected)}}m{{asUpperCamelCase expected}}.HasValue() ? m{{asUpperCamelCase expected}}.Value() : {{asTypedLiteral (chip_tests_config_get_default_value expected) (chip_tests_config_get_type expected)}}
Expand Down
46 changes: 46 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ tests:
command: "readAttribute"
attribute: "nullable_boolean"
response:
saveAs: booValueNull
value: null

- label: "Write attribute NULLABLE_BOOLEAN True"
Expand All @@ -1666,6 +1667,13 @@ tests:
response:
value: true

- label: "Read attribute NULLABLE_BOOLEAN not null"
command: "readAttribute"
attribute: "nullable_boolean"
response:
constraints:
notValue: booValueNull

# Tests for nullable Bitmap8 attribute

- label: "Write attribute NULLABLE_BITMAP8 Max Value"
Expand All @@ -1692,6 +1700,7 @@ tests:
command: "readAttribute"
attribute: "nullable_bitmap8"
response:
saveAs: nullableValue254
value: 254

- label: "Write attribute NULLABLE_BITMAP8 null Value"
Expand All @@ -1706,6 +1715,13 @@ tests:
response:
value: null

- label: "Read attribute NULLABLE_BITMAP8 not 254 Value"
command: "readAttribute"
attribute: "nullable_bitmap8"
response:
constraints:
notValue: nullableValue254

# Tests for nullable Bitmap16 attribute

- label: "Write attribute NULLABLE_BITMAP16 Max Value"
Expand Down Expand Up @@ -2758,6 +2774,7 @@ tests:
command: "readAttribute"
attribute: "nullable_enum_attr"
response:
saveAs: nullableEnumAttr254
value: 254

- label: "Write attribute NULLABLE_SIMPLE_ENUM null Value"
Expand All @@ -2772,6 +2789,13 @@ tests:
response:
value: null

- label: "Read attribute NULLABLE_SIMPLE_ENUM not 254 Value"
command: "readAttribute"
attribute: "nullable_enum_attr"
response:
constraints:
notValue: nullableEnumAttr254

# Tests for Octet String attribute

- label: "Read attribute NULLABLE_OCTET_STRING Default Value"
Expand All @@ -2790,6 +2814,7 @@ tests:
command: "readAttribute"
attribute: "nullable_octet_string"
response:
saveAs: nullableOctetStrTestValue
value: "TestValue"

- label: "Write attribute NULLABLE_OCTET_STRING"
Expand All @@ -2816,6 +2841,13 @@ tests:
response:
value: ""

- label: "Read attribute NULLABLE_OCTET_STRING not TestValue"
command: "readAttribute"
attribute: "nullable_octet_string"
response:
constraints:
notValue: nullableOctetStrTestValue

# Tests for Char String attribute

- label: "Read attribute NULLABLE_CHAR_STRING Default Value"
Expand All @@ -2834,8 +2866,15 @@ tests:
command: "readAttribute"
attribute: "nullable_char_string"
response:
saveAs: nullableCharStringSave
value: "☉T☉"

- label: "Read attribute NULLABLE_CHAR_STRING"
command: "readAttribute"
attribute: "nullable_char_string"
response:
value: nullableCharStringSave

- label: "Write attribute NULLABLE_CHAR_STRING - Value too long"
command: "writeAttribute"
attribute: "nullable_char_string"
Expand All @@ -2860,6 +2899,13 @@ tests:
response:
value: ""

- label: "Read attribute NULLABLE_CHAR_STRING not ☉T☉"
command: "readAttribute"
attribute: "nullable_char_string"
response:
constraints:
notValue: nullableCharStringSave

- label: "Read attribute from nonexistent endpoint."
endpoint: 200
command: "readAttribute"
Expand Down
24 changes: 24 additions & 0 deletions src/app/tests/suites/include/ConstraintsChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ class ConstraintsChecker
return CheckConstraintMaxValue(itemName, current.Value(), static_cast<T>(expected));
}

template <typename T>
bool CheckConstraintNotValue(const char * itemName, const chip::app::DataModel::Nullable<T> & current,
const chip::app::DataModel::Nullable<T> & expected)
{
if (expected.IsNull() && current.IsNull())
{
Exit(std::string(itemName) + " got null for both values, but expected not equal");
return false;
}

if (expected.IsNull() != current.IsNull())
{
return true;
}

return CheckConstraintNotValue(itemName, current.Value(), expected.Value());
}

template <typename T, typename U, std::enable_if_t<!std::is_enum<T>::value, int> = 0>
bool CheckConstraintNotValue(const char * itemName, T current, U expected)
{
Expand All @@ -287,6 +305,12 @@ class ConstraintsChecker
return CheckConstraintNotValue(itemName, chip::to_underlying(current), expected);
}

template <typename T, std::enable_if_t<std::is_enum<T>::value, int> = 0>
bool CheckConstraintNotValue(const char * itemName, T current, T expected)
{
return CheckConstraintNotValue(itemName, chip::to_underlying(current), chip::to_underlying(expected));
}

template <typename T>
bool CheckConstraintNotValue(const char * itemName, chip::BitFlags<T> current, chip::BitFlags<T> expected)
{
Expand Down
Loading

0 comments on commit ba0692c

Please sign in to comment.