PageRenderTime 83ms CodeModel.GetById 11ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSString+HTMLTest.m

http://macfuse.googlecode.com/
Objective C | 242 lines | 191 code | 32 blank | 19 comment | 0 complexity | 5bf47d364a2938da6bab5753df1b1f89 MD5 | raw file
  1//
  2//  GTMNSString+HTMLTest.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 "GTMNSString+HTML.h"
 21
 22@interface GTMNSString_HTMLTest : GTMTestCase
 23@end
 24
 25@implementation GTMNSString_HTMLTest
 26
 27- (void)testStringByEscapingHTML {
 28  unichar chars[] =  
 29  { 34, 38, 39, 60, 62, 338, 339, 352, 353, 376, 710, 732,
 30    8194, 8195, 8201, 8204, 8205, 8206, 8207, 8211, 8212, 8216, 8217, 8218, 
 31    8220, 8221, 8222, 8224, 8225, 8240, 8249, 8250, 8364,  };
 32  
 33  NSString *string1 = [NSString stringWithCharacters:chars
 34                                              length:sizeof(chars) / sizeof(unichar)];
 35  NSString *string2 = 
 36    @""&'<>ŒœŠšŸ"
 37     "ˆ˜   ‌‍‎‏–"
 38     "—‘’‚“”„†‡"
 39     "‰‹›€";
 40
 41  STAssertEqualObjects([string1 gtm_stringByEscapingForHTML],
 42                       string2,
 43                       @"HTML escaping failed");
 44  
 45  STAssertEqualObjects([@"<this & that>" gtm_stringByEscapingForHTML],
 46                       @"&lt;this &amp; that&gt;",
 47                       @"HTML escaping failed");
 48  NSString *string = [NSString stringWithUTF8String:"???&????????"];
 49  NSString *escapeStr = [NSString stringWithUTF8String:"???&amp;????????"];
 50  STAssertEqualObjects([string gtm_stringByEscapingForHTML],
 51                       escapeStr,
 52                       @"HTML escaping failed");
 53  
 54  string = [NSString stringWithUTF8String:"abc?1?<?def&"];
 55  STAssertEqualObjects([string gtm_stringByEscapingForHTML],
 56                       [NSString stringWithUTF8String:"abc?1?&lt;?def&amp;"],
 57                       @"HTML escaping failed");
 58  
 59  // test empty string
 60  STAssertEqualObjects([@"" gtm_stringByEscapingForHTML], @"", nil);
 61} // testStringByEscapingHTML
 62
 63- (void)testStringByEscapingAsciiHTML {
 64  unichar chars[] =  
 65  { 34, 38, 39, 60, 62, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
 66    171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 
 67    186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 
 68    201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 
 69    216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 
 70    231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 
 71    246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 338, 339, 352, 353, 376, 
 72    402, 710, 732, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 
 73    925, 926, 927, 928, 929, 931, 932, 933, 934, 935, 936, 937, 945, 946, 947, 
 74    948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 
 75    963, 964, 965, 966, 967, 968, 969, 977, 978, 982, 8194, 8195, 8201, 8204, 
 76    8205, 8206, 8207, 8211, 8212, 8216, 8217, 8218, 8220, 8221, 8222, 8224, 8225, 
 77    8226, 8230, 8240, 8242, 8243, 8249, 8250, 8254, 8260, 8364, 8472, 8465, 8476, 
 78    8482, 8501, 8592, 8593, 8594, 8595, 8596, 8629, 8656, 8657, 8658, 8659, 8660, 
 79    8704, 8706, 8707, 8709, 8711, 8712, 8713, 8715, 8719, 8721, 8722, 8727, 8730, 
 80    8733, 8734, 8736, 8743, 8744, 8745, 8746, 8747, 8756, 8764, 8773, 8776, 8800, 
 81    8801, 8804, 8805, 8834, 8835, 8836, 8838, 8839, 8853, 8855, 8869, 8901, 8968, 
 82    8969, 8970, 8971, 9001, 9002, 9674, 9824, 9827, 9829, 9830 };
 83  
 84  NSString *string1 = [NSString stringWithCharacters:chars
 85                                              length:sizeof(chars) / sizeof(unichar)];
 86  NSString *string2 = 
 87    @"&quot;&amp;&apos;&lt;&gt;&nbsp;&iexcl;&cent;&pound;&curren;&yen;"
 88    "&brvbar;&sect;&uml;&copy;&ordf;&laquo;&not;&shy;&reg;&macr;&deg;"
 89    "&plusmn;&sup2;&sup3;&acute;&micro;&para;&middot;&cedil;&sup1;"
 90    "&ordm;&raquo;&frac14;&frac12;&frac34;&iquest;&Agrave;&Aacute;"
 91    "&Acirc;&Atilde;&Auml;&Aring;&AElig;&Ccedil;&Egrave;&Eacute;"
 92    "&Ecirc;&Euml;&Igrave;&Iacute;&Icirc;&Iuml;&ETH;&Ntilde;&Ograve;"
 93    "&Oacute;&Ocirc;&Otilde;&Ouml;&times;&Oslash;&Ugrave;&Uacute;"
 94    "&Ucirc;&Uuml;&Yacute;&THORN;&szlig;&agrave;&aacute;&acirc;&atilde;"
 95    "&auml;&aring;&aelig;&ccedil;&egrave;&eacute;&ecirc;&euml;&igrave;"
 96    "&iacute;&icirc;&iuml;&eth;&ntilde;&ograve;&oacute;&ocirc;&otilde;"
 97    "&ouml;&divide;&oslash;&ugrave;&uacute;&ucirc;&uuml;&yacute;&thorn;"
 98    "&yuml;&OElig;&oelig;&Scaron;&scaron;&Yuml;&fnof;&circ;&tilde;"
 99    "&Alpha;&Beta;&Gamma;&Delta;&Epsilon;&Zeta;&Eta;&Theta;&Iota;"
100    "&Kappa;&Lambda;&Mu;&Nu;&Xi;&Omicron;&Pi;&Rho;&Sigma;&Tau;"
101    "&Upsilon;&Phi;&Chi;&Psi;&Omega;&alpha;&beta;&gamma;&delta;"
102    "&epsilon;&zeta;&eta;&theta;&iota;&kappa;&lambda;&mu;&nu;&xi;"
103    "&omicron;&pi;&rho;&sigmaf;&sigma;&tau;&upsilon;&phi;&chi;&psi;"
104    "&omega;&thetasym;&upsih;&piv;&ensp;&emsp;&thinsp;&zwnj;&zwj;"
105    "&lrm;&rlm;&ndash;&mdash;&lsquo;&rsquo;&sbquo;&ldquo;&rdquo;"
106    "&bdquo;&dagger;&Dagger;&bull;&hellip;&permil;&prime;&Prime;"
107    "&lsaquo;&rsaquo;&oline;&frasl;&euro;&weierp;&image;&real;&trade;"
108    "&alefsym;&larr;&uarr;&rarr;&darr;&harr;&crarr;&lArr;&uArr;&rArr;"
109    "&dArr;&hArr;&forall;&part;&exist;&empty;&nabla;&isin;&notin;&ni;"
110    "&prod;&sum;&minus;&lowast;&radic;&prop;&infin;&ang;&and;&or;"
111    "&cap;&cup;&int;&there4;&sim;&cong;&asymp;&ne;&equiv;&le;&ge;"
112    "&sub;&sup;&nsub;&sube;&supe;&oplus;&otimes;&perp;&sdot;&lceil;"
113    "&rceil;&lfloor;&rfloor;&lang;&rang;&loz;&spades;&clubs;&hearts;"
114    "&diams;";
115  
116  STAssertEqualObjects([string1 gtm_stringByEscapingForAsciiHTML],
117                       string2,
118                       @"HTML escaping failed");
119  
120  STAssertEqualObjects([@"<this & that>" gtm_stringByEscapingForAsciiHTML],
121                       @"&lt;this &amp; that&gt;",
122                       @"HTML escaping failed");
123  NSString *string = [NSString stringWithUTF8String:"???????????"];
124  STAssertEqualObjects([string gtm_stringByEscapingForAsciiHTML],
125                       @"&#12497;&#12531;&#12539;&#12489;&#12539;&#12459;"
126                       "&#12531;&#12497;&#12540;&#12491;&#12517;",
127                       @"HTML escaping failed");
128  
129  // Mix in some right - to left
130  string = [NSString stringWithUTF8String:"abc?1?<?def&"];
131  STAssertEqualObjects([string gtm_stringByEscapingForAsciiHTML],
132                       @"abc&#1575;1&#1576;&lt;&#1578;def&amp;",
133                       @"HTML escaping failed");
134} // stringByEscapingAsciiHTML
135
136- (void)testStringByUnescapingHTML {
137  NSString *string1 = 
138  @"&quot;&amp;&apos;&lt;&gt;&nbsp;&iexcl;&cent;&pound;&curren;&yen;"
139  "&brvbar;&sect;&uml;&copy;&ordf;&laquo;&not;&shy;&reg;&macr;&deg;"
140  "&plusmn;&sup2;&sup3;&acute;&micro;&para;&middot;&cedil;&sup1;"
141  "&ordm;&raquo;&frac14;&frac12;&frac34;&iquest;&Agrave;&Aacute;"
142  "&Acirc;&Atilde;&Auml;&Aring;&AElig;&Ccedil;&Egrave;&Eacute;"
143  "&Ecirc;&Euml;&Igrave;&Iacute;&Icirc;&Iuml;&ETH;&Ntilde;&Ograve;"
144  "&Oacute;&Ocirc;&Otilde;&Ouml;&times;&Oslash;&Ugrave;&Uacute;"
145  "&Ucirc;&Uuml;&Yacute;&THORN;&szlig;&agrave;&aacute;&acirc;&atilde;"
146  "&auml;&aring;&aelig;&ccedil;&egrave;&eacute;&ecirc;&euml;&igrave;"
147  "&iacute;&icirc;&iuml;&eth;&ntilde;&ograve;&oacute;&ocirc;&otilde;"
148  "&ouml;&divide;&oslash;&ugrave;&uacute;&ucirc;&uuml;&yacute;&thorn;"
149  "&yuml;&OElig;&oelig;&Scaron;&scaron;&Yuml;&fnof;&circ;&tilde;"
150  "&Alpha;&Beta;&Gamma;&Delta;&Epsilon;&Zeta;&Eta;&Theta;&Iota;"
151  "&Kappa;&Lambda;&Mu;&Nu;&Xi;&Omicron;&Pi;&Rho;&Sigma;&Tau;"
152  "&Upsilon;&Phi;&Chi;&Psi;&Omega;&alpha;&beta;&gamma;&delta;"
153  "&epsilon;&zeta;&eta;&theta;&iota;&kappa;&lambda;&mu;&nu;&xi;"
154  "&omicron;&pi;&rho;&sigmaf;&sigma;&tau;&upsilon;&phi;&chi;&psi;"
155  "&omega;&thetasym;&upsih;&piv;&ensp;&emsp;&thinsp;&zwnj;&zwj;"
156  "&lrm;&rlm;&ndash;&mdash;&lsquo;&rsquo;&sbquo;&ldquo;&rdquo;"
157  "&bdquo;&dagger;&Dagger;&bull;&hellip;&permil;&prime;&Prime;"
158  "&lsaquo;&rsaquo;&oline;&frasl;&euro;&weierp;&image;&real;&trade;"
159  "&alefsym;&larr;&uarr;&rarr;&darr;&harr;&crarr;&lArr;&uArr;&rArr;"
160  "&dArr;&hArr;&forall;&part;&exist;&empty;&nabla;&isin;&notin;&ni;"
161  "&prod;&sum;&minus;&lowast;&radic;&prop;&infin;&ang;&and;&or;"
162  "&cap;&cup;&int;&there4;&sim;&cong;&asymp;&ne;&equiv;&le;&ge;"
163  "&sub;&sup;&nsub;&sube;&supe;&oplus;&otimes;&perp;&sdot;&lceil;"
164  "&rceil;&lfloor;&rfloor;&lang;&rang;&loz;&spades;&clubs;&hearts;"
165  "&diams;";
166  
167  unichar chars[] =  
168  { 34, 38, 39, 60, 62, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170,
169    171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 
170    186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 
171    201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 
172    216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 
173    231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 
174    246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 338, 339, 352, 353, 376, 
175    402, 710, 732, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 
176    925, 926, 927, 928, 929, 931, 932, 933, 934, 935, 936, 937, 945, 946, 947, 
177    948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 
178    963, 964, 965, 966, 967, 968, 969, 977, 978, 982, 8194, 8195, 8201, 8204, 
179    8205, 8206, 8207, 8211, 8212, 8216, 8217, 8218, 8220, 8221, 8222, 8224, 8225, 
180    8226, 8230, 8240, 8242, 8243, 8249, 8250, 8254, 8260, 8364, 8472, 8465, 8476, 
181    8482, 8501, 8592, 8593, 8594, 8595, 8596, 8629, 8656, 8657, 8658, 8659, 8660, 
182    8704, 8706, 8707, 8709, 8711, 8712, 8713, 8715, 8719, 8721, 8722, 8727, 8730, 
183    8733, 8734, 8736, 8743, 8744, 8745, 8746, 8747, 8756, 8764, 8773, 8776, 8800, 
184    8801, 8804, 8805, 8834, 8835, 8836, 8838, 8839, 8853, 8855, 8869, 8901, 8968, 
185    8969, 8970, 8971, 9001, 9002, 9674, 9824, 9827, 9829, 9830 };
186  
187  NSString *string2 = [NSString stringWithCharacters:chars
188                                              length:sizeof(chars) / sizeof(unichar)];
189  STAssertEqualObjects([string1 gtm_stringByUnescapingFromHTML],
190                       string2,
191                       @"HTML unescaping failed");
192  
193  STAssertEqualObjects([@"&#65;&#x42;&#X43;" gtm_stringByUnescapingFromHTML], 
194                       @"ABC", @"HTML unescaping failed");
195  
196  STAssertEqualObjects([@"" gtm_stringByUnescapingFromHTML], 
197                       @"", @"HTML unescaping failed");
198
199  STAssertEqualObjects([@"&#65;&Bang;&#X43;" gtm_stringByUnescapingFromHTML], 
200                       @"A&Bang;C", @"HTML unescaping failed");
201
202  STAssertEqualObjects([@"&#65&Bang;&#X43;" gtm_stringByUnescapingFromHTML], 
203                       @"&#65&Bang;C", @"HTML unescaping failed");
204
205  STAssertEqualObjects([@"&#65;&Bang;&#X43" gtm_stringByUnescapingFromHTML], 
206                       @"A&Bang;&#X43", @"HTML unescaping failed");
207
208  STAssertEqualObjects([@"&#65A;" gtm_stringByUnescapingFromHTML], 
209                       @"&#65A;", @"HTML unescaping failed");
210
211  STAssertEqualObjects([@"&" gtm_stringByUnescapingFromHTML], 
212                       @"&", @"HTML unescaping failed");
213
214  STAssertEqualObjects([@"&;" gtm_stringByUnescapingFromHTML], 
215                       @"&;", @"HTML unescaping failed");
216
217  STAssertEqualObjects([@"&x;" gtm_stringByUnescapingFromHTML], 
218                       @"&x;", @"HTML unescaping failed");
219  
220  STAssertEqualObjects([@"&X;" gtm_stringByUnescapingFromHTML], 
221                       @"&X;", @"HTML unescaping failed");
222  
223  STAssertEqualObjects([@";" gtm_stringByUnescapingFromHTML], 
224                       @";", @"HTML unescaping failed");
225
226  STAssertEqualObjects([@"&lt;this &amp; that&gt;" gtm_stringByUnescapingFromHTML], 
227                       @"<this & that>", @"HTML unescaping failed");
228
229  
230} // testStringByUnescapingHTML
231
232- (void)testStringRoundtrippingEscapedHTML {
233  NSString *string = [NSString stringWithUTF8String:"This test Š™Ž????"];
234  STAssertEqualObjects(string,
235                       [[string gtm_stringByEscapingForHTML] gtm_stringByUnescapingFromHTML],
236                       @"HTML Roundtripping failed");
237  string = [NSString stringWithUTF8String:"This test Š™Ž????"];
238  STAssertEqualObjects(string,
239                       [[string gtm_stringByEscapingForAsciiHTML] gtm_stringByUnescapingFromHTML],
240                       @"HTML Roundtripping failed");
241}
242@end