From b4b1a639658a92ac090dde2749da1acb9ed34661 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Mon, 19 Aug 2024 22:43:13 +0100 Subject: [PATCH] some tests --- client/src/main/kotlin/help/HelpPanel.kt | 6 +-- client/src/main/kotlin/help/RulesIllegal.kt | 2 - .../main/kotlin/help/RulesVectropyBidding.kt | 4 -- .../kotlin/help/RulesVectropyChallenging.kt | 1 - .../test/kotlin/help/AbstractHelpPanelTest.kt | 18 +++++++ client/src/test/kotlin/help/HelpPanelTest.kt | 47 +++++++++++++++++++ .../src/test/kotlin/help/RulesIllegalTest.kt | 5 ++ .../kotlin/help/RulesVectropyBiddingTest.kt | 5 ++ .../help/RulesVectropyChallengingTest.kt | 5 ++ 9 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 client/src/test/kotlin/help/AbstractHelpPanelTest.kt create mode 100644 client/src/test/kotlin/help/HelpPanelTest.kt create mode 100644 client/src/test/kotlin/help/RulesIllegalTest.kt create mode 100644 client/src/test/kotlin/help/RulesVectropyBiddingTest.kt create mode 100644 client/src/test/kotlin/help/RulesVectropyChallengingTest.kt diff --git a/client/src/main/kotlin/help/HelpPanel.kt b/client/src/main/kotlin/help/HelpPanel.kt index 89bcbd6..19ab4ab 100644 --- a/client/src/main/kotlin/help/HelpPanel.kt +++ b/client/src/main/kotlin/help/HelpPanel.kt @@ -128,13 +128,11 @@ abstract class HelpPanel : JPanel() { } } - private fun getWordFromPosition(text: String?, position: Int): String { + private fun getWordFromPosition(text: String, position: Int): String { if (position < 1) { return "" } - val length = text!!.length - val character = text.substring(position - 1, position) if (!isLetter(character)) { @@ -158,7 +156,7 @@ abstract class HelpPanel : JPanel() { } } - if (position < length) { + if (position < text.length) { var i = 1 var characterToTheRight = text.substring(position + i - 1, position + i) diff --git a/client/src/main/kotlin/help/RulesIllegal.kt b/client/src/main/kotlin/help/RulesIllegal.kt index 57103c4..dee0000 100644 --- a/client/src/main/kotlin/help/RulesIllegal.kt +++ b/client/src/main/kotlin/help/RulesIllegal.kt @@ -19,13 +19,11 @@ class RulesIllegal : HelpPanel() { paneOne.text = "The 'Illegal' option provides an alternative to bidding higher or challenging when facing a bid. If a player declares 'Illegal', they claim that the bid they were faced with was perfect. If they are right the opponent loses a card for the next round, else they lose a card - regardless of whether the bid they were faced with was an overbid or an underbid." paneOne.setBounds(21, 54, 429, 220) - paneOne.isEditable = false add(paneOne) title.foreground = EntropyColour.COLOUR_HELP_TITLE title.font = Font("Tahoma", Font.BOLD, 18) title.text = "Illegal!" title.setBounds(21, 25, 159, 30) - title.isEditable = false add(title) finaliseComponents() diff --git a/client/src/main/kotlin/help/RulesVectropyBidding.kt b/client/src/main/kotlin/help/RulesVectropyBidding.kt index aa60d5c..181dca7 100644 --- a/client/src/main/kotlin/help/RulesVectropyBidding.kt +++ b/client/src/main/kotlin/help/RulesVectropyBidding.kt @@ -23,7 +23,6 @@ class RulesVectropyBidding : HelpPanel(), Registry { paneOne.font = Font("SansSerif", Font.PLAIN, 14) paneOne.contentType = "text/html" paneOne.setBounds(21, 54, 429, 230) - paneOne.isEditable = false add(paneOne) paneTwo.font = Font("SansSerif", Font.PLAIN, 14) paneTwo.contentType = "text/html" @@ -31,19 +30,16 @@ class RulesVectropyBidding : HelpPanel(), Registry { "A bid is higher than another if the sum of its elements is higher, with the added restriction that each bid must include at least as many of each individual suit as the one before it. For example, if faced with a bid of (0, 0, 0, 2):\r\n\r\n" paneTwo.font = Font("Tahoma", Font.PLAIN, 14) paneTwo.setBounds(21, 320, 429, 156) - paneTwo.isEditable = false add(paneTwo) title.foreground = EntropyColour.COLOUR_HELP_TITLE title.font = Font("Tahoma", Font.BOLD, 18) title.text = "Bidding" title.setBounds(21, 25, 159, 30) - title.isEditable = false add(title) subtitle.text = "Bid Hierarchy" subtitle.foreground = EntropyColour.COLOUR_HELP_TITLE subtitle.font = Font("Tahoma", Font.BOLD, 18) subtitle.setBounds(21, 290, 159, 30) - subtitle.isEditable = false add(subtitle) finaliseComponents() diff --git a/client/src/main/kotlin/help/RulesVectropyChallenging.kt b/client/src/main/kotlin/help/RulesVectropyChallenging.kt index d0711ef..0cd92fc 100644 --- a/client/src/main/kotlin/help/RulesVectropyChallenging.kt +++ b/client/src/main/kotlin/help/RulesVectropyChallenging.kt @@ -23,7 +23,6 @@ class RulesVectropyChallenging : HelpPanel() { title.text = "Challenging" title.foreground = EntropyColour.COLOUR_HELP_TITLE title.font = Font("Tahoma", Font.BOLD, 18) - title.isEditable = false title.setBounds(21, 25, 216, 30) add(title) diff --git a/client/src/test/kotlin/help/AbstractHelpPanelTest.kt b/client/src/test/kotlin/help/AbstractHelpPanelTest.kt new file mode 100644 index 0000000..1029b78 --- /dev/null +++ b/client/src/test/kotlin/help/AbstractHelpPanelTest.kt @@ -0,0 +1,18 @@ +package help + +import io.kotest.matchers.shouldBe +import javax.swing.JTextPane +import main.kotlin.testCore.AbstractTest +import org.junit.jupiter.api.Test +import utils.getAllChildComponentsForType + +abstract class AbstractHelpPanelTest : AbstractTest() { + abstract fun factory(): T + + @Test + fun `All text panes should be read-only`() { + val pane = factory() + val textPanes = pane.getAllChildComponentsForType() + textPanes.forEach { it.isEditable shouldBe false } + } +} diff --git a/client/src/test/kotlin/help/HelpPanelTest.kt b/client/src/test/kotlin/help/HelpPanelTest.kt new file mode 100644 index 0000000..6c0a379 --- /dev/null +++ b/client/src/test/kotlin/help/HelpPanelTest.kt @@ -0,0 +1,47 @@ +package help + +import io.kotest.matchers.shouldBe +import java.awt.Color +import java.awt.Font +import javax.swing.JTextPane +import main.kotlin.testCore.AbstractTest +import org.junit.jupiter.api.Test +import util.EntropyColour + +class HelpPanelTest : AbstractTest() { + @Test + fun `should report whether text is contained`() { + val panel = FakeHelpPanel() + + panel.contains("first") shouldBe true + panel.contains("second") shouldBe true + panel.contains("third") shouldBe false + } +} + +class FakeHelpPanel : HelpPanel() { + override val nodeName = "Fake Panel" + + private val title = JTextPane() + private val paneOne = JTextPane() + private val paneTwo = JTextPane() + + init { + background = Color.WHITE + layout = null + paneOne.font = Font("SansSerif", Font.PLAIN, 14) + paneOne.contentType = "text/html" + paneOne.text = "First panel." + paneOne.setBounds(21, 54, 429, 220) + add(paneOne) + paneTwo.text = "Second panel." + add(paneTwo) + title.foreground = EntropyColour.COLOUR_HELP_TITLE + title.font = Font("Tahoma", Font.BOLD, 18) + title.text = "Fake Panel" + title.setBounds(21, 25, 159, 30) + add(title) + + finaliseComponents() + } +} diff --git a/client/src/test/kotlin/help/RulesIllegalTest.kt b/client/src/test/kotlin/help/RulesIllegalTest.kt new file mode 100644 index 0000000..9dab0a2 --- /dev/null +++ b/client/src/test/kotlin/help/RulesIllegalTest.kt @@ -0,0 +1,5 @@ +package help + +class RulesIllegalTest : AbstractHelpPanelTest() { + override fun factory() = RulesIllegal() +} diff --git a/client/src/test/kotlin/help/RulesVectropyBiddingTest.kt b/client/src/test/kotlin/help/RulesVectropyBiddingTest.kt new file mode 100644 index 0000000..c3d407c --- /dev/null +++ b/client/src/test/kotlin/help/RulesVectropyBiddingTest.kt @@ -0,0 +1,5 @@ +package help + +class RulesVectropyBiddingTest : AbstractHelpPanelTest() { + override fun factory() = RulesVectropyBidding() +} diff --git a/client/src/test/kotlin/help/RulesVectropyChallengingTest.kt b/client/src/test/kotlin/help/RulesVectropyChallengingTest.kt new file mode 100644 index 0000000..2866f0c --- /dev/null +++ b/client/src/test/kotlin/help/RulesVectropyChallengingTest.kt @@ -0,0 +1,5 @@ +package help + +class RulesVectropyChallengingTest : AbstractHelpPanelTest() { + override fun factory() = RulesVectropyChallenging() +}