package com.cottondb.journal import com.cottondb.journal.faces.DiaryKnobs import com.cottondb.journal.faces.DiarySetting import com.cottondb.journal.faces.DiaryWeather import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test import java.io.File /** The diary's setting, as Loom's JournalDiaryTests pin it. */ class DiarySettingTest { private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } @Test fun each_mood_is_pressed_in_its_own_wax_and_none_is_the_bare_disc() { assertEquals("diary-seal-great", DiarySetting.seal("great")) assertEquals("diary-seal-awful", DiarySetting.seal(" Awful ")) assertEquals("diary-seal-bare", DiarySetting.seal(null)) assertEquals("diary-seal-bare", DiarySetting.seal("ecstatic")) } /** Every mood the kit's column offers has its wax, so no chosen mood draws bare. */ @Test fun every_mood_the_kit_offers_has_its_wax() { val table = JSONObject(File(repo, "crates/cotton-core/kits/personal/tables/journal_entry.json").readText()) val columns = table.getJSONArray("columns") val mood = (0 until columns.length()).map { columns.getJSONObject(it) }.first { it.getString("key") == "mood" } val choices = mood.getJSONArray("choices").let { a -> (0 until a.length()).map { a.getString(it) } } assertEquals(choices, DiarySetting.moods) val weather = (0 until columns.length()).map { columns.getJSONObject(it) }.first { it.getString("key") == "weather" } weather.getJSONArray("choices").let { a -> (0 until a.length()).forEach { assert(DiarySetting.weather(a.getString(it)) != null) { a.getString(it) } } } } @Test fun the_weather_is_its_glyph_and_none_is_none() { assertEquals(DiaryWeather.Sunny, DiarySetting.weather("sunny")) assertEquals(DiaryWeather.Storm, DiarySetting.weather("Storm")) assertNull(DiarySetting.weather(null)) assertNull(DiarySetting.weather("hail")) } @Test fun a_starred_page_pulls_its_ribbon_out() { assertEquals("diary-ribbon-out", DiarySetting.ribbon(true)) assertEquals("diary-ribbon-in", DiarySetting.ribbon(false)) } @Test fun a_page_with_no_title_writes_its_first_sentence_as_one() { val (title, rest) = DiarySetting.words(null, "The storm took the big branch off the neem in the night. It missed the car.") assertEquals("The storm took the big branch off the neem in the night", title) assertEquals("It missed the car.", rest) assertEquals("Tired" to "Slept badly.", DiarySetting.words("Tired", "Slept badly.")) assertEquals(null to "", DiarySetting.words(null, null)) } @Test fun the_words_take_the_rules_the_title_leaves() { assertEquals(0, DiarySetting.wordsRule(0)) assertEquals(2, DiarySetting.wordsRule(2)) assertEquals(DiaryKnobs.rules.size - 2, DiarySetting.wordsRoom(2)) assertEquals(16, DiaryKnobs.rules.size) } }