forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MTRErrorDomain mappings for a few more CHIP_ERROR values. (projec…
…t-chip#36835) Also adds some basic round-tripping tests to catch mistakes.
- Loading branch information
1 parent
c9a5d13
commit 2b8f5d7
Showing
6 changed files
with
191 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <Matter/MTRError.h> | ||
|
||
#import "MTRDefines_Internal.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
MTR_TESTABLE | ||
@interface MTRError : NSObject | ||
|
||
+ (NSError *)errorWithCode:(MTRErrorCode)code; | ||
|
||
// For tests only, since we can't use CHIP_ERROR from there. The "code"s used | ||
// here are integer representations of CHIP_ERROR. Otherwise these functions | ||
// are just like errorForCHIPErrorCode and errorToCHIPErrorCode. | ||
+ (NSError *)errorForCHIPIntegerCode:(uint32_t)code; | ||
+ (uint32_t)errorToCHIPIntegerCode:(NSError * _Nullable)error; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Matter/Matter.h> | ||
#import <XCTest/XCTest.h> | ||
|
||
// NOTE: This is a .mm file, so that it can include MTRError_Internal.h | ||
|
||
#import "MTRError_Testable.h" | ||
|
||
@interface MTRErrorMappingTests : XCTestCase | ||
@end | ||
|
||
@implementation MTRErrorMappingTests | ||
|
||
- (void)testPublicNonInteractionAPIValues | ||
{ | ||
for (int errorCode = 1; errorCode <= MTRMaxErrorCode; errorCode++) { | ||
// A few error codes are not actually representing CHIP_ERROR values. | ||
if (errorCode == MTRErrorCodeWrongAddressType || errorCode == MTRErrorCodeUnknownSchema) { | ||
continue; | ||
} | ||
|
||
// All of these should round-trip appropriately. | ||
__auto_type * error = [NSError errorWithDomain:MTRErrorDomain code:errorCode userInfo:nil]; | ||
__auto_type * newError1 = [MTRError errorWithCode:(MTRErrorCode) errorCode]; | ||
XCTAssertEqual(newError1.domain, error.domain, "Testing error code %d", errorCode); | ||
XCTAssertEqual(newError1.code, error.code, "Testing error code %d", errorCode); | ||
|
||
__auto_type chipError = [MTRError errorToCHIPIntegerCode:error]; | ||
__auto_type * newError2 = [MTRError errorForCHIPIntegerCode:chipError]; | ||
XCTAssertEqual(newError2.domain, error.domain, "Testing error code %d", errorCode); | ||
XCTAssertEqual(newError2.code, error.code, "Testing error code %d", errorCode); | ||
} | ||
|
||
// Check that an unknown value becomes GeneralError. | ||
__auto_type * error = [MTRError errorWithCode:(MTRErrorCode) (MTRMaxErrorCode + 1)]; | ||
XCTAssertEqual(error.domain, MTRErrorDomain); | ||
XCTAssertEqual(error.code, MTRMaxErrorCode + 1); | ||
|
||
__auto_type chipError = [MTRError errorToCHIPIntegerCode:error]; | ||
__auto_type * newError = [MTRError errorForCHIPIntegerCode:chipError]; | ||
XCTAssertEqual(newError.domain, MTRErrorDomain); | ||
XCTAssertEqual(newError.code, MTRErrorCodeGeneralError); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters