package net.sergeych.toread.text

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue

class HyphenationTest {
    private val hyphenation = HyphenationRegistry()

    @Test
    fun selectsEnglishPluginByLanguage() {
        val hyphenated = hyphenation.hyphenate("composition", "en")

        assertNotEquals("composition", hyphenated)
        assertTrue(SoftHyphen in hyphenated)
    }

    @Test
    fun selectsRussianPluginByLanguage() {
        val hyphenated = hyphenation.hyphenate("повествование", "ru")

        assertNotEquals("повествование", hyphenated)
        assertTrue(SoftHyphen in hyphenated)
    }

    @Test
    fun suggestsRussianHyphensAtReadableBreaks() {
        mapOf(
            "молоко" to "мо-ло-ко",
            "корова" to "ко-ро-ва",
            "яблоко" to "яб-ло-ко",
            "повествование" to "по-вест-во-ва-ние",
            "предложение" to "пред-ло-же-ние",
            "компьютер" to "компью-тер",
            "подъезд" to "подъ-езд",
            "большой" to "боль-шой",
            "майор" to "май-ор",
            "масса" to "мас-са",
            "длинный" to "длин-ный",
            "разыскать" to "ра-зыс-кать",
            "розыгрыш" to "ро-зыг-рыш",
        ).forEach { (word, expected) ->
            assertEquals(expected, word.hyphenatedRu())
        }
    }

    @Test
    fun keepsRussianWordsWithoutLegalReadableBreaksUnchanged() {
        listOf("дом", "мир", "стол", "семья", "СССР", "testовый", "книга123").forEach { word ->
            assertEquals(word, word.hyphenatedRu())
        }
        assertEquals("ко-рова", "ко${SoftHyphen}рова".hyphenatedRu())
    }

    private fun String.hyphenatedRu(): String =
        hyphenation.hyphenate(this, "ru").replace(SoftHyphen, '-')
}
