PageRenderTime 110ms CodeModel.GetById 53ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 129 lines | 109 code | 3 blank | 17 comment | 0 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
 19#import "GTMSenTestCase.h"
 20#import "GTMNSScanner+JSON.h"
 21
 22@interface GTMNSScanner_JSONTest : GTMTestCase
 23@end
 24
 25struct {
 26  NSString *testString_;
 27  NSString *resultString_;
 28  BOOL isObject_;
 29} testStrings[] = {
 30  { @"", nil, NO },
 31  { @"\"Empty String\"", nil, NO },
 32  { @"[\"Unclosed array\"", nil, NO },
 33  { @"[\"escape at end of unfinished string\\", nil, NO },
 34  { @"junk, [\"Unclosed array with junk before\"", nil, NO },
 35  { @"\"Unopened array\"]", nil, NO },
 36  { @"\"Unopened array with junk after\"] junk", nil, NO },
 37  { @"[\"array\"]", @"[\"array\"]", NO },
 38  { @"junk [\"array with junk\"]", @"[\"array with junk\"]", NO },
 39  { @"[\"array with junk\"], junk", @"[\"array with junk\"]", NO },
 40  { @"[[[\"nested array\"]]]", @"[[[\"nested array\"]]]", NO },
 41  { @"[[[\"badly nested array\"]]", nil, NO },
 42  { @"[[[\"over nested array\"]]]]", @"[[[\"over nested array\"]]]", NO },
 43  { @"[{]", @"[{]", NO },
 44  { @"[\"closer in quotes\":\"]\"]", @"[\"closer in quotes\":\"]\"]", NO },
 45  { @"[\"escaped closer\":\\]]", @"[\"escaped closer\":\\]", NO },
 46  { @"[\"double escape\":\\\\]", @"[\"double escape\":\\\\]", NO },
 47  { @"[\"doub esc quote\":\"\\\"]\"]", @"[\"doub esc quote\":\"\\\"]\"]", NO },
 48  { @"[\"opener in quotes\":\"[\"]", @"[\"opener in quotes\":\"[\"]", NO },
 49  { @"[\"escaped opener\":\\[]", nil, NO },
 50  { @"[\"escaped opener\":\\[]]", @"[\"escaped opener\":\\[]]", NO },
 51  { @"[\"doub esc quote\":\"\\\"[\"]", @"[\"doub esc quote\":\"\\\"[\"]", NO },
 52  { @"{\"Unclosed object\"", nil, YES },
 53  { @"junk, {\"Unclosed object with junk before\"", nil, YES },
 54  { @"\"Unopened object\"}", nil, YES },
 55  { @"\"Unopened object with junk after\"} junk", nil, YES },
 56  { @"{\"object\"}", @"{\"object\"}", YES },
 57  { @"junk, {\"object with junk\"}", @"{\"object with junk\"}", YES },
 58  { @"{\"object with junk\"}, junk", @"{\"object with junk\"}", YES },
 59  { @"{{{\"nested object\"}}}", @"{{{\"nested object\"}}}", YES },
 60  { @"{{{\"badly nested object\"}}", nil, YES },
 61  { @"{{{\"over nested object\"}}}}", @"{{{\"over nested object\"}}}", YES },
 62  { @"{[}", @"{[}", YES },
 63  { @"{\"closer in quotes\":\"}\"}", @"{\"closer in quotes\":\"}\"}", YES },
 64  { @"{\"escaped closer\":\\}}", @"{\"escaped closer\":\\}", YES },
 65  { @"{\"double escape\":\\\\}", @"{\"double escape\":\\\\}", YES },
 66  { @"{\"doub esc quote\":\"\\\"}\"}", @"{\"doub esc quote\":\"\\\"}\"}", YES },
 67  { @"{\"opener in quotes\":\"{\"}", @"{\"opener in quotes\":\"{\"}", YES },
 68  { @"{\"escaped opener\":\\{}", nil, YES },
 69  { @"{\"escaped opener\":\\{}}", @"{\"escaped opener\":\\{}}", YES },
 70  { @"{\"doub esc quote\":\"\\\"{\"}", @"{\"doub esc quote\":\"\\\"{\"}", YES },
 71};
 72
 73@implementation GTMNSScanner_JSONTest
 74
 75- (void)testJSONObject {
 76  NSCharacterSet *set = [[NSCharacterSet illegalCharacterSet] invertedSet];
 77  for (size_t i = 0; i < sizeof(testStrings) / sizeof(testStrings[0]); ++i) {
 78    NSScanner *scanner
 79      = [NSScanner scannerWithString:testStrings[i].testString_];
 80    [scanner setCharactersToBeSkipped:set];
 81    NSString *array = nil;
 82    BOOL goodArray = [scanner gtm_scanJSONArrayString:&array];
 83    scanner = [NSScanner scannerWithString:testStrings[i].testString_];
 84    [scanner setCharactersToBeSkipped:set];
 85    NSString *object = nil;
 86    BOOL goodObject = [scanner gtm_scanJSONObjectString:&object];
 87    if (testStrings[i].resultString_) {
 88      if (testStrings[i].isObject_) {
 89        STAssertEqualStrings(testStrings[i].resultString_,
 90                             object, @"Test String: %@",
 91                             testStrings[i].testString_);
 92        STAssertNil(array, @"Test String: %@", testStrings[i].testString_);
 93        STAssertTrue(goodObject, @"Test String: %@",
 94                     testStrings[i].testString_);
 95        STAssertFalse(goodArray, @"Test String: %@",
 96                      testStrings[i].testString_);
 97      } else {
 98        STAssertEqualStrings(testStrings[i].resultString_, array,
 99                             @"Test String: %@", testStrings[i].testString_);
100        STAssertNil(object, @"Test String: %@", testStrings[i].testString_);
101        STAssertTrue(goodArray, @"Test String: %@", testStrings[i].testString_);
102        STAssertFalse(goodObject, @"Test String: %@",
103                      testStrings[i].testString_);
104      }
105    } else {
106      STAssertNil(object, @"Test String: %@", testStrings[i].testString_);
107      STAssertNil(array, @"Test String: %@", testStrings[i].testString_);
108      STAssertFalse(goodArray, @"Test String: %@", testStrings[i].testString_);
109      STAssertFalse(goodObject, @"Test String: %@", testStrings[i].testString_);
110    }
111  }
112}
113
114- (void)testScanningCharacters {
115  NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
116  NSString *testString = @"asdfasdf[]:,";
117  NSScanner *scanner = [NSScanner scannerWithString:testString];
118  [scanner setCharactersToBeSkipped:alphaSet];
119  NSString *array = nil;
120  STAssertTrue([scanner gtm_scanJSONArrayString:&array], nil);
121  STAssertEqualStrings(array, @"[]", nil);
122  NSString *nextValue = nil;
123  STAssertTrue([scanner scanString:@":," intoString:&nextValue], nil);
124  STAssertEqualStrings(@":,", nextValue, nil);
125  scanner = [NSScanner scannerWithString:testString];
126  STAssertFalse([scanner gtm_scanJSONArrayString:&array], nil);
127}
128
129@end