Skip to content

Commit

Permalink
Standardize (almost) all tests on Truth (square#1242)
Browse files Browse the repository at this point in the history
* Replace assertj calls with Truth where possible

* Update test dependencies

* adapters

* reflect

* codegen

* moshi

* Add missing inOrder()

* Spotless
  • Loading branch information
ZacSweers committed Sep 27, 2020
1 parent acb2836 commit f192473
Show file tree
Hide file tree
Showing 36 changed files with 448 additions and 300 deletions.
2 changes: 1 addition & 1 deletion adapters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ dependencies {

testCompileOnly(Dependencies.jsr305)
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.assertj)
testImplementation(Dependencies.Testing.truth)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.squareup.moshi.adapters;

import static org.assertj.core.api.Assertions.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.squareup.moshi.Json;
Expand Down Expand Up @@ -48,7 +48,9 @@ public void withoutFallbackValue() throws Exception {
adapter.fromJson(reader);
fail();
} catch (JsonDataException expected) {
assertThat(expected).hasMessage("Expected one of [ROCK, PAPER, scr] but was SPOCK at path $");
assertThat(expected)
.hasMessageThat()
.isEqualTo("Expected one of [ROCK, PAPER, scr] but was SPOCK at path $");
}
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.squareup.moshi.adapters;

import static org.assertj.core.api.Assertions.assertThat;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.squareup.moshi.JsonAdapter;
Expand Down Expand Up @@ -84,7 +84,8 @@ public void unregisteredLabelValue() throws IOException {
fail();
} catch (JsonDataException expected) {
assertThat(expected)
.hasMessage(
.hasMessageThat()
.isEqualTo(
"Expected one of [success, error] for key 'type' but found"
+ " 'data'. Register a subtype for this label.");
}
Expand All @@ -105,7 +106,7 @@ public void specifiedFallbackSubtype() throws IOException {
JsonAdapter<Message> adapter = moshi.adapter(Message.class);

Message message = adapter.fromJson("{\"type\":\"data\",\"value\":\"Okay!\"}");
assertThat(message).isSameAs(fallbackError);
assertThat(message).isSameInstanceAs(fallbackError);
}

@Test
Expand Down Expand Up @@ -176,7 +177,8 @@ public void unregisteredSubtype() {
adapter.toJson(new EmptyMessage());
} catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage(
.hasMessageThat()
.isEqualTo(
"Expected one of [class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Success, class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Error] but found"
Expand All @@ -203,7 +205,8 @@ public void unregisteredSubtypeWithDefaultValue() {
adapter.toJson(new EmptyMessage());
} catch (IllegalArgumentException expected) {
assertThat(expected)
.hasMessage(
.hasMessageThat()
.isEqualTo(
"Expected one of [class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Success, class"
+ " com.squareup.moshi.adapters.PolymorphicJsonAdapterFactoryTest$Error] but found"
Expand Down Expand Up @@ -256,7 +259,9 @@ public void nonStringLabelValue() throws IOException {
adapter.fromJson("{\"type\":{},\"value\":\"Okay!\"}");
fail();
} catch (JsonDataException expected) {
assertThat(expected).hasMessage("Expected a string but was BEGIN_OBJECT at path $.type");
assertThat(expected)
.hasMessageThat()
.isEqualTo("Expected a string but was BEGIN_OBJECT at path $.type");
}
}

Expand All @@ -276,7 +281,9 @@ public void nonObjectDoesNotConsume() throws IOException {
adapter.fromJson(reader);
fail();
} catch (JsonDataException expected) {
assertThat(expected).hasMessage("Expected BEGIN_OBJECT but was STRING at path $");
assertThat(expected)
.hasMessageThat()
.isEqualTo("Expected BEGIN_OBJECT but was STRING at path $");
}
assertThat(reader.nextString()).isEqualTo("Failure");
assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
Expand Down Expand Up @@ -313,7 +320,7 @@ public void uniqueLabels() {
factory.withSubtype(Error.class, "data");
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Labels must be unique.");
assertThat(expected).hasMessageThat().isEqualTo("Labels must be unique.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.squareup.moshi.adapters;

import static org.assertj.core.api.Assertions.assertThat;
import static com.google.common.truth.Truth.assertThat;

import com.squareup.moshi.JsonAdapter;
import java.util.Calendar;
Expand Down
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ object Dependencies {

object Testing {
const val assertj = "org.assertj:assertj-core:3.11.1"
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.2.10"
const val junit = "junit:junit:4.12"
const val truth = "com.google.truth:truth:1.0"
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.2.11"
const val junit = "junit:junit:4.13"
const val truth = "com.google.truth:truth:1.0.1"
}
}
1 change: 0 additions & 1 deletion kotlin/codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ dependencies {
testImplementation(Dependencies.KotlinPoet.metadataSpecs)
testImplementation(Dependencies.KotlinPoet.elementsClassInspector)
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.assertj)
testImplementation(Dependencies.Testing.truth)
testImplementation(Dependencies.Testing.compileTesting)
testImplementation(Dependencies.okio2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.squareup.moshi.kotlin.codegen

import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.SourceFile.Companion.kotlin
import org.assertj.core.api.Assertions.assertThat
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -616,7 +616,7 @@ class JsonClassCodegenProcessorTest {

result.generatedFiles.filter { it.extension == "pro" }.forEach { generatedFile ->
when (generatedFile.nameWithoutExtension) {
"moshi-testPackage.Aliases" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.Aliases" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Aliases
-keepnames class testPackage.Aliases
Expand All @@ -626,7 +626,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.Simple" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.Simple" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Simple
-keepnames class testPackage.Simple
Expand All @@ -636,7 +636,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.Generic" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.Generic" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Generic
-keepnames class testPackage.Generic
Expand All @@ -646,7 +646,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.UsingQualifiers" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.UsingQualifiers" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.UsingQualifiers
-keepnames class testPackage.UsingQualifiers
Expand All @@ -659,7 +659,7 @@ class JsonClassCodegenProcessorTest {
-keep @interface testPackage.MyQualifier
""".trimIndent()
)
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.MixedTypes" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MixedTypes
-keepnames class testPackage.MixedTypes
Expand All @@ -669,7 +669,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.DefaultParams" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.DefaultParams
-keepnames class testPackage.DefaultParams
Expand All @@ -685,7 +685,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.Complex" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.Complex" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.Complex
-keepnames class testPackage.Complex
Expand All @@ -704,7 +704,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.MultipleMasks" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.MultipleMasks
-keepnames class testPackage.MultipleMasks
Expand All @@ -720,7 +720,7 @@ class JsonClassCodegenProcessorTest {
}
""".trimIndent()
)
"moshi-testPackage.NestedType.NestedSimple" -> assertThat(generatedFile).hasContent(
"moshi-testPackage.NestedType.NestedSimple" -> assertThat(generatedFile.readText()).contains(
"""
-if class testPackage.NestedType${'$'}NestedSimple
-keepnames class testPackage.NestedType${'$'}NestedSimple
Expand Down
2 changes: 1 addition & 1 deletion kotlin/reflect/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ dependencies {

testImplementation(kotlin("test"))
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.assertj)
testImplementation(Dependencies.Testing.truth)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.squareup.moshi.kotlin.reflect

import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class KotlinJsonAdapterTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.moshi.kotlin

import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.FromJson
import com.squareup.moshi.Json
import com.squareup.moshi.JsonAdapter
Expand All @@ -26,7 +27,6 @@ import com.squareup.moshi.ToJson
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.squareup.moshi.kotlin.reflect.adapter
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.Assert.fail
import org.junit.Test
Expand Down Expand Up @@ -87,7 +87,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("""{"a":4}""")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Required value 'b' missing at $")
assertThat(expected).hasMessageThat().isEqualTo("Required value 'b' missing at $")
}
}

Expand All @@ -102,7 +102,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("""{"a":4}""")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Required value 'b' (JSON name 'bPrime') missing at \$")
assertThat(expected).hasMessageThat().isEqualTo("Required value 'b' (JSON name 'bPrime') missing at \$")
}
}

Expand All @@ -117,7 +117,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"a\":null}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$.a")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' was null at \$.a")
}
}

Expand All @@ -138,7 +138,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"a\":\"hello\"}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$.a")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' was null at \$.a")
}
}

Expand All @@ -155,7 +155,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"aPrime\":null}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' (JSON name 'aPrime') was null at \$.aPrime")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' (JSON name 'aPrime') was null at \$.aPrime")
}
}

Expand All @@ -176,7 +176,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"aPrime\":\"hello\"}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' (JSON name 'aPrime') was null at \$.aPrime")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' (JSON name 'aPrime') was null at \$.aPrime")
}
}

Expand All @@ -193,7 +193,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"a\":null}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$.a")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' was null at \$.a")
}
}

Expand All @@ -214,7 +214,7 @@ class DualKotlinTest(useReflection: Boolean) {
jsonAdapter.fromJson("{\"a\":\"hello\"}")
fail()
} catch (expected: JsonDataException) {
assertThat(expected).hasMessage("Non-null value 'a' was null at \$.a")
assertThat(expected).hasMessageThat().isEqualTo("Non-null value 'a' was null at \$.a")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package com.squareup.moshi.kotlin.codegen

import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.adapter
import org.assertj.core.api.Assertions.assertThat
import org.intellij.lang.annotations.Language
import org.junit.Test

Expand Down

0 comments on commit f192473

Please sign in to comment.