bindings/Cocoa/MYOttoman_test.m
author Jens Alfke <jens@mooseyard.com>
Thu Sep 24 21:47:06 2009 -0700 (2009-09-24)
changeset 6 f2cd752db494
permissions -rw-r--r--
Initial Cocoa (Objective-C) API.
     1 //
     2 //  MYOttoman_test.m
     3 //  Ottoman
     4 //
     5 //  Created by Jens Alfke on 9/24/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "MYOttoman.h"
    10 #import "MYVersionDictionary.h"
    11 
    12 #import "Test.h"
    13 
    14 static NSData* dataOf (NSString *str) {
    15     return [str dataUsingEncoding: NSUTF8StringEncoding];
    16 }
    17 
    18 /*
    19 static NSString* stringOf (id data) {
    20     if (!data)
    21         return nil;
    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);
    25     return str;
    26 }
    27 */
    28 
    29 static void checkFile (NSString *path) {
    30     NSError *error;
    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);
    36 }
    37 
    38 
    39 TestCase(CreateMYOttoman) {
    40     MYOttoman *o = [[MYOttoman alloc] init];
    41     MYCurrentVersionDictionary *cur = o.currentVersion;
    42     CAssertEq(o.lastVersion,nil);
    43     
    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")];
    49 
    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);
    54 
    55     NSError *error;
    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");
    60     
    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);
    67     
    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"));
    75 
    76     if (![o save: &error])
    77         CAssert(NO, @"save: failed: %@", error);
    78  
    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);
    82 
    83     last = o.lastVersion;
    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"));
    89 
    90     [o close];
    91     [o release];
    92     
    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);
    97     last = o.lastVersion;
    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"));
   103     
   104     MYVersionDictionary *prev = last.previousVersion;
   105     CAssert(prev!=nil);
   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);
   111         
   112     [o close];
   113     [o release];
   114 }
   115 
   116 
   117 int main(int argc, const char**argv) {
   118     const char* testArgs[2] = {"", "Test_All"};
   119     RunTestCases(2,testArgs);
   120     return 0;
   121 }