mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
Standardize (almost) all tests on Truth (#1242)
* Replace assertj calls with Truth where possible * Update test dependencies * adapters * reflect * codegen * moshi * Add missing inOrder() * Spotless
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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 $")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 \$")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.squareup.moshi.kotlin.codegen
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
@@ -28,7 +29,6 @@ import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.Types
|
||||
import com.squareup.moshi.internal.NullSafeJsonAdapter
|
||||
import com.squareup.moshi.kotlin.reflect.adapter
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.fail
|
||||
@@ -177,7 +177,7 @@ class GeneratedAdaptersTest {
|
||||
"""{"data":[null,"why"]}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.data).containsExactly(null, "why")
|
||||
assertThat(instance.data).asList().containsExactly(null, "why").inOrder()
|
||||
assertThat(adapter.toJson(instance)).isEqualTo(json)
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ class GeneratedAdaptersTest {
|
||||
"""{"ints":[0,1]}"""
|
||||
|
||||
val instance = adapter.fromJson(json)!!
|
||||
assertThat(instance.ints).containsExactly(0, 1)
|
||||
assertThat(instance.ints).asList().containsExactly(0, 1).inOrder()
|
||||
assertThat(adapter.toJson(instance)).isEqualTo(json)
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ class GeneratedAdaptersTest {
|
||||
adapter.fromJson(invalidJson)
|
||||
fail("The invalid json should have failed!")
|
||||
} catch (e: JsonDataException) {
|
||||
assertThat(e).hasMessageContaining("foo")
|
||||
assertThat(e).hasMessageThat().contains("foo")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -933,7 +933,7 @@ class GeneratedAdaptersTest {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
assertThat(expected).hasMessageThat().isEqualTo("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -948,7 +948,7 @@ class GeneratedAdaptersTest {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
assertThat(expected).hasMessageThat().isEqualTo("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1272,7 +1272,7 @@ class GeneratedAdaptersTest {
|
||||
moshi.adapter<CustomGeneratedClassMissing>()
|
||||
fail()
|
||||
} catch (e: RuntimeException) {
|
||||
assertThat(e).hasMessageContaining("Failed to find the generated JsonAdapter class")
|
||||
assertThat(e).hasMessageThat().contains("Failed to find the generated JsonAdapter class")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1349,7 +1349,7 @@ class GeneratedAdaptersTest {
|
||||
moshi.adapter(MultipleGenerics::class.java)
|
||||
fail("Should have failed to construct the adapter due to missing generics")
|
||||
} catch (e: RuntimeException) {
|
||||
assertThat(e).hasMessage("Failed to find the generated JsonAdapter constructor for 'class com.squareup.moshi.kotlin.codegen.GeneratedAdaptersTest\$MultipleGenerics'. Suspiciously, the type was not parameterized but the target class 'com.squareup.moshi.kotlin.codegen.GeneratedAdaptersTest_MultipleGenericsJsonAdapter' is generic. Consider using Types#newParameterizedType() to define these missing type variables.")
|
||||
assertThat(e).hasMessageThat().isEqualTo("Failed to find the generated JsonAdapter constructor for 'class com.squareup.moshi.kotlin.codegen.GeneratedAdaptersTest\$MultipleGenerics'. Suspiciously, the type was not parameterized but the target class 'com.squareup.moshi.kotlin.codegen.GeneratedAdaptersTest_MultipleGenericsJsonAdapter' is generic. Consider using Types#newParameterizedType() to define these missing type variables.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1361,7 +1361,7 @@ class GeneratedAdaptersTest {
|
||||
)
|
||||
fail("Should have failed to construct the adapter due to wrong number of generics")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage("TypeVariable mismatch: Expecting 4 types for generic type variables [A, B, C, D], but received 1")
|
||||
assertThat(e).hasMessageThat().isEqualTo("TypeVariable mismatch: Expecting 4 types for generic type variables [A, B, C, D], but received 1")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.squareup.moshi.kotlin.reflect
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.squareup.moshi.FromJson
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonAdapter
|
||||
@@ -26,7 +27,7 @@ import com.squareup.moshi.JsonWriter
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.ToJson
|
||||
import com.squareup.moshi.Types
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayOutputStream
|
||||
@@ -141,7 +142,7 @@ class KotlinJsonAdapterTest {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
assertThat(expected).hasMessageThat().isEqualTo("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +156,7 @@ class KotlinJsonAdapterTest {
|
||||
jsonAdapter.fromJson("""{"a":4,"a":4}""")
|
||||
fail()
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'a' at $.a")
|
||||
assertThat(expected).hasMessageThat().isEqualTo("Multiple values for 'a' at $.a")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +203,7 @@ class KotlinJsonAdapterTest {
|
||||
jsonAdapter.fromJson("""{"a":4,"b":null,"b":6}""")
|
||||
fail()
|
||||
} catch (expected: JsonDataException) {
|
||||
assertThat(expected).hasMessage("Multiple values for 'b' at $.b")
|
||||
assertThat(expected).hasMessageThat().isEqualTo("Multiple values for 'b' at $.b")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,7 +316,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<RequiredTransientConstructorParameter>()
|
||||
fail()
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage(
|
||||
assertThat(expected).hasMessageThat().isEqualTo(
|
||||
"No default value for transient constructor parameter #0 " +
|
||||
"a of fun <init>(kotlin.Int): " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest.RequiredTransientConstructorParameter"
|
||||
@@ -359,7 +360,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<ConstructorParameterWithSameNameAsPropertyButDifferentType>()
|
||||
fail()
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage(
|
||||
assertThat(expected).hasMessageThat().isEqualTo(
|
||||
"'a' has a constructor parameter of type " +
|
||||
"kotlin.Int but a property of type kotlin.String."
|
||||
)
|
||||
@@ -446,7 +447,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<Triple<*, *, *>>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Platform class kotlin.Triple in kotlin.Triple<java.lang.Object, java.lang.Object, java.lang.Object> requires explicit JsonAdapter to be registered"
|
||||
)
|
||||
}
|
||||
@@ -590,7 +591,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<NonPropertyConstructorParameter>()
|
||||
fail()
|
||||
} catch (expected: IllegalArgumentException) {
|
||||
assertThat(expected).hasMessage(
|
||||
assertThat(expected).hasMessageThat().isEqualTo(
|
||||
"No property for required constructor parameter #0 a of fun <init>(" +
|
||||
"kotlin.Int, kotlin.Int): ${NonPropertyConstructorParameter::class.qualifiedName}"
|
||||
)
|
||||
@@ -618,7 +619,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<Interface>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"No JsonAdapter for interface " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$Interface (with no annotations)"
|
||||
)
|
||||
@@ -633,7 +634,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<AbstractClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Cannot serialize abstract class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$AbstractClass"
|
||||
)
|
||||
@@ -648,7 +649,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<InnerClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Cannot serialize inner class " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$InnerClass"
|
||||
)
|
||||
@@ -664,7 +665,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<LocalClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Cannot serialize local class or object expression " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$localClassesNotSupported\$LocalClass"
|
||||
)
|
||||
@@ -677,7 +678,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<ObjectDeclaration>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Cannot serialize object declaration " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$ObjectDeclaration"
|
||||
)
|
||||
@@ -704,7 +705,7 @@ class KotlinJsonAdapterTest {
|
||||
} else {
|
||||
"anonymous class"
|
||||
}
|
||||
assertThat(e).hasMessage(
|
||||
assertThat(e).hasMessageThat().isEqualTo(
|
||||
"Cannot serialize $type " +
|
||||
"com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterTest\$anonymousClassesNotSupported" +
|
||||
"\$expression$1"
|
||||
@@ -989,7 +990,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<PlainKotlinClass>()
|
||||
fail("Should not pass here")
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessageContaining("Reflective serialization of Kotlin classes")
|
||||
assertThat(e).hasMessageThat().contains("Reflective serialization of Kotlin classes")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1142,7 +1143,7 @@ class KotlinJsonAdapterTest {
|
||||
moshi.adapter<SealedClass>()
|
||||
fail()
|
||||
} catch (e: IllegalArgumentException) {
|
||||
assertThat(e).hasMessageContaining("Cannot reflectively serialize sealed class")
|
||||
assertThat(e).hasMessageThat().contains("Cannot reflectively serialize sealed class")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1158,7 +1159,7 @@ class KotlinJsonAdapterTest {
|
||||
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
|
||||
val adapter = moshi.adapter(type)
|
||||
|
||||
assertThat(adapter.fromJson(json)).isEqualToComparingFieldByFieldRecursively(value)
|
||||
Assertions.assertThat(adapter.fromJson(json)).isEqualToComparingFieldByFieldRecursively(value)
|
||||
assertThat(adapter.toJson(value)).isEqualTo(json)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user