Cotton code
88 lines · 4.0 KB · KotlinRaw
package com.cottondb.musicimport com.cottondb.music.faces.Cardimport com.cottondb.music.faces.Singleimport com.cottondb.music.faces.TypeWideimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Test/** The single's setting rules (SongSingleFace.swift), on invented songs. */class SingleSettingTest {    /** Every letter half its size wide. */    private val half = TypeWide { words, size -> words.length * size / 2 }    private fun card(json: String) = Card(Json.parseToJsonElement(json) as JsonObject)    @Test    fun theSleevesPaperIsPickedByTheAlbumElseTheSongElseCream() {        assertEquals(Single.Paper.Blue, Single.paper(card("""{"slots":{"title":"Harbour Lights","album":"Night Ferry"}}""")))        assertEquals(Single.Paper.Cream, Single.paper(card("""{"slots":{"title":"Slow Train","album":"Common Ground"}}""")))        // Every single off one album shares its paper, whatever the song.        assertEquals(Single.paper(card("""{"slots":{"title":"A","album":"Common Ground"}}""")), Single.paper(card("""{"slots":{"title":"B","album":"Common Ground"}}""")))        assertEquals(Single.Paper.Blue, Single.paper(card("""{"slots":{"title":"Slow Train"}}""")))        assertEquals(Single.Paper.Cream, Single.paper(card("""{"slots":{}}""")))        assertEquals("single-sleeve-blue", Single.Paper.Blue.painting)    }    @Test    fun aFavouriteIsOnlyATrueFavourite() {        assertTrue(Single.favourite(card("""{"slots":{"favourite":true}}""")))        assertFalse(Single.favourite(card("""{"slots":{"favourite":false}}""")))        assertFalse(Single.favourite(card("""{"slots":{}}""")))    }    @Test    fun theLabelCarriesTheNameTheArtistInCapitalsAndTheLength() {        assertEquals(listOf("Blue Hour", "SIXTEEN LANTERNS", "5:48"), Single.label(card("""{"slots":{"title":"Blue Hour","subtitle":"Sixteen Lanterns","length":"5:48"}}""")))        assertEquals(listOf("Blue Hour"), Single.label(card("""{"slots":{"title":"Blue Hour"}}""")))    }    @Test    fun theLabelLiesInTheWindowItsWordsInItsPaintingsPixels() {        assertEquals(500f / 340f, Single.k, 1e-6f)        val title = Single.inLabel(Single.titleBox)        assertEquals(320f + 52f * 500f / 340f, title.x, 1e-3f)        assertEquals(236f * 500f / 340f, title.w, 1e-3f)        assertEquals(652f, Single.round.w, 0f)        assertEquals(570f, Single.round.midX, 1e-3f)        assertEquals(582.5f, Single.round.midY, 1e-3f)        assertEquals(154f / 170f, Single.inner, 1e-6f)    }    @Test    fun aShortNameTakesOneLineAtItsSizeALongerOneAStepDownTheLongestTwoLines() {        // 236 wide: 9 letters at 50 stand 225.        assertEquals(50f, Single.title("Blue Hour", half).size, 0f)        assertEquals(1, Single.title("Blue Hour", half).lines.size)        // 11 letters: 275 at 50, 231 at 42.        assertEquals(42f, Single.title("Slow Trains", half).size, 1e-4f)        val long = Single.title("The Slow Train Home Through Sixteen Lanterns", half)        assertEquals(2, long.lines.size)        assertTrue(long.size <= 50f * 0.62f)        assertTrue(long.lines.all { half.wide(it, long.size) <= Single.titleBox.w })    }    @Test    fun theArtistIsSpacedWhileItFitsElseCloseSetAndShrunk() {        val short = Single.artist("Mara Vance", half)        assertEquals("MARA VANCE", short.lines.first())        assertEquals(24f, short.size, 0f)        assertEquals(0.12f, short.tracking, 0f)        val long = Single.artist("Mara Vance and The Low Tide", half)        assertEquals(0f, long.tracking, 0f)        assertTrue(long.size < 24f)        assertTrue(long.size >= 24f * 0.4f)    }    @Test    fun theStarHasFivePointsAndFiveWaists() {        val points = Single.starPoints(92f)        assertEquals(10, points.size)        assertEquals(46f, points[0].first, 1e-3f)        assertEquals(46f * 0.04f * 2, points[0].second, 1e-3f)    }}