Skip to content

Commit 1aa1190

Browse files
committed
Add tests confirming the handling enum prefixes.
There were tests on the Swift code used to generate the enum names, but they didn't completely prove the TextFormat ane JSON format was getting the full names even though the Swift interface has the prefix removed. Tests test ensure the names are as expected yet the two string forms are stil the complete names as expected.
1 parent 9cbea35 commit 1aa1190

10 files changed

+1025
-24
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ TEST_PROTOS= \
118118
Protos/unittest_swift_cycle.proto \
119119
Protos/unittest_swift_enum.proto \
120120
Protos/unittest_swift_enum_optional_default.proto \
121+
Protos/unittest_swift_enum_proto3.proto \
121122
Protos/unittest_swift_extension.proto \
122123
Protos/unittest_swift_extension2.proto \
123124
Protos/unittest_swift_extension3.proto \

Protos/unittest_swift_enum.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ message SwiftEnumTest {
4949
ENUM_TEST_RESERVED_WORD_VAR = 1;
5050
ENUM_TEST_RESERVED_WORD_NOT_RESERVED = 2;
5151
}
52+
53+
repeated EnumTest1 values1 = 1;
54+
repeated EnumTest2 values2 = 2;
55+
repeated EnumTestNoStem values3 = 3;
56+
repeated EnumTestReservedWord values4 = 4;
5257
}
5358

5459
message SwiftEnumWithAliasTest {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2015 Apple, 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+
31+
syntax = "proto3";
32+
33+
package protobuf3_unittest;
34+
35+
message SwiftEnumTest {
36+
enum EnumTest1 {
37+
ENUM_TEST_1_FIRST_VALUE = 0;
38+
ENUM_TEST_1_SECOND_VALUE = 2;
39+
}
40+
enum EnumTest2 {
41+
ENUM_TEST_2_FIRST_VALUE = 0;
42+
SECOND_VALUE = 2;
43+
}
44+
enum EnumTestNoStem {
45+
ENUM_TEST_NO_STEM_1 = 0;
46+
ENUM_TEST_NO_STEM_2 = 2;
47+
}
48+
enum EnumTestReservedWord {
49+
ENUM_TEST_RESERVED_WORD_VAR = 0;
50+
ENUM_TEST_RESERVED_WORD_NOT_RESERVED = 2;
51+
}
52+
53+
repeated EnumTest1 values1 = 1;
54+
repeated EnumTest2 values2 = 2;
55+
repeated EnumTestNoStem values3 = 3;
56+
repeated EnumTestReservedWord values4 = 4;
57+
}
58+
59+
message SwiftEnumWithAliasTest {
60+
enum EnumWithAlias {
61+
option allow_alias = true;
62+
FOO1 = 0;
63+
FOO2 = 0;
64+
BAR1 = 2;
65+
BAR2 = 2;
66+
}
67+
repeated EnumWithAlias values = 1 [packed=true];
68+
}

Reference/unittest_swift_enum.pb.swift

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAP
5252
struct ProtobufUnittest_SwiftEnumTest: SwiftProtobuf.Message {
5353
static let protoMessageName: String = _protobuf_package + ".SwiftEnumTest"
5454

55+
var values1: [ProtobufUnittest_SwiftEnumTest.EnumTest1] = []
56+
57+
var values2: [ProtobufUnittest_SwiftEnumTest.EnumTest2] = []
58+
59+
var values3: [ProtobufUnittest_SwiftEnumTest.EnumTestNoStem] = []
60+
61+
var values4: [ProtobufUnittest_SwiftEnumTest.EnumTestReservedWord] = []
62+
5563
var unknownFields = SwiftProtobuf.UnknownStorage()
5664

5765
enum EnumTest1: SwiftProtobuf.Enum {
@@ -165,7 +173,14 @@ struct ProtobufUnittest_SwiftEnumTest: SwiftProtobuf.Message {
165173
/// initializers are defined in the SwiftProtobuf library. See the Message and
166174
/// Message+*Additions` files.
167175
mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
168-
while let _ = try decoder.nextFieldNumber() {
176+
while let fieldNumber = try decoder.nextFieldNumber() {
177+
switch fieldNumber {
178+
case 1: try decoder.decodeRepeatedEnumField(value: &self.values1)
179+
case 2: try decoder.decodeRepeatedEnumField(value: &self.values2)
180+
case 3: try decoder.decodeRepeatedEnumField(value: &self.values3)
181+
case 4: try decoder.decodeRepeatedEnumField(value: &self.values4)
182+
default: break
183+
}
169184
}
170185
}
171186

@@ -174,6 +189,18 @@ struct ProtobufUnittest_SwiftEnumTest: SwiftProtobuf.Message {
174189
/// other serializer methods are defined in the SwiftProtobuf library. See the
175190
/// `Message` and `Message+*Additions` files.
176191
func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
192+
if !self.values1.isEmpty {
193+
try visitor.visitRepeatedEnumField(value: self.values1, fieldNumber: 1)
194+
}
195+
if !self.values2.isEmpty {
196+
try visitor.visitRepeatedEnumField(value: self.values2, fieldNumber: 2)
197+
}
198+
if !self.values3.isEmpty {
199+
try visitor.visitRepeatedEnumField(value: self.values3, fieldNumber: 3)
200+
}
201+
if !self.values4.isEmpty {
202+
try visitor.visitRepeatedEnumField(value: self.values4, fieldNumber: 4)
203+
}
177204
try unknownFields.traverse(visitor: &visitor)
178205
}
179206
}
@@ -245,9 +272,18 @@ struct ProtobufUnittest_SwiftEnumWithAliasTest: SwiftProtobuf.Message {
245272
fileprivate let _protobuf_package = "protobuf_unittest"
246273

247274
extension ProtobufUnittest_SwiftEnumTest: SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
248-
static let _protobuf_nameMap = SwiftProtobuf._NameMap()
275+
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
276+
1: .same(proto: "values1"),
277+
2: .same(proto: "values2"),
278+
3: .same(proto: "values3"),
279+
4: .same(proto: "values4"),
280+
]
249281

250282
func _protobuf_generated_isEqualTo(other: ProtobufUnittest_SwiftEnumTest) -> Bool {
283+
if self.values1 != other.values1 {return false}
284+
if self.values2 != other.values2 {return false}
285+
if self.values3 != other.values3 {return false}
286+
if self.values4 != other.values4 {return false}
251287
if unknownFields != other.unknownFields {return false}
252288
return true
253289
}

0 commit comments

Comments
 (0)