Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Dsl.kt 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.dmdirc.irctest
  2. @DslMarker
  3. annotation class TestDslMarker
  4. fun testCase(name: String, data: TestCaseContext.() -> Unit) = TestCaseContext(name).apply(data)
  5. @TestDslMarker
  6. class TestCaseContext(val name: String) {
  7. internal val config = ConfigContext()
  8. internal val steps = StepsContext()
  9. fun config(data: ConfigContext.() -> Unit) = config.data()
  10. fun steps(data: StepsContext.() -> Unit) = steps.data()
  11. }
  12. @TestDslMarker
  13. class ConfigContext {
  14. var nick = "nick"
  15. var user = "ident"
  16. var realName = "some name"
  17. var password: String? = null
  18. }
  19. @TestDslMarker
  20. class StepsContext {
  21. private val steps = mutableListOf<Step>()
  22. fun send(line: String) { steps += SendStep(line) }
  23. fun expect(line: String) { steps += SimpleExpectStep(line) }
  24. fun expect(data: ExpectContext.() -> Unit) {
  25. ExpectContext().data()
  26. }
  27. operator fun iterator() = steps.iterator()
  28. }
  29. open class Step
  30. class SendStep(val line: String) : Step()
  31. class SimpleExpectStep(val line: String): Step()
  32. @TestDslMarker
  33. class ExpectContext {
  34. private var commandMatcher: ((String) -> Boolean)? = null
  35. private var paramMatchers = HashMap<Int, (String) -> Boolean>()
  36. val command: String = "COMMAND"
  37. val parameters = ParameterProvider()
  38. infix fun String.toMatch(regex: String) {
  39. val matcher = { it: String -> it.matches(regex.toRegex()) }
  40. when (this) {
  41. command -> commandMatcher = matcher
  42. else -> paramMatchers[this.toInt()] = matcher
  43. }
  44. }
  45. infix fun String.toEqual(value: String) {
  46. val matcher = { it: String -> it == value }
  47. when (this) {
  48. command -> commandMatcher = matcher
  49. else -> paramMatchers[this.toInt()] = matcher
  50. }
  51. }
  52. }
  53. class ParameterProvider {
  54. val count = "count"
  55. operator fun get(index: Int) = "$index"
  56. }