From f302cc56830392abe8808d17773ccc9561d209d4 Mon Sep 17 00:00:00 2001 From: Ayush-Patel-56 Date: Wed, 4 Mar 2026 12:34:04 +0530 Subject: [PATCH] test: add unit tests for CardUtils --- .../src/main/java/com/ichi2/anki/CardUtils.kt | 17 ----- .../test/java/com/ichi2/anki/CardUtilsTest.kt | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 AnkiDroid/src/test/java/com/ichi2/anki/CardUtilsTest.kt diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt index 5744c19cd7de..d0ddaa450cda 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.kt @@ -2,28 +2,11 @@ package com.ichi2.anki import com.ichi2.anki.libanki.Card -import com.ichi2.anki.libanki.Collection -import com.ichi2.anki.libanki.Note -import com.ichi2.utils.HashUtil.hashSetInit /** * Utilities for working on multiple cards */ object CardUtils { - /** - * @return List of corresponding notes without duplicates, even if the input list has multiple cards of the same note. - */ - fun getNotes( - col: Collection, - cards: kotlin.collections.Collection, - ): Set { - val notes: MutableSet = hashSetInit(cards.size) - for (card in cards) { - notes.add(card.note(col)) - } - return notes - } - /** * Returns the deck ID of the given [Card]. * diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/CardUtilsTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/CardUtilsTest.kt new file mode 100644 index 000000000000..82639328ba40 --- /dev/null +++ b/AnkiDroid/src/test/java/com/ichi2/anki/CardUtilsTest.kt @@ -0,0 +1,65 @@ +/* + Copyright (c) 2026 Ayush + + 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 com.ichi2.anki + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class CardUtilsTest : RobolectricTest() { + @Test + fun getDeckIdForCard_regularDeck_returnsDid() { + val note = addBasicNote("Front", "Back") + val card = note.firstCard() + + assertThat("Card should be in a regular deck", card.oDid, equalTo(0L)) + + val deckId = CardUtils.getDeckIdForCard(card) + + assertThat("Should return the card's did", deckId, equalTo(card.did)) + } + + @Test + fun getDeckIdForCard_cramDeck_returnsODid() { + val note = addBasicNote("Front", "Back") + val card = note.firstCard() + + val filteredDid = addDynamicDeck("Filtered") + col.sched.rebuildFilteredDeck(filteredDid) + card.load() + + assertThat("Card should have oDid set in filtered deck", card.oDid != 0L, equalTo(true)) + + val deckId = CardUtils.getDeckIdForCard(card) + + assertThat("Should return the original deck ID (oDid)", deckId, equalTo(card.oDid)) + } + + @Test + fun getDeckIdForCard_zeroODid_returnsDid() { + val note = addBasicNote("Front", "Back") + val card = note.firstCard() + + assertThat("Card should be in a regular deck", card.oDid, equalTo(0L)) + + val deckId = CardUtils.getDeckIdForCard(card) + + assertThat(deckId, equalTo(card.did)) + } +}