Cotton code
88 lines · 4.4 KB · KotlinRaw
package com.cottondb.books.facesimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testclass StripSettingTest {    private val k = StripKnobs    private val room = 340f to 451f    /** A plain measure: every character the same width at each size. */    private fun at(size: Float): (String) -> Float = { it.length * size * 0.5f }    private fun set(h: Highlight) = StripSetting.layout(        h, room.first, room.second, at(k.passageSize * room.first), at(k.titleSize * room.first), at(k.authorSize * room.first),        at(k.noteSize * room.first), k.passageSize * room.first * 0.8f, k.titleSize * room.first * 1.2f,    )    private val short = Highlight("Every lamp is a promise.", null, "The Harbour Lamp", "Asha Mercer", "yellow")    private val noted = short.copy(quote = "The tide does not argue; it arrives, and the harbour makes room for it whether or not the boats are ready.", note = "A beacon, not a wall.", ink = "red")    private val long = noted.copy(quote = (1..80).joinToString(" ") { "word$it" })    /** The passage is set inside curly quotes, once. */    @Test    fun the_passage_is_quoted_once() {        assertEquals("“A line.”", StripSetting.quoted("A line."))        assertEquals("“Said.”", StripSetting.quoted("“Said.”"))        assertEquals("\u201CEvery lamp is a promise.\u201D", set(short).passage.joinToString(" "))    }    /** The strip is as wide as the room, as tall as its words, in the upper middle; a longer passage makes a taller strip up to the limit, past which it is cut with an ellipsis. */    @Test    fun the_strip_is_as_tall_as_its_words() {        val a = set(short)        val b = set(noted)        val c = set(long)        assertEquals(room.first, a.size.width)        assertTrue(a.size.height < room.second * 0.5f)        assertTrue(b.size.height > a.size.height)        assertTrue(c.passage.size <= k.passageMost)        assertTrue(c.passage.last().endsWith("…"))        listOf(a, b, c).forEach { s ->            assertTrue(s.origin.y >= 0f)            assertTrue(s.origin.y + s.size.height <= room.second + 0.5f)            assertTrue(s.origin.y <= room.second - s.origin.y - s.size.height + 0.5f)        }    }    /** A note lays the curl over the lower right corner and no word lies under it; no note, no curl. */    @Test    fun a_note_curls_the_corner_and_no_word_lies_under_it() {        assertNull(set(short).curl)        for (h in listOf(noted, long)) {            val s = set(h)            val curl = s.curl!!            assertEquals(s.size.width, curl.right, 0.01f)            assertTrue(s.note.size in 1..k.noteLines)            val size = s.passageSize            s.passage.forEachIndexed { i, line ->                val bottom = s.passageTop + size * 0.8f + i * size * k.passageStep + size * (k.strokeDrop + k.strokeWidth)                val reach = StripSetting.curlLeft((bottom - curl.top) / (k.curlScale * s.scale))                if (reach != null) assertFalse(line, s.passageLeft + at(size)(line) > curl.left + reach * k.curlScale * s.scale)            }        }    }    /** The middle band repeats to the strip's height, its last repeat partial; the curl's edge is read down its rows. */    @Test    fun the_middle_repeats_and_the_curl_edge_reads() {        assertEquals(emptyList<Pair<Float, Float>>(), StripSetting.middles(500f))        assertEquals(listOf(250f to 400f, 650f to 100f), StripSetting.middles(1000f))        assertNull(StripSetting.curlLeft(10f))        assertEquals(709f, StripSetting.curlLeft(40f)!!, 0.01f)        assertEquals(693.5f, StripSetting.curlLeft(60f)!!, 0.01f)    }    /** The pencil is the highlight's colour by the stored value; the stroke is the same for the same line, runs a hair past it and is uneven. */    @Test    fun the_pencil_is_the_choice_and_the_stroke_is_seeded() {        assertEquals(BooksMaterial.pencilRed, pencil("red"))        assertNull(pencil("Red"))        val one = PencilStroke(200f, 30f, 22f, "0 A line kept.")        val again = PencilStroke(200f, 30f, 22f, "0 A line kept.")        assertEquals(one.upper, again.upper)        assertTrue(one.upper.first().x < 0f && one.upper.last().x > 200f)        val widths = one.upper.zip(one.lower).map { (u, l) -> l.y - u.y }        assertTrue(widths.max() - widths.min() > 22f * 0.03f)    }}