/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. #import "GTMURITemplate.h"
  16. #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5
  17. #import "GTMSenTestCase.h"
  18. #import "GTMScriptRunner.h"
  19. @interface GTMURITemplateTest : GTMTestCase
  20. - (NSDictionary *)loadTestSuitesNamed:(NSString *)testSuitesName;
  21. - (NSDictionary *)parseJSONString:(NSString *)json error:(NSError **)error;
  22. - (void)runTestSuites:(NSDictionary *)testSuites;
  23. @end
  24. @implementation GTMURITemplateTest
  25. - (NSDictionary *)parseJSONString:(NSString *)json error:(NSError **)error {
  26. NSDictionary *result = nil;
  27. // If we ever get a JSON parser in GTM (or the system gets one, next cat?),
  28. // then we can skip this conversion dance.
  29. NSString *fileName = [NSString stringWithFormat:@"URITemplate_%u.plist", arc4random()];
  30. NSString *tempOutPath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  31. GTMScriptRunner *runner = [GTMScriptRunner runnerWithPython];
  32. NSString *command = [NSString stringWithFormat:
  33. @"import Foundation\n"
  34. @"import json\n"
  35. @"str_of_json = \"\"\"%@\"\"\"\n"
  36. @"Foundation.NSDictionary.dictionaryWithDictionary_(json.loads(str_of_json)).writeToFile_atomically_('%@', True)\n",
  37. json, tempOutPath];
  38. NSString *errStr = nil;
  39. NSString *outStr = [runner run:command standardError:&errStr];
  40. STAssertNil(outStr, @"got something on stdout: %@", outStr);
  41. STAssertNil(errStr, @"got something on stderr: %@", errStr);
  42. result = [NSDictionary dictionaryWithContentsOfFile:tempOutPath];
  43. [[NSFileManager defaultManager] removeItemAtPath:tempOutPath
  44. error:NULL];
  45. return result;
  46. }
  47. - (NSDictionary *)loadTestSuitesNamed:(NSString *)testSuitesName {
  48. NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
  49. STAssertNotNil(testBundle, nil);
  50. NSString *testSuitesPath = [testBundle pathForResource:testSuitesName
  51. ofType:nil];
  52. STAssertNotNil(testSuitesPath, @"%@ not found", testSuitesName);
  53. NSError *error = nil;
  54. NSString *testSuitesStr = [NSString stringWithContentsOfFile:testSuitesPath
  55. encoding:NSUTF8StringEncoding
  56. error:&error];
  57. STAssertNil(error, @"Loading %@, error %@", testSuitesName, error);
  58. STAssertNotNil(testSuitesStr, @"Loading %@", testSuitesName);
  59. NSDictionary *testSuites = [self parseJSONString:testSuitesStr
  60. error:&error];
  61. STAssertNil(error, @"Parsing %@, error %@", testSuitesName, error);
  62. STAssertNotNil(testSuites, @"failed to parse");
  63. return testSuites;
  64. }
  65. - (void)runTestSuites:(NSDictionary *)testSuites {
  66. // The file holds a set of named suites...
  67. for (NSString *suiteName in testSuites) {
  68. NSDictionary *suite = [testSuites objectForKey:suiteName];
  69. // Each suite has variables and test cases...
  70. NSDictionary *vars = [suite objectForKey:@"variables"];
  71. NSArray *testCases = [suite objectForKey:@"testcases"];
  72. STAssertTrue([vars count] != 0, @"'%@' no variables?", suiteName);
  73. STAssertTrue([testCases count] != 0, @"'%@' no testcases?", suiteName);
  74. NSUInteger idx = 0;
  75. for (NSArray *testCase in testCases) {
  76. // Each case is an array of the template and value...
  77. STAssertEquals([testCase count], (NSUInteger)2,
  78. @" test index %lu of '%@'", (unsigned long)idx, suiteName);
  79. NSString *testTemplate = [testCase objectAtIndex:0];
  80. NSString *expectedResult = [testCase objectAtIndex:1];
  81. NSString *result = [GTMURITemplate expandTemplate:testTemplate
  82. values:vars];
  83. STAssertEqualObjects(result, expectedResult,
  84. @"template was '%@' (index %lu of '%@')",
  85. testTemplate, (unsigned long)idx, suiteName);
  86. ++idx;
  87. }
  88. }
  89. }
  90. - (void)testRFCSuite {
  91. // All of the examples from the RFC are in the python impl source as json
  92. // test data. A copy is in the GTM tree as GTMURITemplateJSON.txt. The
  93. // original can be found at:
  94. // http://code.google.com/p/uri-templates/source/browse/trunk/testdata.json
  95. NSDictionary *testSuites = [self loadTestSuitesNamed:@"GTMURITemplateRFCTests.json"];
  96. STAssertNotNil(testSuites, nil);
  97. [self runTestSuites:testSuites];
  98. }
  99. - (void)testExtraSuite {
  100. // These are follow up cases not explictly listed in the spec, but does
  101. // as cases to confirm behaviors. The list was sent to the w3c uri list
  102. // for confirmation:
  103. // http://lists.w3.org/Archives/Public/uri/2010Sep/thread.html
  104. NSDictionary *testSuites = [self loadTestSuitesNamed:@"GTMURITemplateExtraTests.json"];
  105. STAssertNotNil(testSuites, nil);
  106. [self runTestSuites:testSuites];
  107. }
  108. @end
  109. #endif // MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5