Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Clean-up & add support for _XCTPreformattedFailureHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Rex committed Sep 12, 2019
1 parent dc59464 commit 6b1df42
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 76 deletions.
3 changes: 2 additions & 1 deletion platform/darwin/test/MGLOfflinePackTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ - (void)testInvalidatingAnInvalidPack {

// Now try again, without asserts
NSAssertionHandler *oldHandler = [NSAssertionHandler currentHandler];
[[[NSThread currentThread] threadDictionary] setValue:[[MGLTestAssertionHandler alloc] init] forKey:NSAssertionHandlerKey];
MGLTestAssertionHandler *newHandler = [[MGLTestAssertionHandler alloc] initWithTestCase:self];
[[[NSThread currentThread] threadDictionary] setValue:newHandler forKey:NSAssertionHandlerKey];

// Make sure this doesn't crash without asserts
[invalidPack invalidate];
Expand Down
14 changes: 10 additions & 4 deletions platform/darwin/test/MGLOfflineStorageTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ + (void)tearDown {

- (void)setUp {
[super setUp];

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
XCTestExpectation *expectation = [self keyValueObservingExpectationForObject:[MGLOfflineStorage sharedOfflineStorage] keyPath:@"packs" handler:^BOOL(id _Nonnull observedObject, NSDictionary * _Nonnull change) {
Expand Down Expand Up @@ -375,7 +375,9 @@ - (void)testRemovePackTwiceInSuccession {
[[MGLOfflineStorage sharedOfflineStorage] removePack:pack withCompletionHandler:nil];

NSAssertionHandler *oldHandler = [NSAssertionHandler currentHandler];
[[[NSThread currentThread] threadDictionary] setValue:[[MGLTestAssertionHandler alloc] init] forKey:NSAssertionHandlerKey];
MGLTestAssertionHandler *newHandler = [[MGLTestAssertionHandler alloc] initWithTestCase:self];

[[[NSThread currentThread] threadDictionary] setValue:newHandler forKey:NSAssertionHandlerKey];

[[MGLOfflineStorage sharedOfflineStorage] removePack:pack withCompletionHandler:^(NSError * _Nullable error) {
XCTAssertEqual(pack.state, MGLOfflinePackStateInvalid, @"Removed pack should be invalid in the completion handler.");
Expand Down Expand Up @@ -424,7 +426,9 @@ - (void)test15536RemovePacksWhileReloading {
}]];

NSAssertionHandler *oldHandler = [NSAssertionHandler currentHandler];
[[[NSThread currentThread] threadDictionary] setValue:[[MGLTestAssertionHandler alloc] init] forKey:NSAssertionHandlerKey];
MGLTestAssertionHandler *newHandler = [[MGLTestAssertionHandler alloc] initWithTestCase:self];

[[[NSThread currentThread] threadDictionary] setValue:newHandler forKey:NSAssertionHandlerKey];

for (MGLOfflinePack *pack in validPacks) {
[storage removePack:pack withCompletionHandler:^(NSError * _Nullable error) {
Expand Down Expand Up @@ -465,7 +469,9 @@ - (void)test15536RemovePacksOnBackgroundQueueWhileReloading {
dispatch_async(queue, ^{
NSArray *packs = storage.packs;
NSAssertionHandler *oldHandler = [NSAssertionHandler currentHandler];
[[[NSThread currentThread] threadDictionary] setValue:[[MGLTestAssertionHandler alloc] init] forKey:NSAssertionHandlerKey];
MGLTestAssertionHandler *newHandler = [[MGLTestAssertionHandler alloc] initWithTestCase:self];

[[[NSThread currentThread] threadDictionary] setValue:newHandler forKey:NSAssertionHandlerKey];

NSArray *validPacks = [packs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
MGLOfflinePack *pack = (MGLOfflinePack*)evaluatedObject;
Expand Down
18 changes: 18 additions & 0 deletions platform/darwin/test/MGLTestAssertionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>

NS_ASSUME_NONNULL_BEGIN

// Use to catch or log assertions that occur in dispatch blocks, timers or
// other asynchronous operations.
@interface MGLTestAssertionHandler : NSAssertionHandler

- (instancetype)initWithTestCase:(XCTestCase *)testCase;
@property (nonatomic, weak) XCTestCase *testCase;

// If YES, use `_XCTPreformattedFailureHandler` to "fail" the test,
// otherwise log the assert.
@property (nonatomic) BOOL shouldFail;
@end

NS_ASSUME_NONNULL_END
77 changes: 77 additions & 0 deletions platform/darwin/test/MGLTestAssertionHandler.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#import "MGLTestAssertionHandler.h"

@implementation MGLTestAssertionHandler

- (instancetype)initWithTestCase:(XCTestCase *)testCase {
if ((self = [super init])) {
_testCase = testCase;
}
return self;
}

- (void)handleFailureInMethod:(SEL)selector
object:(id)object
file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSString *description = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);

NSString *condition = [NSString stringWithFormat:
@"`[%@ %@]`",
object, NSStringFromSelector(selector)
];

if (self.testCase && self.shouldFail) {
_XCTPreformattedFailureHandler(self.testCase,
YES,
fileName,
line,
condition,
description
);
}
else {
NSLog(@"Assertion Failure: %@:%lu: %@ - %@",
fileName,
line,
condition,
description);
}
}

- (void)handleFailureInFunction:(NSString *)functionName
file:(NSString *)fileName
lineNumber:(NSInteger)line
description:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSString *description = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);

NSString *condition = [NSString stringWithFormat:
@"`%@`",
functionName];

if (self.testCase && self.shouldFail) {
_XCTPreformattedFailureHandler(self.testCase,
YES,
fileName,
line,
condition,
description);
}
else {
NSLog(@"Assertion Failure: %@:%lu: %@ - %@",
fileName,
line,
condition,
description);
}
}
@end

1 change: 0 additions & 1 deletion platform/default/src/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ void OfflineDatabase::handleError(const util::IOException& ex, const char* actio
}

void OfflineDatabase::handleError(const std::runtime_error &ex, const char* action) {
// What else needs to happen here??
Log::Error(Event::Database, -1, "Can't %s: %s", action, ex.what());
}

Expand Down
6 changes: 0 additions & 6 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2243,12 +2243,6 @@ - (void)mapView:(MGLMapView *)mapView regionDidChangeWithReason:(MGLCameraChange

[self updateHUD];
[self updateHelperMapViews];

// asdf
MGLCoordinateBounds bounds = [self.mapView convertRect:self.mapView.bounds toCoordinateBoundsFromView:self.mapView];
NSLog(@"coordinate bounds = %@", MGLStringFromCoordinateBounds(bounds));


}

- (void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(MGLUserLocation *)userLocation {
Expand Down
6 changes: 4 additions & 2 deletions platform/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@
ACD0245B2187EABA00D8C8A7 /* MMEMetricsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = ACD024542187EAAF00D8C8A7 /* MMEMetricsManager.m */; };
ACD0245E2187EACB00D8C8A7 /* MMEMetrics.m in Sources */ = {isa = PBXBuildFile; fileRef = ACD024572187EAAF00D8C8A7 /* MMEMetrics.m */; };
ACD0245F2187EACB00D8C8A7 /* MMEMetrics.m in Sources */ = {isa = PBXBuildFile; fileRef = ACD024572187EAAF00D8C8A7 /* MMEMetrics.m */; };
CA0B3C022329DE9A00E4B493 /* MGLTestAssertionHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = CAAA65D82321BBA900F08A39 /* MGLTestAssertionHandler.m */; };
CA0C27922076C804001CE5B7 /* MGLShapeSourceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = CA0C27912076C804001CE5B7 /* MGLShapeSourceTests.m */; };
CA0C27942076CA19001CE5B7 /* MGLMapViewIntegrationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CA0C27932076CA19001CE5B7 /* MGLMapViewIntegrationTest.m */; };
CA1B4A512099FB2200EDD491 /* MGLMapSnapshotterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CA1B4A502099FB2200EDD491 /* MGLMapSnapshotterTest.m */; };
Expand Down Expand Up @@ -1212,8 +1213,8 @@
CA86FF0D22D8D5A0009EB14A /* MGLNetworkConfigurationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLNetworkConfigurationTests.m; sourceTree = "<group>"; };
CA88DC2F21C85D900059ED5A /* MGLStyleURLIntegrationTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLStyleURLIntegrationTest.m; sourceTree = "<group>"; };
CA8FBC0821A47BB100D1203C /* MGLRendererConfigurationTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLRendererConfigurationTests.mm; path = ../../darwin/test/MGLRendererConfigurationTests.mm; sourceTree = "<group>"; };
CAAA65D72321BBA900F08A39 /* MGLTestAssertionHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLTestAssertionHandler.h; sourceTree = "<group>"; };
CAAA65D82321BBA900F08A39 /* MGLTestAssertionHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLTestAssertionHandler.m; sourceTree = "<group>"; };
CAAA65D72321BBA900F08A39 /* MGLTestAssertionHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = MGLTestAssertionHandler.h; path = ../../darwin/test/MGLTestAssertionHandler.h; sourceTree = "<group>"; };
CAAA65D82321BBA900F08A39 /* MGLTestAssertionHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MGLTestAssertionHandler.m; path = ../../darwin/test/MGLTestAssertionHandler.m; sourceTree = "<group>"; };
CAD9D0A922A86D6F001B25EE /* MGLResourceTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = MGLResourceTests.mm; path = ../../darwin/test/MGLResourceTests.mm; sourceTree = "<group>"; };
CAE7AD5320F46EF5003B6782 /* integration-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "integration-Bridging-Header.h"; sourceTree = "<group>"; };
CAE7AD5420F46EF5003B6782 /* MGLMapSnapshotterSwiftTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MGLMapSnapshotterSwiftTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3224,6 +3225,7 @@
CAE7AD5520F46EF5003B6782 /* MGLMapSnapshotterSwiftTests.swift in Sources */,
CA0C27922076C804001CE5B7 /* MGLShapeSourceTests.m in Sources */,
077061DA215DA00E000FEF62 /* MGLTestLocationManager.m in Sources */,
CA0B3C022329DE9A00E4B493 /* MGLTestAssertionHandler.m in Sources */,
CA6914B520E67F50002DB0EE /* MGLAnnotationViewIntegrationTests.mm in Sources */,
CA4F3BE223107793008BAFEA /* MGLCameraTransitionTests.mm in Sources */,
CA4C54FE2324948100A81659 /* MGLSourceTests.swift in Sources */,
Expand Down
17 changes: 0 additions & 17 deletions platform/ios/test/MGLTestAssertionHandler.h

This file was deleted.

41 changes: 0 additions & 41 deletions platform/ios/test/MGLTestAssertionHandler.m

This file was deleted.

Loading

0 comments on commit 6b1df42

Please sign in to comment.