Cotton code
123 lines · 6.2 KB · KotlinRaw
package com.cottondb.musicimport com.cottondb.music.faces.Cardimport com.cottondb.music.faces.Sleeveimport com.cottondb.music.faces.SleeveLineimport com.cottondb.music.faces.TypeWideimport com.cottondb.music.faces.balancedLinesimport com.cottondb.shell.core.Linesimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Test/** The sleeve's setting rules (AlbumSleeveFace.swift: AlbumSleeveSetting), on invented albums. */class SleeveSettingTest {    private val words = Lines(mapOf("cards.sleeve.disc" to "Disc {n}", "cards.sleeve.more" to "and {n} more", "cards.join" to " · "), "Cotton Music")    /** Every letter half its size wide. */    private val half = TypeWide { text, size -> text.length * size / 2 }    private fun card(json: String) = Card(Json.parseToJsonElement(json) as JsonObject)    private fun album(vararg songs: Pair<String, Int>) = card(        """{"slots":{"songs":[${songs.mapIndexed { i, (title, disc) -> """{"record":"r$i","title":"$title","duration":${200 + i},"disc":$disc}""" }.joinToString(",")}]}}""",    )    @Test    fun aShortAlbumListsEverySongNumberedOnItsDisc() {        val got = Sleeve.lines(album("A" to 1, "B" to 1, "C" to 1))        assertEquals(listOf(SleeveLine.Song(1, "A", 200.0, "r0", 0), SleeveLine.Song(2, "B", 201.0, "r1", 1), SleeveLine.Song(3, "C", 202.0, "r2", 2)), got)    }    @Test    fun aSecondDiscIsNamedAndNumberedFromOne() {        val got = Sleeve.lines(album("A" to 1, "B" to 1, "C" to 2))        assertEquals(SleeveLine.Disc(2), got[2])        assertEquals(SleeveLine.Song(1, "C", 202.0, "r2", 2), got[3])    }    @Test    fun moreThanTheBackHoldsEndsOnHowManyMoreNeverOnADiscsName() {        // Five on disc one, nine on disc two: the eight lines kept would end on "Disc 2" after five songs.        val songs = (1..5).map { "a$it" to 1 } + (1..9).map { "b$it" to 2 }        val got = Sleeve.lines(album(*songs.toTypedArray()))        assertEquals(9, got.size)        assertEquals(SleeveLine.More(14 - 7), got.last())        assertTrue(got.dropLast(1).none { it is SleeveLine.More })        val ending = Sleeve.lines(album(*((1..4).map { "a$it" to 1 } + (1..9).map { "b$it" to 2 }).toTypedArray()))        // Four songs, "Disc 2", three songs: eight; then how many more.        assertEquals(SleeveLine.Disc(2), ending[4])        assertEquals(SleeveLine.More(13 - 7), ending.last())        val trimmed = Sleeve.lines(album(*((1..7).map { "a$it" to 1 } + (1..5).map { "b$it" to 2 }).toTypedArray()))        // Seven songs then "Disc 2" would be the eighth line: it is dropped.        assertEquals(8, trimmed.size)        assertEquals(SleeveLine.More(5), trimmed.last())    }    @Test    fun theLinesSayTheirDiscAndHowManyMoreInTheLinesWords() {        assertEquals("Disc 2", Sleeve.said(SleeveLine.Disc(2), words))        assertEquals("and 5 more", Sleeve.said(SleeveLine.More(5), words))        assertNull(Sleeve.said(SleeveLine.Song(1, "A", 1.0, null, 0), words))        assertEquals("01", Sleeve.number(1))        assertEquals("12", Sleeve.number(12))    }    @Test    fun theWholeJoinsTheSongsAndTheLength() {        assertEquals("9 songs · 38:46", Sleeve.whole(card("""{"slots":{"count":"9 songs","length":"38:46"}}"""), words))        assertEquals("38:46", Sleeve.whole(card("""{"slots":{"length":"38:46"}}"""), words))        assertNull(Sleeve.whole(card("""{"slots":{}}"""), words))    }    @Test    fun theBlanksTitleIsInCapitalsAtItsSizeOrTakenDownToFitBalanced() {        // 740 wide: "COMMON" at 200 stands 600, "COMMON GROUND" would be 1300 — two lines, 368 of the room 589.4.        val common = Sleeve.title(card("""{"slots":{"title":"Common Ground"}}"""), half)        assertEquals(listOf("COMMON", "GROUND"), common.lines)        assertEquals(200f, common.size, 0f)        // A word wider than the room shrinks the title before it would be broken.        val long = Sleeve.title(card("""{"slots":{"title":"Harbourlights"}}"""), half)        assertTrue(long.size < 200f)        assertTrue(half.wide("HARBOURLIGHTS", long.size) <= 740f)        // Never below the least size, the lines never more than three.        val largest = Sleeve.title(card("""{"slots":{"title":"The Harbour Lights Sessions, Recorded Live Across Sixteen Lanterns and the Blue Hour Ferry Crossings"}}"""), half)        assertTrue(largest.size >= 84f)        assertTrue(largest.lines.size <= 3)        assertTrue(largest.lines.last().endsWith("…"))        assertEquals(emptyList<String>(), Sleeve.title(card("""{"slots":{}}"""), half).lines)    }    @Test    fun balancedLinesEvenTheBreak() {        // At 10 wide a letter 5: "AAAA BB CC" breaks "AAAA BB" / "CC" greedily at 40; balanced it is "AAAA" / "BB CC".        assertEquals(listOf("AAAA", "BB CC"), balancedLines("AAAA BB CC", half, 10f, 40f, 3))    }    @Test    fun theFrontsArtistIsSpacedAndTakenDownToFit() {        val short = Sleeve.artist(card("""{"slots":{"subtitle":"Mara Vance"}}"""), half)!!        assertEquals("MARA VANCE", short.lines.first())        assertEquals(52f, short.size, 0f)        val long = Sleeve.artist(card("""{"slots":{"subtitle":"Mara Vance and The Low Tide Orchestra"}}"""), half)!!        assertTrue(long.size < 52f && long.size >= 26f)        assertNull(Sleeve.artist(card("""{"slots":{}}"""), half))    }    @Test    fun theBacksHeadTakesTwoLinesAtMostAndASongsTitleIsCutToItsColumn() {        val title = Sleeve.backTitle(card("""{"slots":{"title":"Night Ferry"}}"""), half)!!        assertEquals(listOf("Night Ferry"), title.lines)        assertEquals(68f, title.size, 0f)        val long = Sleeve.backTitle(card("""{"slots":{"title":"The Harbour Lights Sessions, Recorded Live Across Sixteen Lanterns and the Blue Hour Ferry Crossings"}}"""), half)!!        assertTrue(long.lines.size <= 2)        assertTrue(long.size >= 68f * 0.6f - 1e-3f)        assertEquals(660f, Sleeve.titleColumn, 0f)        val cut = Sleeve.songTitle("Slow Train Home Through the Sixteen Lanterns", half)        assertTrue(cut.endsWith("…"))        assertTrue(half.wide(cut, 36f) <= 660f)        assertEquals("", Sleeve.songTitle(null, half))    }}