|
| 1 | +// |
| 2 | +// HandlerTest.m |
| 3 | +// MonalXMPPUnitTests |
| 4 | +// |
| 5 | +// Created by admin on 10.02.25. |
| 6 | +// Copyright © 2025 monal-im.org. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <Foundation/Foundation.h> |
| 10 | +#import <XCTest/XCTest.h> |
| 11 | +#import <monalxmpp/MLConstants.h> |
| 12 | +#import <monalxmpp/MLHandler.h> |
| 13 | + |
| 14 | +@interface HandlerTest : XCTestCase |
| 15 | +//TODO: why is that needed for instance handlers at all? other parts of our code also don't need these declarations. |
| 16 | +//TODO: and weird thing: class handlers don't seem to throw a compiler error in here, too. |
| 17 | +-(void) MLInstanceHandler_handlerTestInstanceHandler_withArguments:(NSDictionary*) _callerArgs andBoundArguments:(NSDictionary*) _boundArgs; |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation HandlerTest |
| 21 | + |
| 22 | +-(void) setUp |
| 23 | +{ |
| 24 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 25 | +} |
| 26 | + |
| 27 | +-(void) tearDown |
| 28 | +{ |
| 29 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 30 | +} |
| 31 | + |
| 32 | +-(void) testMonalVoidBlock |
| 33 | +{ |
| 34 | + id voidblock = NSClassFromString(@"monal_void_block_t"); |
| 35 | + XCTAssert([^{} isKindOfClass:voidblock], "void block should be kind of monal_void_block_t"); |
| 36 | +} |
| 37 | + |
| 38 | +-(void) testVoidBlockIsNSObject |
| 39 | +{ |
| 40 | + XCTAssert([^{} isKindOfClass:[NSObject class]], "void block should be kind of NSObject"); |
| 41 | +} |
| 42 | + |
| 43 | +$$class_handler(handlerTestStringObject, $$ID(NSObject*, dummyObj)) |
| 44 | + XCTAssertEqualObjects(dummyObj, @"dummy string", "handler01 should have gotten 'dummy string'"); |
| 45 | +$$ |
| 46 | + |
| 47 | +-(void) testHandlerStringObject |
| 48 | +{ |
| 49 | + MLHandler* handler = $newHandler([self class], handlerTestStringObject); |
| 50 | + $call(handler, $ID(dummyObj, @"dummy string")); |
| 51 | +} |
| 52 | + |
| 53 | +$$class_handler(handlerTestBlockArgument, $$ID(monal_void_block_t, dummyCallback)) |
| 54 | + dummyCallback(); |
| 55 | +$$ |
| 56 | + |
| 57 | +-(void) testHandlerBlockArgument |
| 58 | +{ |
| 59 | + XCTestExpectation* expectation = [self expectationWithDescription:@"handlerTestBlockArgument should call callback block"]; |
| 60 | + expectation.expectedFulfillmentCount = 1; |
| 61 | + expectation.assertForOverFulfill = YES; |
| 62 | + |
| 63 | + MLHandler* handler = $newHandler([self class], handlerTestBlockArgument); |
| 64 | + $call(handler, $ID(dummyCallback, (^{ |
| 65 | + [expectation fulfill]; |
| 66 | + }))); |
| 67 | + |
| 68 | + [self waitForExpectations:@[expectation] timeout:1.0]; |
| 69 | +} |
| 70 | + |
| 71 | +$$instance_handler(handlerTestInstanceHandler, testcase, $$ID(XCTestCase*, testcase), $$ID(XCTestExpectation*, expectation), $$ID(monal_void_block_t, someCallback), $$ID(NSString*, dummyObj)) |
| 72 | + XCTAssertEqualObjects(dummyObj, @"dummy string", "handler03 should have gotten 'dummy string'"); |
| 73 | + [self handlerTestInstanceHandlerSubcall:expectation]; |
| 74 | + someCallback(); |
| 75 | +$$ |
| 76 | + |
| 77 | +-(void) handlerTestInstanceHandlerSubcall:(XCTestExpectation*) expectation |
| 78 | +{ |
| 79 | + [expectation fulfill]; |
| 80 | +} |
| 81 | + |
| 82 | +-(void) testHandlerInstanceHandler |
| 83 | +{ |
| 84 | + XCTestExpectation* expectation = [self expectationWithDescription:@"handlerTestInstanceHandler should fulfill this twice"]; |
| 85 | + expectation.expectedFulfillmentCount = 2; |
| 86 | + expectation.assertForOverFulfill = YES; |
| 87 | + |
| 88 | + MLHandler* handler = $newHandler(self, handlerTestInstanceHandler); |
| 89 | + $call(handler, $ID(dummyObj, @"dummy string"), $ID(dummyCallback, (^{ |
| 90 | + [expectation fulfill]; |
| 91 | + }))); |
| 92 | + |
| 93 | + [self waitForExpectations:@[expectation] timeout:1.0]; |
| 94 | +} |
| 95 | + |
| 96 | +$$class_handler(handlerTestMandatoryArgument, $$ID(monal_void_block_t, dummyCallback)) |
| 97 | + dummyCallback(); |
| 98 | +$$ |
| 99 | + |
| 100 | +-(void) testHandlerMandatoryArgument |
| 101 | +{ |
| 102 | + MLHandler* handler = $newHandler([self class], handlerTestMandatoryArgument); |
| 103 | + XCTAssertThrows(($call(handler, $ID(something, @"something"))), "missing mandatory dummyCallback should trigger an exception"); |
| 104 | +} |
| 105 | + |
| 106 | +$$class_handler(handlerTestMissingOptionalArgument, $_ID(NSString*, dummy)) |
| 107 | + XCTAssertNil(dummy, "missing optional argument should be nil"); |
| 108 | +$$ |
| 109 | + |
| 110 | +-(void) testHandlerMissingOptionalArgument |
| 111 | +{ |
| 112 | + MLHandler* handler = $newHandler([self class], handlerTestMissingOptionalArgument, $ID(dummy2, @"dummy2")); |
| 113 | + XCTAssertNoThrow(($call(handler, $ID(something, @"something"))), "missing optional dummy argument should not trigger an error"); |
| 114 | +} |
| 115 | + |
| 116 | +$$class_handler(handlerTestGivenOptionalArgument, $_ID(NSString*, dummy)) |
| 117 | + XCTAssertNotNil(dummy, "given optional argument should not be nil"); |
| 118 | +$$ |
| 119 | + |
| 120 | +-(void) testHandlerGivenOptionalArgument |
| 121 | +{ |
| 122 | + MLHandler* handler = $newHandler([self class], handlerTestGivenOptionalArgument, $ID(dummy, @"dummy")); |
| 123 | + XCTAssertNoThrow(($call(handler, $ID(something, @"something"))), "giving optional dummy argument should not trigger an error"); |
| 124 | +} |
| 125 | + |
| 126 | +$$class_handler(handlerTestWithInvalidation01, $_ID(NSString*, dummy)) |
| 127 | + XCTAssertTrue(NO, "this handler should never be called"); |
| 128 | +$$ |
| 129 | +$$class_handler(handlerTestWithInvalidationInvalidation01, $_ID(NSString*, something)) |
| 130 | + XCTAssertEqualObjects(something, @"something01", "handlerTestWithInvalidationInvalidation01 should have gotten 'something01'"); |
| 131 | +$$ |
| 132 | + |
| 133 | +-(void) testHandlerWithInvalidation01 |
| 134 | +{ |
| 135 | + MLHandler* handler = $newHandlerWithInvalidation([self class], handlerTestWithInvalidation01, handlerTestWithInvalidationInvalidation01, $ID(dummy2, @"dummy2")); |
| 136 | + XCTAssertNoThrow(($invalidate(handler, $ID(something, @"something01"))), "calling an invalidation should not trigger an error"); |
| 137 | + XCTAssertThrow(($call(handler, $ID(something, @"something02"))), "calling a handler after its invaidation should trigger an error"); |
| 138 | + XCTAssertThrow(($invalidate(handler, $ID(something, @"something03"))), "calling an invalidation twice should trigger an error"); |
| 139 | +} |
| 140 | + |
| 141 | +$$class_handler(handlerTestWithInvalidation02, $$ID(NSString*, dummy2)) |
| 142 | + XCTAssertEqualObjects(dummy2, @"dummy2", "handlerTestWithInvalidation02 should have gotten 'dummy2'"); |
| 143 | +$$ |
| 144 | +$$class_handler(handlerTestWithInvalidationInvalidation02, $_ID(NSString*, something)) |
| 145 | + XCTAssertTrue(NO, "this invalidation should never be called"); |
| 146 | +$$ |
| 147 | + |
| 148 | +-(void) testHandlerWithInvalidation02 |
| 149 | +{ |
| 150 | + MLHandler* handler = $newHandlerWithInvalidation([self class], handlerTestWithInvalidation02, handlerTestWithInvalidationInvalidation02, $ID(dummy2, @"dummy2")); |
| 151 | + XCTAssertNoThrow(($call(handler, $ID(something, @"something01"))), "calling a handler should not trigger an error"); |
| 152 | + XCTAssertThrow(($invalidate(handler, $ID(something, @"something02"))), "calling an invalidation after its handler should trigger an error"); |
| 153 | +} |
| 154 | + |
| 155 | +@end |
0 commit comments