Skip to content

Testing of extension methods #111

Description

@Nereboss

Is your feature request related to a problem? Please describe.

When writing extension functions, i want to be able to properly test them.

Describe the solution you'd like
Extension of kotterx to better support testing of whole methods that include section and run parts. I believe this is important to make kotter a more viable choice for larger codebases.

Describe alternatives you've considered
I tried simulating keypresses to test user input for the method using the sendKeys function of kotterx and starting it in a coroutine to fire after the method to test was started but i failed to achieve any results.

Additional context
Here is the code for one of the methods i wrote that i want to test:

fun Session.promptConfirm(message: String): Boolean {
    var result = true
    var choice by liveVarOf(true)
    section {
        drawConfirm(message, choice)
    }.runUntilSignal {
        onKeyPressed {
            println(key)
            when (key) {
                Keys.LEFT -> choice = true
                Keys.RIGHT -> choice = false
                Keys.ENTER -> { result = choice; signal() }
            }
        }
    }
    return result
}

private fun MainRenderScope.drawConfirm(message: String, choice: Boolean): Unit {
    green { text("? ") }; text(message)
    black(isBright = true) { textLine("  (use arrow keys to change selection)") }
    if (choice) {
        text("> "); cyan { text("[Yes]") }; textLine(" No ")
    } else {
        text(">  Yes "); cyan { textLine("[No]") }
    }
}

This snippet changes based on user input and only returns once the user pressed enter, so i would expect that i can use functionality of kotterx to simulate these keypresses to test my method.
Im not sure if this is already possible as there there are methods to simulate keypresses

@Test
    fun `should return true when no arrow keys were pressed`() {
        var result = false
        testSession {
            runBlocking {
                launch {
                    delay(1000)
                    println("key sent")
                    it.sendKey(13)
                    println("check for blocking")
                }
                result = myPromptConfirm("Unimportant test message")
            }
        }
        Assertions.assertTrue(result)
  }

This was my attempt at simulating pressing the enter key but the sendKey method kept blocking. From what i have concluded so far, kotterx already gives great tools to test everything that is being rendered, which can be applied for extension methods too if the method extends for example MainRenderScope. But how to test the logic that happens in a run block (like the user input in the example above) is either currently not possible or not documented enough.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature / functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions