Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.serialization.compiler.processing.steps |
| 18 | |
Ember Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 19 | import androidx.serialization.compiler.codegen.CodeGenEnvironment |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 20 | import androidx.serialization.compiler.schema.Enum |
| 21 | import androidx.serialization.schema.Reserved |
| 22 | import com.google.auto.common.BasicAnnotationProcessor |
| 23 | import com.google.common.truth.Correspondence |
| 24 | import com.google.common.truth.Truth.assertThat |
| 25 | import com.google.testing.compile.Compilation |
| 26 | import com.google.testing.compile.CompilationSubject.assertThat |
| 27 | import com.google.testing.compile.Compiler.javac |
| 28 | import com.google.testing.compile.JavaFileObjects |
| 29 | import org.junit.Test |
| 30 | import javax.lang.model.SourceVersion |
| 31 | import javax.tools.JavaFileObject |
| 32 | |
Ember Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 33 | /** Unit tests for [EnumProcessingStep]. */ |
| 34 | class EnumProcessingStepTest { |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 35 | 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 Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 41 | fun testParsing() { |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 42 | 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 Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 60 | 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 Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 74 | "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 Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 93 | } |
| 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 Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 127 | @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 Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 147 | 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 Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 156 | return processor.enum |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private class SchemaCompilationProcessor : BasicAnnotationProcessor() { |
Ember Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 160 | lateinit var enum: Enum |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 161 | |
Ember Rose | f674dd5 | 2020-01-15 13:22:28 -0500 | [diff] [blame^] | 162 | override fun initSteps(): List<ProcessingStep> { |
| 163 | val codeGenEnv = CodeGenEnvironment(EnumProcessingStepTest::class.qualifiedName) |
| 164 | return listOf(EnumProcessingStep(processingEnv, codeGenEnv) { enum = it }) |
| 165 | } |
Ember Rose | 8e6c420 | 2020-01-10 15:32:34 -0500 | [diff] [blame] | 166 | |
| 167 | override fun getSupportedSourceVersion(): SourceVersion { |
| 168 | return SourceVersion.latest() |
| 169 | } |
| 170 | } |
| 171 | } |