I have implemented the NSXMLParserDelegate protocol and appear to be successfully parsing XML with NSXMLParser. The XSD for the XML I am parsing is fairly involved with 100 or so tags and attributes, that can get rather deeply nested at times. I am populating an object hierarchy from the data in the XML documents and all seems to be going well so far.
My question is, how do I effectively test this? There is a lot of “if/else if” logic in my delegate as well as as BOOL flags to keep track of where I am in within the document being parsed. I could have easily introduced a typographical error while writing the delegate.
My initial inclination is to create a test input document that uses all of the elements and attributes specified in the XSD. Once it has been parsed, I could “NSLog()” everything that has been inserted into the object hierarchy, but that seems too labor intensive.
Is there a better way to think about this problem, or shall I just roll up my sleeves and brute force it?
You’ll want to look at using a unit test here. If you can generate one (or more) files that will result in known data structures, you can parse them in unit tests and verify that the data structures are what you expect them to be.
We do this with a lot of XML file types that we manage for both parsing and generation (and for fun, we hook up the output of the generator to the parser in another unit tests).
There is a lot of good background information on setting up Unit Tests from Apple, and a lot of Open Source projects use Unit Testing as well.
With a scheme like this, you end up writing a comparator (or using isEqual: if that works for your object hierarchy) and then you can reuse that for each of your tests. Add a few XML files and manually create the matching object hierarchy in your Unit Tests and you’re all set to go.