@@ -13,19 +13,101 @@ * file. */ #include "config.h" -#import "TestsAppDelegate.h" - -static OFString *module; - -@implementation TestsAppDelegate (OFINIFileTests) -- (void)INIFileTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *output = @"[tests]\r\n" +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFINIFileTests: OTTestCase +{ + OFINIFile *_file; +} +@end + +@implementation OFINIFileTests +- (void)setUp +{ + OFIRI *IRI; + + [super setUp]; + + IRI = [OFIRI IRIWithString: @"embedded:testfile.ini"]; + _file = [[OFINIFile alloc] initWithIRI: IRI + encoding: OFStringEncodingISO8859_1]; +} + +- (void)dealloc +{ + [_file release]; + + [super dealloc]; +} + +- (void)testCategoryForName +{ + OTAssertNotNil([_file categoryForName: @"tests"]); + OTAssertNotNil([_file categoryForName: @"foobar"]); + OTAssertNotNil([_file categoryForName: @"types"]); +} + +- (void)testStringValueForKey +{ + OTAssertEqualObjects( + [[_file categoryForName: @"tests"] stringValueForKey: @"foo"], + @"bar"); + + OTAssertEqualObjects([[_file categoryForName: @"foobar"] + stringValueForKey: @"quxquxqux"], + @"hello\"wörld"); +} + +- (void)testLongLongValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + longLongValueForKey: @"integer" + defaultValue: 2], + 0x20); +} + +- (void)testBoolValueForKeyDefaultValue +{ + OTAssertTrue([[_file categoryForName: @"types"] + boolValueForKey: @"bool" + defaultValue: false]); +} + +- (void)testFloatValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + floatValueForKey: @"float" + defaultValue: 1], + 0.5f); +} + +- (void)testDoubleValueForKeyDefaultValue +{ + OTAssertEqual([[_file categoryForName: @"types"] + doubleValueForKey: @"double" + defaultValue: 3], + 0.25); +} + +- (void)testArrayValueForKey +{ + OFINICategory *types = [_file categoryForName: @"types"]; + OFArray *array = [OFArray arrayWithObjects: @"1", @"2", nil]; + + OTAssertEqualObjects([types arrayValueForKey: @"array1"], array); + OTAssertEqualObjects([types arrayValueForKey: @"array2"], array); + OTAssertEqualObjects([types arrayValueForKey: @"array3"], + [OFArray array]); +} + +- (void)testWriteToIRIEncoding +{ + OFString *expectedOutput = @"[tests]\r\n" @"foo=baz\r\n" @"foobar=baz\r\n" @";comment\r\n" @"new=new\r\n" @"\r\n" @@ -41,98 +123,49 @@ @"bool=false\r\n" @"float=0.25\r\n" @"array1=foo\r\n" @"array1=bar\r\n" @"double=0.75\r\n"; - OFIRI *IRI; - OFINIFile *file; - OFINICategory *tests, *foobar, *types; - OFArray *array; + OFINICategory *tests = [_file categoryForName: @"tests"]; + OFINICategory *foobar = [_file categoryForName: @"foobar"]; + OFINICategory *types = [_file categoryForName: @"types"]; + OFArray *array = [OFArray arrayWithObjects: @"foo", @"bar", nil]; #if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) OFIRI *writeIRI; #endif - module = @"OFINIFile"; - - IRI = [OFIRI IRIWithString: @"embedded:testfile.ini"]; - TEST(@"+[fileWithIRI:encoding:]", - (file = [OFINIFile fileWithIRI: IRI - encoding: OFStringEncodingCodepage437])) - - tests = [file categoryForName: @"tests"]; - foobar = [file categoryForName: @"foobar"]; - types = [file categoryForName: @"types"]; - TEST(@"-[categoryForName:]", - tests != nil && foobar != nil && types != nil) - - module = @"OFINICategory"; - - TEST(@"-[stringValueForKey:]", - [[tests stringValueForKey: @"foo"] isEqual: @"bar"] && - [[foobar stringValueForKey: @"quxquxqux"] isEqual: @"hello\"wörld"]) - - TEST(@"-[setStringValue:forKey:]", - R([tests setStringValue: @"baz" forKey: @"foo"]) && - R([tests setStringValue: @"new" forKey: @"new"]) && - R([foobar setStringValue: @"a\fb" forKey: @"qux3"])) - - TEST(@"-[longLongValueForKey:defaultValue:]", - [types longLongValueForKey: @"integer" defaultValue: 2] == 0x20) - - TEST(@"-[setLongLongValue:forKey:]", - R([types setLongLongValue: 0x10 forKey: @"integer"])) - - TEST(@"-[boolValueForKey:defaultValue:]", - [types boolValueForKey: @"bool" defaultValue: false] == true) - - TEST(@"-[setBoolValue:forKey:]", - R([types setBoolValue: false forKey: @"bool"])) - - TEST(@"-[floatValueForKey:defaultValue:]", - [types floatValueForKey: @"float" defaultValue: 1] == 0.5f) - - TEST(@"-[setFloatValue:forKey:]", - R([types setFloatValue: 0.25f forKey: @"float"])) - - TEST(@"-[doubleValueForKey:defaultValue:]", - [types doubleValueForKey: @"double" defaultValue: 3] == 0.25) - - TEST(@"-[setDoubleValue:forKey:]", - R([types setDoubleValue: 0.75 forKey: @"double"])) - - array = [OFArray arrayWithObjects: @"1", @"2", nil]; - TEST(@"-[arrayValueForKey:]", - [[types arrayValueForKey: @"array1"] isEqual: array] && - [[types arrayValueForKey: @"array2"] isEqual: array] && - [[types arrayValueForKey: @"array3"] isEqual: [OFArray array]]) - - array = [OFArray arrayWithObjects: @"foo", @"bar", nil]; - TEST(@"-[setArrayValue:forKey:]", - R([types setArrayValue: array forKey: @"array1"])) - - TEST(@"-[removeValueForKey:]", - R([foobar removeValueForKey: @"quxqux "]) && - R([types removeValueForKey: @"array2"])) - - module = @"OFINIFile"; + [tests setStringValue: @"baz" forKey: @"foo"]; + [tests setStringValue: @"new" forKey: @"new"]; + [foobar setStringValue: @"a\fb" forKey: @"qux3"]; + [types setLongLongValue: 0x10 forKey: @"integer"]; + [types setBoolValue: false forKey: @"bool"]; + [types setFloatValue: 0.25f forKey: @"float"]; + [types setDoubleValue: 0.75 forKey: @"double"]; + [types setArrayValue: array forKey: @"array1"]; + + [foobar removeValueForKey: @"quxqux "]; + [types removeValueForKey: @"array2"]; /* FIXME: Find a way to write files on Nintendo DS */ #if defined(OF_HAVE_FILES) && !defined(OF_NINTENDO_DS) writeIRI = [OFSystemInfo temporaryDirectoryIRI]; if (writeIRI == nil) writeIRI = [[OFFileManager defaultManager] currentDirectoryIRI]; writeIRI = [writeIRI IRIByAppendingPathComponent: @"objfw-tests.ini" isDirectory: false]; - TEST(@"-[writeToFile:encoding:]", - R([file writeToIRI: writeIRI - encoding: OFStringEncodingCodepage437]) && - [[OFString stringWithContentsOfIRI: writeIRI - encoding: OFStringEncodingCodepage437] - isEqual: output]) - [[OFFileManager defaultManager] removeItemAtIRI: writeIRI]; + + [_file writeToIRI: writeIRI + encoding: OFStringEncodingISO8859_1]; + + @try { + OTAssertEqualObjects([OFString + stringWithContentsOfIRI: writeIRI + encoding: OFStringEncodingISO8859_1], + expectedOutput); + } @finally { + [[OFFileManager defaultManager] removeItemAtIRI: writeIRI]; + } #else - (void)output; + (void)expectedOutput; #endif - - objc_autoreleasePoolPop(pool); } @end