Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added unit test for the bug + made behaviour identical to

initWithString:
  • Loading branch information...
commit e6469c4c09a163228c2b66c033e6dfaa0c83b367 1 parent 9a3cb67
@ntamas ntamas authored schwa committed
Showing with 18 additions and 2 deletions.
  1. +9 −2 Source/CXMLDocument.m
  2. +9 −0 Support/UnitTests/Source/BasicTests.m
View
11 Source/CXMLDocument.m
@@ -125,14 +125,21 @@ - (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:
theDoc = xmlReadMemory([inData bytes], [inData length], NULL, enc, XML_PARSE_RECOVER | XML_PARSE_NOWARNING);
}
- if (theDoc != NULL)
+ if (theDoc != NULL && xmlDocGetRootElement(theDoc) != NULL)
{
_node = (xmlNodePtr)theDoc;
_node->_private = self; // Note. NOT retained (TODO think more about _private usage)
}
else
{
- theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:-1 userInfo:NULL];
+ xmlErrorPtr theLastErrorPtr = xmlGetLastError();
+ NSString* message = [NSString stringWithUTF8String:
+ (theLastErrorPtr ? theLastErrorPtr->message : "Unknown error")];
+ NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ message, NSLocalizedDescriptionKey, NULL];
+ theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:1 userInfo:theUserInfo];
+
+ xmlResetLastError();
}
}
View
9 Support/UnitTests/Source/BasicTests.m
@@ -73,6 +73,15 @@ - (void)test_badXMLTest
STAssertNotNil(theError, NULL);
}
+- (void)test_badXMLTestFromData
+{
+ NSError *theError = NULL;
+ NSData *theData=[@"This is invalid XML." dataUsingEncoding:NSUTF8StringEncoding];
+ CXMLDocument *theXMLDocument = [[[CXMLDocument alloc] initWithData:theData options:0 error:&theError] autorelease];
+ STAssertNil(theXMLDocument, NULL);
+ STAssertNotNil(theError, NULL);
+}
+
- (void)test_nodeNavigation
{
NSError *theError = NULL;
Please sign in to comment.
Something went wrong with that request. Please try again.