1
1
package arcs.core.data.proto
2
2
3
- import arcs.core.data.PrimitiveType
3
+ import arcs.core.data.*
4
4
import arcs.core.testutil.assertThrows
5
+ import arcs.core.testutil.fail
5
6
import arcs.repoutils.runfilesDir
6
7
import com.google.common.truth.Truth.assertThat
7
8
import com.google.protobuf.Message.Builder
@@ -12,6 +13,15 @@ import org.junit.Test
12
13
import org.junit.runner.RunWith
13
14
import org.junit.runners.JUnit4
14
15
16
+ /* *
17
+ * Parses a given proto text as [TypeProto].
18
+ */
19
+ fun parseTypeProtoText (protoText : String ): TypeProto {
20
+ val builder = TypeProto .newBuilder()
21
+ TextFormat .getParser().merge(protoText, builder)
22
+ return builder.build()
23
+ }
24
+
15
25
@RunWith(JUnit4 ::class )
16
26
class TypeProtoDecodersTest {
17
27
@Test
@@ -23,4 +33,33 @@ class TypeProtoDecodersTest {
23
33
PrimitiveTypeProto .UNRECOGNIZED .decode()
24
34
}
25
35
}
36
+
37
+ @Test
38
+ fun decodesPrimitiveTypeAsFieldType () {
39
+ val textField = PrimitiveTypeProto .TEXT .decodeAsFieldType()
40
+ assertThat(textField.primitiveType).isEqualTo(PrimitiveType .Text )
41
+ val numberField = PrimitiveTypeProto .NUMBER .decodeAsFieldType()
42
+ assertThat(numberField.primitiveType).isEqualTo(PrimitiveType .Number )
43
+ val booleanField = PrimitiveTypeProto .BOOLEAN .decodeAsFieldType()
44
+ assertThat(booleanField.primitiveType).isEqualTo(PrimitiveType .Boolean )
45
+ }
46
+
47
+ @Test
48
+ fun decodesTypeProtoAsFieldType () {
49
+ fun checkPrimitive (textProto : String , expected : PrimitiveType ) {
50
+ val primitiveTypeProto = parseTypeProtoText(textProto)
51
+ val field = primitiveTypeProto.decodeAsFieldType()
52
+ when (field) {
53
+ is FieldType .Primitive ->
54
+ assertThat(field.primitiveType).isEqualTo(expected)
55
+ else -> fail(" TypeProto should have been decoded to [FieldType.Primitive]." )
56
+ }
57
+ }
58
+ checkPrimitive(" primitive: TEXT" , PrimitiveType .Text )
59
+ checkPrimitive(" primitive: BOOLEAN" , PrimitiveType .Boolean )
60
+ checkPrimitive(" primitive: NUMBER" , PrimitiveType .Number )
61
+ assertThrows(IllegalArgumentException ::class ) {
62
+ checkPrimitive(""" variable: { name: "Blah"}""" , PrimitiveType .Text )
63
+ }
64
+ }
26
65
}
0 commit comments