Cotton code
91 lines · 4.4 KB · KotlinRaw
package com.cottondb.cinemasimport com.cottondb.cinemas.faces.Cardimport com.cottondb.cinemas.faces.EpisodeLobbyimport com.cottondb.cinemas.faces.FilmPosterimport com.cottondb.cinemas.faces.Measureimport com.cottondb.cinemas.faces.dayStampimport com.cottondb.cinemas.faces.linesimport com.cottondb.shell.player.Heardimport com.cottondb.shell.core.Linesimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.doubleimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Test/** The Cinemas faces' setting rules (ported from FilmPosterFace / EpisodeLobbyFace) and the player's Heard clock, on invented cards. */class CinemaSettingTest {    private val words = Lines(        mapOf(            "cards.film.length_minutes" to "{minutes} min",            "cards.film.length_hours" to "{hours} h {minutes}",            "cards.film.position" to "{at} of {of}",            "cards.film.stub" to "Watched",            "cards.film.stub_converting" to "Converting",            "cards.date.add" to "Add date",        ),        "Cotton Cinemas",    )    /** Every letter a unit wide. */    private val mono = Measure { text, _, size -> text.length * size }    private fun card(json: String) = Card(Json.parseToJsonElement(json) as JsonObject)    @Test    fun aFilmSaysItsLengthInHoursOrMinutes() {        assertEquals("1 h 52", FilmPoster.length(card("""{"slots":{"limit":112}}"""), words))        assertEquals("1 h 05", FilmPoster.length(card("""{"slots":{"limit":65}}"""), words))        assertEquals("48 min", FilmPoster.length(card("""{"slots":{"limit":48}}"""), words))        assertNull(FilmPoster.length(card("""{"slots":{}}"""), words))    }    @Test    fun theStripFillsOnlyPartWayAndNeverForASeriesOrAWatchedFilm() {        assertEquals(0.5, FilmPoster.share(card("""{"slots":{"limit":100,"used":3000}}"""))!!, 1e-9)        assertNull(FilmPoster.share(card("""{"slots":{"limit":100,"used":3000},"stored":{"state":"watched"}}""")))        assertNull(FilmPoster.share(card("""{"slots":{"limit":100,"used":3000},"stored":{"facts":"series"}}""")))    }    @Test    fun aSeriesLaysItsNewestSeasonsNewestAtTheBack() {        val series = card("""{"slots":{},"stored":{"facts":"series"},"related":[{"id":"film_season","said":"5 seasons","count":5}]}""")        assertEquals(listOf(5, 3, 4), FilmPoster.seasons(series))        assertEquals(emptyList<Int>(), FilmPoster.seasons(card("""{"slots":{}}""")))    }    @Test    fun theStubSaysWatchedWithItsDayOrConverting() {        assertEquals("WATCHED" to "12 NOV 2026", FilmPoster.stub(card("""{"slots":{"date":"Thu 12 Nov 2026"},"stored":{"state":"watched"}}"""), words))        assertEquals("CONVERTING" to null, FilmPoster.stub(card("""{"slots":{},"stored":{"state":"needs_mac"}}"""), words))        assertEquals("WATCHED" to null, FilmPoster.stub(card("""{"slots":{"date":"Add date"},"stored":{"state":"watched"}}"""), words))        assertEquals("12 NOV", dayStamp("Thu 12 Nov"))    }    @Test    fun anEpisodeSaysWhereItStandsOnAPlayersClock() {        assertEquals("24:00 of 40:00", EpisodeLobby.place(card("""{"slots":{"limit":40,"used":1440}}"""), words))        assertEquals("40:00 of 40:00", EpisodeLobby.place(card("""{"slots":{"limit":40,"used":5},"stored":{"state":"watched"}}"""), words))        assertEquals("1:52:00", EpisodeLobby.clock(6720.0))        assertEquals("5:30", EpisodeLobby.clock(330.0))    }    @Test    fun wordsBreakAtSpacesAndThePastTheLimitIsCutByLetters() {        assertEquals(listOf("The Fog", "Bell"), lines("The Fog Bell", mono, Measure.Face.Type, 1f, 7f, 2))        assertEquals(listOf("The Fog", "Bell R…"), lines("The Fog Bell Rings", mono, Measure.Face.Type, 1f, 7f, 2))    }    @Test    fun heardCountsForwardPlayButNotAScrub() {        val heard = Heard("film", "f1", from = 10.0)        heard.tick(11.0); heard.tick(12.0); heard.tick(300.0); heard.tick(301.0)        val report = heard.report("stopped", 301.0, 600.0, "An invented phone")!!        assertEquals(3.0, report["seconds"]!!.jsonPrimitive.double, 1e-9)        assertEquals(301.0, report["at_s"]!!.jsonPrimitive.double, 1e-9)        assertEquals("stopped", report["ended"]!!.jsonPrimitive.content)        assertNull("a play ends once", heard.report("stopped", 301.0, 600.0, null))    }}