|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Project CHIP Authors |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +import sys |
| 20 | +import typing |
| 21 | +from dataclasses import dataclass |
| 22 | + |
| 23 | +import chip.clusters as Clusters |
| 24 | +from chip.clusters import Attribute |
| 25 | +from chip.clusters.Types import NullValue |
| 26 | +from MockTestRunner import MockTestRunner |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class TestSpec(): |
| 31 | + max_paths: int |
| 32 | + dmtest_feature_map: int |
| 33 | + expect_pass: bool |
| 34 | + |
| 35 | + |
| 36 | +TEST_CASES = [ |
| 37 | + TestSpec(1, 0, True), |
| 38 | + TestSpec(1, 1, True), # I think this is ok...to have the feature on when it's not needed? |
| 39 | + TestSpec(2, 0, False), |
| 40 | + TestSpec(2, 1, True), |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +def test_spec_to_attribute_cache(test_spec: TestSpec) -> Attribute.AsyncReadTransaction.ReadResponse: |
| 45 | + bi = Clusters.BasicInformation |
| 46 | + bi_attr = bi.Attributes |
| 47 | + gd = Clusters.GeneralDiagnostics |
| 48 | + gd_attr = gd.Attributes |
| 49 | + resp = Attribute.AsyncReadTransaction.ReadResponse({}, [], {}) |
| 50 | + resp.attributes = {0: {bi: {bi_attr.MaxPathsPerInvoke: test_spec.max_paths}, gd: {gd_attr.FeatureMap: test_spec.dmtest_feature_map}}} |
| 51 | + return resp |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + test_runner = MockTestRunner('TC_DGGEN_3_2', 'TC_DGGEN_3_2', 'test_TC_DGGEN_3_2', 0) |
| 56 | + failures = [] |
| 57 | + for idx, t in enumerate(TEST_CASES): |
| 58 | + ok = test_runner.run_test_with_mock_read(test_spec_to_attribute_cache(t)) == t.expect_pass |
| 59 | + if not ok: |
| 60 | + failures.append(f"Test case failure: {idx} {t}") |
| 61 | + |
| 62 | + test_runner.Shutdown() |
| 63 | + print( |
| 64 | + f"Test of tests: run {len(TEST_CASES)}, test response correct: {len(TEST_CASES) - len(failures)} test response incorrect: {len(failures)}") |
| 65 | + for f in failures: |
| 66 | + print(f) |
| 67 | + |
| 68 | + return 1 if failures else 0 |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + sys.exit(main()) |
0 commit comments