• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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 package com.google.auto.value;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.JUnit4;
23 
24 @RunWith(JUnit4.class)
25 public final class AutoBuilderKotlinTest {
26   @AutoBuilder(ofClass = KotlinData.class)
27   abstract static class KotlinDataBuilder {
builder()28     static KotlinDataBuilder builder() {
29       return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataBuilder();
30     }
31 
setInt(int x)32     abstract KotlinDataBuilder setInt(int x);
33 
setString(String x)34     abstract KotlinDataBuilder setString(String x);
35 
build()36     abstract KotlinData build();
37   }
38 
39   @Test
simpleKotlin()40   public void simpleKotlin() {
41     KotlinData x = KotlinDataBuilder.builder().setInt(23).setString("skidoo").build();
42     assertThat(x.getInt()).isEqualTo(23);
43     assertThat(x.getString()).isEqualTo("skidoo");
44   }
45 
46   @AutoBuilder(ofClass = KotlinDataWithNullable.class)
47   abstract static class KotlinDataWithNullableBuilder {
builder()48     static KotlinDataWithNullableBuilder builder() {
49       return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataWithNullableBuilder();
50     }
51 
setAnInt(int x)52     abstract KotlinDataWithNullableBuilder setAnInt(int x);
53 
setAString(String x)54     abstract KotlinDataWithNullableBuilder setAString(String x);
55 
build()56     abstract KotlinDataWithNullable build();
57   }
58 
59   @Test
kotlinWithNullable()60   public void kotlinWithNullable() {
61     KotlinDataWithNullable empty = KotlinDataWithNullableBuilder.builder().build();
62     assertThat(empty.getAnInt()).isNull();
63     assertThat(empty.getAString()).isNull();
64 
65     KotlinDataWithNullable notEmpty =
66         KotlinDataWithNullableBuilder.builder().setAString("answer").setAnInt(42).build();
67     assertThat(notEmpty.getAString()).isEqualTo("answer");
68     assertThat(notEmpty.getAnInt()).isEqualTo(42);
69   }
70 
71   @AutoBuilder(ofClass = KotlinDataWithDefaults.class)
72   abstract static class KotlinDataWithDefaultsBuilder {
builder()73     static KotlinDataWithDefaultsBuilder builder() {
74       return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataWithDefaultsBuilder();
75     }
76 
setAnInt(int x)77     abstract KotlinDataWithDefaultsBuilder setAnInt(int x);
78 
setAString(String x)79     abstract KotlinDataWithDefaultsBuilder setAString(String x);
80 
build()81     abstract KotlinDataWithDefaults build();
82   }
83 
84   @Test
kotlinWithDefaults()85   public void kotlinWithDefaults() {
86     // AutoBuilder doesn't currently try to give the builder the same defaults as the Kotlin class,
87     // but we do at least check that the presence of defaults doesn't throw AutoBuilder off.
88     // When a constructor has default parameters, the Kotlin compiler generates an extra constructor
89     // with two extra parameters: an int bitmask saying which parameters were defaulted, and a
90     // DefaultConstructorMarker parameter to avoid clashing with another constructor that might have
91     // an extra int parameter for some other reason. If AutoBuilder found this constructor it might
92     // be confused, but fortunately the constructor is marked synthetic, and javax.lang.model
93     // doesn't show synthetic elements.
94     KotlinDataWithDefaults x =
95         KotlinDataWithDefaultsBuilder.builder().setAString("answer").setAnInt(42).build();
96     assertThat(x.getAString()).isEqualTo("answer");
97     assertThat(x.getAnInt()).isEqualTo(42);
98   }
99 }
100