Initial Cocoa (Objective-C) API.
5 // Created by Jens Alfke on 9/24/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
10 #import "MYVersionDictionary.h"
14 static NSData* dataOf (NSString *str) {
15 return [str dataUsingEncoding: NSUTF8StringEncoding];
19 static NSString* stringOf (id data) {
22 CAssert([data isKindOfClass: [NSData class]], @"stringOf expected NSData, got %@", [data class]);
23 NSString *str = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
24 CAssert(str!=nil, @"Bad data (couldn't decode UTF-8): %@", data);
29 static void checkFile (NSString *path) {
31 NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath: path error: &error];
32 CAssert(fileInfo, @"Couldn't get info for %@: %@", path,error);
33 CAssert([fileInfo fileSize] >= 512, @"Unexpected file size %lld", [fileInfo fileSize]);
34 NSTimeInterval age = - [[fileInfo fileModificationDate] timeIntervalSinceNow];
35 CAssert(age < 60.0, @"File %@ is too old: %.1lf sec", path,age);
39 TestCase(CreateMYOttoman) {
40 MYOttoman *o = [[MYOttoman alloc] init];
41 MYCurrentVersionDictionary *cur = o.currentVersion;
42 CAssertEq(o.lastVersion,nil);
44 [cur setObject: dataOf(@"first value") forKey: dataOf(@"first key")];
45 [cur setObject: dataOf(@"second value") forKey: dataOf(@"second key")];
46 [cur setObject: dataOf(@"third value") forKey: dataOf(@"third key")];
47 [cur removeObjectForKey: dataOf(@"third key")];
48 [cur removeObjectForKey: dataOf(@"bogus")];
50 CAssertEq(cur.count, 2);
51 CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"first value"));
52 CAssertEqual([cur objectForKey: dataOf(@"second key")], dataOf(@"second value"));
53 CAssertEqual([cur objectForKey: dataOf(@"third key")], nil);
56 if (![o saveAs: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"]
57 overwriteAllowed: YES error: &error])
58 CAssert(NO, @"saveAs: failed: %@", error);
59 checkFile(@"/tmp/myottomantest.ottoman");
61 MYVersionDictionary *last = o.lastVersion;
62 CAssert(last, @"lastVersion is nil after save");
63 CAssertEq(last.generation,0);
64 CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value"));
65 CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value"));
66 CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
68 [cur setObject: dataOf(@"fourth value") forKey: dataOf(@"fourth key")];
69 [cur setObject: dataOf(@"new first value") forKey: dataOf(@"first key")];
70 [cur removeObjectForKey: dataOf(@"second key")];
71 CAssertEq(cur.count, 2);
72 CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
73 CAssertEqual([cur objectForKey: dataOf(@"second key")], nil);
74 CAssertEqual([cur objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
76 if (![o save: &error])
77 CAssert(NO, @"save: failed: %@", error);
79 CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value"));
80 CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value"));
81 CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
84 CAssertEq(last.generation,1);
85 CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
86 CAssertEqual([last objectForKey: dataOf(@"second key")], nil);
87 CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
88 CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
93 o = [[MYOttoman alloc] initWithURL: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"]
94 writeable: NO error: &error];
95 CAssert(o, @"Failed to re-open Ottoman: %@", error);
96 CAssertEq(o.currentVersion, nil);
98 CAssertEq(last.generation,1);
99 CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value"));
100 CAssertEqual([last objectForKey: dataOf(@"second key")], nil);
101 CAssertEqual([last objectForKey: dataOf(@"third key")], nil);
102 CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value"));
104 MYVersionDictionary *prev = last.previousVersion;
106 CAssertEq(prev.generation,0);
107 CAssertEq(prev.previousVersion,nil);
108 CAssertEqual([prev objectForKey: dataOf(@"first key")], dataOf(@"first value"));
109 CAssertEqual([prev objectForKey: dataOf(@"second key")], dataOf(@"second value"));
110 CAssertEqual([prev objectForKey: dataOf(@"third key")], nil);
117 int main(int argc, const char**argv) {
118 const char* testArgs[2] = {"", "Test_All"};
119 RunTestCases(2,testArgs);