|
29 | 29 | import com.google.gson.GsonBuilder; |
30 | 30 | import com.google.gson.annotations.SerializedName; |
31 | 31 | import org.junit.Test; |
| 32 | +import java.util.Arrays; |
32 | 33 |
|
33 | 34 | public final class FieldNamingTest { |
34 | 35 | @Test |
35 | 36 | public void testIdentity() { |
36 | 37 | Gson gson = getGsonWithNamingPolicy(IDENTITY); |
37 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
38 | | - .isEqualTo("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + |
39 | | - "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + |
40 | | - "'annotatedName':7,'lowerId':8,'_9':9}"); |
| 38 | + String[] matches = {"'lowerCamel':1","'UpperCamel':2","'_lowerCamelLeadingUnderscore':3", |
| 39 | + "'_UpperCamelLeadingUnderscore':4","'lower_words':5","'UPPER_WORDS':6", |
| 40 | + "'annotatedName':7","'lowerId':8","'_9':9"}; |
| 41 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
41 | 42 | } |
42 | 43 |
|
43 | 44 | @Test |
44 | 45 | public void testUpperCamelCase() { |
45 | 46 | Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE); |
46 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
47 | | - .isEqualTo("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + |
48 | | - "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + |
49 | | - "'annotatedName':7,'LowerId':8,'_9':9}"); |
| 47 | + String[] matches = {"'LowerCamel':1","'UpperCamel':2","'_LowerCamelLeadingUnderscore':3", |
| 48 | + "'_UpperCamelLeadingUnderscore':4","'Lower_words':5","'UPPER_WORDS':6", |
| 49 | + "'annotatedName':7","'LowerId':8","'_9':9"}; |
| 50 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
50 | 51 | } |
51 | 52 |
|
52 | 53 | @Test |
53 | 54 | public void testUpperCamelCaseWithSpaces() { |
54 | 55 | Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES); |
55 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
56 | | - .isEqualTo("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + |
57 | | - "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," + |
58 | | - "'annotatedName':7,'Lower Id':8,'_9':9}"); |
| 56 | + String[] matches = {"'Lower Camel':1","'Upper Camel':2","'_Lower Camel Leading Underscore':3", |
| 57 | + "'_ Upper Camel Leading Underscore':4","'Lower_words':5","'U P P E R_ W O R D S':6", |
| 58 | + "'annotatedName':7","'Lower Id':8","'_9':9"}; |
| 59 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
59 | 60 | } |
60 | 61 |
|
61 | 62 | @Test |
62 | 63 | public void testUpperCaseWithUnderscores() { |
63 | 64 | Gson gson = getGsonWithNamingPolicy(UPPER_CASE_WITH_UNDERSCORES); |
64 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
65 | | - .isEqualTo("{'LOWER_CAMEL':1,'UPPER_CAMEL':2,'_LOWER_CAMEL_LEADING_UNDERSCORE':3," + |
66 | | - "'__UPPER_CAMEL_LEADING_UNDERSCORE':4,'LOWER_WORDS':5,'U_P_P_E_R__W_O_R_D_S':6," + |
67 | | - "'annotatedName':7,'LOWER_ID':8,'_9':9}"); |
68 | | - } |
| 65 | + String[] matches = {"'LOWER_CAMEL':1","'UPPER_CAMEL':2","'_LOWER_CAMEL_LEADING_UNDERSCORE':3", |
| 66 | + "'__UPPER_CAMEL_LEADING_UNDERSCORE':4","'LOWER_WORDS':5","'U_P_P_E_R__W_O_R_D_S':6", |
| 67 | + "'annotatedName':7","'LOWER_ID':8","'_9':9"}; |
| 68 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
| 69 | + } |
69 | 70 |
|
70 | 71 | @Test |
71 | 72 | public void testLowerCaseWithUnderscores() { |
72 | 73 | Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); |
73 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
74 | | - .isEqualTo("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + |
75 | | - "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," + |
76 | | - "'annotatedName':7,'lower_id':8,'_9':9}"); |
77 | | - } |
| 74 | + String[] matches = {"'lower_camel':1","'upper_camel':2","'_lower_camel_leading_underscore':3", |
| 75 | + "'__upper_camel_leading_underscore':4","'lower_words':5","'u_p_p_e_r__w_o_r_d_s':6", |
| 76 | + "'annotatedName':7","'lower_id':8","'_9':9"}; |
| 77 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
| 78 | + } |
78 | 79 |
|
79 | 80 | @Test |
80 | 81 | public void testLowerCaseWithDashes() { |
81 | 82 | Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES); |
82 | | - assertThat(gson.toJson(new TestNames()).replace('\"', '\'')) |
83 | | - .isEqualTo("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + |
84 | | - "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," + |
85 | | - "'annotatedName':7,'lower-id':8,'_9':9}"); |
| 83 | + String[] matches = {"'lower-camel':1","'upper-camel':2","'_lower-camel-leading-underscore':3", |
| 84 | + "'_-upper-camel-leading-underscore':4","'lower_words':5","'u-p-p-e-r_-w-o-r-d-s':6", |
| 85 | + "'annotatedName':7","'lower-id':8","'_9':9"}; |
| 86 | + assertThat(Arrays.stream(matches).allMatch(gson.toJson(new TestNames()).replace('\"', '\'')::contains)).isTrue(); |
86 | 87 | } |
87 | 88 |
|
88 | 89 | private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){ |
|
0 commit comments