1/*
2 *
3 * Copyright 2018 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19#import <XCTest/XCTest.h>
20
21#import <GRPCClient/GRPCCall.h>
22
23#import "../../GRPCClient/private/NSError+GRPC.h"
24
25@interface NSErrorUnitTests : XCTestCase
26
27@end
28
29@implementation NSErrorUnitTests
30
31- (void)testNSError {
32  const char *kDetails = "test details";
33  const char *kErrorString = "test errorString";
34  NSError *error1 = [NSError grpc_errorFromStatusCode:GRPC_STATUS_OK details:nil errorString:nil];
35  NSError *error2 = [NSError grpc_errorFromStatusCode:GRPC_STATUS_CANCELLED
36                                              details:kDetails
37                                          errorString:kErrorString];
38  NSError *error3 = [NSError grpc_errorFromStatusCode:GRPC_STATUS_UNAUTHENTICATED
39                                              details:kDetails
40                                          errorString:nil];
41  NSError *error4 =
42      [NSError grpc_errorFromStatusCode:GRPC_STATUS_UNAVAILABLE details:nil errorString:nil];
43
44  XCTAssertNil(error1);
45  XCTAssertEqual(error2.code, 1);
46  XCTAssertEqualObjects(error2.domain, @"io.grpc");
47  XCTAssertEqualObjects(error2.userInfo[NSLocalizedDescriptionKey],
48                        [NSString stringWithUTF8String:kDetails]);
49  XCTAssertEqualObjects(error2.userInfo[NSDebugDescriptionErrorKey],
50                        [NSString stringWithUTF8String:kErrorString]);
51  XCTAssertEqual(error3.code, 16);
52  XCTAssertEqualObjects(error3.domain, @"io.grpc");
53  XCTAssertEqualObjects(error3.userInfo[NSLocalizedDescriptionKey],
54                        [NSString stringWithUTF8String:kDetails]);
55  XCTAssertNil(error3.userInfo[NSDebugDescriptionErrorKey]);
56  XCTAssertEqual(error4.code, 14);
57  XCTAssertEqualObjects(error4.domain, @"io.grpc");
58  XCTAssertNil(error4.userInfo[NSLocalizedDescriptionKey]);
59  XCTAssertNil(error4.userInfo[NSDebugDescriptionErrorKey]);
60}
61
62@end
63