Skip to content

Commit

Permalink
Finalise all tests for new UI DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Sep 17, 2023
1 parent 16f6fe3 commit fd6f000
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ class TemplateListEditor(private val originalSettings: Settings = Settings.DEFAU
}
}


/**
* Disposes this editor's resources.
*/
override fun dispose() {
schemeEditor?.dispose()
previewPanel.dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ data class TemplateReference(
* Tries to find a recursive cycle of references within the current [context] starting at [parent], returning `null`
* if there is no such cycle.
*/
private fun getRecursiveLoopOrNull(): List<Template>? {
private fun getReferenceCycleOrNull(): List<Template>? {
fun helper(source: TemplateReference, history: List<TemplateReference>): List<Template>? =
if (source in history) listOf(source.parent)
else source.template?.schemes
Expand All @@ -66,14 +66,18 @@ data class TemplateReference(
}

/**
* Returns `true` if `this` reference can refer to [target] without causing recursion within the current [context].
* Returns `true` if and only if `this` would not be part of a cycle of recursive references in [context] after
* setting [template] to [target].
*/
fun canReference(target: Template) =
templateUuid.let { originalUuid ->
templateUuid = target.uuid
(getRecursiveLoopOrNull() == null)
.also { templateUuid = originalUuid }
}
fun canReference(target: Template): Boolean {
val originalUuid = templateUuid

templateUuid = target.uuid
val createsCycle = getReferenceCycleOrNull() == null
templateUuid = originalUuid

return createsCycle
}


override fun generateUndecoratedStrings(count: Int) =
Expand All @@ -84,12 +88,12 @@ data class TemplateReference(


override fun doValidate(): String? {
val recursion = getRecursiveLoopOrNull()
val cycle = getReferenceCycleOrNull()

return when {
templateUuid == null -> Bundle("reference.error.no_selection")
template == null -> Bundle("reference.error.not_found")
recursion != null -> Bundle("reference.error.recursion", "(${recursion.joinToString("") { it.name }})")
cycle != null -> Bundle("reference.error.recursion", "(${cycle.joinToString("") { it.name }})")
else -> affixDecorator.doValidate() ?: arrayDecorator.doValidate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ fun <E> Cell<ComboBox<E>>.isEditable(editable: Boolean) = this.also { it.compone
/**
* Sets the [filter] on the document of the [ComboBox] in this [Cell], and returns `this`.
*/
fun <E> Cell<ComboBox<E>>.withFilter(filter: DocumentFilter) =
this.also {
((component.editor.editorComponent as? JTextComponent)?.document as? AbstractDocument)?.documentFilter = filter
}
fun <E> Cell<ComboBox<E>>.withFilter(filter: DocumentFilter): Cell<ComboBox<E>> {
((component.editor.editorComponent as? JTextComponent)?.document as? AbstractDocument)?.documentFilter = filter
return this
}

/**
* Sets an item renderer on the [ComboBox] in this [Cell] that renders a label displaying the mapping of an item using
* [toString], and returns `this`.
*/
fun <E> Cell<ComboBox<E>>.withSimpleRenderer(toString: (E) -> String = { it.toString() }) =
this.also { component.setRenderer { _, value, _, _, _ -> JBLabel(toString(value)) } }
fun <E> Cell<ComboBox<E>>.withSimpleRenderer(toString: (E) -> String = { it.toString() }): Cell<ComboBox<E>> {
component.setRenderer { _, value, _, _, _ -> JBLabel(toString(value)) }
return this
}


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@ object TemplateListTest : FunSpec({
}


context("listValidReferenceTargets") {
context("for reference") {
xtest("returns an error if the given reference is not in this list") {
TODO()
}

xtest("returns copies") {
TODO()
}
}

xtest("for template") {
TODO()
}
}


context("getTemplateByUuid") {
test("returns null if no such template can be found") {
val list = TemplateList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,58 @@ object TemplateReferenceTest : FunSpec({
}
}

xcontext("canReference") {
TODO()
context("canReference") {
test("returns true if the reference would be outside its own context") {
reference.canReference(Template()) shouldBe true
}

test("returns true if the reference would not create a cycle") {
reference.canReference(referencedTemplate) shouldBe true
}

test("returns true if the reference would break the current cycle") {
val safeTemplate = Template()
list.templates += safeTemplate
referencedTemplate.schemes += TemplateReference(referencingTemplate.uuid)

list.applyContext(Settings(list))

reference.canReference(safeTemplate) shouldBe true
}

test("returns false if the reference would refer to itself") {
reference.canReference(referencingTemplate) shouldBe false
}

test("returns false if the reference would create a cycle") {
referencedTemplate.schemes += TemplateReference(referencingTemplate.uuid)

list.applyContext(Settings(list))

reference.canReference(referencedTemplate) shouldBe false
}

test("returns false if the reference would refer to a cycle") {
val otherReferencedTemplate = Template(schemes = mutableListOf(TemplateReference(referencedTemplate.uuid)))
list.templates += otherReferencedTemplate
referencedTemplate.schemes += TemplateReference(otherReferencedTemplate.uuid)

list.applyContext(Settings(list))

reference.canReference(referencedTemplate) shouldBe false
}

test("returns true if the reference would not create a cycle but a cycle exists elsewhere") {
val cycleTemplate1 = Template()
list.templates += cycleTemplate1
val cycleTemplate2 = Template(schemes = mutableListOf(TemplateReference(cycleTemplate1.uuid)))
list.templates += cycleTemplate2
cycleTemplate1.schemes += TemplateReference(cycleTemplate2.uuid)

list.applyContext(Settings(list))

reference.canReference(referencedTemplate) shouldBe true
}
}


Expand Down Expand Up @@ -232,15 +282,7 @@ object TemplateReferenceTest : FunSpec({
"fails if the referred template cannot be found" to
row({ reference.also { it.applyContext(Settings()) } }, "reference.error.not_found"),
"fails if the reference is recursive" to
row(
{
referencedTemplate.schemes +=
TemplateReference(referencingTemplate.uuid).also { it.applyContext(Settings(list)) }

reference
},
"reference.error.recursion",
),
row({ reference.also { it.template = referencingTemplate } }, "reference.error.recursion"),
"fails if affix decorator is invalid" to
row({ reference.also { it.affixDecorator.descriptor = """\""" } }, ""),
"fails if array decorator is invalid" to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ object TemplateTest : FunSpec({
}


xtest("canReference") {
TODO()
}


context("generateStrings") {
withData(
mapOf(
Expand Down

This file was deleted.

0 comments on commit fd6f000

Please sign in to comment.