Skip to content

Commit

Permalink
Fix flaky tests in FieldNamingTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhu5 committed Nov 3, 2023
1 parent e685705 commit 8aaad56
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions gson/src/test/java/com/google/gson/functional/FieldNamingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,61 @@
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;
import java.util.Arrays;

public final class FieldNamingTest {
@Test
public void testIdentity() {
Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'lowerId':8,'_9':9}");
String[] matches = {"'lowerCamel':1","'UpperCamel':2","'_lowerCamelLeadingUnderscore':3",
"'_UpperCamelLeadingUnderscore':4","'lower_words':5","'UPPER_WORDS':6",
"'annotatedName':7","'lowerId':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

@Test
public void testUpperCamelCase() {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'LowerId':8,'_9':9}");
String[] matches = {"'LowerCamel':1","'UpperCamel':2","'_LowerCamelLeadingUnderscore':3",
"'_UpperCamelLeadingUnderscore':4","'Lower_words':5","'UPPER_WORDS':6",
"'annotatedName':7","'LowerId':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

@Test
public void testUpperCamelCaseWithSpaces() {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," +
"'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
"'annotatedName':7,'Lower Id':8,'_9':9}");
String[] matches = {"'Lower Camel':1","'Upper Camel':2","'_Lower Camel Leading Underscore':3",
"'_ Upper Camel Leading Underscore':4","'Lower_words':5","'U P P E R_ W O R D S':6",
"'annotatedName':7","'Lower Id':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

@Test
public void testUpperCaseWithUnderscores() {
Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," +
"'__UPPER_CAMEL_LEADING_UNDERSCORE':4,'LOWER_WORDS':5,'U_P_P_E_R__W_O_R_D_S':6," +
"'annotatedName':7,'LOWER_ID':8,'_9':9}");
}
String[] matches = {"'LOWER_CAMEL':1","'UPPER_CAMEL':2","'_LOWER_CAMEL_LEADING_UNDERSCORE':3",
"'__UPPER_CAMEL_LEADING_UNDERSCORE':4","'LOWER_WORDS':5","'U_P_P_E_R__W_O_R_D_S':6",
"'annotatedName':7","'LOWER_ID':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

@Test
public void testLowerCaseWithUnderscores() {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," +
"'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
"'annotatedName':7,'lower_id':8,'_9':9}");
}
String[] matches = {"'lower_camel':1","'upper_camel':2","'_lower_camel_leading_underscore':3",
"'__upper_camel_leading_underscore':4","'lower_words':5","'u_p_p_e_r__w_o_r_d_s':6",
"'annotatedName':7","'lower_id':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

@Test
public void testLowerCaseWithDashes() {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
assertThat(gson.toJson(new TestNames()).replace('\"', '\''))
.isEqualTo("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," +
"'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
"'annotatedName':7,'lower-id':8,'_9':9}");
String[] matches = {"'lower-camel':1","'upper-camel':2","'_lower-camel-leading-underscore':3",
"'_-upper-camel-leading-underscore':4","'lower_words':5","'u-p-p-e-r_-w-o-r-d-s':6",
"'annotatedName':7","'lower-id':8","'_9':9"};
assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue();
}

private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){
Expand Down

0 comments on commit 8aaad56

Please sign in to comment.