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 7.1KB

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