From a2bebc054224e2d94de69adc9d7f3e252864c51a Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 13 Jan 2022 17:16:05 +0100 Subject: [PATCH] [YAML] Add 'saveAs' support for CHAR_STRING and OCTET_STRING --- .../templates/partials/test_cluster.zapt | 28 ++++++++ .../partials/test_cluster_command_value.zapt | 9 ++- .../partials/test_cluster_value_equals.zapt | 3 +- src/app/tests/suites/TestSaveAs.yaml | 68 +++++++++++++++++++ .../common/ClusterTestGeneration.js | 44 ++++++++---- 5 files changed, 137 insertions(+), 15 deletions(-) diff --git a/examples/chip-tool/templates/partials/test_cluster.zapt b/examples/chip-tool/templates/partials/test_cluster.zapt index f666c463e2ee4f..c8bfeaa1e72242 100644 --- a/examples/chip-tool/templates/partials/test_cluster.zapt +++ b/examples/chip-tool/templates/partials/test_cluster.zapt @@ -13,6 +13,23 @@ class {{filename}}: public TestCommand {{/chip_tests_config}} } + ~{{filename}}() + { + {{#chip_tests_items}} + {{#chip_tests_item_response_parameters}} + {{#if saveAs}} + {{#if (isString type)}} + if ({{saveAs}}Buffer != nullptr) + { + chip::Platform::MemoryFree({{saveAs}}Buffer); + {{saveAs}}Buffer = nullptr; + } + {{/if}} + {{/if}} + {{/chip_tests_item_response_parameters}} + {{/chip_tests_items}} + } + /////////// TestCommand Interface ///////// void NextTest() override { @@ -75,6 +92,7 @@ class {{filename}}: public TestCommand {{#chip_tests_items}} {{#chip_tests_item_response_parameters}} {{#if saveAs}} + {{~#if (isString type)}}{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} * {{saveAs}}Buffer = nullptr;{{/if~}} {{chipType}} {{saveAs}}; {{/if}} {{/chip_tests_item_response_parameters}} @@ -357,8 +375,18 @@ class {{filename}}: public TestCommand {{/if}} {{#if saveAs}} + {{#if (isString type)}} + if ({{saveAs}}Buffer != nullptr) + { + chip::Platform::MemoryFree({{saveAs}}Buffer); + } + {{saveAs}}Buffer = static_cast<{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} *>(chip::Platform::MemoryAlloc({{>item}}.size())); + memcpy({{saveAs}}Buffer, {{>item}}.data(), {{>item}}.size()); + {{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{>item}}.size()); + {{else}} {{saveAs}} = {{>item}}; {{/if}} + {{/if}} {{/chip_tests_item_response_parameters}} {{#unless async}}NextTest();{{/unless}} {{/if}} diff --git a/examples/chip-tool/templates/partials/test_cluster_command_value.zapt b/examples/chip-tool/templates/partials/test_cluster_command_value.zapt index 53c59106296911..5cbf0d57c3915c 100644 --- a/examples/chip-tool/templates/partials/test_cluster_command_value.zapt +++ b/examples/chip-tool/templates/partials/test_cluster_command_value.zapt @@ -35,7 +35,10 @@ {{else}} {{container}} = - {{#if_chip_enum type}} + {{~#if (chip_tests_variables_has definedValue)~}} + {{definedValue}}; + {{else~}} + {{~#if_chip_enum type~}} static_cast<{{zapTypeToEncodableClusterObjectType type ns=ns}}>({{definedValue}}); {{else if (isCharString type)}} chip::Span("{{definedValue}}garbage: not in length on purpose", {{utf8StringLength definedValue}}); @@ -49,6 +52,8 @@ {{else}} {{asTypedLiteral definedValue type}}; {{/if_is_bitmap}} - {{/if_chip_enum}} + {{/if_chip_enum~}} + {{~/if~}} + {{/if_is_struct}} {{/if}} diff --git a/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt b/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt index a50b9be89adca2..179e3cda27a993 100644 --- a/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt +++ b/examples/chip-tool/templates/partials/test_cluster_value_equals.zapt @@ -37,7 +37,8 @@ than "global") that are not present in the struct ? }} {{else}} VerifyOrReturn(CheckValue{{#if (isString type)}}AsString{{/if}}("{{label}}", {{actual}}, - {{~#if (isOctetString type)}}chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}}) + {{~#if (chip_tests_variables_has expected)}}{{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)}} {{else}}{{asTypedLiteral expected type}} diff --git a/src/app/tests/suites/TestSaveAs.yaml b/src/app/tests/suites/TestSaveAs.yaml index 1ba2e3559ddc0f..c6a999aab1d97d 100644 --- a/src/app/tests/suites/TestSaveAs.yaml +++ b/src/app/tests/suites/TestSaveAs.yaml @@ -675,3 +675,71 @@ tests: attribute: "vendor_id" response: value: readAttributeVendorIdDefaultValue + + # Tests for CHAR_STRING + + - label: "Read attribute char_string Default Value" + command: "readAttribute" + attribute: "char_string" + response: + saveAs: readAttributeCharStringDefaultValue + value: "" + + - label: "Write attribute char_string Not Default Value" + command: "writeAttribute" + attribute: "char_string" + arguments: + value: "NotDefault" + + - label: "Read attribute char_string Not Default Value" + command: "readAttribute" + attribute: "char_string" + response: + constraints: + notValue: readAttributeCharStringDefaultValue + + - label: "Write attribute char_string Default Value" + command: "writeAttribute" + attribute: "char_string" + arguments: + value: readAttributeCharStringDefaultValue + + - label: "Read attribute char_string Default Value" + command: "readAttribute" + attribute: "char_string" + response: + value: readAttributeCharStringDefaultValue + + # Tests for OCTET_STRING + + - label: "Read attribute octet_string Default Value" + command: "readAttribute" + attribute: "octet_string" + response: + saveAs: readAttributeOctetStringDefaultValue + value: "" + + - label: "Write attribute octet_string Not Default Value" + command: "writeAttribute" + attribute: "octet_string" + arguments: + value: "NotDefault" + + - label: "Read attribute octet_string Not Default Value" + command: "readAttribute" + attribute: "octet_string" + response: + constraints: + notValue: readAttributeOctetStringDefaultValue + + - label: "Write attribute octet_string Default Value" + command: "writeAttribute" + attribute: "octet_string" + arguments: + value: readAttributeOctetStringDefaultValue + + - label: "Read attribute octet_string Default Value" + command: "readAttribute" + attribute: "octet_string" + response: + value: readAttributeOctetStringDefaultValue diff --git a/src/app/zap-templates/common/ClusterTestGeneration.js b/src/app/zap-templates/common/ClusterTestGeneration.js index 19a669a9e8ae8d..872a21a4331735 100644 --- a/src/app/zap-templates/common/ClusterTestGeneration.js +++ b/src/app/zap-templates/common/ClusterTestGeneration.js @@ -503,12 +503,7 @@ function chip_tests_items(options) return templateUtil.collectBlocks(this.tests, options, this); } -function chip_tests_config(options) -{ - return templateUtil.collectBlocks(this.variables.config, options, this); -} - -function getConfigVariable(context, name) +function getVariable(context, key, name) { while (!('variables' in context) && context.parent) { context = context.parent; @@ -518,33 +513,55 @@ function getConfigVariable(context, name) return null; } - return context.variables.config.find(variable => variable.name == name); + return context.variables[key].find(variable => variable.name == name); } -function getConfigVariableOrThrow(context, name) +function getVariableOrThrow(context, key, name) { - const variable = getConfigVariable(context, name); + const variable = getVariable(context, key, name); if (variable == null) { throw new Error(`Variable ${name} can not be found`); } return variable; } +function chip_tests_variables(options) +{ + return templateUtil.collectBlocks(this.variables.tests, options, this); +} + +function chip_tests_variables_has(name, options) +{ + const variable = getVariable(this, 'tests', name); + return !!variable; +} + +function chip_tests_variables_get_type(name, options) +{ + const variable = getVariableOrThrow(this, 'tests', name); + return variable.type; +} + +function chip_tests_config(options) +{ + return templateUtil.collectBlocks(this.variables.config, options, this); +} + function chip_tests_config_has(name, options) { - const variable = getConfigVariable(this, name); + const variable = getVariable(this, 'config', name); return !!variable; } function chip_tests_config_get_default_value(name, options) { - const variable = getConfigVariableOrThrow(this, name); + const variable = getVariableOrThrow(this, 'config', name); return variable.defaultValue; } function chip_tests_config_get_type(name, options) { - const variable = getConfigVariableOrThrow(this, name); + const variable = getVariableOrThrow(this, 'config', name); return variable.type; } @@ -717,6 +734,9 @@ exports.chip_tests_config = chip_tests_config; exports.chip_tests_config_has = chip_tests_config_has; exports.chip_tests_config_get_default_value = chip_tests_config_get_default_value; exports.chip_tests_config_get_type = chip_tests_config_get_type; +exports.chip_tests_variables = chip_tests_variables; +exports.chip_tests_variables_has = chip_tests_variables_has; +exports.chip_tests_variables_get_type = chip_tests_variables_get_type; exports.isTestOnlyCluster = isTestOnlyCluster; exports.isLiteralNull = isLiteralNull; exports.octetStringEscapedForCLiteral = octetStringEscapedForCLiteral;