Skip to content

Commit

Permalink
AutoOneOfProcessor: use Arrays#toString for stringifying arrays, as A…
Browse files Browse the repository at this point in the history
…utoValue already does.

RELNOTES=Generate better toStrings for arrays in AutoOneOf

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=281513198
  • Loading branch information
graememorgan authored and nick-someone committed Nov 21, 2019
1 parent b9280eb commit 0a7c049
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,10 @@ public void chomp() {}

@AutoOneOf(Pet.Kind.class)
public abstract static class Pet {

public static Pet create(Dog dog) {
return AutoOneOf_AutoOneOfTest_Pet.dog(dog);
}

public static Pet create(Cat cat) {
return AutoOneOf_AutoOneOfTest_Pet.cat(cat);
}

public static Pet create(TigerShark shark) {
return AutoOneOf_AutoOneOfTest_Pet.tigerShark(shark);
public enum Kind {
DOG,
CAT,
TIGER_SHARK
}

public abstract Dog dog();
Expand All @@ -84,10 +77,16 @@ public static Pet create(TigerShark shark) {

public abstract TigerShark tigerShark();

public enum Kind {
DOG,
CAT,
TIGER_SHARK
public static Pet dog(Dog dog) {
return AutoOneOf_AutoOneOfTest_Pet.dog(dog);
}

public static Pet cat(Cat cat) {
return AutoOneOf_AutoOneOfTest_Pet.cat(cat);
}

public static Pet tigerShark(TigerShark shark) {
return AutoOneOf_AutoOneOfTest_Pet.tigerShark(shark);
}

public abstract Kind getKind();
Expand All @@ -96,11 +95,11 @@ public enum Kind {
@Test
public void equality() {
Dog marvin1 = Dog.create("Marvin");
Pet petMarvin1 = Pet.create(marvin1);
Pet petMarvin1 = Pet.dog(marvin1);
Dog marvin2 = Dog.create("Marvin");
Pet petMarvin2 = Pet.create(marvin2);
Pet petMarvin2 = Pet.dog(marvin2);
Dog isis = Dog.create("Isis");
Pet petIsis = Pet.create(isis);
Pet petIsis = Pet.dog(isis);
Cat cat = Cat.create();
new EqualsTester()
.addEqualityGroup(marvin1, marvin2)
Expand All @@ -114,14 +113,14 @@ public void equality() {
@Test
public void getCorrectType() {
Dog marvin = Dog.create("Marvin");
Pet petMarvin = Pet.create(marvin);
Pet petMarvin = Pet.dog(marvin);
assertThat(petMarvin.dog()).isSameInstanceAs(marvin);
}

@Test
public void getWrongType() {
Cat cat = Cat.create();
Pet petCat = Pet.create(cat);
Pet petCat = Pet.cat(cat);
try {
petCat.tigerShark();
fail();
Expand All @@ -133,18 +132,18 @@ public void getWrongType() {
@Test
public void string() {
Dog marvin = Dog.create("Marvin");
Pet petMarvin = Pet.create(marvin);
Pet petMarvin = Pet.dog(marvin);
assertThat(petMarvin.toString()).isEqualTo("Pet{dog=Dog{name=Marvin}}");
}

@Test
public void getKind() {
Dog marvin = Dog.create("Marvin");
Pet petMarvin = Pet.create(marvin);
Pet petMarvin = Pet.dog(marvin);
Cat cat = Cat.create();
Pet petCat = Pet.create(cat);
Pet petCat = Pet.cat(cat);
TigerShark shark = TigerShark.create();
Pet petShark = Pet.create(shark);
Pet petShark = Pet.tigerShark(shark);
assertThat(petMarvin.getKind()).isEqualTo(Pet.Kind.DOG);
assertThat(petCat.getKind()).isEqualTo(Pet.Kind.CAT);
assertThat(petShark.getKind()).isEqualTo(Pet.Kind.TIGER_SHARK);
Expand All @@ -153,7 +152,7 @@ public void getKind() {
@Test
public void cannotBeNull() {
try {
Pet.create((Dog) null);
Pet.dog(null);
fail();
} catch (NullPointerException expected) {
}
Expand Down Expand Up @@ -208,15 +207,15 @@ public abstract static class PetWithGet {

public abstract TigerShark getTigerShark();

public static PetWithGet create(Dog dog) {
public static PetWithGet dog(Dog dog) {
return AutoOneOf_AutoOneOfTest_PetWithGet.dog(dog);
}

public static PetWithGet create(Cat cat) {
public static PetWithGet cat(Cat cat) {
return AutoOneOf_AutoOneOfTest_PetWithGet.cat(cat);
}

public static PetWithGet create(TigerShark shark) {
public static PetWithGet tigerShark(TigerShark shark) {
return AutoOneOf_AutoOneOfTest_PetWithGet.tigerShark(shark);
}

Expand All @@ -226,7 +225,7 @@ public static PetWithGet create(TigerShark shark) {
@Test
public void getPrefix() {
Dog marvin = Dog.create("Marvin");
PetWithGet petMarvin = PetWithGet.create(marvin);
PetWithGet petMarvin = PetWithGet.dog(marvin);
assertThat(petMarvin.toString()).isEqualTo("PetWithGet{dog=Dog{name=Marvin}}");
}

Expand Down Expand Up @@ -309,7 +308,7 @@ public enum Kind {

public abstract Dog getDog();

public static OneOfOne create(Dog dog) {
public static OneOfOne dog(Dog dog) {
return AutoOneOf_AutoOneOfTest_OneOfOne.dog(dog);
}

Expand All @@ -319,7 +318,7 @@ public static OneOfOne create(Dog dog) {
@Test
public void oneOfOne() {
Dog marvin = Dog.create("Marvin");
OneOfOne oneOfMarvin = OneOfOne.create(marvin);
OneOfOne oneOfMarvin = OneOfOne.dog(marvin);
assertThat(oneOfMarvin.toString()).isEqualTo("OneOfOne{dog=Dog{name=Marvin}}");
assertThat(oneOfMarvin.getKind()).isEqualTo(OneOfOne.Kind.DOG);
}
Expand Down Expand Up @@ -636,4 +635,26 @@ public void voidPropertyWrongType() {
assertThat(e).hasMessageThat().containsMatch("(?i:string)");
}
}

@AutoOneOf(OneOfArray.Kind.class)
public abstract static class OneOfArray {
public enum Kind {
INTS
}

public abstract Kind getKind();

@SuppressWarnings("mutable")
public abstract int[] ints();

public static OneOfArray ofInts(int[] s) {
return AutoOneOf_AutoOneOfTest_OneOfArray.ints(s);
}
}

@Test
public void arrayToString() {
OneOfArray oneOfArray = OneOfArray.ofInts(new int[] {1, 2});
assertThat(oneOfArray.toString()).isEqualTo("OneOfArray{ints=[1, 2]}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ final class $generatedClass {

@Override
public String toString() {
return "${simpleClassName}{$p.name=" + this.$p + "}";
return "${simpleClassName}{$p.name=" +
#if ($p.kind == "ARRAY")
`java.util.Arrays`.toString(this.$p)
#else
this.$p
#end + "}";
}

#end
Expand Down

0 comments on commit 0a7c049

Please sign in to comment.