forked from ankidroid/Anki-Android
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardTest.kt
More file actions
235 lines (223 loc) · 8.59 KB
/
CardTest.kt
File metadata and controls
235 lines (223 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (c) 2020 Arthur Milchior <arthur@milchior.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.libanki
import android.annotation.SuppressLint
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.libanki.exception.ConfirmModSchemaException
import com.ichi2.anki.libanki.sched.Ease
import com.ichi2.anki.libanki.testutils.ext.addNote
import com.ichi2.testutils.JvmTest
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.hasItemInArray
import org.hamcrest.Matchers.not
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertNotNull
@RunWith(AndroidJUnit4::class)
class CardTest : JvmTest() {
@Test
fun test_toString() {
val output = addBasicNote().firstCard().toString()
assertThat(output, not(containsString("Companion")))
assertThat(output, not(containsString("SKIP_PRINT")))
assertThat(output, not(containsString("note")))
assertThat(output, not(containsString("renderOutput")))
assertThat(output, not(containsString("col")))
assertThat(output, not(containsString("timerStarted")))
}
/******************
** autogenerated from https://github.com/ankitects/anki/blob/2c73dcb2e547c44d9e02c20a00f3c52419dc277b/pylib/tests/test_cards.py*
******************/
@Test
fun test_delete() {
val note = col.newNote()
note.setItem("Front", "1")
note.setItem("Back", "2")
col.addNote(note)
val cid = note.cards()[0].id
col.sched.answerCard(col.sched.card!!, Ease.HARD)
col.removeCardsAndOrphanedNotes(listOf(cid))
assertEquals(0, col.cardCount())
assertEquals(0, col.noteCount())
assertEquals(0, col.db.queryScalar("select count() from notes"))
assertEquals(0, col.db.queryScalar("select count() from cards"))
assertEquals(2, col.db.queryScalar("select count() from graves"))
}
@Test
@SuppressLint("CheckResult") // col.models.current()!!.id
fun test_misc_cards() {
val note = col.newNote()
note.setItem("Front", "1")
note.setItem("Back", "2")
col.addNote(note)
val c = note.cards()[0]
col.notetypes.current().id
assertEquals(0, c.template().ord)
}
@Test
fun test_genrem() {
val note = col.newNote()
note.setItem("Front", "1")
note.setItem("Back", "")
col.addNote(note)
assertEquals(1, note.numberOfCards())
val noteType = col.notetypes.current()
// adding a new template should automatically create cards
var t =
Notetypes.newTemplate("rev").apply {
qfmt = "{{Front}}1"
afmt = ""
}
col.notetypes.addTemplateModChanged(noteType, t)
col.notetypes.save(noteType)
assertEquals(2, note.numberOfCards())
// if the template is changed to remove cards, they'll be removed
t =
noteType.templates[1].apply {
qfmt = "{{Back}}"
}
col.notetypes.save(noteType)
val rep = col.getEmptyCards().emptyCids()
col.removeCardsAndOrphanedNotes(rep)
assertEquals(1, note.numberOfCards())
// if we add to the note, a card should be automatically generated
note.load()
note.setItem("Back", "1")
note.flush()
assertEquals(2, note.numberOfCards())
}
@Test
fun test_gendeck() {
val cloze = col.notetypes.byName("Cloze")
col.notetypes.setCurrent(cloze!!)
val note = col.newNote()
note.setItem("Text", "{{c1::one}}")
col.addNote(note)
assertEquals(1, col.cardCount())
assertEquals(1, note.cards()[0].did)
// set the note type to a new default col
val newId = addDeck("new")
cloze.did = newId
col.notetypes.save(cloze)
// a newly generated card should share the first card's col
note.setItem("Text", "{{c2::two}}")
note.flush()
assertEquals(1, note.cards()[1].did)
// and same with multiple cards
note.setItem("Text", "{{c3::three}}")
note.flush()
assertEquals(1, note.cards()[2].did)
}
@Test
@Throws(ConfirmModSchemaException::class)
fun test_gen_or() {
val noteType = col.notetypes.byName("Basic")
assertNotNull(noteType)
col.notetypes.renameFieldLegacy(noteType, noteType.fields[0], "A")
col.notetypes.renameFieldLegacy(noteType, noteType.fields[1], "B")
val fld2 = col.notetypes.newField("C")
fld2.setOrd(null)
col.notetypes.addFieldLegacy(noteType, fld2)
noteType.templates[0].qfmt = "{{A}}{{B}}{{C}}"
// ensure first card is always generated,
// because at last one card is generated
val tmpl = Notetypes.newTemplate("AND_OR")
tmpl.qfmt = " {{A}} {{#B}} {{#C}} {{B}} {{/C}} {{/B}}"
col.notetypes.addTemplate(noteType, tmpl)
col.notetypes.save(noteType)
col.notetypes.setCurrent(noteType)
var note = col.newNote()
note.setItem("A", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0, 1))
note = col.newNote()
note.setItem("B", "foo")
note.setItem("C", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0, 1))
note = col.newNote()
note.setItem("B", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
note = col.newNote()
note.setItem("C", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
note = col.newNote()
note.setItem("A", "foo")
note.setItem("B", "foo")
note.setItem("C", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0, 1))
note = col.newNote()
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
// First card is generated if no other card
}
@Test
@Throws(ConfirmModSchemaException::class)
fun test_gen_not() {
val noteType = col.notetypes.byName("Basic")
assertNotNull(noteType)
col.notetypes.renameFieldLegacy(noteType, noteType.fields[0], "First")
col.notetypes.renameFieldLegacy(noteType, noteType.fields[1], "Front")
val fld2 = col.notetypes.newField("AddIfEmpty")
fld2.name = "AddIfEmpty"
col.notetypes.addFieldLegacy(noteType, fld2)
// ensure first card is always generated,
// because at last one card is generated
noteType.templates[0].qfmt = "{{AddIfEmpty}}{{Front}}{{First}}"
val tmpl = Notetypes.newTemplate("NOT")
tmpl.qfmt = " {{^AddIfEmpty}} {{Front}} {{/AddIfEmpty}} "
col.notetypes.addTemplate(noteType, tmpl)
col.notetypes.save(noteType)
col.notetypes.setCurrent(noteType)
var note = col.newNote()
note.setItem("First", "foo")
note.setItem("AddIfEmpty", "foo")
note.setItem("Front", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
note = col.newNote()
note.setItem("First", "foo")
note.setItem("AddIfEmpty", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
note = col.newNote()
note.setItem("First", "foo") // ensure first note generated
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0))
note = col.newNote()
note.setItem("First", "foo")
note.setItem("Front", "foo")
col.addNote(note)
assertNoteOrdinalAre(note, arrayOf(0, 1))
}
private fun assertNoteOrdinalAre(
note: Note,
ords: Array<Int>,
) {
val cards = note.cards()
assumeThat(cards.size, equalTo(ords.size))
for (card in cards) {
val ord = card.ord
assumeThat(ords, hasItemInArray(ord))
}
}
}