|
| 1 | +/**************************************************************************************** |
| 2 | + * * |
| 3 | + * Copyright (c) 2025 Shridhar Goel <shridhar.goel@gmail.com> * |
| 4 | + * * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under * |
| 6 | + * the terms of the GNU General Public License as published by the Free Software * |
| 7 | + * Foundation; either version 3 of the License, or (at your option) any later * |
| 8 | + * version. * |
| 9 | + * * |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 11 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 12 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU General Public License along with * |
| 15 | + * this program. If not, see <http://www.gnu.org/licenses/>. * |
| 16 | + ****************************************************************************************/ |
| 17 | + |
| 18 | +package com.ichi2.libanki |
| 19 | + |
| 20 | +import anki.decks.deckTreeNode |
| 21 | +import com.ichi2.libanki.sched.DeckNode |
| 22 | +import org.junit.Assert.assertEquals |
| 23 | +import org.junit.Test |
| 24 | + |
| 25 | +class DeckNodeTest { |
| 26 | + private fun makeNode( |
| 27 | + name: String, |
| 28 | + deckId: Long, |
| 29 | + level: Int, |
| 30 | + collapsed: Boolean = false, |
| 31 | + children: List<DeckNode> = emptyList(), |
| 32 | + ): DeckNode { |
| 33 | + val treeNode = |
| 34 | + deckTreeNode { |
| 35 | + this.name = name |
| 36 | + this.deckId = deckId |
| 37 | + this.level = level |
| 38 | + this.collapsed = collapsed |
| 39 | + children.forEach { this.children.add(it.node) } |
| 40 | + this.reviewCount = 0 |
| 41 | + this.newCount = 0 |
| 42 | + this.learnCount = 0 |
| 43 | + this.filtered = false |
| 44 | + } |
| 45 | + return DeckNode(treeNode, name) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `search finds subdeck even if parent collapsed`() { |
| 50 | + // Science::Math::Algebra::Group |
| 51 | + val group = makeNode("Group", 4, 4) |
| 52 | + val algebra = makeNode("Algebra", 3, 3, children = listOf(group)) |
| 53 | + val math = makeNode("Math", 2, 2, collapsed = true, children = listOf(algebra)) |
| 54 | + val science = makeNode("Science", 1, 1, children = listOf(math)) |
| 55 | + |
| 56 | + val results1 = science.filterAndFlatten("group") |
| 57 | + assertEquals(listOf("Science", "Math", "Algebra", "Group"), results1.map { it.lastDeckNameComponent }) |
| 58 | + |
| 59 | + val results2 = science.filterAndFlatten("math") |
| 60 | + assertEquals(listOf("Science", "Math"), results2.map { it.lastDeckNameComponent }) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `collapsed parent hides children when not searching`() { |
| 65 | + val group = makeNode("Group", 4, 4) |
| 66 | + val algebra = makeNode("Algebra", 3, 3, children = listOf(group)) |
| 67 | + val math = makeNode("Math", 2, 2, collapsed = true, children = listOf(algebra)) |
| 68 | + val science = makeNode("Science", 1, 1, children = listOf(math)) |
| 69 | + |
| 70 | + val results = math.filterAndFlatten(null) |
| 71 | + assertEquals(1, results.size) |
| 72 | + assertEquals("Math", results[0].lastDeckNameComponent) |
| 73 | + } |
| 74 | +} |
0 commit comments