-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Port all Junit assert to Truth asserts #2304
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2be0ab5
Port Junit assert to Truth in `com.google.gson.stream`
MaicolAntali c941f90
Port Junit assert to Truth in `com.google.gson.regression`
MaicolAntali ba55856
Port Junit assert to Truth in `om.google.gson.reflect`
MaicolAntali a013888
Port Junit assert to Truth in `com.google.gson.metrics`
MaicolAntali 36b9b58
Port Junit assert to Truth in `com.google.gson.internal`
MaicolAntali 38c9665
Port Junit assert to Truth in `com.google.gson.internal.sql`
MaicolAntali 4d5ecbc
Port Junit assert to Truth in `com.google.gson.internal.reflect`
MaicolAntali 6ef058d
Port Junit assert to Truth in `com.google.gson.internal.bind`
MaicolAntali cd1ccf4
Port Junit assert to Truth in `com.google.gson.internal.bind.util`
MaicolAntali b3dbf61
Port Junit assert to Truth in `com.google.gson.functional`
MaicolAntali b075faf
Replaces `List.of` with `Arrays.asList` to grant legacy
MaicolAntali 9229737
Simplify `==` asserts
MaicolAntali 8bfd0bd
Simplify `.contain()` asserts + Minor fixes
MaicolAntali 7cdcf44
Merge branch 'master' into truth-asserts
MaicolAntali 78b484c
Simplify asserts
MaicolAntali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,10 +16,7 @@ | |||||
|
||||||
package com.google.gson.functional; | ||||||
|
||||||
import static org.junit.Assert.assertArrayEquals; | ||||||
import static org.junit.Assert.assertEquals; | ||||||
import static org.junit.Assert.assertNull; | ||||||
import static org.junit.Assert.assertTrue; | ||||||
import static com.google.common.truth.Truth.assertThat; | ||||||
import static org.junit.Assert.fail; | ||||||
|
||||||
import com.google.gson.Gson; | ||||||
|
@@ -31,6 +28,7 @@ | |||||
import java.lang.reflect.Type; | ||||||
import java.math.BigDecimal; | ||||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.Collection; | ||||||
import org.junit.Before; | ||||||
import org.junit.Test; | ||||||
|
@@ -51,14 +49,14 @@ public void setUp() throws Exception { | |||||
@Test | ||||||
public void testTopLevelArrayOfIntsSerialization() { | ||||||
int[] target = {1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||||||
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target)); | ||||||
assertThat(gson.toJson(target)).isEqualTo("[1,2,3,4,5,6,7,8,9]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testTopLevelArrayOfIntsDeserialization() { | ||||||
int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||||||
int[] actual = gson.fromJson("[1,2,3,4,5,6,7,8,9]", int[].class); | ||||||
assertArrayEquals(expected, actual); | ||||||
assertThat(actual).isEqualTo(expected); | ||||||
} | ||||||
|
||||||
@Test | ||||||
|
@@ -74,51 +72,49 @@ public void testInvalidArrayDeserialization() { | |||||
@Test | ||||||
public void testEmptyArraySerialization() { | ||||||
int[] target = {}; | ||||||
assertEquals("[]", gson.toJson(target)); | ||||||
assertThat(gson.toJson(target)).isEqualTo("[]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testEmptyArrayDeserialization() { | ||||||
int[] actualObject = gson.fromJson("[]", int[].class); | ||||||
assertTrue(actualObject.length == 0); | ||||||
assertThat(actualObject).hasLength(0); | ||||||
|
||||||
Integer[] actualObject2 = gson.fromJson("[]", Integer[].class); | ||||||
assertTrue(actualObject2.length == 0); | ||||||
assertThat(actualObject2).hasLength(0); | ||||||
|
||||||
actualObject = gson.fromJson("[ ]", int[].class); | ||||||
assertTrue(actualObject.length == 0); | ||||||
assertThat(actualObject).hasLength(0); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testNullsInArraySerialization() { | ||||||
String[] array = {"foo", null, "bar"}; | ||||||
String expected = "[\"foo\",null,\"bar\"]"; | ||||||
String json = gson.toJson(array); | ||||||
assertEquals(expected, json); | ||||||
assertThat(json).isEqualTo(expected); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testNullsInArrayDeserialization() { | ||||||
String json = "[\"foo\",null,\"bar\"]"; | ||||||
String[] expected = {"foo", null, "bar"}; | ||||||
String[] target = gson.fromJson(json, expected.getClass()); | ||||||
for (int i = 0; i < expected.length; ++i) { | ||||||
assertEquals(expected[i], target[i]); | ||||||
} | ||||||
assertThat(target).asList().containsAnyIn(expected); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testSingleNullInArraySerialization() { | ||||||
BagOfPrimitives[] array = new BagOfPrimitives[1]; | ||||||
array[0] = null; | ||||||
String json = gson.toJson(array); | ||||||
assertEquals("[null]", json); | ||||||
assertThat(json).isEqualTo("[null]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testSingleNullInArrayDeserialization() { | ||||||
BagOfPrimitives[] array = gson.fromJson("[null]", BagOfPrimitives[].class); | ||||||
assertNull(array[0]); | ||||||
assertThat(array).asList().containsExactly((Object) null); | ||||||
} | ||||||
|
||||||
@Test | ||||||
|
@@ -127,40 +123,38 @@ public void testNullsInArrayWithSerializeNullPropertySetSerialization() { | |||||
String[] array = {"foo", null, "bar"}; | ||||||
String expected = "[\"foo\",null,\"bar\"]"; | ||||||
String json = gson.toJson(array); | ||||||
assertEquals(expected, json); | ||||||
assertThat(json).isEqualTo(expected); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfStringsSerialization() { | ||||||
String[] target = {"Hello", "World"}; | ||||||
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target)); | ||||||
assertThat(gson.toJson(target)).isEqualTo("[\"Hello\",\"World\"]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfStringsDeserialization() { | ||||||
String json = "[\"Hello\",\"World\"]"; | ||||||
String[] target = gson.fromJson(json, String[].class); | ||||||
assertEquals("Hello", target[0]); | ||||||
assertEquals("World", target[1]); | ||||||
assertThat(target).asList().containsExactly("Hello", "World"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
@Test | ||||||
public void testSingleStringArraySerialization() throws Exception { | ||||||
public void testSingleStringArraySerialization() { | ||||||
String[] s = { "hello" }; | ||||||
String output = gson.toJson(s); | ||||||
assertEquals("[\"hello\"]", output); | ||||||
assertThat(output).isEqualTo("[\"hello\"]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testSingleStringArrayDeserialization() throws Exception { | ||||||
public void testSingleStringArrayDeserialization() { | ||||||
String json = "[\"hello\"]"; | ||||||
String[] arrayType = gson.fromJson(json, String[].class); | ||||||
assertEquals(1, arrayType.length); | ||||||
assertEquals("hello", arrayType[0]); | ||||||
assertThat(arrayType).asList().containsExactly("hello"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfCollectionSerialization() throws Exception { | ||||||
public void testArrayOfCollectionSerialization() { | ||||||
StringBuilder sb = new StringBuilder("["); | ||||||
int arraySize = 3; | ||||||
|
||||||
|
@@ -182,42 +176,42 @@ public void testArrayOfCollectionSerialization() throws Exception { | |||||
sb.append(']'); | ||||||
|
||||||
String json = gson.toJson(arrayOfCollection, typeToSerialize); | ||||||
assertEquals(sb.toString(), json); | ||||||
assertThat(json).isEqualTo(sb.toString()); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfCollectionDeserialization() throws Exception { | ||||||
public void testArrayOfCollectionDeserialization() { | ||||||
String json = "[[1,2],[3,4]]"; | ||||||
Type type = new TypeToken<Collection<Integer>[]>() {}.getType(); | ||||||
Collection<Integer>[] target = gson.fromJson(json, type); | ||||||
|
||||||
assertEquals(2, target.length); | ||||||
assertArrayEquals(new Integer[] {1, 2}, target[0].toArray(new Integer[0])); | ||||||
assertArrayEquals(new Integer[] {3, 4}, target[1].toArray(new Integer[0])); | ||||||
assertThat(target.length).isEqualTo(2); | ||||||
assertThat(target[0].toArray(new Integer[0])).isEqualTo(new Integer[] {1, 2}); | ||||||
assertThat(target[1].toArray(new Integer[0])).isEqualTo(new Integer[] {3, 4}); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception { | ||||||
public void testArrayOfPrimitivesAsObjectsSerialization() { | ||||||
Object[] objs = new Object[] {1, "abc", 0.3f, 5L}; | ||||||
String json = gson.toJson(objs); | ||||||
assertTrue(json.contains("abc")); | ||||||
assertTrue(json.contains("0.3")); | ||||||
assertTrue(json.contains("5")); | ||||||
assertThat(json).contains("abc"); | ||||||
assertThat(json).contains("0.3"); | ||||||
assertThat(json).contains("5"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfPrimitivesAsObjectsDeserialization() throws Exception { | ||||||
public void testArrayOfPrimitivesAsObjectsDeserialization() { | ||||||
String json = "[1,'abc',0.3,1.1,5]"; | ||||||
Object[] objs = gson.fromJson(json, Object[].class); | ||||||
assertEquals(1, ((Number)objs[0]).intValue()); | ||||||
assertEquals("abc", objs[1]); | ||||||
assertEquals(0.3, ((Number)objs[2]).doubleValue(), 0); | ||||||
assertEquals(new BigDecimal("1.1"), new BigDecimal(objs[3].toString())); | ||||||
assertEquals(5, ((Number)objs[4]).shortValue()); | ||||||
assertThat(((Number)objs[0]).intValue()).isEqualTo(1); | ||||||
assertThat(objs[1]).isEqualTo("abc"); | ||||||
assertThat(((Number)objs[2]).doubleValue()).isEqualTo(0.3); | ||||||
assertThat(new BigDecimal(objs[3].toString())).isEqualTo(new BigDecimal("1.1")); | ||||||
assertThat(((Number)objs[4]).shortValue()).isEqualTo(5); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception { | ||||||
public void testObjectArrayWithNonPrimitivesSerialization() { | ||||||
ClassWithObjects classWithObjects = new ClassWithObjects(); | ||||||
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives(); | ||||||
String classWithObjectsJson = gson.toJson(classWithObjects); | ||||||
|
@@ -226,21 +220,21 @@ public void testObjectArrayWithNonPrimitivesSerialization() throws Exception { | |||||
Object[] objects = {classWithObjects, bagOfPrimitives}; | ||||||
String json = gson.toJson(objects); | ||||||
|
||||||
assertTrue(json.contains(classWithObjectsJson)); | ||||||
assertTrue(json.contains(bagOfPrimitivesJson)); | ||||||
assertThat(json).contains(classWithObjectsJson); | ||||||
assertThat(json).contains(bagOfPrimitivesJson); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfNullSerialization() { | ||||||
Object[] array = {null}; | ||||||
String json = gson.toJson(array); | ||||||
assertEquals("[null]", json); | ||||||
assertThat(json).isEqualTo("[null]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testArrayOfNullDeserialization() { | ||||||
String[] values = gson.fromJson("[null]", String[].class); | ||||||
assertNull(values[0]); | ||||||
assertThat(values[0]).isNull(); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -253,20 +247,20 @@ public void testMultidimensionalArraysSerialization() { | |||||
{"Alcoa Inc", "29.01", "0.42", "1.47", "4/1 12:00am", "Manufacturing"} | ||||||
}; | ||||||
String json = gson.toJson(items); | ||||||
assertTrue(json.contains("[[\"3m Co")); | ||||||
assertTrue(json.contains("Manufacturing\"]]")); | ||||||
assertThat(json).contains("[[\"3m Co"); | ||||||
assertThat(json).contains("Manufacturing\"]]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testMultidimensionalObjectArraysSerialization() { | ||||||
Object[][] array = {new Object[] { 1, 2 }}; | ||||||
assertEquals("[[1,2]]", gson.toJson(array)); | ||||||
assertThat(gson.toJson(array)).isEqualTo("[[1,2]]"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testMultidimensionalPrimitiveArraysSerialization() { | ||||||
int[][] array = {{1, 2}, {3, 4}}; | ||||||
assertEquals("[[1,2],[3,4]]", gson.toJson(array)); | ||||||
assertThat(gson.toJson(array)).isEqualTo("[[1,2],[3,4]]"); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -275,7 +269,7 @@ public void testMultidimensionalPrimitiveArraysSerialization() { | |||||
@Test | ||||||
public void testMixingTypesInObjectArraySerialization() { | ||||||
Object[] array = {1, 2, new Object[] {"one", "two", 3}}; | ||||||
assertEquals("[1,2,[\"one\",\"two\",3]]", gson.toJson(array)); | ||||||
assertThat(gson.toJson(array)).isEqualTo("[1,2,[\"one\",\"two\",3]]"); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -286,15 +280,15 @@ public void testMultidimensionalArraysDeserialization() { | |||||
String json = "[['3m Co','71.72','0.02','0.03','4/2 12:00am','Manufacturing']," | ||||||
+ "['Alcoa Inc','29.01','0.42','1.47','4/1 12:00am','Manufacturing']]"; | ||||||
String[][] items = gson.fromJson(json, String[][].class); | ||||||
assertEquals("3m Co", items[0][0]); | ||||||
assertEquals("Manufacturing", items[1][5]); | ||||||
assertThat(items[0][0]).isEqualTo("3m Co"); | ||||||
assertThat(items[1][5]).isEqualTo("Manufacturing"); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testMultidimensionalPrimitiveArraysDeserialization() { | ||||||
String json = "[[1,2],[3,4]]"; | ||||||
int[][] expected = {{1, 2}, {3, 4}}; | ||||||
assertArrayEquals(expected, gson.fromJson(json, int[][].class)); | ||||||
assertThat(gson.fromJson(json, int[][].class)).isEqualTo(expected); | ||||||
} | ||||||
|
||||||
/** http://code.google.com/p/google-gson/issues/detail?id=342 */ | ||||||
|
@@ -304,7 +298,6 @@ public void testArrayElementsAreArrays() { | |||||
new String[] {"test1", "test2"}, | ||||||
new String[] {"test3", "test4"} | ||||||
}; | ||||||
assertEquals("[[\"test1\",\"test2\"],[\"test3\",\"test4\"]]", | ||||||
new Gson().toJson(stringArrays)); | ||||||
assertThat(new Gson().toJson(stringArrays)).isEqualTo("[[\"test1\",\"test2\"],[\"test3\",\"test4\"]]"); | ||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably be
containsExactly(...).inOrder()
as well.