|
15 | 15 | package dev.cel.protobuf; |
16 | 16 |
|
17 | 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static org.junit.Assert.assertThrows; |
18 | 19 |
|
| 20 | +import com.google.protobuf.WireFormat; |
| 21 | +import com.google.testing.junit.testparameterinjector.TestParameter; |
19 | 22 | import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
20 | 23 | import dev.cel.expr.conformance.proto3.TestAllTypesCelLiteDescriptor; |
21 | 24 | import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor; |
@@ -146,4 +149,96 @@ public void fieldDescriptor_nestedMessage_fullyQualifiedNames() { |
146 | 149 | assertThat(fieldLiteDescriptor.getFieldProtoTypeName()) |
147 | 150 | .isEqualTo("cel.expr.conformance.proto3.TestAllTypes.NestedMessage"); |
148 | 151 | } |
| 152 | + |
| 153 | + private enum ProtoFieldTypeTestCase { |
| 154 | + DOUBLE(FieldLiteDescriptor.Type.DOUBLE, 1, WireFormat.FieldType.DOUBLE), |
| 155 | + FLOAT(FieldLiteDescriptor.Type.FLOAT, 2, WireFormat.FieldType.FLOAT), |
| 156 | + INT64(FieldLiteDescriptor.Type.INT64, 3, WireFormat.FieldType.INT64), |
| 157 | + UINT64(FieldLiteDescriptor.Type.UINT64, 4, WireFormat.FieldType.UINT64), |
| 158 | + INT32(FieldLiteDescriptor.Type.INT32, 5, WireFormat.FieldType.INT32), |
| 159 | + FIXED64(FieldLiteDescriptor.Type.FIXED64, 6, WireFormat.FieldType.FIXED64), |
| 160 | + FIXED32(FieldLiteDescriptor.Type.FIXED32, 7, WireFormat.FieldType.FIXED32), |
| 161 | + BOOL(FieldLiteDescriptor.Type.BOOL, 8, WireFormat.FieldType.BOOL), |
| 162 | + STRING(FieldLiteDescriptor.Type.STRING, 9, WireFormat.FieldType.STRING), |
| 163 | + GROUP(FieldLiteDescriptor.Type.GROUP, 10, WireFormat.FieldType.GROUP), |
| 164 | + MESSAGE(FieldLiteDescriptor.Type.MESSAGE, 11, WireFormat.FieldType.MESSAGE), |
| 165 | + BYTES(FieldLiteDescriptor.Type.BYTES, 12, WireFormat.FieldType.BYTES), |
| 166 | + UINT32(FieldLiteDescriptor.Type.UINT32, 13, WireFormat.FieldType.UINT32), |
| 167 | + ENUM(FieldLiteDescriptor.Type.ENUM, 14, WireFormat.FieldType.ENUM), |
| 168 | + SFIXED32(FieldLiteDescriptor.Type.SFIXED32, 15, WireFormat.FieldType.SFIXED32), |
| 169 | + SFIXED64(FieldLiteDescriptor.Type.SFIXED64, 16, WireFormat.FieldType.SFIXED64), |
| 170 | + SINT32(FieldLiteDescriptor.Type.SINT32, 17, WireFormat.FieldType.SINT32), |
| 171 | + SINT64(FieldLiteDescriptor.Type.SINT64, 18, WireFormat.FieldType.SINT64); |
| 172 | + |
| 173 | + private final FieldLiteDescriptor.Type type; |
| 174 | + private final int expectedNumber; |
| 175 | + private final WireFormat.FieldType expectedWireType; |
| 176 | + |
| 177 | + ProtoFieldTypeTestCase( |
| 178 | + FieldLiteDescriptor.Type type, int expectedNumber, WireFormat.FieldType expectedWireType) { |
| 179 | + this.type = type; |
| 180 | + this.expectedNumber = expectedNumber; |
| 181 | + this.expectedWireType = expectedWireType; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void protoFieldType_numbersAndWireTypes(@TestParameter ProtoFieldTypeTestCase testCase) { |
| 187 | + assertThat(testCase.type.getNumber()).isEqualTo(testCase.expectedNumber); |
| 188 | + assertThat(testCase.type.toWireFormatFieldType()).isEqualTo(testCase.expectedWireType); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void protoFieldType_forNumber_roundTripAllTypes( |
| 193 | + @TestParameter FieldLiteDescriptor.Type type) { |
| 194 | + assertThat(FieldLiteDescriptor.Type.forNumber(type.getNumber())).isEqualTo(type); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void protoFieldType_forNumber_outOfRange_throws( |
| 199 | + @TestParameter({"-2147483648", "-1", "0", "19", "100", "2147483647"}) int invalidNumber) { |
| 200 | + IllegalArgumentException e = |
| 201 | + assertThrows( |
| 202 | + IllegalArgumentException.class, |
| 203 | + () -> FieldLiteDescriptor.Type.forNumber(invalidNumber)); |
| 204 | + |
| 205 | + assertThat(e).hasMessageThat().isEqualTo("Unsupported proto type code: " + invalidNumber); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void fieldLiteDescriptor_nullParameters_throws() { |
| 210 | + assertThrows( |
| 211 | + NullPointerException.class, |
| 212 | + () -> |
| 213 | + new FieldLiteDescriptor( |
| 214 | + 1, |
| 215 | + "field", |
| 216 | + null, |
| 217 | + EncodingType.SINGULAR, |
| 218 | + FieldLiteDescriptor.Type.INT32, |
| 219 | + false, |
| 220 | + "")); |
| 221 | + assertThrows( |
| 222 | + NullPointerException.class, |
| 223 | + () -> |
| 224 | + new FieldLiteDescriptor( |
| 225 | + 1, |
| 226 | + "field", |
| 227 | + FieldLiteDescriptor.JavaType.INT, |
| 228 | + null, |
| 229 | + FieldLiteDescriptor.Type.INT32, |
| 230 | + false, |
| 231 | + "")); |
| 232 | + assertThrows( |
| 233 | + NullPointerException.class, |
| 234 | + () -> |
| 235 | + new FieldLiteDescriptor( |
| 236 | + 1, |
| 237 | + "field", |
| 238 | + FieldLiteDescriptor.JavaType.INT, |
| 239 | + EncodingType.SINGULAR, |
| 240 | + null, |
| 241 | + false, |
| 242 | + "")); |
| 243 | + } |
149 | 244 | } |
0 commit comments