Skip to content

Commit

Permalink
Fix saveAs handling for nullable values. (#14602)
Browse files Browse the repository at this point in the history
* test_cluster_placeholder.zapt should never have been checked in (and was identical to test_cluster.zapt).

* Store the saveAs value as the type we expect to actually consume (this means a boxed value on Darwin, and a Nullable as needed in chip-tool).

* Fix comparisons to the saveAs value to work right.
  • Loading branch information
bzbarsky-apple authored Feb 2, 2022
1 parent 3c8d272 commit b8cb560
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 600 deletions.
6 changes: 5 additions & 1 deletion examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
class {{filename}}: public TestCommand
{
public:
{{#if ../credsIssuerConfigArg}}
{{filename}}(CredentialIssuerCommands * credsIssuerConfig): TestCommand("{{filename}}", credsIssuerConfig), mTestIndex(0)
{{else}}
{{filename}}(): TestCommand("{{filename}}"), mTestIndex(0)
{{/if}}
{
{{#chip_tests_config}}
{{#if (isString type)}}
Expand Down Expand Up @@ -93,7 +97,7 @@ class {{filename}}: public TestCommand
{{#chip_tests_item_response_parameters}}
{{#if saveAs}}
{{~#if (isString type)}}{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} * {{saveAs}}Buffer = nullptr;{{/if~}}
{{chipType}} {{saveAs}};
{{zapTypeToDecodableClusterObjectType type ns=../cluster}} {{saveAs}};
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_items}}
Expand Down
415 changes: 0 additions & 415 deletions examples/chip-tool/templates/partials/test_cluster_placeholder.zapt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
{{#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 depth=(incrementDepth depth)}}
{{#if (chip_tests_variables_has expected)}}
{{! Expected value is also a nullable. }}
if ({{expected}}.IsNull()) {
VerifyOrReturn(CheckValueNull("{{label}}", {{actual}}));
}
else
{
VerifyOrReturn(CheckValueNonNull("{{label}}", {{actual}}));
{{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=(concat expected ".Value()") isNullable=false depth=(incrementDepth depth)}}
}
{{else}}
VerifyOrReturn(CheckValueNonNull("{{label}}", {{actual}}));
{{>valueEquals label=(concat label ".Value()") actual=(concat actual ".Value()") expected=expected isNullable=false depth=(incrementDepth depth)}}
{{/if}}
{{/if}}
{{else if isArray}}
{
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/templates/tests-commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public:
}
};

{{>test_cluster tests=(getTests)}}
{{>test_cluster tests=(getTests) credsIssuerConfigArg=true}}

void registerCommandsTests(Commands & commands, CredentialIssuerCommands * credsIssuerConfig)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/placeholder/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"name": "test_cluster",
"path": "../../../examples/chip-tool/templates/partials/test_cluster_placeholder.zapt"
"path": "../../../examples/chip-tool/templates/partials/test_cluster.zapt"
},
{
"name": "commandValue",
Expand Down
2 changes: 1 addition & 1 deletion examples/placeholder/templates/tests-commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "TestCommand.h"

{{#if (getTests)}}
{{>test_cluster tests=(getTests)}}
{{>test_cluster tests=(getTests) credsIssuerConfigArg=false}}
{{/if}}

std::unique_ptr<TestCommand>GetTestCommand(std::string testName)
Expand Down
8 changes: 6 additions & 2 deletions src/app/zap-templates/partials/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function pad(label, len, ch = ' ')
function concat()
{
let str = ''
for (let arg in arguments)
// Last argument is our hash argument.
for (let arg = 0; arg < arguments.length - 1; ++arg)
{
if (typeof arguments[arg] != 'object') {
if (typeof arguments[arg] != 'object' || arguments[arg] instanceof String) {
str += arguments[arg];
} else {
throw new TypeError(
`Unexpected object in concat: ${arg}:${Object.prototype.toString.call(arguments[arg])}, ${JSON.stringify(arguments)}`);
}
}
return str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
{{#if (isLiteralNull expected)}}
XCTAssertTrue({{actual}} == nil);
{{else}}
{{#if (chip_tests_variables_has expected)}}
{{! Expected value is also a nullable. }}
if ({{expected}} == nil) {
XCTAssertTrue({{actual}} == nil);
} else {
{{/if}}
XCTAssertFalse({{actual}} == nil);
{{>check_test_value actual=actual expected=expected cluster=cluster isNullable=false}}
{{#if (chip_tests_variables_has expected)}}
}
{{/if}}
{{/if}}
{{else if isArray}}
XCTAssertEqual([{{actual}} count], {{expected.length}});
Expand All @@ -28,11 +37,7 @@
than "global") that are not present in the struct ? }}
{{else}}
{{#if (chip_tests_variables_has expected)}}
{{#if (isString type)}}
XCTAssertEqualObjects({{actual}}, {{expected}});
{{else}}
XCTAssertEqual([{{actual}} {{asObjectiveCNumberType "" type true}}Value], {{expected}});
{{/if}}
{{else if (isOctetString type)}}
XCTAssertTrue([{{actual}} isEqualToData:[[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral expected}}" length:{{expected.length}}]]);
{{else if (isCharString type)}}
Expand Down
26 changes: 12 additions & 14 deletions src/darwin/Framework/CHIP/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bool testSendCluster{{parent.filename}}_{{asTestIndex index}}_{{asUpperCamelCase
{{/if}}
{{#chip_tests_item_response_parameters}}
{{#if saveAs}}
{{asObjectiveCBasicType type is_mutable=true}} {{saveAs}};
{{asObjectiveCType type ../cluster}} {{saveAs}};
{{/if}}
{{/chip_tests_item_response_parameters}}

Expand Down Expand Up @@ -173,27 +173,25 @@ ResponseHandler {{> subscribeDataCallback}} = nil;
{{#if (isLiteralNull expectedConstraints.notValue)}}
XCTAssertFalse(actualValue == nil);
{{else}}
if (actualValue != nil) {
{{#if (isString type)}}
{{#if (chip_tests_variables_has expectedConstraints.notValue)}}
XCTAssertNotEqualObjects(actualValue, {{expectedConstraints.notValue}});
{{else}}
XCTAssertNotEqual([actualValue {{asObjectiveCNumberType "" type true}}Value], {{asTypedLiteral expectedConstraints.notValue type}});
{{/if}}
}
{{else}}
if (actualValue != nil) {
{{#if (isString type)}}
XCTAssertNotEqualObjects(actualValue, {{expectedConstraints.notValue}});
{{else}}
XCTAssertNotEqual([actualValue {{asObjectiveCNumberType "" type true}}Value], {{asTypedLiteral expectedConstraints.notValue type}});
{{/if}}
}
{{/if}}
{{/if}}
}
{{/if}}
{{/if}}
{{#if saveAs}}
{
{{> actualValue}}
{{#if (isOctetString type)}}
{{saveAs}} = [NSMutableData dataWithData:actualValue];
{{else if (isCharString type)}}
{{saveAs}} = [NSMutableString stringWithString:actualValue];
{{else}}
{{saveAs}} = [actualValue {{asObjectiveCNumberType "" type true}}Value];
{{/if}}
{{saveAs}} = actualValue;
}
{{/if}}
{{/chip_tests_item_response_parameters}}
Expand Down
4 changes: 0 additions & 4 deletions src/darwin/Framework/CHIP/templates/partials/test_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
{{/zcl_struct_items_by_struct_name}}

{{else if (chip_tests_variables_has definedValue)}}
{{#if (isString type)}}
{{target}} = [{{definedValue}} copy];
{{else}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:{{definedValue}}];
{{/if}}
{{else if (isCharString type)}}
{{target}} = @"{{definedValue}}";
{{else if (isOctetString type)}}
Expand Down
Loading

0 comments on commit b8cb560

Please sign in to comment.