GPBWellKnownTypesTest.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBWellKnownTypes.h"
  31. #import <XCTest/XCTest.h>
  32. #import "google/protobuf/AnyTest.pbobjc.h"
  33. // A basically random interval into the future for testing with.
  34. static const NSTimeInterval kFutureOffsetInterval = 15000;
  35. // Nanosecond time accuracy
  36. static const NSTimeInterval kTimeAccuracy = 1e-9;
  37. @interface WellKnownTypesTest : XCTestCase
  38. @end
  39. @implementation WellKnownTypesTest
  40. - (void)testTimeStamp {
  41. // Test Creation.
  42. NSDate *date = [NSDate date];
  43. GPBTimestamp *timeStamp = [[GPBTimestamp alloc] initWithDate:date];
  44. NSDate *timeStampDate = timeStamp.date;
  45. // Comparing timeIntervals instead of directly comparing dates because date
  46. // equality requires the time intervals to be exactly the same, and the
  47. // timeintervals go through a bit of floating point error as they are
  48. // converted back and forth from the internal representation.
  49. XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
  50. timeStampDate.timeIntervalSince1970,
  51. kTimeAccuracy);
  52. NSTimeInterval time = [date timeIntervalSince1970];
  53. GPBTimestamp *timeStamp2 =
  54. [[GPBTimestamp alloc] initWithTimeIntervalSince1970:time];
  55. NSTimeInterval durationTime = timeStamp2.timeIntervalSince1970;
  56. XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
  57. [timeStamp release];
  58. // Test Mutation.
  59. date = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval];
  60. timeStamp2.date = date;
  61. timeStampDate = timeStamp2.date;
  62. XCTAssertEqualWithAccuracy(date.timeIntervalSince1970,
  63. timeStampDate.timeIntervalSince1970,
  64. kTimeAccuracy);
  65. time = date.timeIntervalSince1970;
  66. timeStamp2.timeIntervalSince1970 = time;
  67. durationTime = timeStamp2.timeIntervalSince1970;
  68. XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
  69. [timeStamp2 release];
  70. }
  71. - (void)testDuration {
  72. // Test Creation.
  73. NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
  74. GPBDuration *duration =
  75. [[GPBDuration alloc] initWithTimeIntervalSince1970:time];
  76. NSTimeInterval durationTime = duration.timeIntervalSince1970;
  77. XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
  78. [duration release];
  79. // Test Mutation.
  80. GPBDuration *duration2 =
  81. [[GPBDuration alloc] initWithTimeIntervalSince1970:time];
  82. NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval];
  83. time = date.timeIntervalSince1970;
  84. duration2.timeIntervalSince1970 = time;
  85. durationTime = duration2.timeIntervalSince1970;
  86. XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy);
  87. [duration2 release];
  88. }
  89. - (void)testAnyHelpers {
  90. // Set and extract covers most of the code.
  91. TestAny *subMessage = [TestAny message];
  92. subMessage.int32Value = 12345;
  93. TestAny *message = [TestAny message];
  94. NSError *err = nil;
  95. message.anyValue = [GPBAny anyWithMessage:subMessage error:&err];
  96. XCTAssertNil(err);
  97. NSData *data = message.data;
  98. XCTAssertNotNil(data);
  99. TestAny *message2 = [TestAny parseFromData:data error:&err];
  100. XCTAssertNil(err);
  101. XCTAssertNotNil(message2);
  102. XCTAssertTrue(message2.hasAnyValue);
  103. TestAny *subMessage2 =
  104. (TestAny *)[message.anyValue unpackMessageClass:[TestAny class]
  105. error:&err];
  106. XCTAssertNil(err);
  107. XCTAssertNotNil(subMessage2);
  108. XCTAssertEqual(subMessage2.int32Value, 12345);
  109. // NULL errorPtr in the two calls.
  110. message.anyValue = [GPBAny anyWithMessage:subMessage error:NULL];
  111. NSData *data2 = message.data;
  112. XCTAssertEqualObjects(data2, data);
  113. TestAny *subMessage3 =
  114. (TestAny *)[message.anyValue unpackMessageClass:[TestAny class]
  115. error:NULL];
  116. XCTAssertNotNil(subMessage3);
  117. XCTAssertEqualObjects(subMessage2, subMessage3);
  118. // Try to extract wrong type.
  119. GPBTimestamp *wrongMessage =
  120. (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class]
  121. error:&err];
  122. XCTAssertNotNil(err);
  123. XCTAssertNil(wrongMessage);
  124. XCTAssertEqualObjects(err.domain, GPBWellKnownTypesErrorDomain);
  125. XCTAssertEqual(err.code, GPBWellKnownTypesErrorCodeTypeURLMismatch);
  126. wrongMessage =
  127. (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class]
  128. error:NULL];
  129. XCTAssertNil(wrongMessage);
  130. }
  131. @end