[go: nahoru, domu]

blob: d83a3eeb5aba44b39434384dc41c710aea46f4b3 [file] [log] [blame]
Ember Rose8e6c4202020-01-10 15:32:34 -05001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.serialization.compiler.processing.steps
18
Ember Rosef674dd52020-01-15 13:22:28 -050019import androidx.serialization.compiler.codegen.CodeGenEnvironment
Ember Rose8e6c4202020-01-10 15:32:34 -050020import androidx.serialization.compiler.schema.Enum
21import androidx.serialization.schema.Reserved
22import com.google.auto.common.BasicAnnotationProcessor
23import com.google.common.truth.Correspondence
24import com.google.common.truth.Truth.assertThat
25import com.google.testing.compile.Compilation
26import com.google.testing.compile.CompilationSubject.assertThat
27import com.google.testing.compile.Compiler.javac
28import com.google.testing.compile.JavaFileObjects
29import org.junit.Test
30import javax.lang.model.SourceVersion
31import javax.tools.JavaFileObject
32
Ember Rosef674dd52020-01-15 13:22:28 -050033/** Unit tests for [EnumProcessingStep]. */
34class EnumProcessingStepTest {
Ember Rose8e6c4202020-01-10 15:32:34 -050035 private val enumValueCorrespondence = Correspondence.from({
36 actual: Enum.Value?, expected: Pair<Int, String>? ->
37 actual?.id == expected?.first && actual?.name == expected?.second
38 }, "has ID and name")
39
40 @Test
Ember Rosef674dd52020-01-15 13:22:28 -050041 fun testParsing() {
Ember Rose8e6c4202020-01-10 15:32:34 -050042 val enum = compileEnum(JavaFileObjects.forSourceString("TestEnum", """
43 import androidx.serialization.EnumValue;
44
45 public enum TestEnum {
46 @EnumValue(EnumValue.DEFAULT)
47 DEFAULT,
48 @EnumValue(1)
49 ONE
50 }
51 """.trimIndent()))
52
53 assertThat(enum.values)
54 .comparingElementsUsing(enumValueCorrespondence)
55 .containsExactly(0 to "DEFAULT", 1 to "ONE")
56 assertThat(enum.reserved).isSameInstanceAs(Reserved.empty())
57 }
58
59 @Test
Ember Rose8e6c4202020-01-10 15:32:34 -050060 fun testInvalidPrivateEnum() {
61 val testEnum = JavaFileObjects.forSourceString("com.example.PrivateEnumTest", """
62 package com.example;
63
64 import androidx.serialization.EnumValue;
65
66 public class PrivateEnumTest {
67 private enum PrivateEnum {
68 @EnumValue(0) TEST
69 }
70 }
71 """.trimIndent())
72
73 assertThat(compile(testEnum)).hadErrorContaining(
Ember Rosef674dd52020-01-15 13:22:28 -050074 "Enum com.example.PrivateEnumTest.PrivateEnum is private and cannot be serialized")
75 }
76
77 @Test
78 fun testInvalidPrivateNestedEnum() {
79 val testEnum = JavaFileObjects.forSourceString("PrivateNestedEnumTest", """
80 import androidx.serialization.EnumValue;
81
82 public class PrivateNestedEnumTest {
83 private static class NestedClass {
84 public enum NestedEnum {
85 @EnumValue(EnumValue.DEFAULT) TEST
86 }
87 }
88 }
89 """.trimIndent())
90
91 assertThat(compile(testEnum)).hadErrorContaining(
92 "Enum PrivateNestedEnumTest.NestedClass.NestedEnum is not visible to its package")
Ember Rose8e6c4202020-01-10 15:32:34 -050093 }
94
95 @Test
96 fun testInvalidEnumValueAnnotationLocation() {
97 val testField = JavaFileObjects.forSourceString("EnumValueFieldTest", """
98 import androidx.serialization.EnumValue;
99
100 public class EnumValueFieldTest {
101 @EnumValue(0)
102 public int foo;
103 }
104 """.trimIndent())
105
106 assertThat(compile(testField))
107 .hadErrorContaining("@EnumValue must annotate an enum constant")
108 }
109
110 @Test
111 fun testInvalidMissingEnumValue() {
112 val testEnum = JavaFileObjects.forSourceString("MissingEnumValue", """
113 import androidx.serialization.EnumValue;
114
115 public enum MissingEnumValue {
116 @EnumValue(0)
117 ZERO,
118 ONE
119 }
120 """.trimIndent())
121
122 assertThat(compile(testEnum)).hadErrorContaining(
123 "To avoid unexpected behavior, all enum constants in a serializable enum must be " +
124 "annotated with @EnumValue")
125 }
126
Ember Rosef674dd52020-01-15 13:22:28 -0500127 @Test
128 fun testCoderGeneration() {
129 val testEnum = JavaFileObjects.forSourceString("com.example.Test", """
130 package com.example;
131 import androidx.serialization.EnumValue;
132
133 public enum Test {
134 @EnumValue(EnumValue.DEFAULT)
135 DEFAULT,
136 @EnumValue(1)
137 ONE,
138 @EnumValue(2)
139 TWO
140 }
141 """.trimIndent())
142
143 assertThat(compile(testEnum))
144 .generatedSourceFile("com.example.\$SerializationTestEnumCoder")
145 }
146
Ember Rose8e6c4202020-01-10 15:32:34 -0500147 private fun compile(vararg sources: JavaFileObject): Compilation {
148 return javac().withProcessors(SchemaCompilationProcessor()).compile(*sources)
149 }
150
151 private fun compileEnum(source: JavaFileObject): Enum {
152 val processor = SchemaCompilationProcessor()
153 assertThat(javac().withProcessors(processor).compile(source))
154 .succeededWithoutWarnings()
155
Ember Rosef674dd52020-01-15 13:22:28 -0500156 return processor.enum
Ember Rose8e6c4202020-01-10 15:32:34 -0500157 }
158
159 private class SchemaCompilationProcessor : BasicAnnotationProcessor() {
Ember Rosef674dd52020-01-15 13:22:28 -0500160 lateinit var enum: Enum
Ember Rose8e6c4202020-01-10 15:32:34 -0500161
Ember Rosef674dd52020-01-15 13:22:28 -0500162 override fun initSteps(): List<ProcessingStep> {
163 val codeGenEnv = CodeGenEnvironment(EnumProcessingStepTest::class.qualifiedName)
164 return listOf(EnumProcessingStep(processingEnv, codeGenEnv) { enum = it })
165 }
Ember Rose8e6c4202020-01-10 15:32:34 -0500166
167 override fun getSupportedSourceVersion(): SourceVersion {
168 return SourceVersion.latest()
169 }
170 }
171}