Skip to content

Commit

Permalink
[YAML] Add startsWith/endsWith constraints (#12286)
Browse files Browse the repository at this point in the history
* [YAML] Add startsWith/endsWith constraints

* Update generated test content
  • Loading branch information
vivien-apple authored and pull[bot] committed Jul 20, 2022
1 parent e068d00 commit 1550006
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 2 deletions.
24 changes: 24 additions & 0 deletions examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ bool TestCommand::CheckConstraintFormat(const char * itemName, const char * curr
return true;
}

bool TestCommand::CheckConstraintStartsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
{
std::string value(current.data(), current.size());
if (value.rfind(expected, 0) != 0)
{
Exit(std::string(itemName) + " (\"" + value + "\") does not starts with: \"" + std::string(expected) + "\"");
return false;
}

return true;
}

bool TestCommand::CheckConstraintEndsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
{
std::string value(current.data(), current.size());
if (value.find(expected, value.size() - strlen(expected)) == std::string::npos)
{
Exit(std::string(itemName) + " (\"" + value + "\") does not ends with: \"" + std::string(expected) + "\"");
return false;
}

return true;
}

bool TestCommand::CheckConstraintMinLength(const char * itemName, uint64_t current, uint64_t expected)
{
if (current < expected)
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class TestCommand : public CHIPCommand
bool CheckConstraintFormat(const char * itemName, const char * current, const char * expected);
bool CheckConstraintMinLength(const char * itemName, uint64_t current, uint64_t expected);
bool CheckConstraintMaxLength(const char * itemName, uint64_t current, uint64_t expected);
bool CheckConstraintStartsWith(const char * itemName, const chip::Span<const char> current, const char * expected);
bool CheckConstraintEndsWith(const char * itemName, const chip::Span<const char> current, const char * expected);
template <typename T>
bool CheckConstraintMinValue(const char * itemName, T current, T expected)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ class {{filename}}: public TestCommand
{{/if}}
{{#if expectedConstraints.type}}VerifyOrReturn(CheckConstraintType("{{>item}}", "", "{{expectedConstraints.type}}"));{{/if}}
{{~#if expectedConstraints.format}}VerifyOrReturn(CheckConstraintFormat("{{>item}}", "", "{{expectedConstraints.format}}"));{{/if}}
{{~#if expectedConstraints.startsWith}}VerifyOrReturn(CheckConstraintStartsWith("{{>item}}", {{>item}}, "{{expectedConstraints.startsWith}}"));{{/if}}
{{~#if expectedConstraints.endsWith}}VerifyOrReturn(CheckConstraintEndsWith("{{>item}}", {{>item}}, "{{expectedConstraints.endsWith}}"));{{/if}}
{{~#if expectedConstraints.minLength}}VerifyOrReturn(CheckConstraintMinLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.minLength}}));{{/if}}
{{~#if expectedConstraints.maxLength}}VerifyOrReturn(CheckConstraintMaxLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.maxLength}}));{{/if}}
{{~#if expectedConstraints.minValue}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{>item}}", {{>item}}, {{expectedConstraints.minValue}}));{{/if}}
Expand Down
44 changes: 43 additions & 1 deletion src/app/tests/suites/TestConstraints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ config:
endpoint: 1

tests:
# Tests for UInt32 attribute
# Tests for INT32U attribute

- label: "Write attribute INT32U Value"
command: "writeAttribute"
Expand Down Expand Up @@ -53,3 +53,45 @@ tests:
attribute: "int32u"
arguments:
value: 0

# Tests for CHAR_STRING attribute

- label: "Write attribute CHAR_STRING Value"
command: "writeAttribute"
attribute: "char_string"
arguments:
value: "** Test **"

- label: "Read attribute CHAR_STRING Value MinLength Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
minLength: 5

- label: "Read attribute CHAR_STRING Value MaxLength Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
maxLength: 20

- label: "Read attribute CHAR_STRING Value StartsWith Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
startsWith: "**"

- label: "Read attribute CHAR_STRING Value EndsWith Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
endsWith: "**"

- label: "Write attribute CHAR_STRING Value Back to Default Value"
command: "writeAttribute"
attribute: "char_string"
arguments:
value: ""
12 changes: 12 additions & 0 deletions src/darwin/Framework/CHIP/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ bool testSendCluster{{parent.filename}}_{{asTestIndex index}}_{{asUpperCamelCase
XCTAssertGreaterThanOrEqual([actualValue length], {{expectedConstraints.minLength}});
}
{{/if}}
{{#if expectedConstraints.startsWith}}
{
{{> actualValue}}
XCTAssertTrue([actualValue hasPrefix:@"{{expectedConstraints.startsWith}}"]);
}
{{/if}}
{{#if expectedConstraints.endsWith}}
{
{{> actualValue}}
XCTAssertTrue([actualValue hasSuffix:@"{{expectedConstraints.endsWith}}"]);
}
{{/if}}
{{#if expectedConstraints.maxLength}}
{
{{> actualValue}}
Expand Down
140 changes: 140 additions & 0 deletions src/darwin/Framework/CHIPTests/CHIPClustersTests.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1550006

Please sign in to comment.