Skip to content

Commit 313cb4b

Browse files
Add support for null in yaml tests. (#11139)
Adds support for sending null as a command argument and for null in the expected value.
1 parent 52db0e4 commit 313cb4b

File tree

21 files changed

+170
-41
lines changed

21 files changed

+170
-41
lines changed

examples/chip-tool/commands/tests/TestCommand.h

+24
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ class TestCommand : public CHIPCommand
206206
return false;
207207
}
208208

209+
template <typename T>
210+
bool CheckValueNull(const char * itemName, const chip::app::DataModel::Nullable<T> & value)
211+
{
212+
if (value.IsNull())
213+
{
214+
return true;
215+
}
216+
217+
Exit(std::string(itemName) + " expected to be null but isn't");
218+
return false;
219+
}
220+
221+
template <typename T>
222+
bool CheckValueNonNull(const char * itemName, const chip::app::DataModel::Nullable<T> & value)
223+
{
224+
if (!value.IsNull())
225+
{
226+
return true;
227+
}
228+
229+
Exit(std::string(itemName) + " expected to not be null but is");
230+
return false;
231+
}
232+
209233
chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
210234
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
211235

examples/chip-tool/templates/partials/test_cluster.zapt

+14-6
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,26 @@ class {{filename}}: public TestCommand
172172
{{/if}}
173173
{{#chip_tests_item_response_parameters}}
174174
{{~#*inline "item"}}{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
175+
{{~#*inline "itemValue"}}{{>item}}{{#if isNullable}}.Value(){{/if}}{{/inline}}
175176
{{#if hasExpectedValue}}
176177
{{#if isOptional}}
177178
{{~#*inline "item"}}{{asLowerCamelCase name}}{{/inline}}
178179
VerifyOrReturn(CheckValuePresent("{{> item}}", {{> item}}));
179180
{{/if}}
180-
VerifyOrReturn(CheckValue
181-
{{~#if isList}}AsListLength("{{>item}}", {{>item}}, {{expectedValue.length}})
182-
{{else if isArray}}AsList("{{>item}}", {{>item}}, {{expectedValue}})
183-
{{else if (isString type)}}AsString("{{>item}}", {{>item}}, "{{expectedValue}}")
184-
{{else}}<{{chipType}}>("{{>item}}", {{>item}}, {{expectedValue}}{{asTypeLiteralSuffix type}})
181+
{{#if (isLiteralNull expectedValue)}}
182+
VerifyOrReturn(CheckValueNull("{{> item}}", {{> item}}));
183+
{{else}}
184+
{{#if isNullable}}
185+
VerifyOrReturn(CheckValueNonNull("{{> item}}", {{> item}}));
185186
{{/if}}
186-
);
187+
VerifyOrReturn(CheckValue
188+
{{~#if isList}}AsListLength("{{>itemValue}}", {{>itemValue}}, {{expectedValue.length}})
189+
{{else if isArray}}AsList("{{>itemValue}}", {{>itemValue}}, {{expectedValue}})
190+
{{else if (isString type)}}AsString("{{>itemValue}}", {{>itemValue}}, "{{expectedValue}}")
191+
{{else}}<{{chipType}}>("{{>itemValue}}", {{>itemValue}}, {{expectedValue}}{{asTypeLiteralSuffix type}})
192+
{{/if}}
193+
);
194+
{{/if}}
187195
{{/if}}
188196
{{#if hasExpectedConstraints}}
189197
{{#if isOptional}}

examples/chip-tool/templates/partials/test_cluster_command_value.zapt

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
{{#if ignore}}
33
{{>commandValue ns=ns container=(concat container ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
44
{{else}}
5-
{{>commandValue ns=ns container=(concat container "." label ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
5+
{{>commandValue ns=ns container=(concat container "." (asLowerCamelCase label) ".Emplace()") definedValue=definedValue type=type isOptional=false ignore=true}}
66
{{/if}}
77
{{else if isNullable}}
8-
{{#if ignore}}
9-
{{>commandValue ns=ns container=(concat container ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
8+
{{#if (isLiteralNull definedValue)}}
9+
{{container}}{{#unless ignore}}.{{asLowerCamelCase label}}{{/unless}}.SetNull();
1010
{{else}}
11-
{{>commandValue ns=ns container=(concat container "." label ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
11+
{{#if ignore}}
12+
{{>commandValue ns=ns container=(concat container ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
13+
{{else}}
14+
{{>commandValue ns=ns container=(concat container "." (asLowerCamelCase label) ".SetNonNull()") definedValue=definedValue type=type isNullable=false ignore=true}}
15+
{{/if}}
1216
{{/if}}
1317
{{else if isArray}}
1418

@@ -26,9 +30,9 @@
2630

2731
{{#zcl_struct_items_by_struct_name type}}
2832
{{#if ../ignore}}
29-
{{>commandValue ns=parent.ns container=(concat parent.container "." label) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
33+
{{>commandValue ns=parent.ns container=(concat parent.container "." (asLowerCamelCase label)) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
3034
{{else}}
31-
{{>commandValue ns=parent.ns container=(concat parent.container "." parent.label) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
35+
{{>commandValue ns=parent.ns container=(concat parent.container "." (asLowerCamelCase parent.label)) definedValue=(lookup parent.definedValue name) ignore=../ignore}}
3236
{{/if}}
3337
{{/zcl_struct_items_by_struct_name}}
3438

src/app/clusters/test-cluster-server/test-cluster-server.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ bool emberAfTestClusterClusterTestNullableOptionalRequestCallback(
444444
{
445445
response.value.SetValue(commandData.arg1.Value().Value());
446446
}
447+
448+
response.originalValue.Emplace(commandData.arg1.Value());
447449
}
448450

449451
CHIP_ERROR err = commandObj->AddResponseData(commandPath, response);

src/app/tests/suites/TestClusterComplexTypes.yaml

+5-2
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ tests:
407407
value: false
408408
- name: "value"
409409
value: 5
410+
- name: "originalValue"
411+
value: 5
410412

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

418420
- label: "Send Test Command with optional arg set to null."
419-
disabled: true
420421
command: "testNullableOptionalRequest"
421422
arguments:
422423
values:
@@ -427,4 +428,6 @@ tests:
427428
- name: "wasPresent"
428429
value: true
429430
- name: "wasNull"
430-
value: false
431+
value: true
432+
- name: "originalValue"
433+
value: null

src/app/zap-templates/common/ClusterTestGeneration.js

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ const kResponseName = 'response';
4242
const kDisabledName = 'disabled';
4343
const kResponseErrorName = 'error';
4444

45+
class NullObject {
46+
toString()
47+
{
48+
return "YOU SHOULD HAVE CHECKED (isLiteralNull definedValue)"
49+
}
50+
};
51+
4552
function throwError(test, errorStr)
4653
{
4754
console.error('Error in: ' + test.filename + '.yaml for test with label: "' + test.label + '"\n');
@@ -417,6 +424,8 @@ function chip_tests_item_parameters(options)
417424
}
418425
value[key] = attachGlobal(global, value[key]);
419426
}
427+
} else if (value === null) {
428+
value = new NullObject();
420429
} else {
421430
switch (typeof value) {
422431
case 'number':
@@ -487,6 +496,13 @@ function chip_tests_item_response_parameters(options)
487496
return asBlocks.call(this, promise, options);
488497
}
489498

499+
function isLiteralNull(value, options)
500+
{
501+
// Literal null might look different depending on whether it went through
502+
// attachGlobal or not.
503+
return (value === null) || (value instanceof NullObject);
504+
}
505+
490506
//
491507
// Module exports
492508
//
@@ -496,3 +512,4 @@ exports.chip_tests_item_parameters = chip_tests_item_parameters;
496512
exports.chip_tests_item_response_type = chip_tests_item_response_type;
497513
exports.chip_tests_item_response_parameters = chip_tests_item_response_parameters;
498514
exports.isTestOnlyCluster = isTestOnlyCluster;
515+
exports.isLiteralNull = isLiteralNull;

src/app/zap-templates/zcl/data-model/chip/test-cluster.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,13 @@ limitations under the License.
367367

368368
<command source="server" code="0x06" name="TestNullableOptionalResponse" optional="false" disableDefaultResponse="true">
369369
<description>
370-
Delivers information about the argument TestNullableOptionalRequest had.
370+
Delivers information about the argument TestNullableOptionalRequest had,
371+
and the original value if there was one.
371372
</description>
372373
<arg name="wasPresent" type="BOOLEAN"/>
373374
<arg name="wasNull" type="BOOLEAN" optional="true"/>
374375
<arg name="value" type="INT8U" optional="true"/>
376+
<arg name="originalValue" type="INT8U" optional="true" isNullable="true"/>
375377
</command>
376378

377379
<command source="server" code="0x07" name="TestComplexNullableOptionalResponse" optional="false" disableDefaultResponse="true">

src/controller/java/zap-generated/CHIPClusters-JNI.cpp

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controller/java/zap-generated/chip/devicecontroller/ChipClusters.java

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controller/java/zap-generated/chip/devicecontroller/ClusterInfoMapping.java

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/controller/python/chip/clusters/Objects.py

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/darwin/Framework/CHIP/zap-generated/CHIPCallbackBridge.mm

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/darwin/Framework/CHIP/zap-generated/CHIPCallbackBridge_internal.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zzz_generated/app-common/app-common/zap-generated/callback.h

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zzz_generated/app-common/app-common/zap-generated/cluster-objects.cpp

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zzz_generated/app-common/app-common/zap-generated/cluster-objects.h

+6-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zzz_generated/chip-tool/zap-generated/cluster/Commands.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)