library: add tests for markdown, formatting, dates, nav stack and datastore
- Commit
722066242cb9a2dd945762b2082aeaad7311ad16- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 04:18 UTC (29 days ago)
- Parents
aa7e2bf
Tests for parseMarkdown (bold/italic/code/links/lists/headers/soft-wrap), number/speed formatting, localized month/weekday names and week starts, NavBackStack, DataStoreUtils typed accessors and string sets. Coverage (merged): 2.3% -> 22.7% instructions (377/16300 -> 3698/16300); suite 28 tests, 0 failures. Remaining: Navigation/compose chrome, BackupHelper/agents, GameHub reporters, OrientationManager (sensor/device-bound). (cherry picked from commit 3b276809e602a53dde8d3dc40a91a04ce63c96b9)
Diff
library/src/test/java/com/vayunmathur/library/util/LibraryPureTest.kt
diff --git a/library/src/test/java/com/vayunmathur/library/util/LibraryPureTest.kt b/library/src/test/java/com/vayunmathur/library/util/LibraryPureTest.ktnew file mode 100644index 0000000000..0ec73ef204--- /dev/null+++ b/library/src/test/java/com/vayunmathur/library/util/LibraryPureTest.kt@@ -0,0 +1,123 @@+package com.vayunmathur.library.util++import androidx.compose.ui.text.SpanStyle+import androidx.compose.ui.text.font.FontStyle+import androidx.compose.ui.text.font.FontWeight+import java.util.Locale+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/** Markdown conversion, number formatting, localized date names, misc utils. */[email protected](org.robolectric.RobolectricTestRunner::class)[email protected](sdk = [34])+class LibraryPureTest {++ @Test+ fun markdownPlainTextPassesThrough() {+ assertEquals("hello world", parseMarkdown("hello world").text)+ assertEquals("", parseMarkdown("").text)+ }++ @Test+ fun markdownBoldItalicCodeKeepText() {+ val bold = parseMarkdown("a **bold** word")+ assertEquals("a **bold** word", bold.text)+ assertTrue(bold.spanStyles.any { it.item.fontWeight == FontWeight.Bold })+ val italic = parseMarkdown("an *italic* word")+ assertTrue(italic.spanStyles.any { it.item.fontStyle == FontStyle.Italic })+ val code = parseMarkdown("run `code` now")+ assertEquals("run `code` now", code.text)+ assertTrue(code.spanStyles.isNotEmpty())+ }++ @Test+ fun markdownHiddenMarkersStillContainText() {+ val hidden = parseMarkdown("# Title\n\n**bold**", showMarkers = false)+ assertTrue(hidden.text.contains("Title"))+ assertTrue(hidden.text.contains("bold"))+ assertTrue(hidden.spanStyles.isNotEmpty())+ }++ @Test+ fun markdownListsNormalizeMarkers() {+ val list = parseMarkdown("- one\n- two", showMarkers = true)+ assertTrue(list.text.contains("one"))+ assertTrue(list.text.contains("two"))+ val numbered = parseMarkdown("1. first\n2. second")+ assertTrue(numbered.text.contains("first"))+ }++ @Test+ fun markdownLinksAndStrikethrough() {+ val link = parseMarkdown("see [docs](https://example.com)")+ assertEquals("see [docs](https://example.com)", link.text)+ assertTrue(+ link.getLinkAnnotations(0, link.text.length).isNotEmpty() ||+ link.spanStyles.isNotEmpty(),+ )+ val strike = parseMarkdown("~~old~~ new")+ assertEquals("~~old~~ new", strike.text)+ }++ @Test+ fun markdownSoftWrapMergesLines() {+ val wrapped = parseMarkdown("line one\nline two")+ assertEquals("line one line two", wrapped.text)+ val unwrapped = parseMarkdown("line one\nline two", softWrap = false, process = false)+ assertEquals("line one\nline two", unwrapped.text)+ }++ @Test+ fun markdownBlockquoteAndHeader() {+ val quote = parseMarkdown("> quoted")+ assertTrue(quote.text.contains("quoted"))+ val header = parseMarkdown("## Section")+ assertTrue(header.text.contains("Section"))+ assertTrue(header.paragraphStyles.isNotEmpty() || header.spanStyles.isNotEmpty())+ }++ @Test+ fun numberFormatting() {+ assertEquals("3.14", 3.14159.toStringDigits(2))+ assertEquals("3.1", 3.14159.toStringDigits(1))+ assertEquals("1,234,567", 1234567L.toStringCommas())+ assertEquals("0", 0L.toStringCommas())+ assertTrue(10f.formatSpeed(Locale.US).isNotBlank())+ assertTrue(10f.formatSpeed(Locale.GERMANY).isNotBlank())+ }++ @Test+ fun localizedNamesEnglish() {+ val months = localizedMonthNames(DateNameStyle.FULL, Locale.US)+ assertEquals(12, months.size)+ assertEquals("January", months[0])+ assertEquals("December", months[11])+ assertEquals("AM" to "PM", localizedAmPmMarkers(Locale.US))+ val days = localizedDayOfWeekNames(DateNameStyle.FULL, Locale.US)+ assertEquals(7, days.size)+ assertEquals("Monday", days[0])+ assertEquals("Sunday", days[6])+ // en-US weeks start Sunday (ISO 7); en-GB starts Monday (ISO 1).+ assertEquals(7, localeFirstDayOfWeek(Locale.US))+ assertEquals(1, localeFirstDayOfWeek(Locale.UK))+ assertEquals(listOf(7, 1, 2, 3, 4, 5, 6), localeWeekDayNumbers(Locale.US))+ assertEquals(listOf(1, 2, 3, 4, 5, 6, 7), localeWeekDayNumbers(Locale.UK))+ }++ @Test+ fun miscUtils() {+ assertEquals(42, tryOrDefault(0) { 42 })+ assertEquals(0, tryOrDefault(0) { throw RuntimeException("x") })+ assertEquals(3.14, 3.14159.round(2))+ assertEquals(3.0, 3.14159.round(0))+ assertEquals(listOf("a", "b"), "a\nb".byteInputStream().readLines())+ assertEquals("Hello", "hello".firstLetterUppercase())+ assertEquals("Hello", "Hello".firstLetterUppercase())+ assertEquals("", "".firstLetterUppercase())+ val tuple = Tuple4(1, "two", 3.0, true)+ assertEquals(1, tuple.first)+ assertEquals("two", tuple.second)+ assertEquals(true, tuple.fourth)+ }+}
library/src/test/java/com/vayunmathur/library/util/LibraryStateTest.kt
diff --git a/library/src/test/java/com/vayunmathur/library/util/LibraryStateTest.kt b/library/src/test/java/com/vayunmathur/library/util/LibraryStateTest.ktnew file mode 100644index 0000000000..3fb4ebb0fa--- /dev/null+++ b/library/src/test/java/com/vayunmathur/library/util/LibraryStateTest.kt@@ -0,0 +1,89 @@+package com.vayunmathur.library.util++import androidx.datastore.preferences.core.PreferenceDataStoreFactory+import java.io.File+import java.nio.file.Files+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.runBlocking+import kotlinx.serialization.Serializable+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNull+import kotlin.test.assertTrue++/** NavBackStack behaviour and DataStoreUtils typed accessors. */+class LibraryStateTest {++ @Test+ fun navBackStackAddPopSetReset() {+ val stack = NavBackStack(arrayOf(TestKey("home")))+ assertEquals("home", stack.last().route)+ stack.add(TestKey("detail"))+ assertEquals(2, stack.backStack.size)+ assertEquals("detail", stack.last().route)+ stack.setLast(TestKey("detail2"))+ assertEquals("detail2", stack.last().route)+ stack.set(0, TestKey("root"))+ assertEquals("root", stack.backStack[0].route)+ stack.pop()+ assertEquals("root", stack.last().route)+ stack.pop()+ // Popping an empty stack is a no-op, not a crash.+ stack.pop()+ assertTrue(stack.backStack.isEmpty())+ stack.reset(TestKey("a"), TestKey("b"))+ assertEquals(listOf("a", "b"), stack.backStack.map { it.route })+ stack.clear()+ assertTrue(stack.backStack.isEmpty())+ }++ private fun newStore(): DataStoreUtils {+ val dir = Files.createTempDirectory("ds-test").toFile()+ val file = File(dir, "test.preferences_pb")+ return DataStoreUtils(PreferenceDataStoreFactory.create { file })+ }++ @Test+ fun dataStoreTypedRoundTrips() = runBlocking {+ val store = newStore()+ assertNull(store.getString("missing"))+ assertNull(store.getLong("missing"))+ assertFalse(store.getBoolean("missing", false))+ store.setString("name", "ada")+ assertEquals("ada", store.getStringAwait("name"))+ store.setString("name", "grace", onlyIfAbsent = true)+ assertEquals("ada", store.getStringAwait("name"))+ store.updateString("name") { (it ?: "") + "!" }+ assertEquals("ada!", store.getStringAwait("name"))+ store.setLong("count", 41)+ assertEquals(41L, store.getLong("count"))+ assertTrue(store.setLongIfGreater("count", 42))+ assertFalse(store.setLongIfGreater("count", 40))+ assertEquals(42L, store.getLong("count"))+ store.setDouble("ratio", 2.5)+ assertEquals(2.5, store.doubleFlow("ratio").first())+ store.setBoolean("flag", true)+ assertTrue(store.getBooleanAwait("flag"))+ assertTrue(store.booleanFlow("flag").first())+ assertEquals("ada!", store.stringFlow("name").first())+ store.setByteArray("blob", byteArrayOf(1, 2, 3))+ assertTrue(byteArrayOf(1, 2, 3).contentEquals(store.getByteArrayAwait("blob")!!))+ }++ @Test+ fun dataStoreStringSetsAndRemove() = runBlocking {+ val store = newStore()+ assertEquals(emptySet(), store.getStringSetAwait("tags"))+ assertTrue(store.addStringToSetIfAbsent("tags", "a"))+ assertTrue(store.addStringToSetIfAbsent("tags", "b"))+ assertFalse(store.addStringToSetIfAbsent("tags", "a"))+ assertEquals(setOf("a", "b"), store.getStringSetAwait("tags"))+ assertEquals(setOf("a", "b"), store.stringSetFlow("tags").first())+ store.removeKeys(listOf("tags"))+ assertEquals(emptySet(), store.getStringSetAwait("tags"))+ }+}++@Serializable+private data class TestKey(val route: String) : NavKey