From ea136f443395635f9d2018ffbc2c286d8e232f89 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 8 Nov 2021 11:12:48 -0500 Subject: [PATCH] Change expected-value matching to a valueEquals recursive template. For now this does not change any functionality: there is no change in the generated code. It just cleans up how nullable and optional are handled. --- .../templates/partials/test_cluster.zapt | 20 +------------------ .../partials/test_cluster_value_equals.zapt | 19 ++++++++++++++++++ examples/chip-tool/templates/templates.json | 4 ++++ examples/placeholder/templates/templates.json | 4 ++++ 4 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 examples/chip-tool/templates/partials/test_cluster_value_equals.zapt diff --git a/examples/chip-tool/templates/partials/test_cluster.zapt b/examples/chip-tool/templates/partials/test_cluster.zapt index 46a869fdfea6fc..9946d32a9c4de1 100644 --- a/examples/chip-tool/templates/partials/test_cluster.zapt +++ b/examples/chip-tool/templates/partials/test_cluster.zapt @@ -193,26 +193,8 @@ 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}} - {{#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}}{{#if expectedValue.length}}, {{expectedValue}}{{/if}}) - {{else if (isString type)}}AsString("{{>itemValue}}", {{>itemValue}}, "{{expectedValue}}") - {{else}}<{{chipType}}>("{{>itemValue}}", {{>itemValue}}, {{expectedValue}}{{asTypeLiteralSuffix type}}) - {{/if}} - ); - {{/if}} + {{>valueEquals actual=(asLowerCamelCase name) label=(asLowerCamelCase name) expected=expectedValue}} {{/if}} {{#if hasExpectedConstraints}} {{#if isOptional}} diff --git a/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt b/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt new file mode 100644 index 00000000000000..d39aa60c2799e9 --- /dev/null +++ b/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt @@ -0,0 +1,19 @@ +{{#if isOptional}} + VerifyOrReturn(CheckValuePresent("{{label}}", {{actual}})); + {{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=expected isOptional=false}} +{{else if isNullable}} + {{#if (isLiteralNull expected)}} + VerifyOrReturn(CheckValueNull("{{label}}", {{actual}})); + {{else}} + VerifyOrReturn(CheckValueNonNull("{{label}}", {{actual}})); + {{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=expected isNullable=false}} + {{/if}} +{{else}} + VerifyOrReturn(CheckValue + {{~#if isList}}AsListLength("{{label}}", {{actual}}, {{expected.length}}) + {{else if isArray}}AsList("{{label}}", {{actual}}{{#if expected.length}}, {{expected}}{{/if}}) + {{else if (isString type)}}AsString("{{label}}", {{actual}}, "{{expected}}") + {{else}}<{{chipType}}>("{{label}}", {{actual}}, {{expected}}{{asTypeLiteralSuffix type}}) + {{/if}} + ); +{{/if}} diff --git a/examples/chip-tool/templates/templates.json b/examples/chip-tool/templates/templates.json index 9fa7f7f4d4c38e..17afafd04192de 100644 --- a/examples/chip-tool/templates/templates.json +++ b/examples/chip-tool/templates/templates.json @@ -31,6 +31,10 @@ { "name": "commandValue", "path": "partials/test_cluster_command_value.zapt" + }, + { + "name": "valueEquals", + "path": "partials/test_cluster_value_equals.zapt" } ], "templates": [ diff --git a/examples/placeholder/templates/templates.json b/examples/placeholder/templates/templates.json index 4e422b59efd955..3505d79ab47745 100644 --- a/examples/placeholder/templates/templates.json +++ b/examples/placeholder/templates/templates.json @@ -30,6 +30,10 @@ { "name": "commandValue", "path": "../../../examples/chip-tool/templates/partials/test_cluster_command_value.zapt" + }, + { + "name": "valueEquals", + "path": "../../../examples/chip-tool/templates/partials/test_cluster_value_equals.zapt" } ], "templates": [