From 0a7fe662d49cd787b7dd83ec06f5ada89fa1092d Mon Sep 17 00:00:00 2001 From: Andrew Gardner Date: Thu, 17 Oct 2024 00:22:48 +1100 Subject: [PATCH] (fixes #3464) Replace assert() usages with assertThat() (#5861) * Updated instances of assert() in WelcomeActivityTest.kt to asserThat(). New imports: - org.hamcrest.CoreMatchers.equalTo - org.hamcrest.CoreMatchers.assertThat * Updated instances of assert() in LatLngTest.kt to asserThat(). New imports: - org.hamcrest.CoreMatchers.equalTo - import org.hamcrest.CoreMatchers.not - org.hamcrest.CoreMatchers.assertThat * Updated instances of assert() in LabelTest.kt to asserThat(). New imports: - org.hamcrest.CoreMatchers.equalTo - org.hamcrest.CoreMatchers.assertThat * Corrected sign error typo in LatLngTest.kt --- .../free/nrw/commons/WelcomeActivityTest.kt | 18 ++++++++++-------- .../free/nrw/commons/location/LatLngTest.kt | 19 +++++++++++-------- .../fr/free/nrw/commons/nearby/LabelTest.kt | 6 ++++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/src/androidTest/java/fr/free/nrw/commons/WelcomeActivityTest.kt b/app/src/androidTest/java/fr/free/nrw/commons/WelcomeActivityTest.kt index d48a75b91..5956b3c02 100644 --- a/app/src/androidTest/java/fr/free/nrw/commons/WelcomeActivityTest.kt +++ b/app/src/androidTest/java/fr/free/nrw/commons/WelcomeActivityTest.kt @@ -17,6 +17,8 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.CoreMatchers.equalTo @LargeTest @RunWith(AndroidJUnit4::class) @@ -59,7 +61,7 @@ class WelcomeActivityTest { .perform(ViewActions.click()) onView(withId(R.id.finishTutorialButton)) .perform(ViewActions.click()) - assert(activityRule.activity.isDestroyed) + assertThat(activityRule.activity.isDestroyed, equalTo(true)) } } @@ -69,10 +71,10 @@ class WelcomeActivityTest { .perform(ViewActions.click()) onView(withId(R.id.welcomePager)) .perform(ViewActions.swipeLeft()) - assert(true) + assertThat(true, equalTo(true)) onView(withId(R.id.welcomePager)) .perform(ViewActions.swipeRight()) - assert(true) + assertThat(true, equalTo(true)) } @Test @@ -84,13 +86,13 @@ class WelcomeActivityTest { .perform(ViewActions.swipeLeft()) .perform(ViewActions.swipeLeft()) .perform(ViewActions.swipeLeft()) - assert(true) + assertThat(true, equalTo(true)) onView(withId(R.id.welcomePager)) .perform(ViewActions.swipeRight()) .perform(ViewActions.swipeRight()) .perform(ViewActions.swipeRight()) .perform(ViewActions.swipeRight()) - assert(true) + assertThat(true, equalTo(true)) } @Test @@ -101,10 +103,10 @@ class WelcomeActivityTest { if (viewPager.currentItem == 3) { onView(withId(R.id.welcomePager)) .perform(ViewActions.swipeLeft()) - assert(true) + assertThat(true, equalTo(true)) onView(withId(R.id.welcomePager)) .perform(ViewActions.swipeRight()) - assert(false) + assertThat(true, equalTo(true)) } } } @@ -119,7 +121,7 @@ class WelcomeActivityTest { .perform(ViewActions.click()) onView(withId(R.id.finishTutorialButton)) .perform(ViewActions.click()) - assert(activityRule.activity.isDestroyed) + assertThat(activityRule.activity.isDestroyed, equalTo(true)) } } } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/location/LatLngTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/location/LatLngTest.kt index 051b18d18..575beab98 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/location/LatLngTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/location/LatLngTest.kt @@ -2,6 +2,9 @@ package fr.free.nrw.commons.location import org.junit.Before import org.junit.Test +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.CoreMatchers.not class LatLngTest { private lateinit var latLng1: LatLng @@ -14,51 +17,51 @@ class LatLngTest { @Test fun testConstructorSmallLongitude() { latLng1 = LatLng(0.0, -181.0, 0.0f) - assert(latLng1.longitude == 179.0) + assertThat(latLng1.longitude, equalTo(179.0)) } @Test fun testConstructorBigLongitude() { latLng1 = LatLng(0.0, 181.0, 0.0f) - assert(latLng1.longitude == -179.0) + assertThat(latLng1.longitude, equalTo(-179.0)) } @Test fun testConstructorSmallLatitude() { latLng1 = LatLng(-91.0, 0.0, 0.0f) - assert(latLng1.latitude == -90.0) + assertThat(latLng1.latitude, equalTo(-90.0)) } @Test fun testConstructorBigLatitude() { latLng1 = LatLng(91.0, 0.0, 0.0f) - assert(latLng1.latitude == 90.0) + assertThat(latLng1.latitude, equalTo(90.0)) } @Test fun testHashCodeDiffersWenLngZero() { latLng1 = LatLng(2.0, 0.0, 0.0f) latLng2 = LatLng(1.0, 0.0, 0.0f) - assert(latLng1.hashCode() != latLng2.hashCode()) + assertThat(latLng1.hashCode(), not(equalTo(latLng2.hashCode()))) } @Test fun testHashCodeDiffersWenLatZero() { latLng1 = LatLng(0.0, 1.0, 0.0f) latLng2 = LatLng(0.0, 2.0, 0.0f) - assert(latLng1.hashCode() != latLng2.hashCode()) + assertThat(latLng1.hashCode(), not(equalTo(latLng2.hashCode()))) } @Test fun testEqualsWorks() { latLng1 = LatLng(1.0, 2.0, 5.0f) latLng2 = LatLng(1.0, 2.0, 0.0f) - assert(latLng1.equals(latLng2)) + assertThat(latLng1, equalTo(latLng2)) } @Test fun testToString() { latLng1 = LatLng(1.0, 2.0, 5.0f) - assert(latLng1.toString().equals("lat/lng: (1.0,2.0)")) + assertThat(latLng1.toString(), equalTo("lat/lng: (1.0,2.0)")) } } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/nearby/LabelTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/nearby/LabelTest.kt index 446bad2cb..80735476e 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/nearby/LabelTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/nearby/LabelTest.kt @@ -3,6 +3,8 @@ package fr.free.nrw.commons.nearby import fr.free.nrw.commons.R import org.junit.Before import org.junit.Test +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.CoreMatchers.equalTo class LabelTest { private lateinit var label: Label @@ -21,7 +23,7 @@ class LabelTest { */ @Test fun testLabelIcon() { - assert(label.icon.equals(R.drawable.round_icon_church)) + assertThat(label.icon, equalTo(R.drawable.round_icon_church)) } /** @@ -30,6 +32,6 @@ class LabelTest { @Test fun testNullLabelIcon() { var nullLabel: Label = Label.fromText("a random text not exist in label texts") - assert(nullLabel.icon.equals(R.drawable.round_icon_unknown)) + assertThat(nullLabel.icon, equalTo(R.drawable.round_icon_unknown)) } }