Skip to content

Commit

Permalink
Added a few basic unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Perry authored and David Perry committed May 7, 2011
1 parent 1aa2163 commit b7e5ad9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
1 change: 0 additions & 1 deletion Demo/XMLReaderTests/XMLReaderTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

@interface XMLReaderTests : SenTestCase
{
@private

}

Expand Down
74 changes: 72 additions & 2 deletions Demo/XMLReaderTests/XMLReaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "XMLReaderTests.h"
#import "XMLReader.h"

@implementation XMLReaderTests

Expand All @@ -24,9 +25,78 @@ - (void)tearDown
[super tearDown];
}

- (void)testExample
- (void)testXMLReaderFailsWithNoData
{
STFail(@"Unit tests are not implemented yet in DemoTests");
NSData *data = nil;
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLData:data error:&error];

STAssertNil(dictionary, @"dictionary should be nil");
STAssertNotNil(error, @"error should not be nil");
}

- (void)testXMLReaderFailsWithNoString
{
NSString *string = nil;
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLString:string error:&error];

STAssertNil(dictionary, @"dictionary should be nil");
STAssertNotNil(error, @"error should not be nil");
}

- (void)testXMLReaderFailsWithInvalidPath
{
NSString *path = nil;
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForPath:path error:&error];

STAssertNil(dictionary, @"dictionary should be nil");
STAssertNotNil(error, @"error should not be nil");
}

- (void)testXMLReaderFailsWithInvalidXML
{
NSString *xmlString = @"<goodxml>this is good</goodxml><badxml>this is bad</goodxml>";
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLString:xmlString error:&error];

STAssertNil(dictionary, @"dictionary should be nil");
STAssertNotNil(error, @"error should not be nil");
}

- (void)testXMLReaderSucceedsWithBasicXML
{
NSString *xmlString = @"<outernode><innernode>somevalue</innernode></outernode>";
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLString:xmlString error:&error];

STAssertNotNil(dictionary, @"dictionary should not be nil");
STAssertNil(error, @"error should be nil");
STAssertTrue([[dictionary objectForKey:@"outernode"] isKindOfClass:[NSDictionary class]], @"outernode should be a dictionary");
STAssertTrue([[[dictionary objectForKey:@"outernode"] objectForKey:@"innernode"] isEqualToString:@"somevalue"], @"node values should match");
}

- (void)testXMLReaderSucceedsWithComplexXML
{
NSData *xmlData = [NSData dataWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"complex" ofType:@"xml"]];
STAssertNotNil(xmlData, @"Failed to load XML file");

NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLData:xmlData error:&error];
NSLog(@"%@", [dictionary description]);

STAssertNotNil(dictionary, @"dictionary should not be nil");
STAssertNil(error, @"error should be nil");

// TODO: Spot check a few of the nodes are correct in the dictionary
}


@end
42 changes: 42 additions & 0 deletions Demo/XMLReaderTests/complex.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>

0 comments on commit b7e5ad9

Please sign in to comment.