PageRenderTime 81ms CodeModel.GetById 28ms app.highlight 49ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMURITemplateTest.m

http://macfuse.googlecode.com/
Objective C | 133 lines | 82 code | 24 blank | 27 comment | 4 complexity | 34a0d785eda7824f33197ba1240a46f4 MD5 | raw file
  1/* Copyright (c) 2010 Google Inc.
  2 *
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 *
  7 *     http://www.apache.org/licenses/LICENSE-2.0
  8 *
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS,
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License.
 14 */
 15
 16#import "GTMURITemplate.h"
 17
 18#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
 19
 20#import "GTMSenTestCase.h"
 21#import "GTMScriptRunner.h"
 22
 23@interface GTMURITemplateTest : GTMTestCase
 24- (NSDictionary *)loadTestSuitesNamed:(NSString *)testSuitesName;
 25- (NSDictionary *)parseJSONString:(NSString *)json error:(NSError **)error;
 26- (void)runTestSuites:(NSDictionary *)testSuites;
 27@end
 28
 29@implementation GTMURITemplateTest
 30
 31- (NSDictionary *)parseJSONString:(NSString *)json error:(NSError **)error {
 32  NSDictionary *result = nil;
 33
 34  // If we ever get a JSON parser in GTM (or the system gets one, next cat?),
 35  // then we can skip this conversion dance.
 36
 37  NSString *fileName = [NSString stringWithFormat:@"URITemplate_%u.plist", arc4random()];
 38  NSString *tempOutPath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
 39
 40  GTMScriptRunner *runner = [GTMScriptRunner runnerWithPython];
 41  NSString *command = [NSString stringWithFormat:
 42                       @"import Foundation\n"
 43                       @"import json\n"
 44                       @"str_of_json = \"\"\"%@\"\"\"\n"
 45                       @"Foundation.NSDictionary.dictionaryWithDictionary_(json.loads(str_of_json)).writeToFile_atomically_('%@', True)\n",
 46                       json, tempOutPath];
 47  NSString *errStr = nil;
 48  NSString *outStr = [runner run:command standardError:&errStr];
 49
 50  STAssertNil(outStr, @"got something on stdout: %@", outStr);
 51  STAssertNil(errStr, @"got something on stderr: %@", errStr);
 52  result = [NSDictionary dictionaryWithContentsOfFile:tempOutPath];
 53
 54  [[NSFileManager defaultManager] removeItemAtPath:tempOutPath
 55                                             error:NULL];
 56
 57  return result;
 58}
 59
 60- (NSDictionary *)loadTestSuitesNamed:(NSString *)testSuitesName {
 61  NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
 62  STAssertNotNil(testBundle, nil);
 63
 64  NSString *testSuitesPath = [testBundle pathForResource:testSuitesName
 65                                                  ofType:nil];
 66  STAssertNotNil(testSuitesPath, @"%@ not found", testSuitesName);
 67
 68  NSError *error = nil;
 69  NSString *testSuitesStr = [NSString stringWithContentsOfFile:testSuitesPath
 70                                                      encoding:NSUTF8StringEncoding
 71                                                         error:&error];
 72  STAssertNil(error, @"Loading %@, error %@", testSuitesName, error);
 73  STAssertNotNil(testSuitesStr, @"Loading %@", testSuitesName);
 74
 75  NSDictionary *testSuites = [self parseJSONString:testSuitesStr
 76                                             error:&error];
 77  STAssertNil(error, @"Parsing %@, error %@", testSuitesName, error);
 78  STAssertNotNil(testSuites, @"failed to parse");
 79
 80  return testSuites;
 81}
 82
 83- (void)runTestSuites:(NSDictionary *)testSuites {
 84  // The file holds a set of named suites...
 85  for (NSString *suiteName in testSuites) {
 86    NSDictionary *suite = [testSuites objectForKey:suiteName];
 87    // Each suite has variables and test cases...
 88    NSDictionary *vars = [suite objectForKey:@"variables"];
 89    NSArray *testCases = [suite objectForKey:@"testcases"];
 90    STAssertTrue([vars count] != 0, @"'%@' no variables?", suiteName);
 91    STAssertTrue([testCases count] != 0, @"'%@' no testcases?", suiteName);
 92    NSUInteger idx = 0;
 93    for (NSArray *testCase in testCases) {
 94      // Each case is an array of the template and value...
 95      STAssertEquals([testCase count], (NSUInteger)2,
 96                     @" test index %lu of '%@'", (unsigned long)idx, suiteName);
 97
 98      NSString *testTemplate = [testCase objectAtIndex:0];
 99      NSString *expectedResult = [testCase objectAtIndex:1];
100
101      NSString *result = [GTMURITemplate expandTemplate:testTemplate
102                                                 values:vars];
103      STAssertEqualObjects(result, expectedResult,
104                           @"template was '%@' (index %lu of '%@')",
105                           testTemplate, (unsigned long)idx, suiteName);
106      ++idx;
107    }
108  }
109}
110
111- (void)testRFCSuite {
112  // All of the examples from the RFC are in the python impl source as json
113  // test data.  A copy is in the GTM tree as GTMURITemplateJSON.txt.  The
114  // original can be found at:
115  // http://code.google.com/p/uri-templates/source/browse/trunk/testdata.json
116  NSDictionary *testSuites = [self loadTestSuitesNamed:@"GTMURITemplateRFCTests.json"];
117  STAssertNotNil(testSuites, nil);
118  [self runTestSuites:testSuites];
119}
120
121- (void)testExtraSuite {
122  // These are follow up cases not explictly listed in the spec, but does
123  // as cases to confirm behaviors.  The list was sent to the w3c uri list
124  // for confirmation:
125  //   http://lists.w3.org/Archives/Public/uri/2010Sep/thread.html
126  NSDictionary *testSuites = [self loadTestSuitesNamed:@"GTMURITemplateExtraTests.json"];
127  STAssertNotNil(testSuites, nil);
128  [self runTestSuites:testSuites];
129}
130
131@end
132
133#endif  // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5