My favorites | Sign in
Project Home
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 57 attachment: EncodingTests.m (5.0 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
path//
// EncodingTests.m
// TouchXML
//
// Created by Jorge Pedroso on 5/10/09.
// Copyright 2009 Unsolicited Feedback. All rights reserved.

// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//


#import "EncodingTests.h"
#import "CXMLDocument.h"

static NSString *xmlUTF8Str = @"\n\
<html> \n\
<head> \n\
<title>Новости и аналитика</title> \n\
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\" /> \n\
</head>\n\
</html>";


@implementation EncodingTests

- (void)test_initWithDataReturnsNilWhenInvalidEncoding
{
NSError *error;

NSData *nonUTF8Data = [xmlUTF8Str dataUsingEncoding:NSWindowsCP1251StringEncoding];

CXMLDocument *doc = [[CXMLDocument alloc] initWithData:nonUTF8Data options:0 error:&error];
// STAssertNil(doc, NULL);
// STAssertNotNil(error, NULL);
[doc release];

/* TODO: Unlike NSXMLDocument, CXMLDocument isn't failing parsing when given data with
non UTF-8 encoding, thus the above assertions are commented. */

/* Extra: check NSXMLDocument compatibility — like CXMLDocument, expects failure */
NSXMLDocument *nsdoc = [[NSXMLDocument alloc] initWithData:nonUTF8Data options:0 error:&error];
STAssertNil(nsdoc, NULL);
STAssertNotNil(error, NULL);
[nsdoc release];
}


- (void)test_initWithDataNonUTF8Encoding
{
NSError *error;

NSData *nonUTF8Data = [xmlUTF8Str dataUsingEncoding:NSWindowsCP1251StringEncoding];

/* Check if patch was applied. If not, bail gracefully showing how test will fail
with a non UTF-8 encoding with initWithData:options:error: implementation. */
CXMLDocument *doc;
doc = [CXMLDocument instancesRespondToSelector:@selector(initWithData:encoding:options:error:)]
? [[CXMLDocument alloc] initWithData:nonUTF8Data
encoding:NSWindowsCP1251StringEncoding
options:0
error:&error]
: [[CXMLDocument alloc] initWithData:nonUTF8Data options:0 error:&error];

STAssertNotNil(doc, NULL);
STAssertNil(error, NULL);

NSString *path = @"/html/head/title";
NSArray *nodes = [doc nodesForXPath:path error:&error];
CXMLNode *node = [nodes objectAtIndex:0];
NSString *result = [node stringValue];
STAssertEqualObjects(result, @"Новости и аналитика", nil);

[doc release];

/* Extra: check NSXMLDocument compatibility — like CXMLDocument, expects failure */
NSXMLDocument *nsdoc = [[NSXMLDocument alloc] initWithData:nonUTF8Data options:0 error:&error];
STAssertNil(nsdoc, NULL);
STAssertNotNil(error, NULL);
[nsdoc release];
/* enough, parse went ok */
}


- (void)test_initWithStringNonUTF8Enconding
{
NSError *error;

/* sanity check */
STAssertTrue([xmlUTF8Str canBeConvertedToEncoding:NSWindowsCP1251StringEncoding],
@"XML cannot be converted to windows-1251 without losing data.", nil);

NSData *nonUTF8Data = [xmlUTF8Str dataUsingEncoding:NSWindowsCP1251StringEncoding];
NSString *nonUTF8Str = [[NSString alloc] initWithData:nonUTF8Data encoding:NSWindowsCP1251StringEncoding];

CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString:nonUTF8Str options:0 error:&error];
STAssertNotNil(doc, NULL);
STAssertNil(error, NULL);

NSString *path = @"/html/head/title";
NSArray *nodes = [doc nodesForXPath:path error:&error];
CXMLNode *node = [nodes objectAtIndex:0];
NSString *result = [node stringValue];
STAssertEqualObjects(result, @"Новости и аналитика", nil);

[doc release];


/* Extra: check NSXMLDocument compatibility — like CXMLDocument, expects correct parsing */
NSXMLDocument *nsdoc = [[NSXMLDocument alloc] initWithXMLString:nonUTF8Str options:0 error:&error];
STAssertNotNil(nsdoc, NULL);
STAssertNil(error, NULL);
[nsdoc release];
/* enough, parse went ok */

[nonUTF8Str release];
}

@end
Powered by Google Project Hosting