-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa913d3
commit b52dd2b
Showing
9 changed files
with
390 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
package deckmarkdown | ||
|
||
inline fun hashCodeOf(vararg values: Any?) = values | ||
.fold(0) { acc, v -> (acc + v.hashCode()) * 31 } | ||
.fold(0) { acc, v -> (acc + v.hashCode()) * 31 } | ||
|
||
fun String.recognizeKeyValueLines(): Map<String, String>? = this | ||
.split("\n") | ||
.fold(emptyList<Pair<String, String>>()) { acc, line -> | ||
val matches = KEY_REGEX.matchEntire(line) | ||
if (matches == null) { | ||
val (key, value) = acc.lastOrNull() ?: return@recognizeKeyValueLines null | ||
return@fold acc.dropLast(1) + (key to value + "\n" + line) | ||
} | ||
val (_, key, value) = matches.groupValues | ||
acc + (key to value) | ||
}.toMap() | ||
|
||
private val KEY_REGEX = Regex("^([\\w-]+): ([\\W\\w]*)$") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ankimarkdown | ||
|
||
import ankimarkdown.fakes.FakeAnkiApi | ||
import deckmarkdown.AnkiConnector | ||
import deckmarkdown.Note | ||
import deckmarkdown.api.ApiNote | ||
import deckmarkdown.note.BasicParser | ||
import deckmarkdown.note.DefaultParser | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
abstract class E2ETest { | ||
protected val fakeApi = FakeAnkiApi() | ||
protected val connector = AnkiConnector(api = fakeApi) | ||
protected val deckName = "SOME_DECK_NAME" | ||
|
||
protected fun basicApi( | ||
front: String, | ||
back: String, | ||
extra: String? = null, | ||
noteId: Long = 0 | ||
) = ApiNote( | ||
noteId, deckName, "Basic", mapOf( | ||
BasicParser.FRONT_FIELD to front, | ||
BasicParser.BACK_FIELD to back, | ||
BasicParser.EXTRA_FIELD to extra.orEmpty(), | ||
) | ||
) | ||
|
||
protected fun basicAndReversedApi( | ||
front: String, | ||
back: String, | ||
extra: String? = null, | ||
noteId: Long = 0 | ||
) = ApiNote( | ||
noteId, deckName, "Basic (and reversed card)", mapOf( | ||
BasicParser.FRONT_FIELD to front, | ||
BasicParser.BACK_FIELD to back, | ||
BasicParser.EXTRA_FIELD to extra.orEmpty(), | ||
) | ||
) | ||
|
||
protected fun listApi( | ||
title: String, | ||
items: Map<String, String>, | ||
generalComment: String = "", | ||
extra: String = "", | ||
noteId: Long = 0 | ||
) = ApiNote( | ||
noteId, deckName, "ListDeletion", mapOf( | ||
"Title" to title, | ||
"General Comment" to generalComment, | ||
"Extra" to extra, | ||
*items.toList().flatMapIndexed { index: Int, (text, comment) -> | ||
val num = index + 1 | ||
listOf( | ||
"$num" to text, | ||
"$num comment" to comment | ||
) | ||
}.toTypedArray() | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ankimarkdown | ||
|
||
import ankimarkdown.fakes.FakeAnkiApi | ||
import deckmarkdown.AnkiConnector | ||
import deckmarkdown.Note | ||
import deckmarkdown.api.ApiNote | ||
import deckmarkdown.note.BasicParser | ||
import deckmarkdown.note.DefaultParser | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class PullingTest: E2ETest() { | ||
|
||
@Test | ||
fun general() = testPull( | ||
startingMarkdown = """ | ||
Some text 1 | ||
Some text 2 | ||
""".trimIndent(), | ||
notesOnDeck = listOf( | ||
ApiNote( | ||
noteId = 123, | ||
deckName = deckName, | ||
modelName = "Special model name (for test)", | ||
fields = mapOf( | ||
"field_a" to "This is text a", | ||
"field_b" to "This is text b", | ||
) | ||
) | ||
), | ||
expectedMarkdown = """ | ||
Some text 1 | ||
Some text 2 | ||
@123 | ||
modelName: Special model name (for test) | ||
field_a: This is text a | ||
field_b: This is text b | ||
""".trimIndent(), | ||
) | ||
|
||
private fun testPull( | ||
startingMarkdown: String = "", | ||
notesOnDeck: List<ApiNote>, | ||
expectedMarkdown: String? = null, | ||
) = runTest { | ||
fakeApi.hasNotes(notesOnDeck) | ||
val res = connector.pullDeck(deckName, startingMarkdown) | ||
if (expectedMarkdown != null) { | ||
assertEquals(expectedMarkdown, res.markdown) | ||
} | ||
fakeApi.clean() | ||
} | ||
} |
Oops, something went wrong.