Cotton code
67 lines · 4.1 KB · KotlinRaw
package com.cottondb.books.facesimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testclass HardbackSettingTest {    private val k = HardbackKnobs    /** The ribbon is never a stub: a sixth of the front for a book not begun (y263), on from there by the share (41 % to y664), the front's foot at the whole, past the foot once finished — BookHardbackTests' own numbers. */    @Test    fun the_ribbon_hangs_a_sixth_at_least_and_runs_on_by_the_share() {        assertEquals(263f, HardbackSetting.ribbonEnd("want_to_read", 0.0))        assertEquals(263f, HardbackSetting.ribbonEnd("reading", 0.0))        assertEquals(664f, HardbackSetting.ribbonEnd("reading", 0.41))        assertEquals(k.ribbonFront + k.ribbonFrontHeight, HardbackSetting.ribbonEnd("reading", 1.0))        assertEquals(k.boxH + k.ribbonHang, HardbackSetting.ribbonEnd("finished", 0.2))    }    /** The ribbon comes out of the head in from the spine, over the cover's left margin, its cloth (the painting's columns 2–73) clear of the blank jacket's title. */    @Test    fun the_ribbon_hangs_in_from_the_spine() {        assertTrue(k.ribbon.x + 2 >= k.jacket.x + 40)        assertTrue(k.ribbon.x + 73 < k.title.x)    }    /** A file-name title breaks after its underscores, hyphens and dots and at its capitals, every character kept; a title with a space is words. */    @Test    fun a_file_name_title_breaks_sensibly() {        assertEquals(listOf("the_", "lamp_", "keepers.", "epub"), HardbackSetting.fileNamePieces("the_lamp_keepers.epub"))        assertEquals(listOf("The", "Lamp", "Keeper", "2"), HardbackSetting.fileNamePieces("TheLampKeeper2"))        assertEquals(listOf("The Lamp_Keeper"), HardbackSetting.fileNamePieces("The Lamp_Keeper"))        assertEquals("the_​lamp.​epub", HardbackSetting.breakable("the_lamp.epub"))        val lines = wrap(HardbackSetting.breakable("the_lamp_keepers_daughter.epub"), { it.length * 10f }, 120f)        assertEquals("the_lamp_keepers_daughter.epub", lines.joinToString(""))        lines.forEach { assertTrue(it, it.length * 10f <= 120f) }        lines.dropLast(1).forEach { assertTrue(it, it.endsWith("_") || it.endsWith(".")) }    }    /** A stopped book lies in the painted pose: every layer inside the canvas the face fits, the slip out past the fore-edge about a third of the way down. */    @Test    fun a_stopped_book_lies_turned() {        listOf(k.lyingBook, k.lyingShadow, k.lyingSlip, k.lyingSlipShadow, k.lyingGloss, k.lyingRibbon).forEach { r ->            assertTrue("$r", r.x >= k.lying.x && r.y >= k.lying.y && r.maxX <= k.lying.maxX && r.y + r.h <= k.lying.y + k.lying.h)        }        assertTrue(k.slipQuad.maxOf { it.x } > k.coverQuad[1].x)        val middle = k.slipQuad.map { it.y }.average().toFloat()        val down = (middle - k.coverQuad[1].y) / (k.coverQuad[2].y - k.coverQuad[1].y)        assertTrue("$down", down in 0.2f..0.45f)    }    /** A long author's name is shrunk, then cut at a whole word with an ellipsis, never clipped mid-letter at the frame (hands-on, 22 Nov 2026). */    @Test    fun a_long_author_is_shrunk_then_cut_at_a_word_never_clipped() {        val measure = { text: String, at: Float -> text.length * at * 0.7f }        assertEquals("ASHA VARMA" to 20f, HardbackSetting.authorLine("ASHA VARMA", 20f, 400f, measure))        val (short, size) = HardbackSetting.authorLine("RAVI SHANKAR IYER", 20f, 200f, measure)        assertEquals("RAVI SHANKAR IYER", short)        assertTrue(size < 20f && size >= 10f && measure(short, size) <= 200f)        val (cut, least) = HardbackSetting.authorLine("AN AUTHOR WHOSE NAME RUNS ON FAR LONGER THAN ANY COVER ALLOWS", 20f, 200f, measure)        assertTrue(cut, cut.endsWith("…") && !cut.dropLast(1).endsWith(" "))        assertTrue(measure(cut, least) <= 200f)        assertTrue("whole words: $cut", "AN AUTHOR WHOSE NAME RUNS ON FAR LONGER THAN ANY COVER ALLOWS".startsWith(cut.dropLast(1) + " "))        val (one, _) = HardbackSetting.authorLine("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ", 20f, 100f, measure)        assertTrue(one, one.endsWith("…") && measure(one, 10f) <= 100f)    }}