aboutsummaryrefslogtreecommitdiff
path: root/value/src/it/functional/src/test/java/com/google/auto/value/AutoBuilderKotlinTest.java
blob: 1dc346c92f59262b0a0f75448558629aafbdd75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.auto.value;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class AutoBuilderKotlinTest {
  @AutoBuilder(ofClass = KotlinData.class)
  abstract static class KotlinDataBuilder {
    static KotlinDataBuilder builder() {
      return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataBuilder();
    }

    abstract KotlinDataBuilder setInt(int x);

    abstract KotlinDataBuilder setString(String x);

    abstract KotlinData build();
  }

  @Test
  public void simpleKotlin() {
    KotlinData x = KotlinDataBuilder.builder().setInt(23).setString("skidoo").build();
    assertThat(x.getInt()).isEqualTo(23);
    assertThat(x.getString()).isEqualTo("skidoo");
  }

  @AutoBuilder(ofClass = KotlinDataWithNullable.class)
  abstract static class KotlinDataWithNullableBuilder {
    static KotlinDataWithNullableBuilder builder() {
      return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataWithNullableBuilder();
    }

    abstract KotlinDataWithNullableBuilder setAnInt(int x);

    abstract KotlinDataWithNullableBuilder setAString(String x);

    abstract KotlinDataWithNullable build();
  }

  @Test
  public void kotlinWithNullable() {
    KotlinDataWithNullable empty = KotlinDataWithNullableBuilder.builder().build();
    assertThat(empty.getAnInt()).isNull();
    assertThat(empty.getAString()).isNull();

    KotlinDataWithNullable notEmpty =
        KotlinDataWithNullableBuilder.builder().setAString("answer").setAnInt(42).build();
    assertThat(notEmpty.getAString()).isEqualTo("answer");
    assertThat(notEmpty.getAnInt()).isEqualTo(42);
  }

  @AutoBuilder(ofClass = KotlinDataWithDefaults.class)
  abstract static class KotlinDataWithDefaultsBuilder {
    static KotlinDataWithDefaultsBuilder builder() {
      return new AutoBuilder_AutoBuilderKotlinTest_KotlinDataWithDefaultsBuilder();
    }

    abstract KotlinDataWithDefaultsBuilder setAnInt(int x);

    abstract KotlinDataWithDefaultsBuilder setAString(String x);

    abstract KotlinDataWithDefaults build();
  }

  @Test
  public void kotlinWithDefaults() {
    // AutoBuilder doesn't currently try to give the builder the same defaults as the Kotlin class,
    // but we do at least check that the presence of defaults doesn't throw AutoBuilder off.
    // When a constructor has default parameters, the Kotlin compiler generates an extra constructor
    // with two extra parameters: an int bitmask saying which parameters were defaulted, and a
    // DefaultConstructorMarker parameter to avoid clashing with another constructor that might have
    // an extra int parameter for some other reason. If AutoBuilder found this constructor it might
    // be confused, but fortunately the constructor is marked synthetic, and javax.lang.model
    // doesn't show synthetic elements.
    KotlinDataWithDefaults x =
        KotlinDataWithDefaultsBuilder.builder().setAString("answer").setAnInt(42).build();
    assertThat(x.getAString()).isEqualTo("answer");
    assertThat(x.getAnInt()).isEqualTo(42);
  }
}