Skip to content

Commit

Permalink
Allow mocking of ARTPaginatedResult by allowing it to be subclassed b…
Browse files Browse the repository at this point in the history
…y a user of the SDK

Co-authored-by: Lawrence Forooghian <[email protected]>
  • Loading branch information
umair-ably and lawrence-forooghian committed Oct 7, 2024
1 parent 63e6f00 commit ca4e4cb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
57 changes: 49 additions & 8 deletions Source/ARTPaginatedResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (iOS, test_iOS17_2)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (iOS, test_iOS17_2)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (tvOS, test_tvOS17_2)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (tvOS, test_tvOS17_2)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (macOS, test_macOS)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]

Check failure on line 43 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (macOS, test_macOS)

convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]
rest:(ARTRestInternal *)rest
Expand All @@ -34,6 +48,8 @@ - (instancetype)initWithItems:(NSArray *)items
responseProcessor:(ARTPaginatedResultResponseProcessor)responseProcessor
logger:(ARTInternalLog *)logger {
if (self = [super init]) {

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (iOS, test_iOS17_2)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (iOS, test_iOS17_2)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (tvOS, test_tvOS17_2)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (tvOS, test_tvOS17_2)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (macOS, test_macOS)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]

Check failure on line 50 in Source/ARTPaginatedResult.m

View workflow job for this annotation

GitHub Actions / check (macOS, test_macOS)

convenience initializer should not invoke an initializer on 'super' [-Werror,-Wobjc-designated-initializers]
_initializedViaInit = NO;

_items = items;

_relFirst = relFirst;
Expand Down Expand Up @@ -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<id> *)items {
[self initializedViaInitCheck];
return _items;
}

- (void)first:(void (^)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error))callback {
[self initializedViaInitCheck];

if (callback) {
void (^userCallback)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error) = callback;
callback = ^(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error) {
Expand All @@ -81,6 +120,8 @@ - (void)first:(void (^)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *
}

- (void)next:(void (^)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error))callback {
[self initializedViaInitCheck];

if (callback) {
void (^userCallback)(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error) = callback;
callback = ^(ARTPaginatedResult<id> *_Nullable result, ARTErrorInfo *_Nullable error) {
Expand Down
4 changes: 2 additions & 2 deletions Source/include/Ably/ARTPaginatedResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ca4e4cb

Please sign in to comment.