Skip to content

Commit

Permalink
Modification in test cases (google#2454)
Browse files Browse the repository at this point in the history
* Fixed Typo in GsonBuilder.java

* Suggestions on Test cases

* Modified test cases using assertThrows method (JUnit)

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/GsonTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/JsonArrayAsListTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/JsonStreamParserTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/TypeAdapterTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <[email protected]>

* Update gson/src/test/java/com/google/gson/ToNumberPolicyTest.java

Co-authored-by: Marcono1234 <[email protected]>

---------

Co-authored-by: Marcono1234 <[email protected]>
  • Loading branch information
2 people authored and tibor-universe committed Aug 17, 2024
1 parent 689380c commit e34b588
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 230 deletions.
26 changes: 7 additions & 19 deletions gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import com.google.gson.Gson.FutureTypeAdapter;
import com.google.gson.internal.Excluder;
Expand Down Expand Up @@ -104,12 +104,8 @@ private static final class TestTypeAdapter extends TypeAdapter<Object> {
@Test
public void testGetAdapter_Null() {
Gson gson = new Gson();
try {
gson.getAdapter((TypeToken<?>) null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> gson.getAdapter((TypeToken<?>) null));
assertThat(e).hasMessageThat().isEqualTo("type must not be null");
}

@Test
Expand Down Expand Up @@ -282,13 +278,9 @@ public void testNewJsonWriter_Default() throws IOException {
jsonWriter.value(true);
jsonWriter.endObject();

try {
// Additional top-level value
jsonWriter.value(1);
fail();
} catch (IllegalStateException expected) {
assertThat(expected).hasMessageThat().isEqualTo("JSON must have only one top-level value.");
}
// Additional top-level value
IllegalStateException e = assertThrows(IllegalStateException.class, () -> jsonWriter.value(1));
assertThat(e).hasMessageThat().isEqualTo("JSON must have only one top-level value.");

jsonWriter.close();
assertThat(writer.toString()).isEqualTo("{\"\\u003ctest2\":true}");
Expand Down Expand Up @@ -323,11 +315,7 @@ public void testNewJsonWriter_Custom() throws IOException {
public void testNewJsonReader_Default() throws IOException {
String json = "test"; // String without quotes
JsonReader jsonReader = new Gson().newJsonReader(new StringReader(json));
try {
jsonReader.nextString();
fail();
} catch (MalformedJsonException expected) {
}
assertThrows(MalformedJsonException.class, jsonReader::nextString);
jsonReader.close();
}

Expand Down
94 changes: 23 additions & 71 deletions gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import com.google.gson.common.MoreAsserts;
import java.util.Arrays;
Expand All @@ -37,17 +37,8 @@ public void testGet() {
List<JsonElement> list = a.asList();
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(1));

try {
list.get(-1);
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.get(2);
fail();
} catch (IndexOutOfBoundsException e) {
}
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> list.get(2));

a.add((JsonElement) null);
assertThat(list.get(1)).isEqualTo(JsonNull.INSTANCE);
Expand Down Expand Up @@ -75,24 +66,11 @@ public void testSet() {
assertThat(list.get(0)).isEqualTo(new JsonPrimitive(2));
assertThat(a.get(0)).isEqualTo(new JsonPrimitive(2));

try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(2, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(2, new JsonPrimitive(1)));

NullPointerException e = assertThrows(NullPointerException.class, () -> list.set(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -115,30 +93,14 @@ public void testAdd() {
);
assertThat(list).isEqualTo(expectedList);

try {
list.set(-1, new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.set(list.size(), new JsonPrimitive(1));
fail();
} catch (IndexOutOfBoundsException e) {
}

try {
list.add(0, null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.add(null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, new JsonPrimitive(1)));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(list.size(), new JsonPrimitive(1)));

NullPointerException e = assertThrows(NullPointerException.class, () -> list.add(0, null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");

e = assertThrows(NullPointerException.class, () -> list.add(null));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -157,18 +119,11 @@ public void testAddAll() {
assertThat(list).isEqualTo(expectedList);
assertThat(list).isEqualTo(expectedList);

try {
list.addAll(0, Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
try {
list.addAll(Collections.<JsonElement>singletonList(null));
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}
NullPointerException e = assertThrows(NullPointerException.class, () -> list.addAll(0, Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");

e = assertThrows(NullPointerException.class, () -> list.addAll(Collections.<JsonElement>singletonList(null)));
assertThat(e).hasMessageThat().isEqualTo("Element must be non-null");
}

@Test
Expand All @@ -180,11 +135,8 @@ public void testRemoveIndex() {
assertThat(list.remove(0)).isEqualTo(new JsonPrimitive(1));
assertThat(list).hasSize(0);
assertThat(a).hasSize(0);
try {
list.remove(0);
fail();
} catch (IndexOutOfBoundsException e) {
}

assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0));
}

@Test
Expand Down
13 changes: 3 additions & 10 deletions gson/src/test/java/com/google/gson/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.gson;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import com.google.gson.common.TestTypes.BagOfPrimitives;
import com.google.gson.internal.Streams;
Expand All @@ -37,10 +37,7 @@ public class JsonParserTest {

@Test
public void testParseInvalidJson() {
try {
JsonParser.parseString("[[]");
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("[[]"));
}

@Test
Expand Down Expand Up @@ -81,11 +78,7 @@ public void testParseUnquotedSingleWordStringFails() {

@Test
public void testParseUnquotedMultiWordStringFails() {
String unquotedSentence = "Test is a test..blah blah";
try {
JsonParser.parseString(unquotedSentence);
fail();
} catch (JsonSyntaxException expected) { }
assertThrows(JsonSyntaxException.class, () -> JsonParser.parseString("Test is a test..blah blah"));
}

@Test
Expand Down
43 changes: 10 additions & 33 deletions gson/src/test/java/com/google/gson/JsonStreamParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.google.gson;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.io.EOFException;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -72,57 +72,34 @@ public void testNoSideEffectForHasNext() {
public void testCallingNextBeyondAvailableInput() {
JsonElement unused1 = parser.next();
JsonElement unused2 = parser.next();
try {
parser.next();
fail("Parser should not go beyond available input");
} catch (NoSuchElementException expected) {
}
// Parser should not go beyond available input
assertThrows(NoSuchElementException.class, parser::next);
}

@Test
public void testEmptyInput() {
JsonStreamParser parser = new JsonStreamParser("");
try {
parser.next();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
JsonIOException e = assertThrows(JsonIOException.class, parser::next);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);

parser = new JsonStreamParser("");
try {
parser.hasNext();
fail();
} catch (JsonIOException e) {
assertThat(e.getCause()).isInstanceOf(EOFException.class);
}
e = assertThrows(JsonIOException.class, parser::hasNext);
assertThat(e).hasCauseThat().isInstanceOf(EOFException.class);
}

@Test
public void testIncompleteInput() {
JsonStreamParser parser = new JsonStreamParser("[");
assertThat(parser.hasNext()).isTrue();
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}

@Test
public void testMalformedInput() {
JsonStreamParser parser = new JsonStreamParser(":");
try {
parser.hasNext();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::hasNext);

parser = new JsonStreamParser(":");
try {
parser.next();
fail();
} catch (JsonSyntaxException e) {
}
assertThrows(JsonSyntaxException.class, parser::next);
}
}
Loading

0 comments on commit e34b588

Please sign in to comment.