1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 13:57:10 +09:00

Added tests.

This commit is contained in:
Ubu 2019-03-31 13:52:39 +03:00
parent 99c928cc8b
commit 90e00562d2
9 changed files with 275 additions and 12 deletions

View file

@ -24,7 +24,7 @@ sealed class ContentType {
object H1 : ContentType()
object H2 : ContentType()
object H3 : ContentType()
object OL : ContentType()
object NumberedList : ContentType()
object UL : ContentType()
object Quote : ContentType()
object Toggle : ContentType()
@ -48,7 +48,7 @@ fun Int.toContentType(): ContentType =
3 -> ContentType.H1
4 -> ContentType.H2
5 -> ContentType.H3
6 -> ContentType.OL
6 -> ContentType.NumberedList
7 -> ContentType.UL
8 -> ContentType.Quote
9 -> ContentType.Toggle
@ -68,7 +68,7 @@ fun ContentType.toNumericalCode(): Int {
ContentType.H3 -> 5
ContentType.H4 -> 11
ContentType.UL -> 7
ContentType.OL -> 6
ContentType.NumberedList -> 6
else -> TODO()
}
}

View file

@ -24,10 +24,20 @@ data class ContentParam(val map : Map<String, Any?>) {
companion object {
fun numberedListDefaultParam(): ContentParam {
fun empty() : ContentParam {
return ContentParam(
mapOf(
"number" to 1,
"number" to null,
"checked" to null
)
)
}
fun numberedList(number : Int = 1): ContentParam {
return ContentParam(
mapOf(
"number" to number,
"checked" to null
)
)

View file

@ -12,6 +12,13 @@ interface BlockContentTypeConverter {
*/
fun convert(block : Block, type : ContentType) : Block
/**
* @param blocks list of blocks
* @param target block that we need to convert
* @param targetType type for a new block
*/
fun convert(blocks : List<Block>, target : Block, targetType : ContentType) : List<Block>
fun getPermittedTypes(typeInitial: ContentType): Set<ContentType>
fun getForbiddenTypes(typeInitial: ContentType): Set<ContentType>
}
@ -23,7 +30,7 @@ class BlockContentTypeConverterImpl :
fun getPermittedTypes(typeInitial: ContentType): Set<ContentType> =
setOf(
ContentType.P, ContentType.Code, ContentType.H1, ContentType.H2,
ContentType.H3, ContentType.OL, ContentType.UL, ContentType.Quote,
ContentType.H3, ContentType.NumberedList, ContentType.UL, ContentType.Quote,
ContentType.Toggle, ContentType.Check, ContentType.H4
)
@ -37,11 +44,11 @@ class BlockContentTypeConverterImpl :
override fun convert(block: Block, type: ContentType): Block {
return when(type) {
ContentType.OL -> {
ContentType.NumberedList -> {
block.copy(
contentType = type,
content = block.content.copy(
param = ContentParam.numberedListDefaultParam()
param = ContentParam.numberedList()
)
)
}
@ -51,6 +58,13 @@ class BlockContentTypeConverterImpl :
)
}
}
}
override fun convert(blocks: List<Block>, target: Block, targetType: ContentType): List<Block> {
if (target.contentType == targetType)
return blocks
else
TODO()
}
}

View file

@ -38,7 +38,7 @@ class EditorViewModel(
is EditBlockAction.Header4Click -> convertBlock(block = action.block, contentType = ContentType.H4)
is EditBlockAction.HighLightClick -> convertBlock(block = action.block, contentType = ContentType.Quote)
is EditBlockAction.BulletClick -> convertBlock(block = action.block, contentType = ContentType.UL)
is EditBlockAction.NumberedClick -> convertBlock(block = action.block, contentType = ContentType.OL)
is EditBlockAction.NumberedClick -> convertBlock(block = action.block, contentType = ContentType.NumberedList)
is EditBlockAction.CheckBoxClick -> convertBlock(block = action.block, contentType = ContentType.Check)
is EditBlockAction.CodeClick -> convertBlock(block = action.block, contentType = ContentType.Code)
}.also { progress.accept(EditorState.HideToolbar) }

View file

@ -88,7 +88,7 @@ class EditBlockToolbar : ConstraintLayout {
ContentType.H4 -> btnHeader4
ContentType.UL -> btnBulleted
ContentType.Quote -> btnHighlighted
ContentType.OL -> btnNumberedList
ContentType.NumberedList -> btnNumberedList
ContentType.Check -> btnCheckbox
ContentType.Code -> btnCode
else -> btnText

View file

@ -10,7 +10,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.agileburo.anytype.core_utils.swap
import com.agileburo.anytype.feature_editor.R
import com.agileburo.anytype.feature_editor.domain.Block
import com.agileburo.anytype.feature_editor.domain.ContentType
import com.agileburo.anytype.feature_editor.presentation.model.BlockView
import com.agileburo.anytype.feature_editor.presentation.util.BlockViewDiffUtil
@ -114,7 +113,7 @@ class EditorAdapter(
is ContentType.Quote -> HOLDER_QUOTE
is ContentType.Check -> HOLDER_CHECKBOX
is ContentType.Code -> HOLDER_CODE_SNIPPET
is ContentType.OL -> HOLDER_NUMBERED
is ContentType.NumberedList -> HOLDER_NUMBERED
is ContentType.UL -> HOLDER_BULLET
else -> throw IllegalStateException("Implement Toggle!!!")
}

View file

@ -0,0 +1,168 @@
package com.agileburo.anytype.feature_editor
import com.agileburo.anytype.feature_editor.domain.ContentParam
import com.agileburo.anytype.feature_editor.domain.ContentType
import com.agileburo.anytype.feature_editor.factory.BlockFactory
import com.agileburo.anytype.feature_editor.presentation.converter.BlockContentTypeConverterImpl
import org.junit.Test
class NumberedListMechanicsTest {
private val converter by lazy {
BlockContentTypeConverterImpl()
}
@Test
fun `when content type is the same, converter should return the same list`() {
val blocks = listOf(
BlockFactory.makeBlock(contentType = ContentType.P),
BlockFactory.makeBlock(contentType = ContentType.P)
)
val targetType = ContentType.P
val target = blocks.first()
val result = converter.convert(
blocks = blocks,
target = target,
targetType = targetType
)
assert(result == blocks)
}
@Test
fun `if previous block is not an item of numbered list and we create numbered list item, then its number param = 1`() {
val blocks = listOf(
BlockFactory.makeBlock(contentType = ContentType.P),
BlockFactory.makeBlock(contentType = ContentType.P)
)
val targetType = ContentType.NumberedList
val target = blocks.last()
val result = converter.convert(
blocks = blocks,
target = target,
targetType = targetType
)
result.apply {
assert(size == 2)
assert(first() == blocks.first())
assert(last().contentType == ContentType.NumberedList)
assert(last().content.param.number == 1)
}
}
@Test
fun `if previous block is an item of numbered list, then the next numbered list item should have number incremented`() {
val blocks = listOf(
BlockFactory.makeBlock(
contentType = ContentType.NumberedList,
contentParam = ContentParam.numberedList(number = 1)
),
BlockFactory.makeBlock(contentType = ContentType.P)
)
val targetType = ContentType.NumberedList
val target = blocks.last()
val result = converter.convert(
blocks = blocks,
target = target,
targetType = targetType
)
result.apply {
assert(size == 2)
assert(first() == blocks.first())
assert(last().contentType == ContentType.NumberedList)
assert(last().content.param.number == 2)
}
}
@Test
fun `the first block and the third one are items of numbered list, if we convert second block to numbered list, we should have a correct number sequence`() {
val blocks = listOf(
BlockFactory.makeBlock(
contentType = ContentType.NumberedList,
contentParam = ContentParam.numberedList(number = 1)
),
BlockFactory.makeBlock(contentType = ContentType.P),
BlockFactory.makeBlock(
contentType = ContentType.NumberedList,
contentParam = ContentParam.numberedList(number = 1)
)
)
val targetType = ContentType.NumberedList
val target = blocks[1]
val result = converter.convert(
blocks = blocks,
target = target,
targetType = targetType
)
result.apply {
assert(size == 3)
assert(get(0).contentType == ContentType.NumberedList)
assert(get(0).content.param.number == 1)
assert(get(1).contentType == ContentType.NumberedList)
assert(get(1).content.param.number == 2)
assert(get(2).contentType == ContentType.NumberedList)
assert(get(2).content.param.number == 3)
}
}
@Test
fun `when we have three items of numbered list, when we convert item in the middle, numbers should be also updated`() {
val blocks = listOf(
BlockFactory.makeBlock(
contentParam = ContentParam.numberedList(1),
contentType = ContentType.NumberedList
),
BlockFactory.makeBlock(
contentParam = ContentParam.numberedList(2),
contentType = ContentType.NumberedList
),
BlockFactory.makeBlock(
contentParam = ContentParam.numberedList(3),
contentType = ContentType.NumberedList
)
)
val targetType = ContentType.P
val target = blocks[1]
val result = converter.convert(
blocks = blocks,
target = target,
targetType = targetType
)
result.apply {
assert(size == 3)
assert(get(0).contentType == ContentType.NumberedList)
assert(get(0).content.param.number == 1)
assert(get(1).contentType == ContentType.P)
assert(get(2).contentType == ContentType.NumberedList)
assert(get(2).content.param.number == 1)
}
}
}

View file

@ -0,0 +1,24 @@
package com.agileburo.anytype.feature_editor.factory
import com.agileburo.anytype.feature_editor.domain.*
object BlockFactory {
fun makeBlock(
contentType: ContentType = ContentType.P,
contentParam: ContentParam = ContentParam.empty(),
marks : List<Mark> = emptyList()
) : Block {
return Block(
id = DataFactory.randomString(),
parentId = DataFactory.randomString(),
contentType = contentType,
content = Content.Text(
text = DataFactory.randomString(),
param = contentParam,
marks = marks
)
)
}
}

View file

@ -0,0 +1,48 @@
package com.agileburo.anytype.feature_editor.factory
import java.util.*
import java.util.concurrent.ThreadLocalRandom
object DataFactory {
fun randomUuid(): String {
return UUID.randomUUID().toString()
}
fun randomString(): String {
return randomUuid()
}
fun randomInt(): Int {
return ThreadLocalRandom.current().nextInt(0, 1000 + 1)
}
fun randomInt(min : Int = 0, max: Int): Int {
return ThreadLocalRandom.current().nextInt(min, max)
}
fun randomLong(): Long {
return randomInt().toLong()
}
fun randomFloat(): Float {
return randomInt().toFloat()
}
fun randomDouble(): Double {
return randomInt().toDouble()
}
fun randomBoolean(): Boolean {
return Math.random() < 0.5
}
fun makeStringList(count: Int): List<String> {
val items = mutableListOf<String>()
repeat(count) {
items.add(randomUuid())
}
return items
}
}