Skip to content

Commit

Permalink
[YAML] Add isLowerCase/isUpperCase/isHexString constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jan 11, 2022
1 parent aaa7ad0 commit adcefe6
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ class {{filename}}: public TestCommand
{{~#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.isUpperCase}}VerifyOrReturn(CheckConstraintIsUpperCase("{{>item}}", {{>item}}, {{expectedConstraints.isUpperCase}}));{{/if}}
{{~#if expectedConstraints.isLowerCase}}VerifyOrReturn(CheckConstraintIsLowerCase("{{>item}}", {{>item}}, {{expectedConstraints.isLowerCase}}));{{/if}}
{{~#if expectedConstraints.isHexString}}VerifyOrReturn(CheckConstraintIsHexString("{{>item}}", {{>item}}, {{expectedConstraints.isHexString}}));{{/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}}, {{asTypedLiteral expectedConstraints.minValue type}}));{{/if}}
Expand Down
71 changes: 71 additions & 0 deletions src/app/tests/suites/TestConstraints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,77 @@ tests:
constraints:
endsWith: "**"

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

- label:
"Read attribute CHAR_STRING Value isLowerCase/isUpperCase Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
isLowerCase: true
isUpperCase: "false"

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

- label:
"Read attribute CHAR_STRING Value isLowerCase/isUpperCase Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
isUpperCase: true
isLowerCase: "false"

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

- label:
"Read attribute CHAR_STRING Value isLowerCase/isUpperCase Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
isUpperCase: "false"
isLowerCase: "false"

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

- label: "Read attribute CHAR_STRING Value isHexString Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
isHexString: "false"

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

- label: "Read attribute CHAR_STRING Value isHexString Constraints"
command: "readAttribute"
attribute: "char_string"
response:
constraints:
isHexString: true

- label: "Write attribute CHAR_STRING Value Back to Default Value"
command: "writeAttribute"
attribute: "char_string"
Expand Down
99 changes: 99 additions & 0 deletions src/app/tests/suites/include/ConstraintsChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,105 @@ class ConstraintsChecker
return true;
}

bool CheckConstraintIsUpperCase(const char * itemName, const chip::CharSpan current, bool expectUpperCase)
{
std::string value(current.data(), current.size());
return CheckConstraintIsUpperCase(itemName, value.c_str(), expectUpperCase);
}

bool CheckConstraintIsUpperCase(const char * itemName, const char * current, bool expectUpperCase)
{
bool isUpperCase = true;
for (size_t i = 0; i < strlen(current); i++)
{
if (!isupper(current[i]))
{
isUpperCase = false;
break;
}
}

if (expectUpperCase && !isUpperCase)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is not an upppercase string");
return false;
}

if (!expectUpperCase && isUpperCase)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is an upppercase string");
return false;
}

return true;
}

bool CheckConstraintIsLowerCase(const char * itemName, const chip::CharSpan current, bool expectLowerCase)
{
std::string value(current.data(), current.size());
return CheckConstraintIsLowerCase(itemName, value.c_str(), expectLowerCase);
}

bool CheckConstraintIsLowerCase(const char * itemName, const char * current, bool expectLowerCase)
{
bool isLowerCase = true;
for (size_t i = 0; i < strlen(current); i++)
{
if (isupper(current[i]))
{
isLowerCase = false;
break;
}
}

if (expectLowerCase && !isLowerCase)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is not a lowercase string");
return false;
}

if (!expectLowerCase && isLowerCase)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is a lowercase string");
return false;
}

return true;
}

bool CheckConstraintIsHexString(const char * itemName, const chip::CharSpan current, bool expectHexString)
{
std::string value(current.data(), current.size());
return CheckConstraintIsHexString(itemName, value.c_str(), expectHexString);
}

bool CheckConstraintIsHexString(const char * itemName, const char * current, bool expectHexString)
{
bool isHexString = true;
for (size_t i = 0; i < strlen(current); i++)
{
if (!isxdigit(current[i]))
{
isHexString = false;
break;
}
}

if (expectHexString && !isHexString)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is not a hexadecimal string");
return false;
}

if (!expectHexString && isHexString)
{
Exit(std::string(itemName) + " (\"" + std::string(current) + "\") is a hexadecimal string");
return false;
}

return true;
}

template <typename T>
bool CheckConstraintMinValue(const char * itemName, T current, T expected)
{
Expand Down
Loading

0 comments on commit adcefe6

Please sign in to comment.