Cotton code
131 lines · 7.3 KB · KotlinRaw
package com.cottondb.musicimport com.cottondb.music.faces.Cardimport com.cottondb.music.faces.CassetteKnobsimport com.cottondb.music.faces.CassetteSongimport com.cottondb.music.faces.CassetteTapeimport com.cottondb.music.faces.CassetteWordsimport com.cottondb.music.faces.ListCassetteimport com.cottondb.music.faces.Measureimport com.cottondb.music.faces.SongCassetteimport com.cottondb.music.faces.TrackPlayimport com.cottondb.shell.core.Linesimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport kotlin.math.sqrt/** The cassette's setting rules (CassetteParts.swift, SongCassetteFace, ListCassetteFace), on invented cards. */class CassetteSettingTest {    private val words = Lines(mapOf("cards.cassette.more_short" to "+{n}"), "Cotton Music")    /** Every letter half its size wide. */    private val m = Measure { said, _, size -> said.length * size * 0.5f }    private fun card(slots: String, face: String) =        Card(Json.parseToJsonElement("""{"id":"c","face":"$face","slots":$slots}""").jsonObject)    private val nightDrives = card(        """{"title":"Night Drives","count":"3 songs","length":"11:41","songs":[           {"title":"Harbour Lights","duration":221},{"title":"Slow Train","duration":302},{"title":"Blue Hour","duration":178,"unavailable":true}]}""",        "list.cassette",    )    @Test fun aListsTapeReadsItsSongs() {        val said = ListCassette.words(nightDrives)        assertEquals("Night Drives", said.title)        assertEquals(listOf("Harbour Lights", "Slow Train", "Blue Hour"), said.titles)        assertNull(said.byline)        assertEquals("11:41", said.length)        assertFalse(said.steps)        assertEquals(CassetteSong("Blue Hour", 178.0, available = false), said.songs[2])        val blank = ListCassette.words(card("""{"title":"For the new flat","count":"0 songs","songs":[]}""", "list.cassette"))        assertEquals("0 songs", blank.byline)        assertTrue(blank.songs.isEmpty())    }    @Test fun aSongsTapeReadsTheSong() {        val song = SongCassette.words(card("""{"title":"Harbour Lights","subtitle":"The Night Ferries","album":"Coastline","length":"4:02"}""", "song.cassette"))        assertEquals("The Night Ferries / Coastline", song.byline)        assertEquals("4:02", song.length)        assertFalse(song.steps)        assertEquals(0.0, song.songs.single().seconds, 0.0)        val player = card("""{"title":"Harbour Lights","artist":"The Night Ferries","duration":242,"now_playing":"specimen-song"}""", "song.cassette")        val held = SongCassette.words(player, TrackPlay(held = true, duration = 300.0))        assertEquals("The Night Ferries", held.byline)        assertEquals("4:02", held.length)        assertTrue(held.steps)        assertEquals(242.0, held.songs.single().seconds, 0.0)        // The player's length where the card keeps none.        val bare = SongCassette.words(card("""{"title":"Harbour Lights"}""", "song.cassette"), TrackPlay(held = true, duration = 61.0))        assertEquals("1:01", bare.length)        assertNull(bare.byline)        assertEquals(0, SongCassette.onTheSong(TrackPlay(held = true)).song)        assertNull(SongCassette.onTheSong(TrackPlay.still).song)    }    @Test fun thePacksWindWithThePlace() {        val songs = CassetteTape.songs(nightDrives)        val play = TrackPlay(held = true, playing = true, elapsed = 134.0, duration = 302.0, song = 1)        assertEquals(355.0, CassetteTape.position(songs, play), 1e-9)        assertEquals(355.0 / 701.0, CassetteTape.wound(songs, play), 1e-9)        assertEquals(0.0, CassetteTape.position(songs, play.copy(song = null)), 0.0)        assertEquals(221.0 + 302.0, CassetteTape.position(songs, play.copy(elapsed = 9999.0)), 1e-9)        assertEquals(0.0, CassetteTape.wound(emptyList(), play), 0.0)        assertEquals(95f, CassetteTape.radius(0.0), 1e-4f)        assertEquals(280f, CassetteTape.radius(1.0), 1e-4f)        assertEquals(sqrt(95f * 95f + 0.5f * (280f * 280f - 95f * 95f)), CassetteTape.radius(0.5), 1e-3f)        val pack = CassetteTape.pack(280f, CassetteKnobs.hubs[0])        assertEquals(576f, pack.w, 1e-4f)        assertEquals(338f - 288f, pack.x, 1e-4f)    }    @Test fun theHubsTurnAndTheCounterSaysWhereItStands() {        val said = ListCassette.words(nightDrives)        val play = TrackPlay(held = true, playing = true, elapsed = 134.0, duration = 302.0, song = 1)        assertEquals(355.0 * 48, CassetteTape.turned(said, play, reduceMotion = false), 1e-9)        assertEquals(0.0, CassetteTape.turned(said, play, reduceMotion = true), 0.0)        assertEquals("2:14", CassetteTape.counter(said, play))        assertEquals("1:00", CassetteTape.counter(said, play, dragged = 60.0))        assertEquals("11:41", CassetteTape.counter(said, TrackPlay.still))        assertFalse(CassetteTape.scrubs(said, play))        assertTrue(CassetteTape.scrubs(said.copy(steps = true), play))        assertEquals(151.0, CassetteTape.scrubbed(140.5f, 281f, 302.0)!!, 1e-9)        assertEquals(302.0, CassetteTape.scrubbed(900f, 281f, 302.0)!!, 0.0)        assertNull(CassetteTape.scrubbed(10f, 0f, 302.0))    }    @Test fun theHandLineNamesAsManyAsFit() {        val titles = listOf("Harbour Lights", "Slow Train", "Blue Hour")        assertEquals("Harbour Lights / Slow Train / Blue Hour", CassetteTape.named(titles, 3, words))        assertEquals("Harbour Lights / Slow Train +1", CassetteTape.named(titles, 2, words))        assertEquals("+3", CassetteTape.named(titles, 0, words))        val said = CassetteWords(titles = titles)        assertEquals("Harbour Lights / Slow Train / Blue Hour", CassetteTape.line(said, m, Measure.Face.Caslon, words)!!.words)        // Forty songs: as many as fit in 960 at 42 (45 letters), then "+N".        val many = (1..40).map { "Song $it" }        val line = CassetteTape.line(CassetteWords(titles = many), m, Measure.Face.Caslon, words)!!        assertTrue(line.words.length * 21f <= 960f)        assertTrue(line.words.endsWith("+" + (40 - line.words.split(" / ").size)))        // Not one fits: the first cut in its middle.        val long = CassetteTape.line(CassetteWords(titles = listOf("A".repeat(80), "B")), m, Measure.Face.Caslon, words)!!        assertTrue(long.words.contains("…"))        assertTrue(long.words.length * 21f <= 960f)        assertTrue(long.words.endsWith("+1"))        // A byline is set whole, taken down and cut.        assertEquals("The Night Ferries / Coastline", CassetteTape.line(CassetteWords(byline = "The Night Ferries / Coastline"), m, Measure.Face.Caslon, words)!!.words)        assertNull(CassetteTape.line(CassetteWords(), m, Measure.Face.Caslon, words))    }    @Test fun theShellIsDrawnAtMostThreeHundredAndFortyWide() {        assertEquals(340f, CassetteTape.width(400f, 800f), 1e-4f)        assertEquals(300f / (1300f / 1200f), CassetteTape.width(300f, 800f), 1e-3f)        assertEquals(100f / (900f / 745f) * 1200f / 745f, CassetteTape.width(400f, 100f), 1e-3f)        val title = CassetteTape.title("Everything we played on the long drive north to the lake house", m, Measure.Face.Caslon)        assertEquals(104f * 0.45f, title.size, 1e-3f)        assertTrue(title.words.endsWith("…"))    }}