You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DslTest.kt 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.dmdirc.ktirc
  2. import org.junit.jupiter.api.Assertions.assertEquals
  3. import org.junit.jupiter.api.Assertions.assertTrue
  4. import org.junit.jupiter.api.Test
  5. import org.junit.jupiter.api.assertThrows
  6. internal class IrcClientConfigBuilderTest {
  7. @Test
  8. fun `throws if server is defined twice`() {
  9. assertThrows<IllegalStateException> {
  10. IrcClientConfigBuilder().apply {
  11. server { host = "1" }
  12. server { host = "2" }
  13. }
  14. }
  15. }
  16. @Test
  17. fun `throws if no host is provided`() {
  18. assertThrows<IllegalStateException> {
  19. IrcClientConfigBuilder().apply {
  20. server {}
  21. }
  22. }
  23. }
  24. @Test
  25. fun `throws if empty host is provided`() {
  26. assertThrows<IllegalStateException> {
  27. IrcClientConfigBuilder().apply {
  28. server("")
  29. }
  30. }
  31. }
  32. @Test
  33. fun `throws if profile is defined twice`() {
  34. assertThrows<IllegalStateException> {
  35. IrcClientConfigBuilder().apply {
  36. profile { nickname = "acidBurn" }
  37. profile { nickname = "zeroCool" }
  38. }
  39. }
  40. }
  41. @Test
  42. fun `throws if no nickname is provided`() {
  43. assertThrows<IllegalStateException> {
  44. IrcClientConfigBuilder().apply {
  45. profile {}
  46. }
  47. }
  48. }
  49. @Test
  50. fun `throws if empty nickname is provided`() {
  51. assertThrows<IllegalStateException> {
  52. IrcClientConfigBuilder().apply {
  53. profile("")
  54. }
  55. }
  56. }
  57. @Test
  58. fun `throws if sasl is defined twice`() {
  59. assertThrows<IllegalStateException> {
  60. IrcClientConfigBuilder().apply {
  61. sasl {}
  62. sasl {}
  63. }
  64. }
  65. }
  66. @Test
  67. fun `throws if server is not defined`() {
  68. assertThrows<IllegalStateException> {
  69. IrcClientConfigBuilder().apply {
  70. profile { nickname = "acidBurn" }
  71. }.build()
  72. }
  73. }
  74. @Test
  75. fun `throws if profile is not defined`() {
  76. assertThrows<IllegalStateException> {
  77. IrcClientConfigBuilder().apply {
  78. server { host = "thegibson.com" }
  79. }.build()
  80. }
  81. }
  82. @Test
  83. fun `applies server settings`() {
  84. val config = IrcClientConfigBuilder().apply {
  85. profile { nickname = "acidBurn" }
  86. server {
  87. host = "thegibson.com"
  88. port = 1337
  89. password = "h4cktheplan3t"
  90. useTls = true
  91. }
  92. }.build()
  93. assertEquals("thegibson.com", config.server.host)
  94. assertEquals(1337, config.server.port)
  95. assertEquals("h4cktheplan3t", config.server.password)
  96. assertTrue(config.server.useTls)
  97. }
  98. @Test
  99. fun `applies server settings with convenience function`() {
  100. val config = IrcClientConfigBuilder().apply {
  101. profile { nickname = "acidBurn" }
  102. server("thegibson.com", 1337, true, "h4cktheplan3t")
  103. }.build()
  104. assertEquals("thegibson.com", config.server.host)
  105. assertEquals(1337, config.server.port)
  106. assertEquals("h4cktheplan3t", config.server.password)
  107. assertTrue(config.server.useTls)
  108. }
  109. @Test
  110. fun `applies server settings with convenience function and block`() {
  111. val config = IrcClientConfigBuilder().apply {
  112. profile { nickname = "acidBurn" }
  113. server("thegibson.com", 1337) {
  114. password = "h4cktheplan3t"
  115. useTls = true
  116. }
  117. }.build()
  118. assertEquals("thegibson.com", config.server.host)
  119. assertEquals(1337, config.server.port)
  120. assertEquals("h4cktheplan3t", config.server.password)
  121. assertTrue(config.server.useTls)
  122. }
  123. @Test
  124. fun `applies profile settings`() {
  125. val config = IrcClientConfigBuilder().apply {
  126. profile {
  127. nickname = "acidBurn"
  128. username = "acidB"
  129. realName = "Kate"
  130. }
  131. server { host = "thegibson.com" }
  132. }.build()
  133. assertEquals("acidBurn", config.profile.nickname)
  134. assertEquals("acidB", config.profile.username)
  135. assertEquals("Kate", config.profile.realName)
  136. }
  137. @Test
  138. fun `applies profile settings with convenience function`() {
  139. val config = IrcClientConfigBuilder().apply {
  140. profile("acidBurn", "acidB", "Kate")
  141. server { host = "thegibson.com" }
  142. }.build()
  143. assertEquals("acidBurn", config.profile.nickname)
  144. assertEquals("acidB", config.profile.username)
  145. assertEquals("Kate", config.profile.realName)
  146. }
  147. @Test
  148. fun `applies profile settings with convenience function and block`() {
  149. val config = IrcClientConfigBuilder().apply {
  150. profile("acidBurn", "acidB") {
  151. realName = "Kate"
  152. }
  153. server { host = "thegibson.com" }
  154. }.build()
  155. assertEquals("acidBurn", config.profile.nickname)
  156. assertEquals("acidB", config.profile.username)
  157. assertEquals("Kate", config.profile.realName)
  158. }
  159. @Test
  160. fun `applies sasl settings`() {
  161. val config = IrcClientConfigBuilder().apply {
  162. profile { nickname = "acidBurn" }
  163. server { host = "thegibson.com" }
  164. sasl {
  165. username = "acidBurn"
  166. password = "h4ckthepl@net"
  167. }
  168. }.build()
  169. assertEquals("acidBurn", config.sasl?.username)
  170. assertEquals("h4ckthepl@net", config.sasl?.password)
  171. }
  172. }
  173. internal class SaslConfigTest {
  174. @Test
  175. fun `mechanisms function clears all existing mechanisms`() {
  176. val config = SaslConfig().apply {
  177. mechanisms += "TEST"
  178. mechanisms("FOO", "BAR")
  179. }
  180. assertEquals(setOf("FOO", "BAR"), config.mechanisms)
  181. }
  182. @Test
  183. fun `defaults to plain and scram mechanisms`() {
  184. val config = SaslConfig()
  185. assertEquals(setOf("PLAIN", "SCRAM-SHA-1", "SCRAM-SHA-256"), config.mechanisms)
  186. }
  187. }