Skip to content

Commit

Permalink
[YAML] Make some partials for checking the constraints and saving val…
Browse files Browse the repository at this point in the history
…ues (#15414)

* Use an helper instead of multiple inline partials for determining the name of the property for the maybeCheckExpectedValue,maybeCheckExpectedContraints,maybeSaveAs partials

* [YAML] Move template saveAs blocks into dedicated partials

* [YAML] Move template blocks used to check the response content into dedicated partials

* Allow digits into the isUpperCase constraint

* Update generated content
  • Loading branch information
vivien-apple authored Feb 23, 2022
1 parent df88ca4 commit 70ae428
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 418 deletions.
28 changes: 28 additions & 0 deletions examples/chip-tool/templates/tests/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,40 @@
* limitations under the License.
*/

const { asLowerCamelCase } = require('../../../../src/app/zap-templates/templates/app/helper.js');
const { isTestOnlyCluster } = require('../../../../src/app/zap-templates/common/simulated-clusters/SimulatedClusters.js');

function utf8StringLength(str)
{
return new TextEncoder().encode(str).length
}

/*
* Returns the name to use for accessing a given property of
* a decodable type.
*
*/
function asPropertyValue(options)
{
let name = '';

// The decodable type for simulated cluster is a struct by default, even if the
// command just returns a single value.
if (isTestOnlyCluster(this.parent.cluster)) {
name = 'value.'
}

name += asLowerCamelCase(this.name);

if (this.isOptional && !options.hash.dontUnwrapValue) {
name += '.Value()';
}

return name;
}

//
// Module exports
//
exports.utf8StringLength = utf8StringLength;
exports.asPropertyValue = asPropertyValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{#if hasExpectedConstraints}}
{{#if isOptional}}
VerifyOrReturn(CheckValuePresent("{{asPropertyValue dontUnwrapValue=true}}", {{asPropertyValue dontUnwrapValue=true}}));
{{/if}}

{{~#if (hasProperty expectedConstraints "type")}}VerifyOrReturn(CheckConstraintType("{{asPropertyValue}}", "", "{{expectedConstraints.type}}"));{{/if}}

{{~#if (hasProperty expectedConstraints "format")}}VerifyOrReturn(CheckConstraintFormat("{{asPropertyValue}}", "", "{{expectedConstraints.format}}"));{{/if}}

{{~#if (hasProperty expectedConstraints "startsWith")}}VerifyOrReturn(CheckConstraintStartsWith("{{asPropertyValue}}", {{asPropertyValue}}, "{{expectedConstraints.startsWith}}"));{{/if}}

{{~#if (hasProperty expectedConstraints "endsWith")}}VerifyOrReturn(CheckConstraintEndsWith("{{asPropertyValue}}", {{asPropertyValue}}, "{{expectedConstraints.endsWith}}"));{{/if}}

{{~#if (hasProperty expectedConstraints "isUpperCase")}}VerifyOrReturn(CheckConstraintIsUpperCase("{{asPropertyValue}}", {{asPropertyValue}}, {{expectedConstraints.isUpperCase}}));{{/if}}

{{~#if (hasProperty expectedConstraints "isLowerCase")}}VerifyOrReturn(CheckConstraintIsLowerCase("{{asPropertyValue}}", {{asPropertyValue}}, {{expectedConstraints.isLowerCase}}));{{/if}}

{{~#if (hasProperty expectedConstraints "isHexString")}}VerifyOrReturn(CheckConstraintIsHexString("{{asPropertyValue}}", {{asPropertyValue}}, {{expectedConstraints.isHexString}}));{{/if}}

{{~#if (hasProperty expectedConstraints "minLength")}}VerifyOrReturn(CheckConstraintMinLength("{{asPropertyValue}}", {{asPropertyValue}}.size(), {{expectedConstraints.minLength}}));{{/if}}

{{~#if (hasProperty expectedConstraints "maxLength")}}VerifyOrReturn(CheckConstraintMaxLength("{{asPropertyValue}}", {{asPropertyValue}}.size(), {{expectedConstraints.maxLength}}));{{/if}}

{{~#if (hasProperty expectedConstraints "minValue")}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{asPropertyValue}}", {{asPropertyValue}}, {{asTypedLiteral expectedConstraints.minValue type}}));{{/if}}

{{~#if (hasProperty expectedConstraints "maxValue")}}VerifyOrReturn(CheckConstraintMaxValue<{{chipType}}>("{{asPropertyValue}}", {{asPropertyValue}}, {{asTypedLiteral expectedConstraints.maxValue type}}));{{/if}}

{{~#if (hasProperty expectedConstraints "notValue")}}
{{#if (isLiteralNull expectedConstraints.notValue)}}
VerifyOrReturn(CheckValueNonNull("{{asPropertyValue}}", {{asPropertyValue}}));
{{else}}
VerifyOrReturn(CheckConstraintNotValue("{{asPropertyValue}}", {{asPropertyValue}}, {{asTypedLiteral expectedConstraints.notValue type}}));
{{/if}}
{{/if}}
{{/if}}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{~#if hasExpectedValue}}
{{~>valueEquals actual=(asPropertyValue dontUnwrapValue=true) label=(asLowerCamelCase name) expected=expectedValue depth=0~}}
{{~/if~}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#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({{asPropertyValue}}.size()));
memcpy({{saveAs}}Buffer, {{asPropertyValue}}.data(), {{asPropertyValue}}.size());
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{asPropertyValue}}.size());
{{else}}
{{saveAs}} = {{asPropertyValue}};
{{/if}}
{{/if}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#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~}}
{{zapTypeToDecodableClusterObjectType type ns=../cluster}} {{saveAs}};
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_items}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#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}}
106 changes: 9 additions & 97 deletions examples/chip-tool/templates/tests/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,7 @@ class {{filename}}Suite: public TestCommand

~{{filename}}Suite()
{
{{#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}}
{{>teardownSaveAs}}
}

/////////// TestCommand Interface /////////
Expand Down Expand Up @@ -93,16 +81,9 @@ class {{filename}}Suite: public TestCommand
chip::Optional<{{chipType}}> m{{asUpperCamelCase name}};
{{/chip_tests_config}}

{{#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~}}
{{zapTypeToDecodableClusterObjectType type ns=../cluster}} {{saveAs}};
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_items}}
{{>setupSaveAs}}

void OnDiscoveryCommandsResults(const DiscoveryCommandResult & nodeData) override
void OnDiscoveryCommandsResults(const DiscoveryCommandResult & value) override
{
bool isExpectedDnssdResult = false;
{{#chip_tests_items}}
Expand All @@ -111,39 +92,9 @@ class {{filename}}Suite: public TestCommand
{
isExpectedDnssdResult = true;
{{#chip_tests_item_response_parameters}}
{{#*inline "itemValue"}}nodeData.{{name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
{{~#if hasExpectedValue}}
{{#if isOptional}}VerifyOrReturn(CheckValuePresent("{{name}}", nodeData.{{name}}));{{/if}}
VerifyOrReturn(CheckValue("{{name}}", {{>itemValue}},
{{#if (chip_tests_config_has expectedValue)}}
m{{asUpperCamelCase expectedValue}}.HasValue() ? m{{asUpperCamelCase expectedValue}}.Value() : {{asTypedLiteral (chip_tests_config_get_default_value expectedValue) (chip_tests_config_get_type expectedValue)}}
{{else}}
{{expectedValue}}
{{/if}}
));
{{/if}}
{{#if hasExpectedConstraints}}
{{#if isOptional}}VerifyOrReturn(CheckValuePresent("{{name}}", nodeData.{{name}}));{{/if}}
{{#if (hasProperty expectedConstraints "minLength")}}VerifyOrReturn(CheckConstraintMinLength("{{name}}", {{>itemValue}}.size(), {{expectedConstraints.minLength}}));{{/if}}
{{#if (hasProperty expectedConstraints "maxLength")}}VerifyOrReturn(CheckConstraintMaxLength("{{name}}", {{>itemValue}}.size(), {{expectedConstraints.maxLength}}));{{/if}}
{{#if (hasProperty expectedConstraints "minValue")}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{name}}", {{>itemValue}}, {{asTypedLiteral expectedConstraints.minValue type}}));{{/if}}
{{#if (hasProperty expectedConstraints "maxValue")}}VerifyOrReturn(CheckConstraintMaxValue<{{chipType}}>("{{name}}", {{>itemValue}}, {{asTypedLiteral expectedConstraints.maxValue type}}));{{/if}}
{{#if (hasProperty expectedConstraints "notValue")}}VerifyOrReturn(CheckConstraintNotValue("{{name}}", {{>itemValue}}, {{asTypedLiteral expectedConstraints.notValue type}}));{{/if}}
{{/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({{>itemValue}}.size()));
memcpy({{saveAs}}Buffer, {{>itemValue}}.data(), {{>itemValue}}.size());
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{>itemValue}}.size());
{{else}}
{{saveAs}} = {{>itemValue}};
{{/if}}
{{/if}}
{{>maybeCheckExpectedValue}}
{{>maybeCheckExpectedConstraints}}
{{>maybeSaveAs}}
{{/chip_tests_item_response_parameters}}
}
{{/if}}
Expand Down Expand Up @@ -409,48 +360,9 @@ class {{filename}}Suite: public TestCommand
mReceivedReport_{{index}} = true;
{{/if}}
{{#chip_tests_item_response_parameters}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
{{#if hasExpectedValue}}
{{>valueEquals actual=(asLowerCamelCase name) label=(asLowerCamelCase name) expected=expectedValue depth=0}}
{{/if}}
{{#if hasExpectedConstraints}}
{{#if isOptional}}
{{~#*inline "item"}}{{asLowerCamelCase name}}{{/inline}}
VerifyOrReturn(CheckValuePresent("{{> item}}", {{> item}}));
{{/if}}
{{#if (hasProperty expectedConstraints "type")}}VerifyOrReturn(CheckConstraintType("{{>item}}", "", "{{expectedConstraints.type}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "format")}}VerifyOrReturn(CheckConstraintFormat("{{>item}}", "", "{{expectedConstraints.format}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "startsWith")}}VerifyOrReturn(CheckConstraintStartsWith("{{>item}}", {{>item}}, "{{expectedConstraints.startsWith}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "endsWith")}}VerifyOrReturn(CheckConstraintEndsWith("{{>item}}", {{>item}}, "{{expectedConstraints.endsWith}}"));{{/if}}
{{~#if (hasProperty expectedConstraints "isUpperCase")}}VerifyOrReturn(CheckConstraintIsUpperCase("{{>item}}", {{>item}}, {{expectedConstraints.isUpperCase}}));{{/if}}
{{~#if (hasProperty expectedConstraints "isLowerCase")}}VerifyOrReturn(CheckConstraintIsLowerCase("{{>item}}", {{>item}}, {{expectedConstraints.isLowerCase}}));{{/if}}
{{~#if (hasProperty expectedConstraints "isHexString")}}VerifyOrReturn(CheckConstraintIsHexString("{{>item}}", {{>item}}, {{expectedConstraints.isHexString}}));{{/if}}
{{~#if (hasProperty expectedConstraints "minLength")}}VerifyOrReturn(CheckConstraintMinLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.minLength}}));{{/if}}
{{~#if (hasProperty expectedConstraints "maxLength")}}VerifyOrReturn(CheckConstraintMaxLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.maxLength}}));{{/if}}
{{~#if (hasProperty expectedConstraints "minValue")}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.minValue type}}));{{/if}}
{{~#if (hasProperty expectedConstraints "maxValue")}}VerifyOrReturn(CheckConstraintMaxValue<{{chipType}}>("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.maxValue type}}));{{/if}}
{{~#if (hasProperty expectedConstraints "notValue")}}
{{#if (isLiteralNull expectedConstraints.notValue)}}
VerifyOrReturn(CheckValueNonNull("{{>item}}", {{>item}}));
{{else}}
VerifyOrReturn(CheckConstraintNotValue("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.notValue type}}));
{{/if}}
{{/if}}
{{/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}}
{{>maybeCheckExpectedValue}}
{{>maybeCheckExpectedConstraints}}
{{>maybeSaveAs}}
{{/chip_tests_item_response_parameters}}
{{#unless async}}NextTest();{{/unless}}
{{/if}}
Expand Down
20 changes: 20 additions & 0 deletions examples/chip-tool/templates/tests/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
"name": "test_cluster",
"path": "partials/test_cluster.zapt"
},
{
"name": "maybeCheckExpectedValue",
"path": "partials/checks/maybeCheckExpectedValue.zapt"
},
{
"name": "maybeCheckExpectedConstraints",
"path": "partials/checks/maybeCheckExpectedConstraints.zapt"
},
{
"name": "setupSaveAs",
"path": "partials/saveAs/setupSaveAs.zapt"
},
{
"name": "teardownSaveAs",
"path": "partials/saveAs/teardownSaveAs.zapt"
},
{
"name": "maybeSaveAs",
"path": "partials/saveAs/maybeSaveAs.zapt"
},
{
"name": "commandValue",
"path": "partials/command_value.zapt"
Expand Down
8 changes: 8 additions & 0 deletions examples/placeholder/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
"name": "test_cluster",
"path": "../../../examples/chip-tool/templates/tests/partials/test_cluster.zapt"
},
{
"name": "setupSaveAs",
"path": "../../../examples/chip-tool/templates/tests/partials/saveAs/setupSaveAs.zapt"
},
{
"name": "teardownSaveAs",
"path": "../../../examples/chip-tool/templates/tests/partials/saveAs/teardownSaveAs.zapt"
},
{
"name": "commandValue",
"path": "../../../examples/chip-tool/templates/tests/partials/command_value.zapt"
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/suites/include/ConstraintsChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class ConstraintsChecker
bool isUpperCase = true;
for (size_t i = 0; i < strlen(current); i++)
{
if (!isupper(current[i]))
if (!isdigit(current[i]) && !isupper(current[i]))
{
isUpperCase = false;
break;
Expand Down
Loading

0 comments on commit 70ae428

Please sign in to comment.