open class View(
val id: String,
var isVisible: Boolean
) {
fun show() {
isVisible = true
}
fun hide() {
isVisible = false
}
}
class TextView(
id: String,
var text: String
) : View(id, true)
class Toggle(
id: String,
) : View(id, true) {
var isOn: Boolean = false
fun click() {
isOn = !isOn
}
}
Example solution in playground
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import org.junit.Test
import kotlin.test.assertEquals
open class View(
val id: String,
var isVisible: Boolean
) {
fun show() {
isVisible = true
}
fun hide() {
isVisible = false
}
}
class TextView(
id: String,
var text: String
) : View(id, true)
class Toggle(
id: String,
) : View(id, true) {
var isOn: Boolean = false
fun click() {
isOn = !isOn
}
}
fun main() {
val view = View(
id = "v1",
isVisible = false,
)
println(view.id) // v1
println(view.isVisible) // false
val textView = TextView(
id = "tv1",
text = "Hello, World!",
)
println(textView.id) // tv1
println(textView.text) // Hello, World!
println(textView.isVisible) // true
textView.text = "Welcome to Kotlin!"
println(textView.text) // Welcome to Kotlin!
textView.hide()
println(textView.isVisible) // false
val toggle = Toggle(
id = "toggle1",
)
println(toggle.id) // toggle1
println(toggle.isOn) // false
toggle.click()
println(toggle.isOn) // true
println(toggle.isVisible) // true
toggle.hide()
println(toggle.isVisible) // false
toggle.show()
println(toggle.isVisible) // true
}
class GuiTests {
@Test
fun `view show should set isVisible to true`() {
val view = View("view1", false)
view.show()
assertTrue(view.isVisible)
}
@Test
fun `view hide should set isVisible to false`() {
val view = View("view2", true)
view.hide()
assertFalse(view.isVisible)
}
@Test
fun `toggle click should toggle isOn state`() {
val toggle = Toggle("toggle1")
toggle.click()
assertTrue(toggle.isOn)
toggle.click()
assertFalse(toggle.isOn)
}
@Test
fun `text view setText should update text property`() {
val textView = TextView("textView1", "Hello")
textView.text = "World"
assertEquals("World", textView.text)
}
@Test
fun `toggle should inherit properties from view`() {
val toggle = Toggle("toggle2")
assertTrue(toggle.isVisible)
toggle.hide()
assertFalse(toggle.isVisible)
}
@Test
fun `text view should inherit properties from view`() {
val textView = TextView("textView2", "Hello")
assertTrue(textView.isVisible)
}
@Test
fun `toggle should start with isOn set to false`() {
val toggle = Toggle("toggle3")
assertFalse(toggle.isOn)
}
@Test
fun `toggle should toggle isOn state correctly`() {
val toggle = Toggle("toggle4")
toggle.click()
assertTrue(toggle.isOn)
toggle.click()
assertFalse(toggle.isOn)
toggle.click()
assertTrue(toggle.isOn)
}
@Test
fun `view show and hide interactions`() {
val view = View("view3", false)
view.show()
assertTrue(view.isVisible)
view.hide()
assertFalse(view.isVisible)
}
@Test
fun `text view setText and visibility interactions`() {
val textView = TextView("textView3", "Hello")
textView.text = "World"
assertEquals("World", textView.text)
textView.hide()
assertFalse(textView.isVisible)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.