|
| 1 | +package org.inferred.freebuilder.processor.property; |
| 2 | + |
| 3 | +import com.google.common.testing.EqualsTester; |
| 4 | + |
| 5 | +import org.inferred.freebuilder.FreeBuilder; |
| 6 | +import org.inferred.freebuilder.processor.Processor; |
| 7 | +import org.inferred.freebuilder.processor.source.SourceBuilder; |
| 8 | +import org.inferred.freebuilder.processor.source.feature.FeatureSet; |
| 9 | +import org.inferred.freebuilder.processor.source.feature.StaticFeatureSet; |
| 10 | +import org.inferred.freebuilder.processor.source.testing.BehaviorTester; |
| 11 | +import org.inferred.freebuilder.processor.source.testing.TestBuilder; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.rules.ExpectedException; |
| 15 | + |
| 16 | +public class ArrayPropertiesTest { |
| 17 | + |
| 18 | + @Rule public final ExpectedException thrown = ExpectedException.none(); |
| 19 | + |
| 20 | + private final FeatureSet features = new StaticFeatureSet(); |
| 21 | + private final BehaviorTester behaviorTester = BehaviorTester.create(features); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testToString() { |
| 25 | + behaviorTester |
| 26 | + .with(new Processor(features)) |
| 27 | + .with(SourceBuilder.forTesting() |
| 28 | + .addLine("package com.example;") |
| 29 | + .addLine("@%s", FreeBuilder.class) |
| 30 | + .addLine("public interface DataType {") |
| 31 | + .addLine(" int[] ints();") |
| 32 | + .addLine(" String[] strings();") |
| 33 | + .addLine("") |
| 34 | + .addLine(" class Builder extends DataType_Builder {}") |
| 35 | + .addLine("}")) |
| 36 | + .with(testBuilder() |
| 37 | + .addLine("DataType value = new DataType.Builder()") |
| 38 | + .addLine(" .ints(new int[] { 1, 2, 3 })") |
| 39 | + .addLine(" .strings(new String[] { \"a\", \"b\", \"c\" })") |
| 40 | + .addLine(" .build();") |
| 41 | + .addLine("assertThat(value.toString()).isEqualTo(") |
| 42 | + .addLine(" \"DataType{ints=[1, 2, 3], strings=[a, b, c]}\");") |
| 43 | + .build()) |
| 44 | + .runTest(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testEquality() { |
| 49 | + behaviorTester |
| 50 | + .with(new Processor(features)) |
| 51 | + .with(SourceBuilder.forTesting() |
| 52 | + .addLine("package com.example;") |
| 53 | + .addLine("@%s", FreeBuilder.class) |
| 54 | + .addLine("public interface DataType {") |
| 55 | + .addLine(" int[] ints();") |
| 56 | + .addLine("") |
| 57 | + .addLine(" class Builder extends DataType_Builder {}") |
| 58 | + .addLine("}")) |
| 59 | + .with(testBuilder() |
| 60 | + .addLine("int[] a = { 1, 2, 3 };") |
| 61 | + .addLine("int[] b = { 1, 2, 3 };") |
| 62 | + .addLine("int[] c = new int[0];") |
| 63 | + .addLine("new %s()", EqualsTester.class) |
| 64 | + .addLine(" .addEqualityGroup(") |
| 65 | + .addLine(" new DataType.Builder().ints(a).build(),") |
| 66 | + .addLine(" new DataType.Builder().ints(a).build())") |
| 67 | + .addLine(" .addEqualityGroup(") |
| 68 | + .addLine(" new DataType.Builder().ints(b).build(),") |
| 69 | + .addLine(" new DataType.Builder().ints(b).build())") |
| 70 | + .addLine(" .addEqualityGroup(") |
| 71 | + .addLine(" new DataType.Builder().ints(c).build(),") |
| 72 | + .addLine(" new DataType.Builder().ints(c).build())") |
| 73 | + .addLine(" .testEquals();") |
| 74 | + .build()) |
| 75 | + .runTest(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void issuesMutabilityWarning() { |
| 80 | + behaviorTester |
| 81 | + .with(new Processor(features)) |
| 82 | + .with(SourceBuilder.forTesting() |
| 83 | + .addLine("package com.example;") |
| 84 | + .addLine("@%s", FreeBuilder.class) |
| 85 | + .addLine("public interface DataType {") |
| 86 | + .addLine(" int[] ints();") |
| 87 | + .addLine("") |
| 88 | + .addLine(" class Builder extends DataType_Builder {}") |
| 89 | + .addLine("}")) |
| 90 | + .compiles() |
| 91 | + .withWarningThat(warning -> warning |
| 92 | + .hasMessage("This property returns a mutable array that can be modified by the caller. " |
| 93 | + + "FreeBuilder will use reference equality for this property. If possible, prefer " |
| 94 | + + "an immutable type like List. You can suppress this warning with " |
| 95 | + + "@SuppressWarnings(\"mutable\").") |
| 96 | + .inFile("/com/example/DataType.java") |
| 97 | + .onLine(7)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void canSuppressMutabilityWarningOnClass() { |
| 102 | + behaviorTester |
| 103 | + .with(new Processor(features)) |
| 104 | + .with(SourceBuilder.forTesting() |
| 105 | + .addLine("package com.example;") |
| 106 | + .addLine("@%s", FreeBuilder.class) |
| 107 | + .addLine("@SuppressWarnings(\"mutable\")") |
| 108 | + .addLine("public interface DataType {") |
| 109 | + .addLine(" int[] ints();") |
| 110 | + .addLine("") |
| 111 | + .addLine(" class Builder extends DataType_Builder {}") |
| 112 | + .addLine("}")) |
| 113 | + .compiles() |
| 114 | + .withNoWarnings(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void canSuppressMutabilityWarningOnMethod() { |
| 119 | + behaviorTester |
| 120 | + .with(new Processor(features)) |
| 121 | + .with(SourceBuilder.forTesting() |
| 122 | + .addLine("package com.example;") |
| 123 | + .addLine("@%s", FreeBuilder.class) |
| 124 | + .addLine("public interface DataType {") |
| 125 | + .addLine(" @SuppressWarnings(\"mutable\")") |
| 126 | + .addLine(" int[] ints();") |
| 127 | + .addLine("") |
| 128 | + .addLine(" class Builder extends DataType_Builder {}") |
| 129 | + .addLine("}")) |
| 130 | + .compiles() |
| 131 | + .withNoWarnings(); |
| 132 | + } |
| 133 | + |
| 134 | + private static TestBuilder testBuilder() { |
| 135 | + return new TestBuilder().addImport("com.example.DataType"); |
| 136 | + } |
| 137 | +} |
0 commit comments