Skip to content

Commit

Permalink
Add a hasValue constraint in YAML. (#18770)
Browse files Browse the repository at this point in the history
* Add a hasValue constraint in YAML.

Some commands require or forbid certain fields to be present in
certain conditions; we should have a way to test for that in YAML.

* Fix spellcheck job.
  • Loading branch information
bzbarsky-apple authored May 25, 2022
1 parent d956c63 commit ec634d0
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ hardknott
hardwarever
HardwareVersion
HardwareVersionString
hasValue
hci
hciattach
hciconfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{{#if hasExpectedConstraints}}
{{#if isOptional}}
VerifyOrReturn(CheckValuePresent("{{asPropertyValue dontUnwrapValue=true}}", {{asPropertyValue dontUnwrapValue=true}}));
{{~#if isOptional}}
{{~#if (hasProperty expectedConstraints "hasValue")}}VerifyOrReturn(CheckConstraintHasValue("{{asPropertyValue dontUnwrapValue=true}}", {{asPropertyValue dontUnwrapValue=true}}, {{expectedConstraints.hasValue}}));
{{else}}
if ({{asPropertyValue dontUnwrapValue=true}}.HasValue()) {
{{/if}}
{{/if}}

{{~#if (hasProperty expectedConstraints "type")}}VerifyOrReturn(CheckConstraintType("{{asPropertyValue}}", "", "{{expectedConstraints.type}}"));{{/if}}
Expand Down Expand Up @@ -32,5 +35,10 @@
VerifyOrReturn(CheckConstraintNotValue("{{asPropertyValue}}", {{asPropertyValue}}, {{asTypedLiteral expectedConstraints.notValue type}}));
{{/if}}
{{/if}}
{{~#if isOptional}}
{{~#unless (hasProperty expectedConstraints "hasValue")}}
}
{{/unless}}
{{/if}}
{{/if}}

4 changes: 4 additions & 0 deletions src/app/tests/suites/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ properties:

| Name | Description | Required |
| ----------- | ----------------------------------------------------------- | ---------------------- |
| hasValue | If true, must have value. If false, must not have value. | No (If other provided) |
| minValue | Minimum value to expect from the command response. | No (If other provided) |
| maxValue | Maximum value to expect from the command response. | No (If other provided) |
| notValue | Validate the the value is not what is provided. | No (If other provided) |
Expand All @@ -178,6 +179,9 @@ properties:
| isUpperCase | Validates if the char_string is upper case. | No (If other provided) |
| isHexString | Checks whether the char_string is a hex string. | No (If other provided) |

Note: The hasValue constraint is only applied to optional fields. The other
constraints are ignored for optional fields that do not have a value.

Additionally, the object exhibits the following autogenerated properties:

| Name | Description |
Expand Down
20 changes: 20 additions & 0 deletions src/app/tests/suites/certification/Test_TC_DL_2_11.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ tests:
value: 0x0
- name: "LocalStartTime"
constraints:
hasValue: true
type: epoch-s
- name: "LocalEndTime"
constraints:
hasValue: true
type: epoch-s

- label:
Expand Down Expand Up @@ -171,6 +173,12 @@ tests:
value: 21
- name: "status"
value: 0x85
- name: "LocalStartTime"
constraints:
hasValue: false
- name: "LocalEndTime"
constraints:
hasValue: false

- label:
"send Get Year Day Schedule Command to DUT and verify NOT_FOUND
Expand All @@ -190,6 +198,12 @@ tests:
value: 5
- name: "status"
value: 0x8B
- name: "LocalStartTime"
constraints:
hasValue: false
- name: "LocalEndTime"
constraints:
hasValue: false

- label:
"send Get Year Day Schedule Command to DUT and verify NOT_FOUND
Expand All @@ -209,6 +223,12 @@ tests:
value: 2
- name: "status"
value: 0x8B
- name: "LocalStartTime"
constraints:
hasValue: false
- name: "LocalEndTime"
constraints:
hasValue: false

- label: "Send Set Year Day Schedule Command to DUT"
command: "SetYearDaySchedule"
Expand Down
35 changes: 35 additions & 0 deletions src/app/tests/suites/certification/Test_TC_DL_2_8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,27 @@ tests:
value: 0x0
- name: "daysMask"
constraints:
hasValue: true
minValue: 0
maxValue: 6
- name: "startHour"
constraints:
hasValue: true
minValue: 0
maxValue: 23
- name: "startMinute"
constraints:
hasValue: true
minValue: 0
maxValue: 59
- name: "endHour"
constraints:
hasValue: true
minValue: 0
maxValue: 23
- name: "endMinute"
constraints:
hasValue: true
minValue: 0
maxValue: 59

Expand Down Expand Up @@ -171,6 +176,21 @@ tests:
value: 1
- name: "status"
value: 0x85
- name: "daysMask"
constraints:
hasValue: false
- name: "startHour"
constraints:
hasValue: false
- name: "startMinute"
constraints:
hasValue: false
- name: "endHour"
constraints:
hasValue: false
- name: "endMinute"
constraints:
hasValue: false

- label: "Clear all week day schedules for the first user"
command: "ClearWeekDaySchedule"
Expand All @@ -197,3 +217,18 @@ tests:
value: 1
- name: "status"
value: 0x8B
- name: "daysMask"
constraints:
hasValue: false
- name: "startHour"
constraints:
hasValue: false
- name: "startMinute"
constraints:
hasValue: false
- name: "endHour"
constraints:
hasValue: false
- name: "endMinute"
constraints:
hasValue: false
19 changes: 19 additions & 0 deletions src/app/tests/suites/include/ConstraintsChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,23 @@ class ConstraintsChecker
}
return CheckConstraintNotValue(itemName, current, expected.Value());
}

template <typename T>
bool CheckConstraintHasValue(const char * itemName, const chip::Optional<T> & current, bool expected)
{
if (current.HasValue() == expected)
{
return true;
}

if (current.HasValue())
{
Exit(std::string(itemName) + " not expected to have a value but does");
}
else
{
Exit(std::string(itemName) + " expected to have a value but doesn't");
}
return false;
}
};
78 changes: 59 additions & 19 deletions zzz_generated/chip-tool/zap-generated/test/Commands.h

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

0 comments on commit ec634d0

Please sign in to comment.