Skip to content

Commit

Permalink
DRAFT: Implement MLHandler and SDP parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Feb 10, 2025
1 parent 8185b6b commit fefe9af
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 25 deletions.
28 changes: 5 additions & 23 deletions Monal/Classes/MonalAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ -(void) runParserTests

-(void) runSDPTests
{
DDLogVerbose(@"SDP2XML: %@", [HelperTools sdp2xml:@"v=0\n\
NSArray<MLXMLNode*>* xmlNodes = [HelperTools sdp2xml:@"v=0\n\
o=- 2005859539484728435 2 IN IP4 127.0.0.1\n\
s=-\n\
t=0 0\n\
Expand Down Expand Up @@ -288,29 +288,10 @@ -(void) runSDPTests
a=setup:actpass\n\
a=mid:2\n\
a=sctp-port:5000\n\
a=max-message-size:262144\n" withInitiator:YES]);
}

$$class_handler(handlerTest01, $$ID(NSObject*, dummyObj))
DDLogError(@"HandlerTest01 completed");
$$
a=max-message-size:262144\n" withInitiator:YES];

$$class_handler(handlerTest02, $$ID(monal_void_block_t, dummyCallback))
DDLogError(@"HandlerTest02 completed");
$$

-(void) runHandlerTests
{
DDLogError(@"NSClassFromString: '%@'", NSClassFromString(@"monal_void_block_t"));

if([^{} isKindOfClass:[NSObject class]])
DDLogError(@"isKindOfClass");

MLHandler* handler01 = $newHandler([self class], handlerTest01);
$call(handler01, $ID(dummyObj, [NSString new]));

MLHandler* handler02 = $newHandler([self class], handlerTest02);
$call(handler02, $ID(dummyCallback, ^{}));
DDLogVerbose(@"SDP2XML: %@", xmlNodes);
DDLogDebug(@"XML2SDP: %@", [HelperTools xml2sdp:xmlNodes[2] withInitiator:YES]);
}

-(id) init
Expand All @@ -334,6 +315,7 @@ -(id) init
//[self runSDPTests];
//[HelperTools flushLogsWithTimeout:0.250];
//[self runHandlerTests];
//exit(0);
return self;
}

Expand Down
16 changes: 14 additions & 2 deletions Monal/Monal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@
26E8462824EABAED00ECE419 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 26E8462A24EABAED00ECE419 /* Main.storyboard */; };
26F9794D1ACAC73A0008E005 /* MLContactCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 26F9794C1ACAC73A0008E005 /* MLContactCell.xib */; };
26FE3BCB1C61A6C3003CC230 /* MLResizingTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = 26FE3BCA1C61A6C3003CC230 /* MLResizingTextView.m */; };
341F44662CAF427500AA6C7D /* MLPromise.h in Headers */ = {isa = PBXBuildFile; fileRef = 341F44642CAF427500AA6C7D /* MLPromise.h */; settings = {ATTRIBUTES = (Public, ); }; };
341F44672CAF427500AA6C7D /* MLPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = 341F44652CAF427500AA6C7D /* MLPromise.m */; };
340F0CA02D2425A4006224D0 /* MLMessage.h in Headers */ = {isa = PBXBuildFile; fileRef = 26611024238F17B20030A4EE /* MLMessage.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA12D2425AA006224D0 /* MLXMPPConnection.h in Headers */ = {isa = PBXBuildFile; fileRef = 26611015238F08AC0030A4EE /* MLXMPPConnection.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA22D2425C4006224D0 /* XMPPPresence.h in Headers */ = {isa = PBXBuildFile; fileRef = 264E34591787BAB100BC7BD0 /* XMPPPresence.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA32D2425D2006224D0 /* MLXMPPServer.h in Headers */ = {isa = PBXBuildFile; fileRef = 2661100B238F08820030A4EE /* MLXMPPServer.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA42D2425D9006224D0 /* MLXMPPIdentity.h in Headers */ = {isa = PBXBuildFile; fileRef = 26611010238F08980030A4EE /* MLXMPPIdentity.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA52D2425E1006224D0 /* MLLogFileManager.h in Headers */ = {isa = PBXBuildFile; fileRef = C15D504824C727CC002F75BB /* MLLogFileManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
340F0CA62D24267B006224D0 /* MLXMLNode.h in Headers */ = {isa = PBXBuildFile; fileRef = 260C51D2177F44AD0039634B /* MLXMLNode.h */; settings = {ATTRIBUTES = (Public, ); }; };
341F44662CAF427500AA6C7D /* MLPromise.h in Headers */ = {isa = PBXBuildFile; fileRef = 341F44642CAF427500AA6C7D /* MLPromise.h */; settings = {ATTRIBUTES = (Public, ); }; };
341F44672CAF427500AA6C7D /* MLPromise.m in Sources */ = {isa = PBXBuildFile; fileRef = 341F44652CAF427500AA6C7D /* MLPromise.m */; };
3424B4E72D232BE2009F6503 /* MLContact.h in Headers */ = {isa = PBXBuildFile; fileRef = 26611029238F17CA0030A4EE /* MLContact.h */; settings = {ATTRIBUTES = (Public, ); }; };
3424B4E82D232BE7009F6503 /* DataLayer.h in Headers */ = {isa = PBXBuildFile; fileRef = 2601D9A80FBF255D004DB939 /* DataLayer.h */; settings = {ATTRIBUTES = (Public, ); }; };
3424B4E92D232BEB009F6503 /* xmpp.h in Headers */ = {isa = PBXBuildFile; fileRef = 260C51CF177F08F50039634B /* xmpp.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -188,6 +188,9 @@
84C1CD502A8C764D007076ED /* SwiftHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C1CD4F2A8C764D007076ED /* SwiftHelpers.swift */; };
84C1CD522A8F617F007076ED /* MLStreamRedirect.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C1CD512A8F617F007076ED /* MLStreamRedirect.m */; };
84C1CD542A8F6196007076ED /* MLStreamRedirect.h in Headers */ = {isa = PBXBuildFile; fileRef = 84C1CD532A8F6196007076ED /* MLStreamRedirect.h */; };
84C6F6032D597FB20015575E /* ParserTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C6F6022D597FB20015575E /* ParserTest.m */; };
84C6F6052D597FD10015575E /* SDPTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C6F6042D597FD10015575E /* SDPTest.m */; };
84C6F6072D597FF40015575E /* HandlerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C6F6062D597FF40015575E /* HandlerTest.m */; };
84D31CE628653B83006D7926 /* WebRTCClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D31CE528653B83006D7926 /* WebRTCClient.swift */; };
84E231F32C16A9CE00735FB7 /* SVGView in Frameworks */ = {isa = PBXBuildFile; productRef = 84E231F22C16A9CE00735FB7 /* SVGView */; };
84E55E7D2964424E003E191A /* ActiveChatsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 261A6284176C156500059090 /* ActiveChatsViewController.m */; };
Expand Down Expand Up @@ -626,6 +629,9 @@
84C1CD4F2A8C764D007076ED /* SwiftHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftHelpers.swift; sourceTree = "<group>"; };
84C1CD512A8F617F007076ED /* MLStreamRedirect.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MLStreamRedirect.m; sourceTree = "<group>"; };
84C1CD532A8F6196007076ED /* MLStreamRedirect.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MLStreamRedirect.h; sourceTree = "<group>"; };
84C6F6022D597FB20015575E /* ParserTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ParserTest.m; sourceTree = "<group>"; };
84C6F6042D597FD10015575E /* SDPTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDPTest.m; sourceTree = "<group>"; };
84C6F6062D597FF40015575E /* HandlerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HandlerTest.m; sourceTree = "<group>"; };
84D31CE528653B83006D7926 /* WebRTCClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebRTCClient.swift; path = Classes/WebRTCClient.swift; sourceTree = SOURCE_ROOT; };
84F194C32C0FE70900F0A994 /* es-AR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-AR"; path = "external/es-AR.lproj/Main.strings"; sourceTree = "<group>"; };
84F194C42C0FE74500F0A994 /* es-AR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-AR"; path = "external/es-AR.lproj/Settings.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1461,6 +1467,9 @@
C1049188261301530054AC9E /* MonalXMPPUnitTests.swift */,
C104918A261301530054AC9E /* Info.plist */,
C1B940E826144AF400E9D290 /* MonalXMPPUnitTests-Bridging-Header.h */,
84C6F6022D597FB20015575E /* ParserTest.m */,
84C6F6042D597FD10015575E /* SDPTest.m */,
84C6F6062D597FF40015575E /* HandlerTest.m */,
);
path = MonalXMPPUnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -2272,8 +2281,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
84C6F6072D597FF40015575E /* HandlerTest.m in Sources */,
84C6F6032D597FB20015575E /* ParserTest.m in Sources */,
C1049189261301530054AC9E /* MonalXMPPUnitTests.swift in Sources */,
C1B940EA26144AF500E9D290 /* AESGCMTest.m in Sources */,
84C6F6052D597FD10015575E /* SDPTest.m in Sources */,
C1049199261301710054AC9E /* MLCryptoTest.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
155 changes: 155 additions & 0 deletions Monal/MonalXMPPUnitTests/HandlerTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//
// HandlerTest.m
// MonalXMPPUnitTests
//
// Created by admin on 10.02.25.
// Copyright © 2025 monal-im.org. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import <monalxmpp/MLConstants.h>
#import <monalxmpp/MLHandler.h>

@interface HandlerTest : XCTestCase
//TODO: why is that needed for instance handlers at all? other parts of our code also don't need these declarations.
//TODO: and weird thing: class handlers don't seem to throw a compiler error in here, too.
-(void) MLInstanceHandler_handlerTestInstanceHandler_withArguments:(NSDictionary*) _callerArgs andBoundArguments:(NSDictionary*) _boundArgs;
@end

@implementation HandlerTest

-(void) setUp
{
// Put setup code here. This method is called before the invocation of each test method in the class.
}

-(void) tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

-(void) testMonalVoidBlock
{
id voidblock = NSClassFromString(@"monal_void_block_t");
XCTAssert([^{} isKindOfClass:voidblock], "void block should be kind of monal_void_block_t");
}

-(void) testVoidBlockIsNSObject
{
XCTAssert([^{} isKindOfClass:[NSObject class]], "void block should be kind of NSObject");
}

$$class_handler(handlerTestStringObject, $$ID(NSObject*, dummyObj))
XCTAssertEqualObjects(dummyObj, @"dummy string", "handler01 should have gotten 'dummy string'");
$$

-(void) testHandlerStringObject
{
MLHandler* handler = $newHandler([self class], handlerTestStringObject);
$call(handler, $ID(dummyObj, @"dummy string"));
}

$$class_handler(handlerTestBlockArgument, $$ID(monal_void_block_t, dummyCallback))
dummyCallback();
$$

-(void) testHandlerBlockArgument
{
XCTestExpectation* expectation = [self expectationWithDescription:@"handlerTestBlockArgument should call callback block"];
expectation.expectedFulfillmentCount = 1;
expectation.assertForOverFulfill = YES;

MLHandler* handler = $newHandler([self class], handlerTestBlockArgument);
$call(handler, $ID(dummyCallback, (^{
[expectation fulfill];
})));

[self waitForExpectations:@[expectation] timeout:1.0];
}

$$instance_handler(handlerTestInstanceHandler, testcase, $$ID(XCTestCase*, testcase), $$ID(XCTestExpectation*, expectation), $$ID(monal_void_block_t, someCallback), $$ID(NSString*, dummyObj))

Check failure on line 71 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

no visible @interface for 'XCTestCase' declares the selector 'MLInstanceHandler_handlerTestInstanceHandler_withArguments:andBoundArguments:'
XCTAssertEqualObjects(dummyObj, @"dummy string", "handler03 should have gotten 'dummy string'");
[self handlerTestInstanceHandlerSubcall:expectation];
someCallback();
$$

-(void) handlerTestInstanceHandlerSubcall:(XCTestExpectation*) expectation
{
[expectation fulfill];
}

-(void) testHandlerInstanceHandler
{
XCTestExpectation* expectation = [self expectationWithDescription:@"handlerTestInstanceHandler should fulfill this twice"];
expectation.expectedFulfillmentCount = 2;
expectation.assertForOverFulfill = YES;

MLHandler* handler = $newHandler(self, handlerTestInstanceHandler);
$call(handler, $ID(dummyObj, @"dummy string"), $ID(dummyCallback, (^{
[expectation fulfill];
})));

[self waitForExpectations:@[expectation] timeout:1.0];
}

$$class_handler(handlerTestMandatoryArgument, $$ID(monal_void_block_t, dummyCallback))
dummyCallback();
$$

-(void) testHandlerMandatoryArgument
{
MLHandler* handler = $newHandler([self class], handlerTestMandatoryArgument);
XCTAssertThrows(($call(handler, $ID(something, @"something"))), "missing mandatory dummyCallback should trigger an exception");

Check failure on line 103 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
}

$$class_handler(handlerTestMissingOptionalArgument, $_ID(NSString*, dummy))
XCTAssertNil(dummy, "missing optional argument should be nil");
$$

-(void) testHandlerMissingOptionalArgument
{
MLHandler* handler = $newHandler([self class], handlerTestMissingOptionalArgument, $ID(dummy2, @"dummy2"));
XCTAssertNoThrow(($call(handler, $ID(something, @"something"))), "missing optional dummy argument should not trigger an error");

Check failure on line 113 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
}

$$class_handler(handlerTestGivenOptionalArgument, $_ID(NSString*, dummy))
XCTAssertNotNil(dummy, "given optional argument should not be nil");
$$

-(void) testHandlerGivenOptionalArgument
{
MLHandler* handler = $newHandler([self class], handlerTestGivenOptionalArgument, $ID(dummy, @"dummy"));
XCTAssertNoThrow(($call(handler, $ID(something, @"something"))), "giving optional dummy argument should not trigger an error");

Check failure on line 123 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
}

$$class_handler(handlerTestWithInvalidation01, $_ID(NSString*, dummy))
XCTAssertTrue(NO, "this handler should never be called");
$$
$$class_handler(handlerTestWithInvalidationInvalidation01, $_ID(NSString*, something))
XCTAssertEqualObjects(something, @"something01", "handlerTestWithInvalidationInvalidation01 should have gotten 'something01'");
$$

-(void) testHandlerWithInvalidation01
{
MLHandler* handler = $newHandlerWithInvalidation([self class], handlerTestWithInvalidation01, handlerTestWithInvalidationInvalidation01, $ID(dummy2, @"dummy2"));
XCTAssertNoThrow(($invalidate(handler, $ID(something, @"something01"))), "calling an invalidation should not trigger an error");

Check failure on line 136 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
XCTAssertThrow(($call(handler, $ID(something, @"something02"))), "calling a handler after its invaidation should trigger an error");

Check failure on line 137 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

call to undeclared function 'XCTAssertThrow'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

Check failure on line 137 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
XCTAssertThrow(($invalidate(handler, $ID(something, @"something03"))), "calling an invalidation twice should trigger an error");

Check failure on line 138 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
}

$$class_handler(handlerTestWithInvalidation02, $$ID(NSString*, dummy2))
XCTAssertEqualObjects(dummy2, @"dummy2", "handlerTestWithInvalidation02 should have gotten 'dummy2'");
$$
$$class_handler(handlerTestWithInvalidationInvalidation02, $_ID(NSString*, something))
XCTAssertTrue(NO, "this invalidation should never be called");
$$

-(void) testHandlerWithInvalidation02
{
MLHandler* handler = $newHandlerWithInvalidation([self class], handlerTestWithInvalidation02, handlerTestWithInvalidationInvalidation02, $ID(dummy2, @"dummy2"));
XCTAssertNoThrow(($call(handler, $ID(something, @"something01"))), "calling a handler should not trigger an error");

Check failure on line 151 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

expected expression
XCTAssertThrow(($invalidate(handler, $ID(something, @"something02"))), "calling an invalidation after its handler should trigger an error");

Check failure on line 152 in Monal/MonalXMPPUnitTests/HandlerTest.m

View workflow job for this annotation

GitHub Actions / Run automated tests

call to undeclared function 'XCTAssertThrow'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
}

@end
9 changes: 9 additions & 0 deletions Monal/MonalXMPPUnitTests/ParserTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// ParserTest.m
// MonalXMPPUnitTests
//
// Created by admin on 10.02.25.
// Copyright © 2025 monal-im.org. All rights reserved.
//

#import <Foundation/Foundation.h>
Loading

0 comments on commit fefe9af

Please sign in to comment.