Skip to content

Commit

Permalink
[YAML] Add 'saveAs' support for CHAR_STRING and OCTET_STRING
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jan 13, 2022
1 parent 502963f commit a2bebc0
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 15 deletions.
28 changes: 28 additions & 0 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char>("{{definedValue}}garbage: not in length on purpose", {{utf8StringLength definedValue}});
Expand All @@ -49,6 +52,8 @@
{{else}}
{{asTypedLiteral definedValue type}};
{{/if_is_bitmap}}
{{/if_chip_enum}}
{{/if_chip_enum~}}
{{~/if~}}

{{/if_is_struct}}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
68 changes: 68 additions & 0 deletions src/app/tests/suites/TestSaveAs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 32 additions & 12 deletions src/app/zap-templates/common/ClusterTestGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a2bebc0

Please sign in to comment.