PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. #import "GTMSenTestCase.h"
  19. #import "GTMNSString+XML.h"
  20. @interface GTMNSString_XMLTest : GTMTestCase
  21. @end
  22. @implementation GTMNSString_XMLTest
  23. - (void)testStringBySanitizingAndEscapingForXML {
  24. // test the substitutions cases
  25. UniChar chars[] = {
  26. 'z', 0, 'z', 1, 'z', 4, 'z', 5, 'z', 34, 'z', 38, 'z', 39, 'z',
  27. 60, 'z', 62, 'z', ' ', 'z', 0xd800, 'z', 0xDFFF, 'z', 0xE000,
  28. 'z', 0xFFFE, 'z', 0xFFFF, 'z', '\n', 'z', '\r', 'z', '\t', 'z' };
  29. NSString *string1 = [NSString stringWithCharacters:chars
  30. length:sizeof(chars) / sizeof(UniChar)];
  31. NSString *string2 =
  32. [NSString stringWithFormat:@"zzzzz"z&z'z<z>z zzz%Czzz\nz\rz\tz",
  33. (unsigned short)0xE000];
  34. STAssertEqualObjects([string1 gtm_stringBySanitizingAndEscapingForXML],
  35. string2,
  36. @"Sanitize and Escape for XML failed");
  37. // force the backing store of the NSString to test extraction paths
  38. char ascBuffer[] = "a\01bcde\nf";
  39. NSString *ascString =
  40. [[[NSString alloc] initWithBytesNoCopy:ascBuffer
  41. length:sizeof(ascBuffer) / sizeof(char)
  42. encoding:NSASCIIStringEncoding
  43. freeWhenDone:NO] autorelease];
  44. STAssertEqualObjects([ascString gtm_stringBySanitizingAndEscapingForXML],
  45. @"abcde\nf",
  46. @"Sanitize and Escape for XML from asc buffer failed");
  47. // test empty string
  48. STAssertEqualObjects([@"" gtm_stringBySanitizingAndEscapingForXML], @"", nil);
  49. }
  50. - (void)testStringBySanitizingToXMLSpec {
  51. // test the substitutions cases
  52. UniChar chars[] = {
  53. 'z', 0, 'z', 1, 'z', 4, 'z', 5, 'z', 34, 'z', 38, 'z', 39, 'z',
  54. 60, 'z', 62, 'z', ' ', 'z', 0xd800, 'z', 0xDFFF, 'z', 0xE000,
  55. 'z', 0xFFFE, 'z', 0xFFFF, 'z', '\n', 'z', '\r', 'z', '\t', 'z' };
  56. NSString *string1 = [NSString stringWithCharacters:chars
  57. length:sizeof(chars) / sizeof(UniChar)];
  58. NSString *string2 =
  59. [NSString stringWithFormat:@"zzzzz\"z&z'z<z>z zzz%Czzz\nz\rz\tz",
  60. (unsigned short)0xE000];
  61. STAssertEqualObjects([string1 gtm_stringBySanitizingToXMLSpec],
  62. string2,
  63. @"Sanitize for XML failed");
  64. // force the backing store of the NSString to test extraction paths
  65. char ascBuffer[] = "a\01bcde\nf";
  66. NSString *ascString =
  67. [[[NSString alloc] initWithBytesNoCopy:ascBuffer
  68. length:sizeof(ascBuffer) / sizeof(char)
  69. encoding:NSASCIIStringEncoding
  70. freeWhenDone:NO] autorelease];
  71. STAssertEqualObjects([ascString gtm_stringBySanitizingToXMLSpec],
  72. @"abcde\nf",
  73. @"Sanitize and Escape for XML from asc buffer failed");
  74. // test empty string
  75. STAssertEqualObjects([@"" gtm_stringBySanitizingToXMLSpec], @"", nil);
  76. }
  77. @end