Skip to content

Commit

Permalink
Enable some more string tests. (#12281)
Browse files Browse the repository at this point in the history
The chip-tool yaml test harness was not measuring the length of
strings correctly (using length in UTF-16 code units, not in UTF-8
code units).  Fixing that lets us enable a few more tests.

This also improves notValue failure reporting and removes the
operator== on Span that should not exist.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 18, 2022
1 parent 61a0f9f commit b6a5610
Show file tree
Hide file tree
Showing 9 changed files with 1,286 additions and 1,150 deletions.
35 changes: 25 additions & 10 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,34 @@ class TestCommand : public CHIPCommand

return true;
}
template <typename T>
bool CheckConstraintNotValue(const char * itemName, T current, T expected)
template <typename T, typename U>
bool CheckConstraintNotValue(const char * itemName, T current, U expected)
{
if (current == expected)
{
if (std::is_same<T, const chip::Span<const uint8_t>>::value || std::is_same<T, const chip::Span<const char>>::value)
{
Exit(std::string(itemName) + " value == notValue");
}
else
{
Exit(std::string(itemName) + " value == notValue: " + std::to_string(current) + " == " + std::to_string(expected));
}
Exit(std::string(itemName) + " got unexpected value: " + std::to_string(current));
return false;
}

return true;
}

bool CheckConstraintNotValue(const char * itemName, chip::CharSpan current, chip::CharSpan expected)
{
if (current.data_equal(expected))
{
Exit(std::string(itemName) + " got unexpected value: " + std::string(current.data(), current.size()));
return false;
}

return true;
}

bool CheckConstraintNotValue(const char * itemName, chip::ByteSpan current, chip::ByteSpan expected)
{
if (current.data_equal(expected))
{
Exit(std::string(itemName) + " got unexpected value of size: " + std::to_string(current.size()));
return false;
}

Expand Down
6 changes: 6 additions & 0 deletions examples/chip-tool/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ function asTypeMaxValue(type)
return templateUtil.templatePromise(this.global, promise);
}

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

//
// Module exports
//
exports.asDelimitedCommand = asDelimitedCommand;
exports.asTypeMinValue = asTypeMinValue;
exports.asTypeMaxValue = asTypeMaxValue;
exports.utf8StringLength = utf8StringLength;
2 changes: 1 addition & 1 deletion examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class {{filename}}: public TestCommand
{{~#if expectedConstraints.maxLength}}VerifyOrReturn(CheckConstraintMaxLength("{{>item}}", {{>item}}.size(), {{expectedConstraints.maxLength}}));{{/if}}
{{~#if expectedConstraints.minValue}}VerifyOrReturn(CheckConstraintMinValue<{{chipType}}>("{{>item}}", {{>item}}, {{expectedConstraints.minValue}}));{{/if}}
{{~#if expectedConstraints.maxValue}}VerifyOrReturn(CheckConstraintMaxValue<{{chipType}}>("{{>item}}", {{>item}}, {{expectedConstraints.maxValue}}));{{/if}}
{{~#if expectedConstraints.notValue}}VerifyOrReturn(CheckConstraintNotValue<{{chipType}}>("{{>item}}", {{>item}}, {{expectedConstraints.notValue}}));{{/if}}
{{~#if expectedConstraints.notValue}}VerifyOrReturn(CheckConstraintNotValue("{{>item}}", {{>item}}, {{asTypedLiteral expectedConstraints.notValue type}}));{{/if}}
{{/if}}

{{#if saveAs}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{{#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", {{definedValue.length}});
chip::Span<const char>("{{definedValue}}garbage: not in length on purpose", {{utf8StringLength definedValue}});
{{else if (isOctetString type)}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral definedValue}}garbage: not in length on purpose"), {{definedValue.length}});
{{else}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{{else}}
VerifyOrReturn(CheckValue
{{~#if (isOctetString type)}}AsString("{{label}}", {{actual}}, chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}}))
{{else if (isCharString type)}}AsString("{{label}}", {{actual}}, chip::CharSpan("{{expected}}", {{expected.length}}))
{{else if (isCharString type)}}AsString("{{label}}", {{actual}}, chip::CharSpan("{{expected}}", {{utf8StringLength expected}}))
{{else}}("{{label}}", {{actual}}, {{asTypedLiteral expected type}})
{{/if}}
);
Expand Down
4 changes: 1 addition & 3 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,6 @@ tests:
- label: "Read attribute CHAR_STRING"
command: "readAttribute"
attribute: "char_string"
disabled: true
response:
value: "☉T☉"

Expand All @@ -696,9 +695,8 @@ tests:
- label: "Read attribute CHAR_STRING"
command: "readAttribute"
attribute: "char_string"
disabled: true
response:
value: "TestVal"
value: "T☉"

- label: "Write attribute CHAR_STRING - Empty"
command: "writeAttribute"
Expand Down
Loading

0 comments on commit b6a5610

Please sign in to comment.