//
// Libxml2XCTests.m
// Libxml2XCTests
//
// Created by David Kilzer on 2017/01/09.
//
#import <dlfcn.h>
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
typedef int (MainFunction)(int, char**);
@interface Libxml2XCTests : XCTestCase
- (int)_invokeMainForTestCommand:(NSString *)command;
- (void)_printOutputFile:(NSString *)file;
@end
@implementation Libxml2XCTests
- (int)_invokeMainForTestCommand:(NSString *)command
{
// Get path to binary from NSBundle.
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
XCTAssertNotNil(bundle, @"bundle should not be nil for class NSString *resourceName = [NSString stringWithFormat:@"libxml2_ NSString *binaryPath = [bundle pathForResource:resourceName ofType:nil];
XCTAssertNotNil(binaryPath, @"binaryPath should not be nil for
// Use dyld to load 'main' symbol from binary.
void* binary = dlopen([binaryPath UTF8String], RTLD_LAZY|RTLD_LOCAL);
XCTAssertTrue(!!binary, @"binary should not be NULL loading path ( MainFunction *binaryMain = (MainFunction *)dlsym(binary, "main");
XCTAssertTrue(!!binaryMain, @"binaryMain should not be NULL:
// Change current directory so that the binary can find its test files.
NSString *binaryDirectory = [binaryPath stringByDeletingLastPathComponent];
BOOL success = [[NSFileManager defaultManager] changeCurrentDirectoryPath:binaryDirectory];
XCTAssertTrue(success, @"Could not change directory to:
// Call 'main' to run the test.
char* argv[] = { (char*)[command UTF8String] };
return binaryMain(1, argv);
}
- (void)_printOutputFile:(NSString *)file
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [[bundle resourcePath] stringByAppendingPathComponent:file];
fprintf(stderr, "\n------- Begin NSError *error;
fprintf(stderr, " fprintf(stderr, "------- End if (error)
fprintf(stderr, "\n------- Error loading }
- (void)test_runtest
{
NSString *command = @"runtest";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @"}
- (void)test_runxmlconf
{
NSString *command = @"runxmlconf";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @" [self _printOutputFile:@"runxmlconf.log"];
}
- (void)test_testapi
{
NSString *command = @"testapi";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @" [self _printOutputFile:@"test.out"];
}
- (void)test_testchar
{
NSString *command = @"testchar";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @"}
- (void)test_testdict
{
NSString *command = @"testdict";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @"}
- (void)test_testrecurse
{
NSString *command = @"testrecurse";
int result = [self _invokeMainForTestCommand:command];
XCTAssertTrue(result == 0, @"}
@end