PageRenderTime 46ms CodeModel.GetById 11ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/gdata-objectivec-client/Source/Tests/GDataDateTimeTest.m

http://macfuse.googlecode.com/
Objective C | 120 lines | 80 code | 19 blank | 21 comment | 6 complexity | 9a912802a08a1027e8b2b951e6d7ffa0 MD5 | raw file
  1/* Copyright (c) 2007 Google Inc.
  2*
  3* Licensed under the Apache License, Version 2.0 (the "License");
  4* you may not use this file except in compliance with the License.
  5* You may obtain a copy of the License at
  6*
  7*     http://www.apache.org/licenses/LICENSE-2.0
  8*
  9* Unless required by applicable law or agreed to in writing, software
 10* distributed under the License is distributed on an "AS IS" BASIS,
 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12* See the License for the specific language governing permissions and
 13* limitations under the License.
 14*/
 15
 16//
 17//  GDataDateTimeTest.m
 18//
 19
 20#import <SenTestingKit/SenTestingKit.h>
 21
 22#import "GDataDateTime.h"
 23
 24#define typeof __typeof__ // fixes http://www.brethorsting.com/blog/2006/02/stupid-issue-with-ocunit.html
 25
 26@interface GDataDateTimeTest : SenTestCase
 27@end
 28
 29@implementation GDataDateTimeTest
 30- (void)testGDataDateTime {
 31  
 32  const NSCalendarUnit kComponents = 
 33    NSEraCalendarUnit
 34    | NSYearCalendarUnit 
 35    | NSMonthCalendarUnit 
 36    | NSDayCalendarUnit
 37    | NSHourCalendarUnit
 38    | NSMinuteCalendarUnit
 39    | NSSecondCalendarUnit;
 40  
 41  struct DateTimeTestRecord {
 42    NSString *dateTimeStr;
 43    NSInteger year;
 44    NSInteger month;
 45    NSInteger day;
 46    NSInteger hour;
 47    NSInteger minute;
 48    NSInteger second;
 49    NSInteger timeZoneOffsetSeconds;
 50    BOOL isUniversalTime;
 51    BOOL hasTime;
 52  };
 53  
 54  struct DateTimeTestRecord tests[] = {
 55    { @"2006-10-14T15:00:00-01:00", 2006, 10, 14, 15, 0, 0, -60*60, 0, 1 },
 56    { @"2006-10-14T15:00:00Z", 2006, 10, 14, 15, 0, 0, 0, 1, 1 },
 57    { @"2006-10-14", 2006, 10, 14, 12, 0, 0, 0, 1, 0 },
 58    { nil, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 59  };
 60  
 61  int idx;
 62  for (idx = 0; tests[idx].dateTimeStr != nil; idx++) {
 63  
 64    NSString *testString1 = tests[idx].dateTimeStr;
 65    GDataDateTime *dateTime = [GDataDateTime dateTimeWithRFC3339String:testString1];
 66    NSString *outputString = [dateTime RFC3339String];
 67    
 68    STAssertEqualObjects(outputString, testString1, @"failed to recreate string %@ as %@", testString1, outputString); 
 69
 70    NSDate *outputDate = [dateTime date];
 71    
 72    NSCalendar *cal = [dateTime calendar];
 73    NSDateComponents *outputComponents = [cal components:kComponents 
 74                                                fromDate:outputDate];
 75    STAssertEquals([outputComponents year], tests[idx].year, @"bad year");
 76    STAssertEquals([outputComponents month], tests[idx].month, @"bad month");
 77    STAssertEquals([outputComponents day], tests[idx].day, @"bad day");
 78    STAssertEquals([outputComponents hour], tests[idx].hour, @"bad hour");
 79    STAssertEquals([outputComponents minute], tests[idx].minute, @"bad minute");
 80    STAssertEquals([outputComponents second], tests[idx].second, @"bad second");
 81
 82    STAssertEquals([[dateTime timeZone] secondsFromGMT], tests[idx].timeZoneOffsetSeconds, @"bad timezone");
 83    STAssertEquals([dateTime isUniversalTime], tests[idx].isUniversalTime, @"bad Zulu value");
 84    STAssertEquals([dateTime hasTime], tests[idx].hasTime, @"bad hasTime value");
 85    
 86    if ([dateTime hasTime]) {
 87      // remove the time, test the output
 88      [dateTime setHasTime:NO];
 89      NSString *outputStringWithoutTime = [dateTime RFC3339String];
 90      STAssertFalse([dateTime hasTime], @"should have time removed");
 91      STAssertTrue([testString1 hasPrefix:outputStringWithoutTime]
 92                   && [testString1 length] > [outputStringWithoutTime length],
 93                   @"bad string after time removed");
 94    } else {
 95      // add time, test the output
 96      [dateTime setHasTime:YES];
 97      NSString *outputStringWithTime = [dateTime RFC3339String];
 98      STAssertTrue([dateTime hasTime], @"should have time added");
 99      STAssertTrue([outputStringWithTime hasPrefix:testString1]
100                   && [testString1 length] < [outputStringWithTime length],
101                   @"bad string after time removed");
102    }
103  }
104}
105
106- (void)testTimeZonePreservation {
107  NSTimeZone *denverTZ = [NSTimeZone timeZoneWithName:@"America/Denver"];
108  NSCalendarDate *date = [NSCalendarDate dateWithYear:2007 month:01 day:01 
109                                                 hour:01 minute:01 second:01 
110                                             timeZone:denverTZ];
111  
112  GDataDateTime *dateTime = [GDataDateTime dateTimeWithDate:date 
113                                                   timeZone:denverTZ];
114  NSTimeZone *testTZ = [dateTime timeZone];
115  STAssertEqualObjects(testTZ, denverTZ, @"Time zone changed");
116}
117@end
118
119//2006-11-20 17:53:23.880 otest[5401] timezone=GMT-0100 (GMT-0100) offset -3600
120//2006-11-20 17:53:23.880 otest[5401] era:1 year:2006 month:10 day:14   hour:15 min:0 sec:0