-
Notifications
You must be signed in to change notification settings - Fork 472
/
Copy pathTest_JSON_Array.swift
164 lines (155 loc) · 6.54 KB
/
Test_JSON_Array.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Tests/SwiftProtobufTests/Test_JSON_Array.swift - Exercise JSON flat array coding
//
// Copyright (c) 2014 - 2019 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------
///
/// JSON is a major new feature for Proto3. This test suite exercises
/// the JSON coding for all primitive types, including boundary and error
/// cases.
///
// -----------------------------------------------------------------------------
import Foundation
import SwiftProtobuf
import XCTest
final class Test_JSON_Array: XCTestCase, PBTestHelpers {
typealias MessageTestType = SwiftProtoTesting_Proto3_TestAllTypes
private func configureTwoObjects(_ o: inout [MessageTestType]) {
var o1 = MessageTestType()
o1.optionalInt32 = 1
o1.optionalInt64 = 2
o1.optionalUint32 = 3
o1.optionalUint64 = 4
o1.optionalSint32 = 5
o1.optionalSint64 = 6
o1.optionalFixed32 = 7
o1.optionalFixed64 = 8
o1.optionalSfixed32 = 9
o1.optionalSfixed64 = 10
o1.optionalFloat = 11
o1.optionalDouble = 12
o1.optionalBool = true
o1.optionalString = "abc"
o1.optionalBytes = Data([65, 66])
var nested = MessageTestType.NestedMessage()
nested.bb = 7
o1.optionalNestedMessage = nested
var foreign = SwiftProtoTesting_Proto3_ForeignMessage()
foreign.c = 88
o1.optionalForeignMessage = foreign
var importMessage = SwiftProtoTesting_Import_ImportMessage()
importMessage.d = -9
o1.optionalImportMessage = importMessage
o1.optionalNestedEnum = .baz
o1.optionalForeignEnum = .foreignBaz
// o1.optionalImportEnum = .importBaz
var publicImportMessage = SwiftProtoTesting_Import_PublicImportMessage()
publicImportMessage.e = -999999
o1.optionalPublicImportMessage = publicImportMessage
o1.repeatedInt32 = [1, 2]
o1.repeatedInt64 = [3, 4]
o1.repeatedUint32 = [5, 6]
o1.repeatedUint64 = [7, 8]
o1.repeatedSint32 = [9, 10]
o1.repeatedSint64 = [11, 12]
o1.repeatedFixed32 = [13, 14]
o1.repeatedFixed64 = [15, 16]
o1.repeatedSfixed32 = [17, 18]
o1.repeatedSfixed64 = [19, 20]
o1.repeatedFloat = [21, 22]
o1.repeatedDouble = [23, 24]
o1.repeatedBool = [true, false]
o1.repeatedString = ["abc", "def"]
o1.repeatedBytes = [Data(), Data([65, 66])]
var nested2 = nested
nested2.bb = -7
o1.repeatedNestedMessage = [nested, nested2]
var foreign2 = foreign
foreign2.c = -88
o1.repeatedForeignMessage = [foreign, foreign2]
var importMessage2 = importMessage
importMessage2.d = 999999
o1.repeatedNestedEnum = [.bar, .baz]
o1.repeatedForeignEnum = [.foreignBar, .foreignBaz]
var publicImportMessage2 = publicImportMessage
publicImportMessage2.e = 999999
o1.oneofUint32 = 99
o.append(o1)
let o2 = MessageTestType()
o.append(o2)
}
func testTwoObjectsWithMultipleFields() {
let expected: String =
("[{"
+ "\"optionalInt32\":1,"
+ "\"optionalInt64\":\"2\","
+ "\"optionalUint32\":3,"
+ "\"optionalUint64\":\"4\","
+ "\"optionalSint32\":5,"
+ "\"optionalSint64\":\"6\","
+ "\"optionalFixed32\":7,"
+ "\"optionalFixed64\":\"8\","
+ "\"optionalSfixed32\":9,"
+ "\"optionalSfixed64\":\"10\","
+ "\"optionalFloat\":11.0,"
+ "\"optionalDouble\":12.0,"
+ "\"optionalBool\":true,"
+ "\"optionalString\":\"abc\","
+ "\"optionalBytes\":\"QUI=\","
+ "\"optionalNestedMessage\":{\"bb\":7},"
+ "\"optionalForeignMessage\":{\"c\":88},"
+ "\"optionalImportMessage\":{\"d\":-9},"
+ "\"optionalNestedEnum\":\"BAZ\","
+ "\"optionalForeignEnum\":\"FOREIGN_BAZ\","
// + "\"optionalImportEnum\":\"IMPORT_BAZ\","
+ "\"optionalPublicImportMessage\":{\"e\":-999999},"
+ "\"repeatedInt32\":[1,2],"
+ "\"repeatedInt64\":[\"3\",\"4\"],"
+ "\"repeatedUint32\":[5,6],"
+ "\"repeatedUint64\":[\"7\",\"8\"],"
+ "\"repeatedSint32\":[9,10],"
+ "\"repeatedSint64\":[\"11\",\"12\"],"
+ "\"repeatedFixed32\":[13,14],"
+ "\"repeatedFixed64\":[\"15\",\"16\"],"
+ "\"repeatedSfixed32\":[17,18],"
+ "\"repeatedSfixed64\":[\"19\",\"20\"],"
+ "\"repeatedFloat\":[21.0,22.0],"
+ "\"repeatedDouble\":[23.0,24.0],"
+ "\"repeatedBool\":[true,false],"
+ "\"repeatedString\":[\"abc\",\"def\"],"
+ "\"repeatedBytes\":[\"\",\"QUI=\"],"
+ "\"repeatedNestedMessage\":[{\"bb\":7},{\"bb\":-7}],"
+ "\"repeatedForeignMessage\":[{\"c\":88},{\"c\":-88}],"
+ "\"repeatedNestedEnum\":[\"BAR\",\"BAZ\"],"
+ "\"repeatedForeignEnum\":[\"FOREIGN_BAR\",\"FOREIGN_BAZ\"],"
+ "\"oneofUint32\":99"
+ "},{}]")
assertJSONArrayEncode(expected, configure: configureTwoObjects)
}
func testRepeatedNestedMessage() {
assertJSONArrayEncode(
"[{\"repeatedNestedMessage\":[{\"bb\":1}]},{\"repeatedNestedMessage\":[{\"bb\":1},{\"bb\":2}]}]"
) { (o: inout [MessageTestType]) in
var o1 = MessageTestType()
var sub1 = SwiftProtoTesting_Proto3_TestAllTypes.NestedMessage()
sub1.bb = 1
o1.repeatedNestedMessage = [sub1]
o.append(o1)
var o2 = MessageTestType()
var sub2 = SwiftProtoTesting_Proto3_TestAllTypes.NestedMessage()
sub2.bb = 1
var sub3 = SwiftProtoTesting_Proto3_TestAllTypes.NestedMessage()
sub3.bb = 2
o2.repeatedNestedMessage = [sub2, sub3]
o.append(o2)
}
assertJSONArrayDecodeSucceeds("[{\"repeatedNestedMessage\": []}]") {
$0[0].repeatedNestedMessage == []
}
assertJSONArrayDecodeFails("{\"repeatedNestedMessage\": []}")
}
}