Skip to content

Commit

Permalink
fix: Type hinting MPIdentityApiRequest.identities (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 authored Apr 17, 2024
1 parent 87ce71c commit f56879f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
30 changes: 29 additions & 1 deletion UnitTests/MPIdentityApiRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ @interface MPIdentityApiRequestTests : MPBaseTestCase

@end

@interface MPIdentityApiRequest ()
@property (nonatomic) NSMutableDictionary<NSNumber*, NSObject*> *mutableIdentities;
@end

@implementation MPIdentityApiRequestTests

- (void)testSetNilIdentity {
Expand All @@ -24,7 +28,7 @@ - (void)testSetNullIdentity {
XCTAssertEqualObjects([NSNull null], [request.identities objectForKey:@(MPIdentityOther)]);
}

- (void)testsetIdentity {
- (void)testSetIdentity {
MPIdentityApiRequest *request = [[MPIdentityApiRequest alloc] init];
[request setIdentity:@"foo" identityType:MPIdentityOther];
XCTAssertEqualObjects(@"foo", [request.identities objectForKey:@(MPIdentityOther)]);
Expand All @@ -49,4 +53,28 @@ - (void)testIdentitiesAreNotNull {
XCTAssertNotEqualObjects(request.customerId, [NSNull null]);
}

- (void)testSetEmail {
MPIdentityApiRequest *request = [[MPIdentityApiRequest alloc] init];
XCTAssertNil(request.email);
request.email = @"[email protected]";
XCTAssertEqualObjects(@"[email protected]", request.email);
XCTAssertEqualObjects(@"[email protected]", request.mutableIdentities[@(MPIdentityEmail)]);

request.email = nil;
XCTAssertNil(request.email);
XCTAssertEqualObjects(request.mutableIdentities[@(MPIdentityEmail)], [NSNull null]);
}

- (void)testSetCustomerId {
MPIdentityApiRequest *request = [[MPIdentityApiRequest alloc] init];
XCTAssertNil(request.customerId);
request.customerId = @"some id";
XCTAssertEqualObjects(@"some id", request.customerId);
XCTAssertEqualObjects(@"some id", request.mutableIdentities[@(MPIdentityCustomerId)]);

request.customerId = nil;
XCTAssertNil(request.customerId);
XCTAssertEqualObjects(request.mutableIdentities[@(MPIdentityCustomerId)], [NSNull null]);
}

@end
15 changes: 15 additions & 0 deletions UnitTests/MPSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,19 @@ class mParticle_Swift_SDKTests: XCTestCase {
XCTAssertNotNil(MPIdentityErrorResponseCode(rawValue: 500))
XCTAssertNotNil(MPIdentityErrorResponseCode(rawValue: 502))
}

func testMPIdentityApiRequestIdentitiesInterop() {
let request = MPIdentityApiRequest()
request.setIdentity("test id", identityType: .customerId)
request.setIdentity("[email protected]", identityType: .email)

var identities = [NSNumber: NSObject]()
identities[NSNumber(value: MPIdentity.customerId.rawValue)] = NSString(string: "test id")
identities[NSNumber(value: MPIdentity.email.rawValue)] = NSString(string: "[email protected]")
XCTAssertEqual(identities, request.identities);

request.email = nil
identities[NSNumber(value: MPIdentity.email.rawValue)] = NSNull()
XCTAssertEqual(identities, request.identities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ - (instancetype)initWithIdentityRequest:(MPIdentityApiRequest *)request kitConfi
}

- (NSDictionary<NSNumber *,NSString *> *)userIdentities {
NSDictionary<NSNumber *, NSString *> *unfilteredUserIdentities = self.request.identities;
NSDictionary<NSNumber *, NSObject *> *unfilteredUserIdentities = self.request.identities;
NSMutableDictionary *filteredUserIdentities = [NSMutableDictionary dictionary];

for (NSNumber* key in unfilteredUserIdentities) {
Expand Down
20 changes: 10 additions & 10 deletions mParticle-Apple-SDK/Identity/MPIdentityApiRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#import "MPIdentityDTO.h"

@interface MPIdentityApiRequest ()
@property (nonatomic) NSMutableDictionary<NSNumber*, NSString*> *mutableIdentities;
@property (nonatomic) NSMutableDictionary<NSNumber*, NSObject*> *mutableIdentities;
@end

@implementation MPIdentityApiRequest
Expand Down Expand Up @@ -50,30 +50,30 @@ + (MPIdentityApiRequest *)requestWithUser:(MParticleUser *) user {
}

- (NSString *)email {
NSString *result = _mutableIdentities[@(MPIdentityEmail)];
if ([result isEqual:[NSNull null]]) {
result = nil;
NSObject *result = _mutableIdentities[@(MPIdentityEmail)];
if ([result isKindOfClass:[NSString class]]) {
return (NSString *)result;
}
return result;
return nil;
}

- (void)setEmail:(NSString *)email {
[self setIdentity:email identityType:MPIdentityEmail];
}

- (NSString *)customerId {
NSString *result = _mutableIdentities[@(MPIdentityCustomerId)];
if ([result isEqual:[NSNull null]]) {
result = nil;
NSObject *result = _mutableIdentities[@(MPIdentityCustomerId)];
if ([result isKindOfClass:[NSString class]]) {
return (NSString *)result;
}
return result;
return nil;
}

- (void)setCustomerId:(NSString *)customerId {
[self setIdentity:customerId identityType:MPIdentityCustomerId];
}

- (NSDictionary<NSNumber*, NSString*> *)identities {
- (NSDictionary<NSNumber*, NSObject*> *)identities {
return [_mutableIdentities copy];
}

Expand Down
2 changes: 1 addition & 1 deletion mParticle-Apple-SDK/Include/MPIdentityApiRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong, nullable) NSString *email;
@property (nonatomic, strong, nullable) NSString *customerId;
@property (nonatomic, strong, nullable, readonly) NSDictionary<NSNumber*, NSString*> *identities;
@property (nonatomic, strong, nullable, readonly) NSDictionary<NSNumber*, NSObject*> *identities;

@end

Expand Down

0 comments on commit f56879f

Please sign in to comment.