OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2015 Google Inc. All rights reserved. | 2 // Copyright 2015 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 14 matching lines...) Expand all Loading... |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 #import "GPBWellKnownTypes.h" | 31 #import "GPBWellKnownTypes.h" |
32 | 32 |
33 #import <XCTest/XCTest.h> | 33 #import <XCTest/XCTest.h> |
34 | 34 |
35 #import "google/protobuf/AnyTest.pbobjc.h" | |
36 | |
37 // A basically random interval into the future for testing with. | 35 // A basically random interval into the future for testing with. |
38 static const NSTimeInterval kFutureOffsetInterval = 15000; | 36 static const NSTimeInterval kFutureOffsetInterval = 15000; |
39 | 37 |
40 // Nanosecond time accuracy | 38 // Nanosecond time accuracy |
41 static const NSTimeInterval kTimeAccuracy = 1e-9; | 39 static const NSTimeInterval kTimeAccuracy = 1e-9; |
42 | 40 |
43 @interface WellKnownTypesTest : XCTestCase | 41 @interface WellKnownTypesTest : XCTestCase |
44 @end | 42 @end |
45 | 43 |
46 @implementation WellKnownTypesTest | 44 @implementation WellKnownTypesTest |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 GPBDuration *duration2 = | 92 GPBDuration *duration2 = |
95 [[GPBDuration alloc] initWithTimeIntervalSince1970:time]; | 93 [[GPBDuration alloc] initWithTimeIntervalSince1970:time]; |
96 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval]; | 94 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:kFutureOffsetInterval]; |
97 time = date.timeIntervalSince1970; | 95 time = date.timeIntervalSince1970; |
98 duration2.timeIntervalSince1970 = time; | 96 duration2.timeIntervalSince1970 = time; |
99 durationTime = duration2.timeIntervalSince1970; | 97 durationTime = duration2.timeIntervalSince1970; |
100 XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy); | 98 XCTAssertEqualWithAccuracy(time, durationTime, kTimeAccuracy); |
101 [duration2 release]; | 99 [duration2 release]; |
102 } | 100 } |
103 | 101 |
104 - (void)testAnyHelpers { | |
105 | |
106 // Set and extract covers most of the code. | |
107 | |
108 TestAny *subMessage = [TestAny message]; | |
109 subMessage.int32Value = 12345; | |
110 TestAny *message = [TestAny message]; | |
111 NSError *err = nil; | |
112 message.anyValue = [GPBAny anyWithMessage:subMessage error:&err]; | |
113 XCTAssertNil(err); | |
114 | |
115 NSData *data = message.data; | |
116 XCTAssertNotNil(data); | |
117 | |
118 TestAny *message2 = [TestAny parseFromData:data error:&err]; | |
119 XCTAssertNil(err); | |
120 XCTAssertNotNil(message2); | |
121 XCTAssertTrue(message2.hasAnyValue); | |
122 | |
123 TestAny *subMessage2 = | |
124 (TestAny *)[message.anyValue unpackMessageClass:[TestAny class] | |
125 error:&err]; | |
126 XCTAssertNil(err); | |
127 XCTAssertNotNil(subMessage2); | |
128 XCTAssertEqual(subMessage2.int32Value, 12345); | |
129 | |
130 // NULL errorPtr in the two calls. | |
131 | |
132 message.anyValue = [GPBAny anyWithMessage:subMessage error:NULL]; | |
133 NSData *data2 = message.data; | |
134 XCTAssertEqualObjects(data2, data); | |
135 | |
136 TestAny *subMessage3 = | |
137 (TestAny *)[message.anyValue unpackMessageClass:[TestAny class] | |
138 error:NULL]; | |
139 XCTAssertNotNil(subMessage3); | |
140 XCTAssertEqualObjects(subMessage2, subMessage3); | |
141 | |
142 // Try to extract wrong type. | |
143 | |
144 GPBTimestamp *wrongMessage = | |
145 (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class] | |
146 error:&err]; | |
147 XCTAssertNotNil(err); | |
148 XCTAssertNil(wrongMessage); | |
149 XCTAssertEqualObjects(err.domain, GPBWellKnownTypesErrorDomain); | |
150 XCTAssertEqual(err.code, GPBWellKnownTypesErrorCodeTypeURLMismatch); | |
151 | |
152 wrongMessage = | |
153 (GPBTimestamp *)[message.anyValue unpackMessageClass:[GPBTimestamp class] | |
154 error:NULL]; | |
155 XCTAssertNil(wrongMessage); | |
156 } | |
157 | |
158 @end | 102 @end |
OLD | NEW |