Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes to fall back on absolute NSBundle paths for test data. Copy tes…

…t data into bundle in iOS tests
  • Loading branch information...
commit db3c79be83d560ed9f6e105e379595381fea3a81 1 parent 7f4edf4
@samoneblacksock samoneblacksock authored
View
23 SBJson.xcodeproj/project.pbxproj
@@ -194,6 +194,13 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
+ A0E92A8E16276E3F0091EDF6 /* Assets */ = {
+ isa = PBXGroup;
+ children = (
+ );
+ name = Assets;
+ sourceTree = "<group>";
+ };
BC1232311391D5CC00131607 = {
isa = PBXGroup;
children = (
@@ -278,6 +285,7 @@
BC8851581391D6DD00370E55 /* Tests */ = {
isa = PBXGroup;
children = (
+ A0E92A8E16276E3F0091EDF6 /* Assets */,
BC88522B1391D6DD00370E55 /* ErrorTest.m */,
BC88522C1391D6DD00370E55 /* FormatTest.m */,
BC88522D1391D6DD00370E55 /* JsonCheckerTest.m */,
@@ -485,6 +493,7 @@
BCC2626E13920FC7003D9994 /* Sources */,
BCC2626F13920FC7003D9994 /* Frameworks */,
BCC2627013920FC7003D9994 /* Resources */,
+ A0E92BF5162772BB0091EDF6 /* ShellScript */,
BCC2627113920FC7003D9994 /* ShellScript */,
);
buildRules = (
@@ -554,6 +563,20 @@
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
+ A0E92BF5162772BB0091EDF6 /* ShellScript */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ "",
+ );
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "mkdir -p \"${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/Tests/\"\ncp -r \"${PROJECT_DIR}/Tests/Data/\" \"${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/Tests/Data\"\ncp -r \"${PROJECT_DIR}/Tests/Stream/\" \"${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/Tests/Stream\"";
+ };
BC1232501391D5CC00131607 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
View
6 Tests/JsonCheckerTest.m
@@ -45,12 +45,14 @@ - (void)setUp {
- (void)foreachFilePrefixedBy:(NSString*)prefix inSuite:(NSString*)suite apply:(void(^)(NSString*))block {
NSString *file;
- NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:suite];
+ NSString *rootPath = [[self class] pathForSuite:suite];
+ NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:rootPath];
+
while ((file = [enumerator nextObject])) {
if (![file hasPrefix:prefix])
continue;
- NSString *path = [suite stringByAppendingPathComponent:file];
+ NSString *path = [rootPath stringByAppendingPathComponent:file];
if ([[NSFileManager defaultManager] isReadableFileAtPath:path]) {
block(path);
count++;
View
12 Tests/JsonTestCase.h
@@ -33,6 +33,9 @@
#import <SBJson/SBJson.h>
+typedef void (^ JsonTestCaseBlock)(NSString *, NSString *);
+
+
@interface JsonTestCase : SenTestCase {
SBJsonWriter * writer;
SBJsonParser * parser;
@@ -41,6 +44,13 @@
- (NSString*)otherFileName;
-- (void) foreachTestInSuite: (NSString*) suite apply: (void (^)(NSString *, NSString *)) block ;
+- (void)foreachTestInSuite:(NSString *)suite apply:(JsonTestCaseBlock)block;
+
+/*
+ * If you want to get a valid path to enumerate over, this method
+ * deals with iOS vs Mac OSX. The path returned is guarenteed to
+ * work with NSFileManager enumeratorWithPath:
+ */
++ (NSString *)pathForSuite:(NSString *)suite;
@end
View
42 Tests/JsonTestCase.m
@@ -33,6 +33,7 @@
#import "JsonTestCase.h"
+
@implementation JsonTestCase
- (void)setUp {
@@ -45,19 +46,44 @@ - (NSString*)otherFileName {
return @"output";
}
-- (void)foreachTestInSuite:(NSString*)suite apply:(void(^)(NSString*, NSString*))block {
- NSString *file;
- NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:suite];
- while ((file = [enumerator nextObject])) {
- NSString *path = [suite stringByAppendingPathComponent:file];
- NSString *inpath = [path stringByAppendingPathComponent:@"input"];
++ (NSString *)pathForSuite:(NSString *)suite {
+ // First, can we just find the files from a relative path (Mac OSX tests)
+ NSFileManager *manager = [NSFileManager new];
+ BOOL isDir = NO;
+ if ([manager fileExistsAtPath:suite isDirectory:&isDir] && YES == isDir) {
+ return suite;
+ } else {
+ // Fall back to checking to see if the files are found in a bundle (fix for iOS tests)
+ for (NSBundle *bundle in [NSBundle allBundles]) {
+ NSString *path = [NSString stringWithFormat:@"%@/%@", [bundle resourcePath], suite];
+ isDir = YES;
+ if (NO == [manager fileExistsAtPath:path isDirectory:&isDir] || NO == isDir)
+ continue;
+
+ NSLog(@"Valid bundle path for suite '%@' : %@", suite, path);
+ return path;
+ }
+ }
+
+ return nil;
+}
- if ([[NSFileManager defaultManager] isReadableFileAtPath:inpath]) {
+- (void)foreachTestInSuite:(NSString *)suite apply:(JsonTestCaseBlock)block {
+ NSFileManager *manager = [NSFileManager new];
+ NSString *rootPath = [[self class] pathForSuite:suite];
+ NSEnumerator *enumerator = [manager enumeratorAtPath:rootPath];
+
+ for (NSString *file in enumerator) {
+ NSString *path = [rootPath stringByAppendingPathComponent:file];
+ NSString *inpath = [path stringByAppendingPathComponent:@"input"];
+
+ if ([manager isReadableFileAtPath:inpath]) {
NSString *outpath = [path stringByAppendingPathComponent:[self otherFileName]];
- STAssertTrue([[NSFileManager defaultManager] isReadableFileAtPath:outpath], nil);
+ STAssertTrue([manager isReadableFileAtPath:outpath], nil);
block(inpath, outpath);
count++;
}
}
}
+
@end
View
10 Tests/StreamParserIntegrationTest.m
@@ -34,6 +34,8 @@
#import <SenTestingKit/SenTestingKit.h>
#import <SBJson/SBJson.h>
+#import "JsonTestCase.h"
+
@interface StreamParserIntegrationTest : SenTestCase < SBJsonStreamParserAdapterDelegate> {
SBJsonStreamParser *parser;
SBJsonStreamParserAdapter *adapter;
@@ -55,7 +57,8 @@ - (void)setUp {
arrayCount = objectCount = 0u;
- path = @"Tests/Stream";
+ NSString *suite = @"Tests/Stream/";
+ path = [JsonTestCase pathForSuite:suite];
files = [[NSFileManager defaultManager] enumeratorAtPath:path];
}
@@ -79,6 +82,11 @@ - (void)testMultipleDocuments {
while ((fileName = [files nextObject])) {
NSString *file = [path stringByAppendingPathComponent:fileName];
+ // Don't accidentally test directories. That would be bad.
+ BOOL isDir = NO;
+ if (NO == [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDir] || YES == isDir)
+ continue;
+
NSData *data = [NSData dataWithContentsOfMappedFile:file];
STAssertNotNil(data, nil);
Please sign in to comment.
Something went wrong with that request. Please try again.