Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests in FieldNamingTest #2521

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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