mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 12:27:52 +08:00
Reorganization and cleanups of Unit Tests
* Reorganized tests to accommodate split into Logic & Application. * Eliminated 'Spec' naming in favor of 'Test' as the suite is entirely based on SenTest. * Pulled majority of testing support classes up into the library and documented them. * Introduced RKApplicationTests app for running the RKTableController UI tests
This commit is contained in:
78
Code/Support/NSBundle+RKAdditions.h
Normal file
78
Code/Support/NSBundle+RKAdditions.h
Normal file
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// NSBundle+RKAdditions.h
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 2/1/12.
|
||||
// Copyright (c) 2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIImage.h>
|
||||
|
||||
/**
|
||||
Provides convenience methods for accessing data in resources
|
||||
within an NSBundle.
|
||||
*/
|
||||
@interface NSBundle (RKAdditions)
|
||||
|
||||
/**
|
||||
Returns the MIME Type for the resource identified by the specified name and file extension.
|
||||
|
||||
@param name The name of the resource file.
|
||||
@param extension If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
|
||||
@return The MIME Type for the resource file or nil if the file could not be located.
|
||||
*/
|
||||
- (NSString *)MIMETypeForResource:(NSString *)name withExtension:(NSString *)extension;
|
||||
|
||||
/**
|
||||
Creates and returns a data object by reading every byte from the resource identified by the specified name and file extension.
|
||||
|
||||
@param name The name of the resource file.
|
||||
@param extension If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
|
||||
@return A data object by reading every byte from the resource file.
|
||||
*/
|
||||
- (NSData *)dataWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension;
|
||||
|
||||
/**
|
||||
Creates and returns a string object by reading data from the resource identified by the specified name and file extension using a given encoding.
|
||||
|
||||
@param name The name of the resource file.
|
||||
@param extension If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
|
||||
@param encoding The encoding of the resource file.
|
||||
@return A string created by reading data from the resource file using the encoding.
|
||||
*/
|
||||
- (NSString *)stringWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension encoding:(NSStringEncoding)encoding;
|
||||
|
||||
/**
|
||||
Creates and returns an image object by loading the image data from the resource identified by the specified name and file extension.
|
||||
|
||||
@param name The name of the resource file.
|
||||
@param extension If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
|
||||
@return A new image object for the specified file, or nil if the method could not initialize the image from the specified file.
|
||||
*/
|
||||
- (UIImage *)imageWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension;
|
||||
|
||||
/**
|
||||
Creates and returns an object representation of the data from the resource identified by the specified name and file extension by reading the
|
||||
data as a string and parsing it using a parser appropriate for the MIME Type of the file.
|
||||
|
||||
@param name The name of the resource file.
|
||||
@param extension If extension is an empty string or nil, the extension is assumed not to exist and the file is the first file encountered that exactly matches name.
|
||||
@return A new image object for the specified file, or nil if the method could not initialize the image from the specified file.
|
||||
@see RKParserRegistry
|
||||
*/
|
||||
- (id)parsedObjectWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension;
|
||||
|
||||
@end
|
||||
93
Code/Support/NSBundle+RKAdditions.m
Normal file
93
Code/Support/NSBundle+RKAdditions.m
Normal file
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// NSBundle+RKAdditions.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Blake Watters on 2/1/12.
|
||||
// Copyright (c) 2012 RestKit. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#import "NSBundle+RKAdditions.h"
|
||||
#import "NSString+RestKit.h"
|
||||
#import "RKLog.h"
|
||||
#import "RKParser.h"
|
||||
#import "RKParserRegistry.h"
|
||||
|
||||
@implementation NSBundle (RKAdditions)
|
||||
|
||||
- (NSString *)MIMETypeForResource:(NSString *)name withExtension:(NSString *)extension {
|
||||
NSString *resourcePath = [self pathForResource:name ofType:extension];
|
||||
if (resourcePath) {
|
||||
return [resourcePath MIMETypeForPathExtension];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSData *)dataWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension {
|
||||
NSString *resourcePath = [self pathForResource:name ofType:extension];
|
||||
if (! resourcePath) {
|
||||
RKLogWarning(@"%@ Failed to locate Resource with name '%@' and extension '%@': File Not Found.", self, resourcePath, extension);
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [NSData dataWithContentsOfFile:resourcePath];
|
||||
}
|
||||
|
||||
- (NSString *)stringWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension encoding:(NSStringEncoding)encoding {
|
||||
NSError* error = nil;
|
||||
NSString *resourcePath = [self pathForResource:name ofType:extension];
|
||||
if (! resourcePath) {
|
||||
RKLogWarning(@"%@ Failed to locate Resource with name '%@' and extension '%@': File Not Found.", self, resourcePath, extension);
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSString* fixtureData = [NSString stringWithContentsOfFile:resourcePath encoding:encoding error:&error];
|
||||
if (fixtureData == nil && error) {
|
||||
RKLogWarning(@"Failed to read ");
|
||||
}
|
||||
|
||||
return fixtureData;
|
||||
}
|
||||
|
||||
- (UIImage *)imageWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension {
|
||||
NSString *resourcePath = [self pathForResource:name ofType:extension];
|
||||
if (! resourcePath) {
|
||||
RKLogWarning(@"%@ Failed to locate Resource with name '%@' and extension '%@': File Not Found.", self, resourcePath, extension);
|
||||
return nil;
|
||||
}
|
||||
|
||||
return [UIImage imageWithContentsOfFile:resourcePath];
|
||||
}
|
||||
|
||||
- (id)parsedObjectWithContentsOfResource:(NSString *)name withExtension:(NSString *)extension {
|
||||
NSError* error = nil;
|
||||
NSString* resourceContents = [self stringWithContentsOfResource:name withExtension:extension encoding:NSUTF8StringEncoding];
|
||||
NSString* MIMEType = [self MIMETypeForResource:name withExtension:extension];
|
||||
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
|
||||
if (! parser) {
|
||||
RKLogError(@"%@ Unable to parse Resource with name '%@' and extension '%@': failed to find parser registered to handle MIME Type '%@'", self, name, extension, MIMEType);
|
||||
return nil;
|
||||
}
|
||||
|
||||
id object = [parser objectFromString:resourceContents error:&error];
|
||||
if (object == nil) {
|
||||
RKLogCritical(@"%@ Failed to parse resource with name '%@' and extension '%@'. Error: %@", self, name, extension, [error localizedDescription]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -135,8 +135,13 @@ RK_FIX_CATEGORY_BUG(NSString_RestKit)
|
||||
return [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
- (NSDictionary *)fileExtensionsToMIMETypesDictionary {
|
||||
return [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"json", nil];
|
||||
}
|
||||
|
||||
- (NSString *)MIMETypeForPathExtension {
|
||||
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[self pathExtension], NULL);
|
||||
NSString *fileExtension = [self pathExtension];
|
||||
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef) fileExtension, NULL);
|
||||
if (uti != NULL) {
|
||||
CFStringRef mime = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
|
||||
CFRelease(uti);
|
||||
@@ -146,8 +151,9 @@ RK_FIX_CATEGORY_BUG(NSString_RestKit)
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
|
||||
// Consult our internal dictionary of mappings if not found
|
||||
return [[self fileExtensionsToMIMETypesDictionary] valueForKey:fileExtension];
|
||||
}
|
||||
|
||||
- (BOOL)isIPAddress {
|
||||
|
||||
@@ -61,4 +61,5 @@ _lcl_component(RestKitSupport, "restkit.support",
|
||||
_lcl_component(RestKitSupportParsers, "restkit.support.parsers", "RestKit/Support/Parsers")
|
||||
_lcl_component(RestKitThree20, "restkit.three20", "RestKit/Three20")
|
||||
_lcl_component(RestKitUI, "restkit.ui", "RestKit/UI")
|
||||
_lcl_component(RestKitTesting, "restkit.testing", "RestKit/Testing")
|
||||
_lcl_component(App, "app", "App")
|
||||
|
||||
Reference in New Issue
Block a user