Skip to content

Commit

Permalink
Add AffixDecoratorEditorTest class
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Sep 16, 2023
1 parent 62d5ef6 commit 9db39ae
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=com.fwdekker
# TODO: Also remove `beta` from `TemplateSettings` storage file!
# TODO: Also remove `beta` from `PersistentSettings' annotation`!
version=3.0.0-beta.3

# Compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ class AffixDecoratorEditor(
* @return the camelCase concatenation of [prefix] and [name]
*/
private fun camelConcat(prefix: String, name: String) =
if (prefix != "") "${prefix}${name[0].uppercase(Locale.getDefault())}${name.drop(1)}"
else name
if (prefix == "") name
else "${prefix}${name[0].uppercase(Locale.getDefault())}${name.drop(1)}"
7 changes: 7 additions & 0 deletions src/test/kotlin/com/fwdekker/randomness/AssertJHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import io.kotest.core.spec.BeforeAny
import io.kotest.core.spec.style.scopes.ContainerScope
import io.kotest.core.test.TestType
import org.assertj.swing.core.GenericTypeMatcher
import org.assertj.swing.driver.ComponentDriver
import org.assertj.swing.edt.GuiActionRunner
import org.assertj.swing.fixture.AbstractComponentFixture
import org.assertj.swing.fixture.AbstractTwoStateButtonFixture
import org.assertj.swing.fixture.FrameFixture
import org.assertj.swing.fixture.JComboBoxFixture
Expand Down Expand Up @@ -73,6 +75,11 @@ fun FrameFixture.getActionButton(accessibleName: String): ActionButton =
find(matcher(ActionButton::class.java) { it.accessibleContext.accessibleName == accessibleName })


fun <S, C : Component, D : ComponentDriver> AbstractComponentFixture<S, C, D>.requireEnabledIs(enabled: Boolean) =
if (enabled) this.requireEnabled()
else this.requireDisabled()


// TODO: Document these functions!
@Suppress("UNCHECKED_CAST")
fun <T> KMutableProperty0<T>.prop(): MutableProperty<Any?> = MutableProperty({ get() }, { set(it as T) })
Expand Down
2 changes: 2 additions & 0 deletions src/test/kotlin/com/fwdekker/randomness/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import io.kotest.matchers.nulls.beNull
import io.kotest.matchers.should


// TODO: Merge this file with `AssertJHelper`?

/**
* @see beEmptyArray
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,149 @@
package com.fwdekker.randomness.affix

import com.fwdekker.randomness.afterNonContainer
import com.fwdekker.randomness.beforeNonContainer
import com.fwdekker.randomness.editorApplyTestFactory
import com.fwdekker.randomness.editorFieldsTestFactory
import com.fwdekker.randomness.guiGet
import com.fwdekker.randomness.guiRun
import com.fwdekker.randomness.prop
import com.fwdekker.randomness.requireEnabledIs
import com.fwdekker.randomness.textProp
import com.intellij.testFramework.fixtures.IdeaTestFixture
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
import com.intellij.ui.layout.selected
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.core.NamedTag
import io.kotest.core.spec.style.FunSpec
import io.kotest.data.row
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import org.assertj.swing.edt.FailOnThreadViolationRepaintManager
import org.assertj.swing.fixture.Containers
import org.assertj.swing.fixture.FrameFixture
import javax.swing.JCheckBox


/**
* Unit tests for [AffixDecoratorEditor].
*/
object AffixDecoratorEditorTest : FunSpec({
tags(NamedTag("TODO")) // TODO
tags(NamedTag("Editor"), NamedTag("IdeaFixture"), NamedTag("Swing"))


xcontext("TODO") {
// TODO: Add these tests
lateinit var ideaFixture: IdeaTestFixture
lateinit var frame: FrameFixture

lateinit var scheme: AffixDecorator
lateinit var editor: AffixDecoratorEditor


beforeContainer {
FailOnThreadViolationRepaintManager.install()
}

beforeNonContainer {
ideaFixture = IdeaTestFixtureFactory.getFixtureFactory().createBareFixture()
ideaFixture.setUp()

scheme = AffixDecorator(enabled = true)
editor = guiGet { AffixDecoratorEditor(scheme, presets = listOf("a", "b", "c")) }
frame = Containers.showInFrame(editor.rootComponent)
}

afterNonContainer {
frame.cleanUp()
ideaFixture.tearDown()
}


context("constructor") {
context("enableMnemonic") {
test("enables mnemonics if true") {
frame.checkBox("affixEnabled").target().mnemonic shouldNotBe 0
}

test("disables mnemonics if false") {
frame.cleanUp()
editor = guiGet { AffixDecoratorEditor(scheme, presets = listOf("."), enableMnemonic = false) }
frame = Containers.showInFrame(editor.rootComponent)

frame.checkBox("affixEnabled").target().mnemonic shouldBe 0
}
}

context("namePrefix") {
test("does not prefix component names if the prefix is an empty string") {
shouldNotThrowAny { frame.checkBox("affixEnabled") }
shouldNotThrowAny { frame.comboBox("affixDescriptor") }
}

test("prefixes component names by the given prefix and retains camel case") {
frame.cleanUp()
editor = guiGet { AffixDecoratorEditor(scheme, presets = listOf("."), namePrefix = "prefix") }
frame = Containers.showInFrame(editor.rootComponent)

shouldNotThrowAny { frame.checkBox("prefixAffixEnabled") }
shouldNotThrowAny { frame.comboBox("prefixAffixDescriptor") }
}
}
}


context("input handling") {
context("ifEnabled and affixEnabled") {
lateinit var toggle: JCheckBox


beforeNonContainer {
toggle = guiGet { JCheckBox().also { it.isSelected = false } }
}


@Suppress("BooleanLiteralArgument") // Argument names are clear from lambda later on
withData(
nameFn = { "enabledIf=${it.a}, checkboxChecked=${it.b}" },
listOf(
row(null, false, true, false),
row(null, true, true, true),
row(false, false, false, false),
row(false, true, false, false),
row(true, false, true, false),
row(true, true, true, true),
)
) { (predicateState, checkboxState, expectedCheckboxEnabled, expectedDescriptorEnabled) ->
val predicate = if (predicateState == null) null else toggle.selected

frame.cleanUp()
editor = guiGet { AffixDecoratorEditor(scheme, presets = listOf("."), enabledIf = predicate) }
frame = Containers.showInFrame(editor.rootComponent)

guiRun {
if (predicateState != null) toggle.isSelected = predicateState
frame.checkBox().target().isSelected = checkboxState
}

frame.checkBox().requireEnabledIs(expectedCheckboxEnabled)
frame.comboBox().requireEnabledIs(expectedDescriptorEnabled)
}
}
}


include(editorApplyTestFactory { editor })

include(
editorFieldsTestFactory(
{ editor },
mapOf(
"descriptor" to
row(
{ frame.comboBox("affixDescriptor").textProp() },
{ editor.scheme::descriptor.prop() },
"non-preset string",
),
)
)
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ object ArrayDecoratorEditorTest : FunSpec({
}


context("embedded") {
test("shows the titled separator if the editor is not embedded") {
frame.panel(matcher(TitledSeparator::class.java)).requireVisible()
}
context("constructor") {
context("embedded") {
test("shows the titled separator if the editor is not embedded") {
frame.panel(matcher(TitledSeparator::class.java)).requireVisible()
}

test("hides the titled separator if the editor is embedded") {
frame.cleanUp()
editor = guiGet { ArrayDecoratorEditor(scheme, embedded = true) }
frame = showInFrame(editor.rootComponent)
test("hides the titled separator if the editor is embedded") {
frame.cleanUp()
editor = guiGet { ArrayDecoratorEditor(scheme, embedded = true) }
frame = showInFrame(editor.rootComponent)

frame.panel(matcher(TitledSeparator::class.java)).requireNotVisible()
frame.panel(matcher(TitledSeparator::class.java)).requireNotVisible()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.fwdekker.randomness.guiGet
import com.fwdekker.randomness.guiRun
import com.fwdekker.randomness.isSelectedProp
import com.fwdekker.randomness.prop
import com.fwdekker.randomness.requireEnabledIs
import com.fwdekker.randomness.textProp
import com.fwdekker.randomness.valueProp
import com.intellij.testFramework.fixtures.IdeaTestFixture
Expand Down Expand Up @@ -86,7 +87,7 @@ object IntegerSchemeEditorTest : FunSpec({
}

context("toggles the grouping separator depending on base value and checkbox state") {
@Suppress("BooleanLiteralArgument") // Argument names clear from lambda later on
@Suppress("BooleanLiteralArgument") // Argument names are clear from lambda later on
withData(
row(8, false, false, false),
row(8, true, false, false),
Expand All @@ -100,10 +101,8 @@ object IntegerSchemeEditorTest : FunSpec({
frame.checkBox("groupingSeparatorEnabled").target().isSelected = checkBoxChecked
}

frame.checkBox("groupingSeparatorEnabled")
.let { if (expectedCheckBoxEnabled) it.requireEnabled() else it.requireDisabled() }
frame.comboBox("groupingSeparator")
.let { if (expectedInputEnabled) it.requireEnabled() else it.requireDisabled() }
frame.checkBox("groupingSeparatorEnabled").requireEnabledIs(expectedCheckBoxEnabled)
frame.comboBox("groupingSeparator").requireEnabledIs(expectedInputEnabled)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ object TemplateJTreeTest : FunSpec({
}

context("canMoveSchemeByOnePosition") {
@Suppress("BooleanLiteralArgument") // Argument names clear from lambda later on
@Suppress("BooleanLiteralArgument") // Argument names are clear from lambda later on
withData(
mapOf<String, Row3<() -> Scheme, Boolean, Boolean>>(
"first template" to row({ tree.myModel.list.templates[0] }, false, false),
Expand Down

0 comments on commit 9db39ae

Please sign in to comment.