Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Aug 19, 2024
1 parent 0bc7bd0 commit b4b1a63
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 11 deletions.
6 changes: 2 additions & 4 deletions client/src/main/kotlin/help/HelpPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions client/src/main/kotlin/help/RulesIllegal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ class RulesIllegal : HelpPanel() {
paneOne.text =
"<html>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 <font color=\"blue\"><u>perfect</u></font>. 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.</html>"
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()
Expand Down
4 changes: 0 additions & 4 deletions client/src/main/kotlin/help/RulesVectropyBidding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@ 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"
paneTwo.text =
"<html>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<ul style=\"margin-left:10px; padding:0px\">\r\n<li style=\"margin-bottom: 6px;\"> (1, 0, 0, 2) is a valid higher bid because it includes (0, 0, 0, 2).</li>\r\n<li style=\"margin-bottom:6px;\"> (5, 5, 5, 0) is a higher bid, but this is <b>not</b> legal because it contains fewer spades than the bid before it.</li></ul></html>"
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()
Expand Down
1 change: 0 additions & 1 deletion client/src/main/kotlin/help/RulesVectropyChallenging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions client/src/test/kotlin/help/AbstractHelpPanelTest.kt
Original file line number Diff line number Diff line change
@@ -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<T : HelpPanel> : AbstractTest() {
abstract fun factory(): T

@Test
fun `All text panes should be read-only`() {
val pane = factory()
val textPanes = pane.getAllChildComponentsForType<JTextPane>()
textPanes.forEach { it.isEditable shouldBe false }
}
}
47 changes: 47 additions & 0 deletions client/src/test/kotlin/help/HelpPanelTest.kt
Original file line number Diff line number Diff line change
@@ -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 = "<html>First panel.</html>"
paneOne.setBounds(21, 54, 429, 220)
add(paneOne)
paneTwo.text = "<html>Second panel.</html>"
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()
}
}
5 changes: 5 additions & 0 deletions client/src/test/kotlin/help/RulesIllegalTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package help

class RulesIllegalTest : AbstractHelpPanelTest<RulesIllegal>() {
override fun factory() = RulesIllegal()
}
5 changes: 5 additions & 0 deletions client/src/test/kotlin/help/RulesVectropyBiddingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package help

class RulesVectropyBiddingTest : AbstractHelpPanelTest<RulesVectropyBidding>() {
override fun factory() = RulesVectropyBidding()
}
5 changes: 5 additions & 0 deletions client/src/test/kotlin/help/RulesVectropyChallengingTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package help

class RulesVectropyChallengingTest : AbstractHelpPanelTest<RulesVectropyChallenging>() {
override fun factory() = RulesVectropyChallenging()
}

0 comments on commit b4b1a63

Please sign in to comment.