/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSScanner+JSONTest.m

http://macfuse.googlecode.com/ · Objective C · 129 lines · 105 code · 7 blank · 17 comment · 5 complexity · 237f0d9f7c3514665801bebc8d1d78dc MD5 · raw file

  1. //
  2. // GTMNSScanner+JSONTest.m
  3. //
  4. // Copyright 2005-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMNSScanner+JSON.h"
  20. @interface GTMNSScanner_JSONTest : GTMTestCase
  21. @end
  22. struct {
  23. NSString *testString_;
  24. NSString *resultString_;
  25. BOOL isObject_;
  26. } testStrings[] = {
  27. { @"", nil, NO },
  28. { @"\"Empty String\"", nil, NO },
  29. { @"[\"Unclosed array\"", nil, NO },
  30. { @"[\"escape at end of unfinished string\\", nil, NO },
  31. { @"junk, [\"Unclosed array with junk before\"", nil, NO },
  32. { @"\"Unopened array\"]", nil, NO },
  33. { @"\"Unopened array with junk after\"] junk", nil, NO },
  34. { @"[\"array\"]", @"[\"array\"]", NO },
  35. { @"junk [\"array with junk\"]", @"[\"array with junk\"]", NO },
  36. { @"[\"array with junk\"], junk", @"[\"array with junk\"]", NO },
  37. { @"[[[\"nested array\"]]]", @"[[[\"nested array\"]]]", NO },
  38. { @"[[[\"badly nested array\"]]", nil, NO },
  39. { @"[[[\"over nested array\"]]]]", @"[[[\"over nested array\"]]]", NO },
  40. { @"[{]", @"[{]", NO },
  41. { @"[\"closer in quotes\":\"]\"]", @"[\"closer in quotes\":\"]\"]", NO },
  42. { @"[\"escaped closer\":\\]]", @"[\"escaped closer\":\\]", NO },
  43. { @"[\"double escape\":\\\\]", @"[\"double escape\":\\\\]", NO },
  44. { @"[\"doub esc quote\":\"\\\"]\"]", @"[\"doub esc quote\":\"\\\"]\"]", NO },
  45. { @"[\"opener in quotes\":\"[\"]", @"[\"opener in quotes\":\"[\"]", NO },
  46. { @"[\"escaped opener\":\\[]", nil, NO },
  47. { @"[\"escaped opener\":\\[]]", @"[\"escaped opener\":\\[]]", NO },
  48. { @"[\"doub esc quote\":\"\\\"[\"]", @"[\"doub esc quote\":\"\\\"[\"]", NO },
  49. { @"{\"Unclosed object\"", nil, YES },
  50. { @"junk, {\"Unclosed object with junk before\"", nil, YES },
  51. { @"\"Unopened object\"}", nil, YES },
  52. { @"\"Unopened object with junk after\"} junk", nil, YES },
  53. { @"{\"object\"}", @"{\"object\"}", YES },
  54. { @"junk, {\"object with junk\"}", @"{\"object with junk\"}", YES },
  55. { @"{\"object with junk\"}, junk", @"{\"object with junk\"}", YES },
  56. { @"{{{\"nested object\"}}}", @"{{{\"nested object\"}}}", YES },
  57. { @"{{{\"badly nested object\"}}", nil, YES },
  58. { @"{{{\"over nested object\"}}}}", @"{{{\"over nested object\"}}}", YES },
  59. { @"{[}", @"{[}", YES },
  60. { @"{\"closer in quotes\":\"}\"}", @"{\"closer in quotes\":\"}\"}", YES },
  61. { @"{\"escaped closer\":\\}}", @"{\"escaped closer\":\\}", YES },
  62. { @"{\"double escape\":\\\\}", @"{\"double escape\":\\\\}", YES },
  63. { @"{\"doub esc quote\":\"\\\"}\"}", @"{\"doub esc quote\":\"\\\"}\"}", YES },
  64. { @"{\"opener in quotes\":\"{\"}", @"{\"opener in quotes\":\"{\"}", YES },
  65. { @"{\"escaped opener\":\\{}", nil, YES },
  66. { @"{\"escaped opener\":\\{}}", @"{\"escaped opener\":\\{}}", YES },
  67. { @"{\"doub esc quote\":\"\\\"{\"}", @"{\"doub esc quote\":\"\\\"{\"}", YES },
  68. };
  69. @implementation GTMNSScanner_JSONTest
  70. - (void)testJSONObject {
  71. NSCharacterSet *set = [[NSCharacterSet illegalCharacterSet] invertedSet];
  72. for (size_t i = 0; i < sizeof(testStrings) / sizeof(testStrings[0]); ++i) {
  73. NSScanner *scanner
  74. = [NSScanner scannerWithString:testStrings[i].testString_];
  75. [scanner setCharactersToBeSkipped:set];
  76. NSString *array = nil;
  77. BOOL goodArray = [scanner gtm_scanJSONArrayString:&array];
  78. scanner = [NSScanner scannerWithString:testStrings[i].testString_];
  79. [scanner setCharactersToBeSkipped:set];
  80. NSString *object = nil;
  81. BOOL goodObject = [scanner gtm_scanJSONObjectString:&object];
  82. if (testStrings[i].resultString_) {
  83. if (testStrings[i].isObject_) {
  84. STAssertEqualStrings(testStrings[i].resultString_,
  85. object, @"Test String: %@",
  86. testStrings[i].testString_);
  87. STAssertNil(array, @"Test String: %@", testStrings[i].testString_);
  88. STAssertTrue(goodObject, @"Test String: %@",
  89. testStrings[i].testString_);
  90. STAssertFalse(goodArray, @"Test String: %@",
  91. testStrings[i].testString_);
  92. } else {
  93. STAssertEqualStrings(testStrings[i].resultString_, array,
  94. @"Test String: %@", testStrings[i].testString_);
  95. STAssertNil(object, @"Test String: %@", testStrings[i].testString_);
  96. STAssertTrue(goodArray, @"Test String: %@", testStrings[i].testString_);
  97. STAssertFalse(goodObject, @"Test String: %@",
  98. testStrings[i].testString_);
  99. }
  100. } else {
  101. STAssertNil(object, @"Test String: %@", testStrings[i].testString_);
  102. STAssertNil(array, @"Test String: %@", testStrings[i].testString_);
  103. STAssertFalse(goodArray, @"Test String: %@", testStrings[i].testString_);
  104. STAssertFalse(goodObject, @"Test String: %@", testStrings[i].testString_);
  105. }
  106. }
  107. }
  108. - (void)testScanningCharacters {
  109. NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
  110. NSString *testString = @"asdfasdf[]:,";
  111. NSScanner *scanner = [NSScanner scannerWithString:testString];
  112. [scanner setCharactersToBeSkipped:alphaSet];
  113. NSString *array = nil;
  114. STAssertTrue([scanner gtm_scanJSONArrayString:&array], nil);
  115. STAssertEqualStrings(array, @"[]", nil);
  116. NSString *nextValue = nil;
  117. STAssertTrue([scanner scanString:@":," intoString:&nextValue], nil);
  118. STAssertEqualStrings(@":,", nextValue, nil);
  119. scanner = [NSScanner scannerWithString:testString];
  120. STAssertFalse([scanner gtm_scanJSONArrayString:&array], nil);
  121. }
  122. @end