1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/bindings/Cocoa/MYOttoman_test.m Thu Sep 24 21:47:06 2009 -0700
1.3 @@ -0,0 +1,121 @@
1.4 +//
1.5 +// MYOttoman_test.m
1.6 +// Ottoman
1.7 +//
1.8 +// Created by Jens Alfke on 9/24/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "MYOttoman.h"
1.13 +#import "MYVersionDictionary.h"
1.14 +
1.15 +#import "Test.h"
1.16 +
1.17 +static NSData* dataOf (NSString *str) {
1.18 + return [str dataUsingEncoding: NSUTF8StringEncoding];
1.19 +}
1.20 +
1.21 +/*
1.22 +static NSString* stringOf (id data) {
1.23 + if (!data)
1.24 + return nil;
1.25 + CAssert([data isKindOfClass: [NSData class]], @"stringOf expected NSData, got %@", [data class]);
1.26 + NSString *str = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
1.27 + CAssert(str!=nil, @"Bad data (couldn't decode UTF-8): %@", data);
1.28 + return str;
1.29 +}
1.30 +*/
1.31 +
1.32 +static void checkFile (NSString *path) {
1.33 + NSError *error;
1.34 + NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath: path error: &error];
1.35 + CAssert(fileInfo, @"Couldn't get info for %@: %@", path,error);
1.36 + CAssert([fileInfo fileSize] >= 512, @"Unexpected file size %lld", [fileInfo fileSize]);
1.37 + NSTimeInterval age = - [[fileInfo fileModificationDate] timeIntervalSinceNow];
1.38 + CAssert(age < 60.0, @"File %@ is too old: %.1lf sec", path,age);
1.39 +}
1.40 +
1.41 +
1.42 +TestCase(CreateMYOttoman) {
1.43 + MYOttoman *o = [[MYOttoman alloc] init];
1.44 + MYCurrentVersionDictionary *cur = o.currentVersion;
1.45 + CAssertEq(o.lastVersion,nil);
1.46 +
1.47 + [cur setObject: dataOf(@"first value") forKey: dataOf(@"first key")];
1.48 + [cur setObject: dataOf(@"second value") forKey: dataOf(@"second key")];
1.49 + [cur setObject: dataOf(@"third value") forKey: dataOf(@"third key")];
1.50 + [cur removeObjectForKey: dataOf(@"third key")];
1.51 + [cur removeObjectForKey: dataOf(@"bogus")];
1.52 +
1.53 + CAssertEq(cur.count, 2);
1.54 + CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"first value"));
1.55 + CAssertEqual([cur objectForKey: dataOf(@"second key")], dataOf(@"second value"));
1.56 + CAssertEqual([cur objectForKey: dataOf(@"third key")], nil);
1.57 +
1.58 + NSError *error;
1.59 + if (![o saveAs: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"]
1.60 + overwriteAllowed: YES error: &error])
1.61 + CAssert(NO, @"saveAs: failed: %@", error);
1.62 + checkFile(@"/tmp/myottomantest.ottoman");
1.63 +
1.64 + MYVersionDictionary *last = o.lastVersion;
1.65 + CAssert(last, @"lastVersion is nil after save");
1.66 + CAssertEq(last.generation,0);
1.67 + CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value"));
1.68 + CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value"));
1.69 + CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
1.70 +
1.71 + [cur setObject: dataOf(@"fourth value") forKey: dataOf(@"fourth key")];
1.72 + [cur setObject: dataOf(@"new first value") forKey: dataOf(@"first key")];
1.73 + [cur removeObjectForKey: dataOf(@"second key")];
1.74 + CAssertEq(cur.count, 2);
1.75 + CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
1.76 + CAssertEqual([cur objectForKey: dataOf(@"second key")], nil);
1.77 + CAssertEqual([cur objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
1.78 +
1.79 + if (![o save: &error])
1.80 + CAssert(NO, @"save: failed: %@", error);
1.81 +
1.82 + CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value"));
1.83 + CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value"));
1.84 + CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
1.85 +
1.86 + last = o.lastVersion;
1.87 + CAssertEq(last.generation,1);
1.88 + CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
1.89 + CAssertEqual([last objectForKey: dataOf(@"second key")], nil);
1.90 + CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
1.91 + CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
1.92 +
1.93 + [o close];
1.94 + [o release];
1.95 +
1.96 + o = [[MYOttoman alloc] initWithURL: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"]
1.97 + writeable: NO error: &error];
1.98 + CAssert(o, @"Failed to re-open Ottoman: %@", error);
1.99 + CAssertEq(o.currentVersion, nil);
1.100 + last = o.lastVersion;
1.101 + CAssertEq(last.generation,1);
1.102 + CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
1.103 + CAssertEqual([last objectForKey: dataOf(@"second key")], nil);
1.104 + CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
1.105 + CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
1.106 +
1.107 + MYVersionDictionary *prev = last.previousVersion;
1.108 + CAssert(prev!=nil);
1.109 + CAssertEq(prev.generation,0);
1.110 + CAssertEq(prev.previousVersion,nil);
1.111 + CAssertEqual([prev objectForKey: dataOf(@"first key")], dataOf(@"first value"));
1.112 + CAssertEqual([prev objectForKey: dataOf(@"second key")], dataOf(@"second value"));
1.113 + CAssertEqual([prev objectForKey: dataOf(@"third key")], nil);
1.114 +
1.115 + [o close];
1.116 + [o release];
1.117 +}
1.118 +
1.119 +
1.120 +int main(int argc, const char**argv) {
1.121 + const char* testArgs[2] = {"", "Test_All"};
1.122 + RunTestCases(2,testArgs);
1.123 + return 0;
1.124 +}