replace junit with kotest

This commit is contained in:
Phillip Thelen 2021-09-17 09:21:20 +02:00
parent 41edd2ddbd
commit 7cc7c6f87e
17 changed files with 247 additions and 214 deletions

View file

@ -47,7 +47,6 @@ dependencies {
//Dependency Injection
implementation 'com.google.dagger:dagger:2.38'
testImplementation 'junit:junit:4.12'
kapt 'com.google.dagger:dagger-compiler:2.38'
compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
@ -84,9 +83,9 @@ dependencies {
// Image Management Library
implementation("io.coil-kt:coil:1.2.2")
implementation("io.coil-kt:coil-gif:1.2.2")
//Tests
testImplementation(platform('org.junit:junit-bom:5.8.0'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation 'io.kotest:kotest-runner-junit5:4.6.2'
testImplementation 'androidx.test:core:1.4.0'
testImplementation "io.mockk:mockk:1.12.0"
testImplementation "io.mockk:mockk-android:1.12.0"
@ -260,6 +259,12 @@ android {
}
}
android.testOptions {
unitTests.all {
useJUnitPlatform()
}
}
Properties props = new Properties()
def propFile = new File('signingrelease.properties')
if (propFile.canRead()) {

View file

@ -10,7 +10,7 @@ import io.reactivex.rxjava3.core.Flowable
import javax.inject.Inject
class BuyRewardUseCase @Inject
constructor(private val taskRepository: TaskRepository, private val soundManager: SoundManager, postExecutionThread: PostExecutionThread) : UseCase<BuyRewardUseCase.RequestValues, TaskScoringResult>(postExecutionThread) {
constructor(private val taskRepository: TaskRepository, private val soundManager: SoundManager, postExecutionThread: PostExecutionThread) : UseCase<BuyRewardUseCase.RequestValues, TaskScoringResult?>(postExecutionThread) {
override fun buildUseCaseObservable(requestValues: RequestValues): Flowable<TaskScoringResult?> {
return taskRepository

View file

@ -18,7 +18,8 @@ open class QuestBoss : RealmObject(), BaseObject {
var rage: QuestBossRage? = null
fun hasRage(): Boolean {
val hasRage: Boolean
get() {
return rage?.value ?: 0.0 > 0.0
}
}

View file

@ -0,0 +1,16 @@
package com.habitrpg.android.habitica
import android.content.Context
import io.kotest.core.spec.style.AnnotationSpec
import io.mockk.MockKAnnotations
import io.mockk.impl.annotations.MockK
open class BaseAnnotationTestCase: AnnotationSpec() {
@MockK
lateinit var mockContext: Context
@BeforeAll
fun initMocks() {
MockKAnnotations.init(this, relaxed = true)
}
}

View file

@ -1,11 +0,0 @@
package com.habitrpg.android.habitica
import io.mockk.MockKAnnotations
import org.junit.Before
class BaseTestCase {
@Before
fun initMocks() {
MockKAnnotations.init(this, relaxed = true)
}
}

View file

@ -1,19 +1,23 @@
package com.habitrpg.android.habitica.extensions
import junit.framework.TestCase
import java.util.*
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import io.kotest.matchers.shouldBe
import java.util.Date
class DateExtensionsTest : TestCase() {
class DateExtensionsTest : BaseAnnotationTestCase() {
@Test
fun testGetShortRemainingStringWithDay() {
assertEquals("24d 1h 3m", (Date().time + 2077400000L).getShortRemainingString())
"24d 1h 3m" shouldBe (Date().time + 2077400000L).getShortRemainingString()
}
@Test
fun testGetShortRemainingStringWithHour() {
assertEquals("5h 46m", (Date().time + 20774000L).getShortRemainingString())
"5h 46m" shouldBe (Date().time + 20774000L).getShortRemainingString()
}
@Test
fun testGetShortRemainingStringWithMinute() {
assertEquals("34m 37s", (Date().time + 2077400L).getShortRemainingString())
"34m 37s" shouldBe (Date().time + 2077400L).getShortRemainingString()
}
}

View file

@ -1,5 +1,6 @@
package com.habitrpg.android.habitica.helpers
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.Locale
@ -7,49 +8,45 @@ import java.util.Locale
class HealthFormatterTest {
@Test
fun shouldRoundValuesGreaterThanOneDown() {
assertEquals(49.0, HealthFormatter.format(49.9), DELTA)
assertEquals(9.0, HealthFormatter.format(9.9999), DELTA)
assertEquals(1.0, HealthFormatter.format(1.9), DELTA)
assertEquals(1.0, HealthFormatter.format(1.0001), DELTA)
49.0 shouldBe HealthFormatter.format(49.9)
9.0 shouldBe HealthFormatter.format(9.9999)
1.0 shouldBe HealthFormatter.format(1.9)
1.0 shouldBe HealthFormatter.format(1.0001)
assertEquals("49", HealthFormatter.formatToString(49.9, Locale.US))
assertEquals("9", HealthFormatter.formatToString(9.9999, Locale.US))
assertEquals("1", HealthFormatter.formatToString(1.9, Locale.US))
assertEquals("1", HealthFormatter.formatToString(1.0001, Locale.US))
"49" shouldBe HealthFormatter.formatToString(49.9, Locale.US)
"9" shouldBe HealthFormatter.formatToString(9.9999, Locale.US)
"1" shouldBe HealthFormatter.formatToString(1.9, Locale.US)
"1" shouldBe HealthFormatter.formatToString(1.0001, Locale.US)
}
@Test
fun shouldRoundValuesBetweenZeroAndOneUpToOneDecimalPlace() {
assertEquals(1.0, HealthFormatter.format(0.99), DELTA)
assertEquals(0.2, HealthFormatter.format(0.11), DELTA)
assertEquals(0.1, HealthFormatter.format(0.0001), DELTA)
1.0 shouldBe HealthFormatter.format(0.99)
0.2 shouldBe HealthFormatter.format(0.11)
0.1 shouldBe HealthFormatter.format(0.0001)
assertEquals("1", HealthFormatter.formatToString(0.99, Locale.US))
assertEquals("0.2", HealthFormatter.formatToString(0.11, Locale.US))
assertEquals("0.1", HealthFormatter.formatToString(0.0001, Locale.US))
"1" shouldBe HealthFormatter.formatToString(0.99, Locale.US)
"0.2" shouldBe HealthFormatter.formatToString(0.11, Locale.US)
"0.1" shouldBe HealthFormatter.formatToString(0.0001, Locale.US)
}
@Test
fun shouldRoundNegativeValuesDown() {
assertEquals(-1.0, HealthFormatter.format(-0.1), DELTA)
assertEquals(-2.0, HealthFormatter.format(-2.0), DELTA)
-1.0 shouldBe HealthFormatter.format(-0.1)
-2.0 shouldBe HealthFormatter.format(-2.0)
assertEquals("-1", HealthFormatter.formatToString(-0.1, Locale.US))
assertEquals("-2", HealthFormatter.formatToString(-2.0, Locale.US))
"-1" shouldBe HealthFormatter.formatToString(-0.1, Locale.US)
"-2" shouldBe HealthFormatter.formatToString(-2.0, Locale.US)
}
@Test
fun shouldLeaveAcceptableValuesAsTheyAre() {
assertEquals(20.0, HealthFormatter.format(20), DELTA)
assertEquals(0.0, HealthFormatter.format(0), DELTA)
assertEquals(0.9, HealthFormatter.format(0.9), DELTA)
20.0 shouldBe HealthFormatter.format(20)
0.0 shouldBe HealthFormatter.format(0)
0.9 shouldBe HealthFormatter.format(0.9)
assertEquals("20", HealthFormatter.formatToString(20, Locale.US))
assertEquals("0", HealthFormatter.formatToString(0, Locale.US))
assertEquals("0.9", HealthFormatter.formatToString(0.9, Locale.US))
}
companion object {
private const val DELTA = 0.0
"20" shouldBe HealthFormatter.formatToString(20, Locale.US)
"0" shouldBe HealthFormatter.formatToString(0, Locale.US)
"0.9" shouldBe HealthFormatter.formatToString(0.9, Locale.US)
}
}

View file

@ -3,51 +3,46 @@ package com.habitrpg.android.habitica.helpers
import android.content.Context
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.helpers.NumberAbbreviator.abbreviate
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import io.mockk.mockk
class NumberAbbreviatorTest {
@MockK private lateinit var context: Context
@BeforeEach
fun setUp() {
every { context.getString(R.string.thousand_abbrev) } returns "k"
every { context.getString(R.string.million_abbrev) } returns "m"
every { context.getString(R.string.billion_abbrev) } returns "b"
every { context.getString(R.string.trillion_abbrev) } returns "t"
class NumberAbbreviatorTest: StringSpec({
val mockContext = mockk<Context>()
beforeEach {
every { mockContext.getString(R.string.thousand_abbrev) } returns "k"
every { mockContext.getString(R.string.million_abbrev) } returns "m"
every { mockContext.getString(R.string.billion_abbrev) } returns "b"
every { mockContext.getString(R.string.trillion_abbrev) } returns "t"
}
@Test
fun testThatItDoesntAbbreviatesSmallNumbers() {
abbreviate(context, 215.0, 2) shouldBe "215"
abbreviate(context, 2.05, 2) shouldBe "2.05"
"doesn't abbreviate small numbers" {
abbreviate(mockContext, 215.0, 2) shouldBe "215"
abbreviate(mockContext, 2.05, 2) shouldBe "2.05"
}
@Test
fun testThatItAbbreviatesThousand() {
abbreviate(context, 1550.0, 2) shouldBe "1.55k"
"it abbreviates thousands" {
abbreviate(mockContext, 1550.0, 2) shouldBe "1.55k"
}
@Test
fun testThatItAbbreviatesMillion() {
abbreviate(context, 9990000.0, 2) shouldBe "9.99m"
"it abbreviates millions" {
abbreviate(mockContext, 9990000.0, 2) shouldBe "9.99m"
}
@Test
fun testThatItAbbreviatesBillion() {
abbreviate(context, 1990000000.0, 2) shouldBe "1.99b"
"it abbreviates billions" {
abbreviate(mockContext, 1990000000.0, 2) shouldBe "1.99b"
}
@Test
fun testThatItAbbreviatesThousandWithoutAdditionalDecimals() {
abbreviate(context, 1000.0, 2) shouldBe "1k"
abbreviate(context, 1500.0, 2) shouldBe "1.5k"
"it abbreviates thousands without additional decimals" {
abbreviate(mockContext, 1000.0, 2) shouldBe "1k"
abbreviate(mockContext, 1500.0, 2) shouldBe "1.5k"
}
@Test
fun voidtestThatitRoundsCorrectly() {
abbreviate(context, 9999.0, 2) shouldBe "9.99k"
"it rounds correctly" {
abbreviate(mockContext, 9999.0, 2) shouldBe "9.99k"
}
}
afterEach { clearMocks(mockContext) }
})

View file

@ -1,17 +1,17 @@
package com.habitrpg.android.habitica.helpers
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.helpers.UserStatComputer.AttributeRow
import com.habitrpg.android.habitica.helpers.UserStatComputer.EquipmentRow
import com.habitrpg.android.habitica.models.inventory.Equipment
import com.habitrpg.android.habitica.models.members.Member
import com.habitrpg.android.habitica.models.user.Stats
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test
import io.kotest.matchers.shouldBe
import java.util.ArrayList
class UserStatComputerTest {
class UserStatComputerTest: BaseAnnotationTestCase() {
private val userStatComputer: UserStatComputer = UserStatComputer()
private val user: Member = Member()
private val equipment: Equipment
@ -26,9 +26,9 @@ class UserStatComputerTest {
fun shouldReturnCorrectEquipmentRow() {
val statsRows = userStatComputer.computeClassBonus(equipmentList, user)
val equipmentRow = statsRows[0] as EquipmentRow
assertEquals(key, equipmentRow.gearKey)
assertEquals(text, equipmentRow.text)
assertEquals("STR 1, INT 2, CON 4, PER 3", equipmentRow.stats)
key shouldBe equipmentRow.gearKey
text shouldBe equipmentRow.text
"STR 1, INT 2, CON 4, PER 3" shouldBe equipmentRow.stats
}
@Test
@ -37,13 +37,13 @@ class UserStatComputerTest {
equipment.klass = Stats.ROGUE
val statsRows = userStatComputer.computeClassBonus(equipmentList, user)
val attributeRow = statsRows[2] as AttributeRow
assertEquals(R.string.profile_class_bonus.toLong(), attributeRow.labelId.toLong())
assertEquals((str * 0.5f).toDouble(), attributeRow.strVal.toDouble(), 0.01)
assertEquals((intStat * 0.0f).toDouble(), attributeRow.intVal.toDouble(), 0.01)
assertEquals((con * 0.0f).toDouble(), attributeRow.conVal.toDouble(), 0.01)
assertEquals((per * 0.5f).toDouble(), attributeRow.perVal.toDouble(), 0.01)
assertFalse(attributeRow.roundDown)
assertFalse(attributeRow.summary)
R.string.profile_class_bonus.toLong() shouldBe attributeRow.labelId.toLong()
(str * 0.5f).toDouble() shouldBe attributeRow.strVal.toDouble()
(intStat * 0.0f).toDouble() shouldBe attributeRow.intVal.toDouble()
(con * 0.0f).toDouble() shouldBe attributeRow.conVal.toDouble()
(per * 0.5f).toDouble() shouldBe attributeRow.perVal.toDouble()
attributeRow.roundDown shouldBe false
attributeRow.summary shouldBe false
}
@Test
@ -53,13 +53,13 @@ class UserStatComputerTest {
equipment.specialClass = Stats.ROGUE
val statsRows = userStatComputer.computeClassBonus(equipmentList, user)
val attributeRow = statsRows[2] as AttributeRow
assertEquals(R.string.profile_class_bonus.toLong(), attributeRow.labelId.toLong())
assertEquals((str * 0.5f).toDouble(), attributeRow.strVal.toDouble(), 0.01)
assertEquals((intStat * 0.0f).toDouble(), attributeRow.intVal.toDouble(), 0.01)
assertEquals((con * 0.0f).toDouble(), attributeRow.conVal.toDouble(), 0.01)
assertEquals((per * 0.5f).toDouble(), attributeRow.perVal.toDouble(), 0.01)
assertFalse(attributeRow.roundDown)
assertFalse(attributeRow.summary)
R.string.profile_class_bonus.toLong() shouldBe attributeRow.labelId.toLong()
(str * 0.5f).toDouble() shouldBe attributeRow.strVal.toDouble()
(intStat * 0.0f).toDouble() shouldBe attributeRow.intVal.toDouble()
(con * 0.0f).toDouble() shouldBe attributeRow.conVal.toDouble()
(per * 0.5f).toDouble() shouldBe attributeRow.perVal.toDouble()
attributeRow.roundDown shouldBe false
attributeRow.summary shouldBe false
}
@Test
@ -69,13 +69,13 @@ class UserStatComputerTest {
equipment.specialClass = ""
val statsRows = userStatComputer.computeClassBonus(equipmentList, user)
val attributeRow = statsRows[2] as AttributeRow
assertEquals(R.string.profile_class_bonus.toLong(), attributeRow.labelId.toLong())
assertEquals((str * 0.0f).toDouble(), attributeRow.strVal.toDouble(), 0.01)
assertEquals((intStat * 0.0f).toDouble(), attributeRow.intVal.toDouble(), 0.01)
assertEquals((con * 0.0f).toDouble(), attributeRow.conVal.toDouble(), 0.01)
assertEquals((per * 0.0f).toDouble(), attributeRow.perVal.toDouble())
assertFalse(attributeRow.roundDown)
assertFalse(attributeRow.summary)
R.string.profile_class_bonus.toLong() shouldBe attributeRow.labelId.toLong()
(str * 0.0f).toDouble() shouldBe attributeRow.strVal.toDouble()
(intStat * 0.0f).toDouble() shouldBe attributeRow.intVal.toDouble()
(con * 0.0f).toDouble() shouldBe attributeRow.conVal.toDouble()
(per * 0.0f).toDouble() shouldBe attributeRow.perVal.toDouble()
attributeRow.roundDown shouldBe false
attributeRow.summary shouldBe false
}
init {

View file

@ -1,15 +1,15 @@
package com.habitrpg.android.habitica.models
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import com.habitrpg.android.habitica.models.user.SubscriptionPlan
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import io.kotest.core.spec.style.AnnotationSpec
import io.kotest.matchers.shouldBe
import java.util.Calendar
import java.util.Date
class SubscriptionPlanTest {
class SubscriptionPlanTest: BaseAnnotationTestCase() {
private var plan: SubscriptionPlan? = null
@BeforeEach
@AnnotationSpec.BeforeEach
fun setUp() {
plan = SubscriptionPlan()
plan!!.customerId = "fake_customer_id"
@ -19,7 +19,7 @@ class SubscriptionPlanTest {
@get:Test
val isActiveForNoTerminationDate: Unit
get() {
Assertions.assertTrue(plan!!.isActive)
plan?.isActive shouldBe true
}
@get:Test
@ -29,7 +29,7 @@ class SubscriptionPlanTest {
calendar.time = Date()
calendar.add(Calendar.DATE, 1)
plan!!.dateTerminated = calendar.time
Assertions.assertTrue(plan!!.isActive)
plan?.isActive shouldBe true
}
@get:Test
@ -39,6 +39,6 @@ class SubscriptionPlanTest {
calendar.time = Date()
calendar.add(Calendar.DATE, -1)
plan!!.dateTerminated = calendar.time
Assertions.assertFalse(plan!!.isActive)
plan?.isActive shouldBe false
}
}

View file

@ -1,15 +1,14 @@
package com.habitrpg.android.habitica.models
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import com.habitrpg.android.habitica.models.user.Items
import com.habitrpg.android.habitica.models.user.OwnedMount
import com.habitrpg.android.habitica.models.user.OwnedPet
import com.habitrpg.android.habitica.models.user.User
import io.kotest.matchers.shouldBe
import io.realm.RealmList
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class UserTest {
class UserTest: BaseAnnotationTestCase() {
private var user: User? = null
@BeforeEach
fun setup() {
@ -28,13 +27,13 @@ class UserTest {
pets.add(OwnedPet())
pets.add(OwnedPet())
user!!.items!!.pets = pets
Assertions.assertEquals(5, user!!.petsFoundCount)
user?.petsFoundCount shouldBe 5
}
@get:Test
val petsFoundCount_onNoPetCollectionAvailable_shouldReturnZero: Unit
get() {
Assertions.assertEquals(0, user!!.petsFoundCount)
user?.petsFoundCount shouldBe 0
}
@get:Test
@ -47,12 +46,12 @@ class UserTest {
mounts.add(OwnedMount())
mounts.add(OwnedMount())
user!!.items!!.mounts = mounts
Assertions.assertEquals(5, user!!.mountsTamedCount)
user?.mountsTamedCount shouldBe 5
}
@get:Test
val mountsTamedCount_onNoMountCollectionAvailable_shouldReturnZero: Unit
get() {
Assertions.assertEquals(0, user!!.mountsTamedCount)
user?.mountsTamedCount shouldBe 0
}
}

View file

@ -1,18 +1,16 @@
package com.habitrpg.android.habitica.models.inventory
import android.content.Context
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.extensions.getTranslatedType
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.Test
private const val FAKE_STANDARD = "Standard"
private const val FAKE_PREMIUM = "premium"
class MountTest {
@MockK private lateinit var mockContext: Context
class MountTest: BaseAnnotationTestCase() {
private var mount: Mount = Mount()
@Test

View file

@ -1,18 +1,16 @@
package com.habitrpg.android.habitica.models.inventory
import android.content.Context
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.extensions.getTranslatedType
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.Test
private const val FAKE_STANDARD = "Standard"
private const val FAKE_PREMIUM = "premium"
class PetTest {
@MockK private lateinit var mockContext: Context
class PetTest: BaseAnnotationTestCase() {
private var pet: Pet = Pet()
@Test

View file

@ -0,0 +1,23 @@
package com.habitrpg.android.habitica.models.inventory
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
class QuestBossTest : StringSpec({
val boss = QuestBoss()
beforeEach {
boss.rage = QuestBossRage()
}
"returns false for 0" {
boss.rage?.value = 0.0
boss.hasRage shouldBe false
}
"returns true for more than 0" {
boss.rage?.value = 1000.0
boss.hasRage shouldBe true
}
"returns false for no value" {
boss.rage = null
boss.hasRage shouldBe false
}
})

View file

@ -0,0 +1,8 @@
package com.habitrpg.android.habitica.models.members
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
class MemberTest : StringSpec({
"hasClass" { }
})

View file

@ -1,80 +0,0 @@
package com.habitrpg.android.habitica.utils;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Type;
import java.util.Date;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
public class DateDeserializerTest {
DateDeserializer deserializer;
JsonDeserializationContext deserializationContext;
JsonSerializationContext serializationContext;
Long referenceTimestamp;
@Before
public void setup() {
this.deserializer = new DateDeserializer();
this.deserializationContext = new JsonDeserializationContext() {
@Override
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return null;
}
};
this.serializationContext = new JsonSerializationContext() {
@Override
public JsonElement serialize(Object src) {
return null;
}
@Override
public JsonElement serialize(Object src, Type typeOfSrc) {
return null;
}
};
this.referenceTimestamp = Long.valueOf("1443445200000");
}
@Test
public void validateNormalDateDeserialize() {
Date date = this.deserializer.deserialize(new JsonPrimitive("2015-09-28T13:00:00.000Z"), Date.class, this.deserializationContext);
assertThat(date, is(new Date(referenceTimestamp)));
}
@Test
public void validateTimestampDeserialize() {
Date date = this.deserializer.deserialize(new JsonPrimitive(referenceTimestamp), Date.class, this.deserializationContext);
assertThat(date, is(new Date(referenceTimestamp)));
}
@Test
public void validateEmptyDeserialize() {
Date date = this.deserializer.deserialize(new JsonPrimitive(""), Date.class, this.deserializationContext);
assertNull(date);
}
@Test
public void validateNormalDateSerialize() {
JsonElement dateElement = this.deserializer.serialize(new Date(referenceTimestamp), Date.class, this.serializationContext);
assertThat(dateElement.getAsString(), is("2015-09-28T13:00:00.000Z"));
}
@Test
public void validateEmptySerialize() {
JsonElement dateElement = this.deserializer.serialize(null, Date.class, this.serializationContext);
assertThat(dateElement.getAsString(), is(""));
}
}

View file

@ -0,0 +1,80 @@
package com.habitrpg.android.habitica.utils
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.habitrpg.android.habitica.BaseAnnotationTestCase
import io.kotest.matchers.shouldBe
import java.lang.reflect.Type
import java.util.Date
class DateDeserializerTest: BaseAnnotationTestCase() {
var deserializer = DateDeserializer()
lateinit var deserializationContext: JsonDeserializationContext
lateinit var serializationContext: JsonSerializationContext
var referenceTimestamp: Long = 1443445200000
@Before
fun setup() {
deserializationContext = object : JsonDeserializationContext {
override fun <T> deserialize(json: JsonElement, typeOfT: Type): T? {
return null
}
}
serializationContext = object : JsonSerializationContext {
override fun serialize(src: Any): JsonElement? {
return null
}
override fun serialize(src: Any, typeOfSrc: Type): JsonElement? {
return null
}
}
}
@Test
fun validateNormalDateDeserialize() {
val date = deserializer.deserialize(
JsonPrimitive("2015-09-28T13:00:00.000Z"),
Date::class.java,
deserializationContext
)
date shouldBe Date(referenceTimestamp)
}
@Test
fun validateTimestampDeserialize() {
val date = deserializer.deserialize(
JsonPrimitive(referenceTimestamp),
Date::class.java,
deserializationContext
)
date shouldBe Date(referenceTimestamp)
}
@Test
fun validateEmptyDeserialize() {
val date =
deserializer.deserialize(JsonPrimitive(""), Date::class.java, deserializationContext)
date shouldBe null
}
@Test
fun validateNormalDateSerialize() {
val dateElement: JsonElement = deserializer!!.serialize(
Date(
referenceTimestamp!!
), Date::class.java, serializationContext
)
dateElement.asString shouldBe "2015-09-28T13:00:00.000Z"
}
@Test
fun validateEmptySerialize() {
val dateElement: JsonElement =
deserializer!!.serialize(null, Date::class.java, serializationContext)
dateElement.asString shouldBe ""
}
}