PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  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. // GDataDateTimeTest.m
  17. //
  18. #import <SenTestingKit/SenTestingKit.h>
  19. #import "GDataDateTime.h"
  20. #define typeof __typeof__ // fixes http://www.brethorsting.com/blog/2006/02/stupid-issue-with-ocunit.html
  21. @interface GDataDateTimeTest : SenTestCase
  22. @end
  23. @implementation GDataDateTimeTest
  24. - (void)testGDataDateTime {
  25. const NSCalendarUnit kComponents =
  26. NSEraCalendarUnit
  27. | NSYearCalendarUnit
  28. | NSMonthCalendarUnit
  29. | NSDayCalendarUnit
  30. | NSHourCalendarUnit
  31. | NSMinuteCalendarUnit
  32. | NSSecondCalendarUnit;
  33. struct DateTimeTestRecord {
  34. NSString *dateTimeStr;
  35. NSInteger year;
  36. NSInteger month;
  37. NSInteger day;
  38. NSInteger hour;
  39. NSInteger minute;
  40. NSInteger second;
  41. NSInteger timeZoneOffsetSeconds;
  42. BOOL isUniversalTime;
  43. BOOL hasTime;
  44. };
  45. struct DateTimeTestRecord tests[] = {
  46. { @"2006-10-14T15:00:00-01:00", 2006, 10, 14, 15, 0, 0, -60*60, 0, 1 },
  47. { @"2006-10-14T15:00:00Z", 2006, 10, 14, 15, 0, 0, 0, 1, 1 },
  48. { @"2006-10-14", 2006, 10, 14, 12, 0, 0, 0, 1, 0 },
  49. { nil, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  50. };
  51. int idx;
  52. for (idx = 0; tests[idx].dateTimeStr != nil; idx++) {
  53. NSString *testString1 = tests[idx].dateTimeStr;
  54. GDataDateTime *dateTime = [GDataDateTime dateTimeWithRFC3339String:testString1];
  55. NSString *outputString = [dateTime RFC3339String];
  56. STAssertEqualObjects(outputString, testString1, @"failed to recreate string %@ as %@", testString1, outputString);
  57. NSDate *outputDate = [dateTime date];
  58. NSCalendar *cal = [dateTime calendar];
  59. NSDateComponents *outputComponents = [cal components:kComponents
  60. fromDate:outputDate];
  61. STAssertEquals([outputComponents year], tests[idx].year, @"bad year");
  62. STAssertEquals([outputComponents month], tests[idx].month, @"bad month");
  63. STAssertEquals([outputComponents day], tests[idx].day, @"bad day");
  64. STAssertEquals([outputComponents hour], tests[idx].hour, @"bad hour");
  65. STAssertEquals([outputComponents minute], tests[idx].minute, @"bad minute");
  66. STAssertEquals([outputComponents second], tests[idx].second, @"bad second");
  67. STAssertEquals([[dateTime timeZone] secondsFromGMT], tests[idx].timeZoneOffsetSeconds, @"bad timezone");
  68. STAssertEquals([dateTime isUniversalTime], tests[idx].isUniversalTime, @"bad Zulu value");
  69. STAssertEquals([dateTime hasTime], tests[idx].hasTime, @"bad hasTime value");
  70. if ([dateTime hasTime]) {
  71. // remove the time, test the output
  72. [dateTime setHasTime:NO];
  73. NSString *outputStringWithoutTime = [dateTime RFC3339String];
  74. STAssertFalse([dateTime hasTime], @"should have time removed");
  75. STAssertTrue([testString1 hasPrefix:outputStringWithoutTime]
  76. && [testString1 length] > [outputStringWithoutTime length],
  77. @"bad string after time removed");
  78. } else {
  79. // add time, test the output
  80. [dateTime setHasTime:YES];
  81. NSString *outputStringWithTime = [dateTime RFC3339String];
  82. STAssertTrue([dateTime hasTime], @"should have time added");
  83. STAssertTrue([outputStringWithTime hasPrefix:testString1]
  84. && [testString1 length] < [outputStringWithTime length],
  85. @"bad string after time removed");
  86. }
  87. }
  88. }
  89. - (void)testTimeZonePreservation {
  90. NSTimeZone *denverTZ = [NSTimeZone timeZoneWithName:@"America/Denver"];
  91. NSCalendarDate *date = [NSCalendarDate dateWithYear:2007 month:01 day:01
  92. hour:01 minute:01 second:01
  93. timeZone:denverTZ];
  94. GDataDateTime *dateTime = [GDataDateTime dateTimeWithDate:date
  95. timeZone:denverTZ];
  96. NSTimeZone *testTZ = [dateTime timeZone];
  97. STAssertEqualObjects(testTZ, denverTZ, @"Time zone changed");
  98. }
  99. @end
  100. //2006-11-20 17:53:23.880 otest[5401] timezone=GMT-0100 (GMT-0100) offset -3600
  101. //2006-11-20 17:53:23.880 otest[5401] era:1 year:2006 month:10 day:14 hour:15 min:0 sec:0