PageRenderTime 64ms CodeModel.GetById 20ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMNSString+XMLTest.m

http://macfuse.googlecode.com/
Objective C | 93 lines | 54 code | 16 blank | 23 comment | 0 complexity | 07df739d9d212df78907f4ad8f9ce0e9 MD5 | raw file
 1//
 2//  GTMNSString+XMLTest.m
 3//
 4//  Copyright 2007-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
20#import "GTMSenTestCase.h"
21#import "GTMNSString+XML.h"
22
23
24@interface GTMNSString_XMLTest : GTMTestCase
25@end
26
27@implementation GTMNSString_XMLTest
28
29- (void)testStringBySanitizingAndEscapingForXML {
30  // test the substitutions cases
31  UniChar chars[] = {
32    'z', 0, 'z', 1, 'z', 4, 'z', 5, 'z', 34, 'z', 38, 'z', 39, 'z',
33    60, 'z', 62, 'z', ' ', 'z', 0xd800, 'z', 0xDFFF, 'z', 0xE000,
34    'z', 0xFFFE, 'z', 0xFFFF, 'z', '\n', 'z', '\r', 'z', '\t', 'z' };
35
36  NSString *string1 = [NSString stringWithCharacters:chars
37                                              length:sizeof(chars) / sizeof(UniChar)];
38  NSString *string2 =
39   [NSString stringWithFormat:@"zzzzz"z&z'z<z>z zzz%Czzz\nz\rz\tz",
40    (unsigned short)0xE000];
41
42  STAssertEqualObjects([string1 gtm_stringBySanitizingAndEscapingForXML],
43                       string2,
44                       @"Sanitize and Escape for XML failed");
45  
46  // force the backing store of the NSString to test extraction paths
47  char ascBuffer[] = "a\01bcde\nf";
48  NSString *ascString =
49    [[[NSString alloc] initWithBytesNoCopy:ascBuffer
50                                    length:sizeof(ascBuffer) / sizeof(char)
51                                  encoding:NSASCIIStringEncoding
52                              freeWhenDone:NO] autorelease];
53  STAssertEqualObjects([ascString gtm_stringBySanitizingAndEscapingForXML],
54                       @"abcde\nf",
55                       @"Sanitize and Escape for XML from asc buffer failed");
56
57  // test empty string
58  STAssertEqualObjects([@"" gtm_stringBySanitizingAndEscapingForXML], @"", nil);
59}
60
61- (void)testStringBySanitizingToXMLSpec {
62  // test the substitutions cases
63  UniChar chars[] = {
64    'z', 0, 'z', 1, 'z', 4, 'z', 5, 'z', 34, 'z', 38, 'z', 39, 'z',
65    60, 'z', 62, 'z', ' ', 'z', 0xd800, 'z', 0xDFFF, 'z', 0xE000,
66    'z', 0xFFFE, 'z', 0xFFFF, 'z', '\n', 'z', '\r', 'z', '\t', 'z' };
67  
68  NSString *string1 = [NSString stringWithCharacters:chars
69                                              length:sizeof(chars) / sizeof(UniChar)];
70  NSString *string2 =
71    [NSString stringWithFormat:@"zzzzz\"z&z'z<z>z zzz%Czzz\nz\rz\tz",
72     (unsigned short)0xE000];
73  
74  STAssertEqualObjects([string1 gtm_stringBySanitizingToXMLSpec],
75                       string2,
76                       @"Sanitize for XML failed");
77
78  // force the backing store of the NSString to test extraction paths
79  char ascBuffer[] = "a\01bcde\nf";
80  NSString *ascString =
81  [[[NSString alloc] initWithBytesNoCopy:ascBuffer
82                                  length:sizeof(ascBuffer) / sizeof(char)
83                                encoding:NSASCIIStringEncoding
84                            freeWhenDone:NO] autorelease];
85  STAssertEqualObjects([ascString gtm_stringBySanitizingToXMLSpec],
86                       @"abcde\nf",
87                       @"Sanitize and Escape for XML from asc buffer failed");
88
89  // test empty string
90  STAssertEqualObjects([@"" gtm_stringBySanitizingToXMLSpec], @"", nil);
91}
92
93@end