From ca4e4cb5049cd25b41ec5ef560dfb944fe9cfceb Mon Sep 17 00:00:00 2001 From: Umair Date: Mon, 7 Oct 2024 21:41:00 +0100 Subject: [PATCH] Allow mocking of ARTPaginatedResult by allowing it to be subclassed by a user of the SDK Co-authored-by: Lawrence Forooghian --- Source/ARTPaginatedResult.m | 57 ++++++++++++++++++++---- Source/include/Ably/ARTPaginatedResult.h | 4 +- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Source/ARTPaginatedResult.m b/Source/ARTPaginatedResult.m index 39b37bacc..fa5cfd128 100644 --- a/Source/ARTPaginatedResult.m +++ b/Source/ARTPaginatedResult.m @@ -9,14 +9,17 @@ #import "ARTInternalLog.h" @implementation ARTPaginatedResult { - ARTRestInternal *_rest; - dispatch_queue_t _userQueue; - dispatch_queue_t _queue; - NSMutableURLRequest *_relFirst; - NSMutableURLRequest *_relCurrent; - NSMutableURLRequest *_relNext; - ARTPaginatedResultResponseProcessor _responseProcessor; - ARTQueuedDealloc *_dealloc; + BOOL _initializedViaInit; + + // All of the below instance variables are non-nil if and only if _initializedViaInit is NO + ARTRestInternal *_Nullable _rest; + dispatch_queue_t _Nullable _userQueue; + dispatch_queue_t _Nullable _queue; + NSMutableURLRequest *_Nullable _relFirst; + NSMutableURLRequest *_Nullable _relCurrent; + NSMutableURLRequest *_Nullable _relNext; + ARTPaginatedResultResponseProcessor _Nullable _responseProcessor; + ARTQueuedDealloc *_Nullable _dealloc; } @synthesize rest = _rest; @@ -25,6 +28,17 @@ @implementation ARTPaginatedResult { @synthesize relFirst = _relFirst; @synthesize relCurrent = _relCurrent; @synthesize relNext = _relNext; +@synthesize hasNext = _hasNext; +@synthesize isLast = _isLast; +@synthesize items = _items; + +- (instancetype)init { + if (self = [super init]) { + _initializedViaInit = YES; + } + + return self; +} - (instancetype)initWithItems:(NSArray *)items rest:(ARTRestInternal *)rest @@ -34,6 +48,8 @@ - (instancetype)initWithItems:(NSArray *)items responseProcessor:(ARTPaginatedResultResponseProcessor)responseProcessor logger:(ARTInternalLog *)logger { if (self = [super init]) { + _initializedViaInit = NO; + _items = items; _relFirst = relFirst; @@ -67,7 +83,30 @@ - (instancetype)initWithItems:(NSArray *)items return self; } +- (void)initializedViaInitCheck { + if (_initializedViaInit) { + [NSException raise:NSInternalInconsistencyException format:@"When initializing this class using -init, you need to override this method in a subclass"]; + } +} + +- (BOOL)hasNext { + [self initializedViaInitCheck]; + return _hasNext; +} + +- (BOOL)isLast { + [self initializedViaInitCheck]; + return _isLast; +} + +- (NSArray *)items { + [self initializedViaInitCheck]; + return _items; +} + - (void)first:(void (^)(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error))callback { + [self initializedViaInitCheck]; + if (callback) { void (^userCallback)(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error) = callback; callback = ^(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error) { @@ -81,6 +120,8 @@ - (void)first:(void (^)(ARTPaginatedResult *_Nullable result, ARTErrorInfo * } - (void)next:(void (^)(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error))callback { + [self initializedViaInitCheck]; + if (callback) { void (^userCallback)(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error) = callback; callback = ^(ARTPaginatedResult *_Nullable result, ARTErrorInfo *_Nullable error) { diff --git a/Source/include/Ably/ARTPaginatedResult.h b/Source/include/Ably/ARTPaginatedResult.h index a5b73561f..e5b275ea9 100644 --- a/Source/include/Ably/ARTPaginatedResult.h +++ b/Source/include/Ably/ARTPaginatedResult.h @@ -26,8 +26,8 @@ NS_SWIFT_SENDABLE */ @property (nonatomic, readonly) BOOL isLast; -/// :nodoc: -- (instancetype)init UNAVAILABLE_ATTRIBUTE; +/// If you use this initializer, trying to call any of the methods or properties in `ARTPaginatedResult` will throw an exception; you must provide your own implementation in a subclass. This initializer exists purely to allow you to provide a mock implementation of this class in your tests. +- (instancetype)init NS_DESIGNATED_INITIALIZER; /** * Returns a new `ARTPaginatedResult` for the first page of results.