From d228793e9b36bfd7007ef93a9945b311c51e34f5 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Wed, 22 Jan 2025 12:41:19 -0700 Subject: [PATCH] musikr: fix tests Had to remove some, but they were outdated anyway. --- .../auxio/music/shim/MusikrShimModule.kt | 5 +- .../main/java/org/oxycblt/musikr/Musikr.kt | 4 + .../org/oxycblt/musikr/tag/parse/TagFields.kt | 22 --- .../musikr/tag/interpret/ID3GenreTest.kt | 55 +++++++ .../oxycblt/musikr/tag/parse/TagUtilTest.kt | 146 ------------------ .../org/oxycblt/musikr/util/TagFieldTest.kt | 73 +++++++++ 6 files changed, 135 insertions(+), 170 deletions(-) create mode 100644 musikr/src/test/java/org/oxycblt/musikr/tag/interpret/ID3GenreTest.kt delete mode 100644 musikr/src/test/java/org/oxycblt/musikr/tag/parse/TagUtilTest.kt create mode 100644 musikr/src/test/java/org/oxycblt/musikr/util/TagFieldTest.kt diff --git a/app/src/main/java/org/oxycblt/auxio/music/shim/MusikrShimModule.kt b/app/src/main/java/org/oxycblt/auxio/music/shim/MusikrShimModule.kt index f9bf6d4b5..abeba8e62 100644 --- a/app/src/main/java/org/oxycblt/auxio/music/shim/MusikrShimModule.kt +++ b/app/src/main/java/org/oxycblt/auxio/music/shim/MusikrShimModule.kt @@ -25,7 +25,7 @@ import dagger.hilt.InstallIn import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.components.SingletonComponent import javax.inject.Singleton -import org.oxycblt.musikr.cache.SongCache +import org.oxycblt.musikr.cache.MutableSongCache import org.oxycblt.musikr.cache.db.DBSongCache import org.oxycblt.musikr.playlist.db.StoredPlaylists @@ -34,7 +34,8 @@ import org.oxycblt.musikr.playlist.db.StoredPlaylists class MusikrShimModule { @Singleton @Provides - fun songCache(@ApplicationContext context: Context): SongCache = DBSongCache.from(context) + fun songCache(@ApplicationContext context: Context): MutableSongCache = + DBSongCache.from(context) @Singleton @Provides diff --git a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt index bc1f243b3..2d905f222 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/Musikr.kt @@ -153,6 +153,7 @@ private class MusikrImpl( .buffer(Channel.UNLIMITED) .onStart { onProgress(IndexingProgress.Songs(0, 0)) } .onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) } + val typeDiversion = explored.divert { when (it) { @@ -162,15 +163,18 @@ private class MusikrImpl( } val known = typeDiversion.right val new = typeDiversion.left + val extracted = extractStep .extract(new) .buffer(Channel.UNLIMITED) .onEach { onProgress(IndexingProgress.Songs(++extractedCount, exploredCount)) } .onCompletion { onProgress(IndexingProgress.Indeterminate) } + val complete = merge(typeDiversion.manager, known, extracted.filterIsInstance()) val library = evaluateStep.evaluate(complete) + LibraryResultImpl(storage, library) } } diff --git a/musikr/src/main/java/org/oxycblt/musikr/tag/parse/TagFields.kt b/musikr/src/main/java/org/oxycblt/musikr/tag/parse/TagFields.kt index 778de5380..2030d01df 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/tag/parse/TagFields.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/tag/parse/TagFields.kt @@ -57,33 +57,11 @@ internal fun Metadata.disc() = (xiph["TOTALDISCS"] ?: xiph["DISCTOTAL"] ?: xiph["DISCC"])?.run { first() }) ?: (mp4["disk"] ?: id3v2["TPOS"])?.run { first().parseJoinedPosition() }) -/** - * Parse an ID3v2-style position + total [String] field. These fields consist of a number and an - * (optional) total value delimited by a /. - * - * @return The position value extracted from the string field, or null if: - * - The position could not be parsed - * - The position was zeroed AND the total value was not present/zeroed - * - * @see transformPosition - */ private fun String.parseJoinedPosition() = split('/', limit = 2).let { transformPosition(it[0].toIntOrNull(), it.getOrNull(1)?.toIntOrNull()) } -/** - * Parse a vorbis-style position + total field. These fields consist of two fields for the position - * and total numbers. - * - * @param pos The position value, or null if not present. - * @param total The total value, if not present. - * @return The position value extracted from the field, or null if: - * - The position could not be parsed - * - The position was zeroed AND the total value was not present/zeroed - * - * @see transformPosition - */ private fun parseSeparatedPosition(pos: String?, total: String?) = pos?.let { posStr -> posStr.toIntOrNull()?.let { transformPosition(it, total?.toIntOrNull()) } diff --git a/musikr/src/test/java/org/oxycblt/musikr/tag/interpret/ID3GenreTest.kt b/musikr/src/test/java/org/oxycblt/musikr/tag/interpret/ID3GenreTest.kt new file mode 100644 index 000000000..11cde61bd --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/tag/interpret/ID3GenreTest.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Auxio Project + * ID3GenreTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.tag.interpret + +import org.junit.Assert.assertEquals +import org.junit.Test + +class ID3GenreTest { + @Test + fun parseId3v2Genre_multi() { + assertEquals( + listOf("Post-Rock", "Shoegaze", "Glitch"), + listOf("Post-Rock", "Shoegaze", "Glitch").parseId3GenreNames()) + } + + @Test + fun parseId3v2Genre_multiId3v1() { + assertEquals( + listOf("Post-Rock", "Shoegaze", "Glitch"), + listOf("176", "178", "Glitch").parseId3GenreNames()) + } + + @Test + fun parseId3v2Genre_wackId3() { + assertEquals(null, listOf("2941").parseId3GenreNames()) + } + + @Test + fun parseId3v2Genre_singleId3v23() { + assertEquals( + listOf("Post-Rock", "Shoegaze", "Remix", "Cover", "Glitch"), + listOf("(176)(178)(RX)(CR)Glitch").parseId3GenreNames()) + } + + @Test + fun parseId3v2Genre_singleId3v1() { + assertEquals(listOf("Post-Rock"), listOf("176").parseId3GenreNames()) + } +} diff --git a/musikr/src/test/java/org/oxycblt/musikr/tag/parse/TagUtilTest.kt b/musikr/src/test/java/org/oxycblt/musikr/tag/parse/TagUtilTest.kt deleted file mode 100644 index bb6f1be08..000000000 --- a/musikr/src/test/java/org/oxycblt/musikr/tag/parse/TagUtilTest.kt +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (c) 2023 Auxio Project - * TagUtilTest.kt is part of Auxio. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.oxycblt.musikr.tag.parse - -import org.junit.Assert.assertEquals -import org.junit.Test -import org.oxycblt.musikr.tag.format.parseId3GenreNames -import org.oxycblt.musikr.tag.format.parseSlashPositionField -import org.oxycblt.musikr.tag.format.parseXiphPositionField -import org.oxycblt.musikr.util.correctWhitespace -import org.oxycblt.musikr.util.splitEscaped - -class TagUtilTest { - @Test - fun splitEscaped_correct() { - assertEquals(listOf("a", "b", "c"), "a,b,c".splitEscaped { it == ',' }) - } - - @Test - fun splitEscaped_escaped() { - assertEquals(listOf("a,b", "c"), "a\\,b,c".splitEscaped { it == ',' }) - } - - @Test - fun splitEscaped_whitespace() { - assertEquals(listOf("a ", " b", " c ", " "), "a , b, c , ".splitEscaped { it == ',' }) - } - - @Test - fun splitEscaped_escapedWhitespace() { - assertEquals(listOf("a , b", " c ", " "), ("a \\, b, c , ".splitEscaped { it == ',' })) - } - - @Test - fun correctWhitespace_stringCorrect() { - assertEquals( - "asymptotic self-improvement", - " asymptotic self-improvement ".correctWhitespace(), - ) - } - - @Test - fun correctWhitespace_stringOopsAllWhitespace() { - assertEquals(null, "".correctWhitespace()) - assertEquals(null, " ".correctWhitespace()) - } - - @Test - fun correctWhitespace_listCorrect() { - assertEquals( - listOf("asymptotic self-improvement", "daemons never stop", "tcp phagocyte"), - listOf(" asymptotic self-improvement ", " daemons never stop", "tcp phagocyte") - .correctWhitespace(), - ) - } - - @Test - fun correctWhitespace_listOopsAllWhitespace() { - assertEquals( - listOf("tcp phagocyte"), listOf(" ", "", " tcp phagocyte").correctWhitespace()) - } - - @Test - fun parseId3v2PositionField_correct() { - assertEquals(16, "16/32".parseSlashPositionField()) - assertEquals(16, "16".parseSlashPositionField()) - } - - @Test - fun parseId3v2PositionField_zeroed() { - assertEquals(null, "0".parseSlashPositionField()) - assertEquals(0, "0/32".parseSlashPositionField()) - } - - @Test - fun parseId3v2PositionField_wack() { - assertEquals(16, "16/".parseSlashPositionField()) - assertEquals(null, "a".parseSlashPositionField()) - assertEquals(null, "a/b".parseSlashPositionField()) - } - - @Test - fun parseVorbisPositionField_correct() { - assertEquals(16, parseXiphPositionField("16", "32")) - assertEquals(16, parseXiphPositionField("16", null)) - } - - @Test - fun parseVorbisPositionField_zeroed() { - assertEquals(null, parseXiphPositionField("0", null)) - assertEquals(0, parseXiphPositionField("0", "32")) - } - - @Test - fun parseVorbisPositionField_wack() { - assertEquals(null, parseXiphPositionField("a", null)) - assertEquals(null, parseXiphPositionField("a", "b")) - } - - @Test - fun parseId3v2Genre_multi() { - assertEquals( - listOf("Post-Rock", "Shoegaze", "Glitch"), - listOf("Post-Rock", "Shoegaze", "Glitch").parseId3GenreNames()) - } - - @Test - fun parseId3v2Genre_multiId3v1() { - assertEquals( - listOf("Post-Rock", "Shoegaze", "Glitch"), - listOf("176", "178", "Glitch").parseId3GenreNames()) - } - - @Test - fun parseId3v2Genre_wackId3() { - assertEquals(null, listOf("2941").parseId3GenreNames()) - } - - @Test - fun parseId3v2Genre_singleId3v23() { - assertEquals( - listOf("Post-Rock", "Shoegaze", "Remix", "Cover", "Glitch"), - listOf("(176)(178)(RX)(CR)Glitch").parseId3GenreNames()) - } - - @Test - fun parsId3v2Genre_singleId3v1() { - assertEquals(listOf("Post-Rock"), listOf("176").parseId3GenreNames()) - } -} diff --git a/musikr/src/test/java/org/oxycblt/musikr/util/TagFieldTest.kt b/musikr/src/test/java/org/oxycblt/musikr/util/TagFieldTest.kt new file mode 100644 index 000000000..3cf373344 --- /dev/null +++ b/musikr/src/test/java/org/oxycblt/musikr/util/TagFieldTest.kt @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Auxio Project + * TagFieldTest.kt is part of Auxio. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.oxycblt.musikr.util + +import org.junit.Assert.assertEquals +import org.junit.Test + +class TagFieldTest { + @Test + fun splitEscaped_correct() { + assertEquals(listOf("a", "b", "c"), "a,b,c".splitEscaped { it == ',' }) + } + + @Test + fun splitEscaped_escaped() { + assertEquals(listOf("a,b", "c"), "a\\,b,c".splitEscaped { it == ',' }) + } + + @Test + fun splitEscaped_whitespace() { + assertEquals(listOf("a ", " b", " c ", " "), "a , b, c , ".splitEscaped { it == ',' }) + } + + @Test + fun splitEscaped_escapedWhitespace() { + assertEquals(listOf("a , b", " c ", " "), ("a \\, b, c , ".splitEscaped { it == ',' })) + } + + @Test + fun correctWhitespace_stringCorrect() { + assertEquals( + "asymptotic self-improvement", + " asymptotic self-improvement ".correctWhitespace(), + ) + } + + @Test + fun correctWhitespace_stringOopsAllWhitespace() { + assertEquals(null, "".correctWhitespace()) + assertEquals(null, " ".correctWhitespace()) + } + + @Test + fun correctWhitespace_listCorrect() { + assertEquals( + listOf("asymptotic self-improvement", "daemons never stop", "tcp phagocyte"), + listOf(" asymptotic self-improvement ", " daemons never stop", "tcp phagocyte") + .correctWhitespace(), + ) + } + + @Test + fun correctWhitespace_listOopsAllWhitespace() { + assertEquals( + listOf("tcp phagocyte"), listOf(" ", "", " tcp phagocyte").correctWhitespace()) + } +}