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

Port all Junit assert to Truth asserts #2304

Merged
merged 15 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public void testContains() {
a.add(1);

List<JsonElement> list = a.asList();
assertThat(list.contains(new JsonPrimitive(1))).isTrue();
assertThat(list.contains(new JsonPrimitive(2))).isFalse();
assertThat(list.contains(null)).isFalse();
assertThat(list).contains(new JsonPrimitive(1));
assertThat(list).doesNotContain(new JsonPrimitive(2));
assertThat(list).doesNotContain(null);

@SuppressWarnings({"unlikely-arg-type", "CollectionIncompatibleType"})
boolean containsInt = list.contains(1); // should only contain JsonPrimitive(1)
Expand Down
4 changes: 2 additions & 2 deletions gson/src/test/java/com/google/gson/JsonArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ public void testRemove() {
JsonPrimitive a = new JsonPrimitive("a");
array.add(a);
assertThat(array.remove(a)).isTrue();
assertThat(array.contains(a)).isFalse();
assertThat(array).doesNotContain(a);
array.add(a);
array.add(new JsonPrimitive("b"));
assertThat(array.remove(1).getAsString()).isEqualTo("b");
assertThat(array).hasSize(1);
assertThat(array.contains(a)).isTrue();
assertThat(array).contains(a);
}

@Test
Expand Down
3 changes: 1 addition & 2 deletions gson/src/test/java/com/google/gson/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ public void testKeySet() {

assertThat(a.size()).isEqualTo(2);
assertThat(a.keySet()).hasSize(2);
assertThat(a.keySet().contains("foo")).isTrue();
assertThat(a.keySet().contains("bar")).isTrue();
assertThat(a.keySet()).containsExactly("foo", "bar").inOrder();

a.addProperty("1", true);
a.addProperty("2", false);
Expand Down
105 changes: 49 additions & 56 deletions gson/src/test/java/com/google/gson/functional/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
Copy link
Collaborator

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.

Suggested change
assertThat(target).asList().containsAnyIn(expected);
assertThat(target).asList().containsExactly(expected).inOrder();

}

@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
Expand All @@ -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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assertThat(target).asList().containsExactly("Hello", "World");
assertThat(target).asList().containsExactly("Hello", "World").inOrder();

}

@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;

Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand All @@ -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]]");
}

/**
Expand All @@ -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]]");
}

/**
Expand All @@ -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 */
Expand All @@ -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\"]]");
}
}
Loading