Skip to content

Commit

Permalink
Add support for null in yaml tests. (project-chip#11139)
Browse files Browse the repository at this point in the history
Adds support for sending null as a command argument and for null in
the expected value.
  • Loading branch information
bzbarsky-apple authored and PSONALl committed Dec 2, 2021
1 parent 8cdfa5e commit 9f1ad39
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 41 deletions.
24 changes: 24 additions & 0 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ class TestCommand : public CHIPCommand
return false;
}

template <typename T>
bool CheckValueNull(const char * itemName, const chip::app::DataModel::Nullable<T> & value)
{
if (value.IsNull())
{
return true;
}

Exit(std::string(itemName) + " expected to be null but isn't");
return false;
}

template <typename T>
bool CheckValueNonNull(const char * itemName, const chip::app::DataModel::Nullable<T> & value)
{
if (!value.IsNull())
{
return true;
}

Exit(std::string(itemName) + " expected to not be null but is");
return false;
}

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;

Expand Down
20 changes: 14 additions & 6 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,26 @@ class {{filename}}: public TestCommand
{{/if}}
{{#chip_tests_item_response_parameters}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
{{~#*inline "itemValue"}}{{>item}}{{#if isNullable}}.Value(){{/if}}{{/inline}}
{{#if hasExpectedValue}}
{{#if isOptional}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{/inline}}
VerifyOrReturn(CheckValuePresent("{{> item}}", {{> item}}));
{{/if}}
VerifyOrReturn(CheckValue
{{~#if isList}}AsListLength("{{>item}}", {{>item}}, {{expectedValue.length}})
{{else if isArray}}AsList("{{>item}}", {{>item}}, {{expectedValue}})
{{else if (isString type)}}AsString("{{>item}}", {{>item}}, "{{expectedValue}}")
{{else}}<{{chipType}}>("{{>item}}", {{>item}}, {{expectedValue}}{{asTypeLiteralSuffix type}})
{{#if (isLiteralNull expectedValue)}}
VerifyOrReturn(CheckValueNull("{{> item}}", {{> item}}));
{{else}}
{{#if isNullable}}
VerifyOrReturn(CheckValueNonNull("{{> item}}", {{> item}}));
{{/if}}
);
VerifyOrReturn(CheckValue
{{~#if isList}}AsListLength("{{>itemValue}}", {{>itemValue}}, {{expectedValue.length}})
{{else if isArray}}AsList("{{>itemValue}}", {{>itemValue}}, {{expectedValue}})
{{else if (isString type)}}AsString("{{>itemValue}}", {{>itemValue}}, "{{expectedValue}}")
{{else}}<{{chipType}}>("{{>itemValue}}", {{>itemValue}}, {{expectedValue}}{{asTypeLiteralSuffix type}})
{{/if}}
);
{{/if}}
{{/if}}
{{#if hasExpectedConstraints}}
{{#if isOptional}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
{{#if ignore}}
{{>commandValue ns=ns container=(concat container ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
{{else}}
{{>commandValue ns=ns container=(concat container "." label ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
{{>commandValue ns=ns container=(concat container "." (asLowerCamelCase label) ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
{{/if}}
{{else if isNullable}}
{{#if ignore}}
{{>commandValue ns=ns container=(concat container ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
{{#if (isLiteralNull definedValue)}}
{{container}}{{#unless ignore}}.{{asLowerCamelCase label}}{{/unless}}.SetNull();
{{else}}
{{>commandValue ns=ns container=(concat container "." label ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
{{#if ignore}}
{{>commandValue ns=ns container=(concat container ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
{{else}}
{{>commandValue ns=ns container=(concat container "." (asLowerCamelCase label) ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
{{/if}}
{{/if}}
{{else if isArray}}

Expand All @@ -26,9 +30,9 @@

{{#zcl_struct_items_by_struct_name type}}
{{#if ../ignore}}
{{>commandValue ns=parent.ns container=(concat parent.container "." label) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
{{>commandValue ns=parent.ns container=(concat parent.container "." (asLowerCamelCase label)) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
{{else}}
{{>commandValue ns=parent.ns container=(concat parent.container "." parent.label) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
{{>commandValue ns=parent.ns container=(concat parent.container "." (asLowerCamelCase parent.label)) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
{{/if}}
{{/zcl_struct_items_by_struct_name}}

Expand Down
2 changes: 2 additions & 0 deletions src/app/clusters/test-cluster-server/test-cluster-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ bool emberAfTestClusterClusterTestNullableOptionalRequestCallback(
{
response.value.SetValue(commandData.arg1.Value().Value());
}

response.originalValue.Emplace(commandData.arg1.Value());
}

CHIP_ERROR err = commandObj->AddResponseData(commandPath, response);
Expand Down
7 changes: 5 additions & 2 deletions src/app/tests/suites/TestClusterComplexTypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ tests:
value: false
- name: "value"
value: 5
- name: "originalValue"
value: 5

- label: "Send Test Command without its optional arg."
command: "testNullableOptionalRequest"
Expand All @@ -416,7 +418,6 @@ tests:
value: false

- label: "Send Test Command with optional arg set to null."
disabled: true
command: "testNullableOptionalRequest"
arguments:
values:
Expand All @@ -427,4 +428,6 @@ tests:
- name: "wasPresent"
value: true
- name: "wasNull"
value: false
value: true
- name: "originalValue"
value: null
17 changes: 17 additions & 0 deletions src/app/zap-templates/common/ClusterTestGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const kResponseName = 'response';
const kDisabledName = 'disabled';
const kResponseErrorName = 'error';

class NullObject {
toString()
{
return "YOU SHOULD HAVE CHECKED (isLiteralNull definedValue)"
}
};

function throwError(test, errorStr)
{
console.error('Error in: ' + test.filename + '.yaml for test with label: "' + test.label + '"\n');
Expand Down Expand Up @@ -417,6 +424,8 @@ function chip_tests_item_parameters(options)
}
value[key] = attachGlobal(global, value[key]);
}
} else if (value === null) {
value = new NullObject();
} else {
switch (typeof value) {
case 'number':
Expand Down Expand Up @@ -487,6 +496,13 @@ function chip_tests_item_response_parameters(options)
return asBlocks.call(this, promise, options);
}

function isLiteralNull(value, options)
{
// Literal null might look different depending on whether it went through
// attachGlobal or not.
return (value === null) || (value instanceof NullObject);
}

//
// Module exports
//
Expand All @@ -496,3 +512,4 @@ exports.chip_tests_item_parameters = chip_tests_item_parameters;
exports.chip_tests_item_response_type = chip_tests_item_response_type;
exports.chip_tests_item_response_parameters = chip_tests_item_response_parameters;
exports.isTestOnlyCluster = isTestOnlyCluster;
exports.isLiteralNull = isLiteralNull;
4 changes: 3 additions & 1 deletion src/app/zap-templates/zcl/data-model/chip/test-cluster.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,13 @@ limitations under the License.

<command source="server" code="0x06" name="TestNullableOptionalResponse" optional="false" disableDefaultResponse="true">
<description>
Delivers information about the argument TestNullableOptionalRequest had.
Delivers information about the argument TestNullableOptionalRequest had,
and the original value if there was one.
</description>
<arg name="wasPresent" type="BOOLEAN"/>
<arg name="wasNull" type="BOOLEAN" optional="true"/>
<arg name="value" type="INT8U" optional="true"/>
<arg name="originalValue" type="INT8U" optional="true" isNullable="true"/>
</command>

<command source="server" code="0x07" name="TestComplexNullableOptionalResponse" optional="false" disableDefaultResponse="true">
Expand Down
6 changes: 3 additions & 3 deletions src/controller/java/zap-generated/CHIPClusters-JNI.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/controller/python/chip/clusters/Objects.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion zzz_generated/app-common/app-common/zap-generated/callback.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zzz_generated/chip-tool/zap-generated/cluster/Commands.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f1ad39

Please sign in to comment.