~ knotless Search/
↑↓ move↵ open⇧↵ all results

grain.social/grain-android

1 collaborator
0
1
main
57 lines · 2.1 KB Raw History
package social.grain

import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.data.location.Coordinate
import social.grain.feature.explore.LocationPin
import social.grain.feature.explore.pinBounds
import social.grain.feature.explore.placeFeatures
import social.grain.ui.components.localityOf

/** The pins as the map wants them: GeoJSON points, and a box around them. */
@RunWith(AndroidJUnit4::class)
class PlaceFeaturesTest {

    private val lisbon = LocationPin("Lisbon, PT", "85390cb7fffffff", 3, Coordinate(38.72, -9.14))
    private val porto = LocationPin("Porto, PT", "853913d3fffffff", 1, Coordinate(41.15, -8.61))

    @Test
    fun eachPinBecomesAPointCarryingWhatATapNeeds() {
        val features = placeFeatures(listOf(lisbon, porto))

        assertEquals(2, features.features.size)
        val first = features.features.first()
        assertEquals(-9.14, first.geometry.longitude, 1e-9)
        assertEquals(38.72, first.geometry.latitude, 1e-9)
        assertEquals("Lisbon, PT", first.properties["name"]?.jsonPrimitive?.content)
        assertEquals("85390cb7fffffff", first.properties["h3"]?.jsonPrimitive?.content)
        assertEquals("3", first.properties["count"]?.jsonPrimitive?.content)
    }

    @Test
    fun theBoundsFrameEveryPin() {
        val bounds = pinBounds(listOf(lisbon, porto))!!

        assertEquals(-9.14, bounds.west, 1e-9)
        assertEquals(38.72, bounds.south, 1e-9)
        assertEquals(-8.61, bounds.east, 1e-9)
        assertEquals(41.15, bounds.north, 1e-9)
    }

    @Test
    fun theMapLabelIsTheLocalityAlone() {
        assertEquals("Nazaré", localityOf("Nazaré, Leiria, PT"))
        assertEquals("Lisbon", localityOf("Lisbon, PT"))
        assertEquals("Portugal", localityOf("Portugal"))
        assertEquals(", PT", localityOf(", PT"))
    }

    @Test
    fun nothingToFrameIsNoBoundsRatherThanAnEmptyBox() {
        assertNull(pinBounds(emptyList()))
    }
}