Browse Source

Add simpler server/profile methods to DSL

tags/v0.9.0
Chris Smith 5 years ago
parent
commit
497e661fee
3 changed files with 114 additions and 10 deletions
  1. 3
    0
      CHANGELOG
  2. 38
    10
      src/main/kotlin/com/dmdirc/ktirc/Dsl.kt
  3. 73
    0
      src/test/kotlin/com/dmdirc/ktirc/DslTest.kt

+ 3
- 0
CHANGELOG View File

@@ -1,5 +1,8 @@
1 1
 vNEXT (in development)
2 2
 
3
+ * Improve DSL for creating an IrcClient to allow parameters to be passed to server and profile
4
+   e.g. IrcClient { server("irc.example.com", 6667) }
5
+
3 6
 v0.8.0
4 7
 
5 8
  * Added support for SCRAM-SHA-1 and SCRAM-SHA-256 SASL mechanisms

+ 38
- 10
src/main/kotlin/com/dmdirc/ktirc/Dsl.kt View File

@@ -40,29 +40,58 @@ internal data class IrcClientConfig(val server: ServerConfig, val profile: Profi
40 40
 class IrcClientConfigBuilder {
41 41
 
42 42
     private var server: ServerConfig? = null
43
+        set(value) {
44
+            check(field == null) { "server may only be specified once" }
45
+            check(!value?.host.isNullOrEmpty()) { "server.host must be specified" }
46
+            field = value
47
+        }
48
+
43 49
     private var profile: ProfileConfig? = null
50
+        set(value) {
51
+            check(field == null) { "profile may only be specified once" }
52
+            check(!value?.nickname.isNullOrEmpty()) { "profile.nickname must be specified" }
53
+            field = value
54
+        }
55
+
44 56
     private var sasl: SaslConfig? = null
57
+        set(value) {
58
+            check(field == null) { "sasl may only be specified once" }
59
+            field = value
60
+        }
45 61
 
46 62
     /**
47 63
      * Configures the server that the IrcClient will connect to.
48 64
      *
49
-     * At a minimum, [ServerConfig.host] must be supplied.
65
+     * See [ServerConfig] for details of each parameter.
66
+     *
67
+     * @param block Optional additional configuration to apply to the [ServerConfig]
50 68
      */
51 69
     @IrcClientDsl
52
-    fun server(block: ServerConfig.() -> Unit) {
53
-        check(server == null) { "server may only be specified once" }
54
-        server = ServerConfig().apply(block).also { check(it.host.isNotEmpty()) { "server.host must be specified" } }
70
+    fun server(host: String? = null, port: Int? = null, useTls: Boolean? = null, password: String? = null, block: (ServerConfig.() -> Unit)? = null) {
71
+        server = ServerConfig().apply {
72
+            host?.let { this.host = it }
73
+            port?.let { this.port = it }
74
+            useTls?.let { this.useTls = it }
75
+            password?.let { this.password = it }
76
+            block?.let { apply(it) }
77
+        }
55 78
     }
56 79
 
57 80
     /**
58 81
      * Configures the profile of the IrcClient user.
59 82
      *
60
-     * At a minimum, [ProfileConfig.nickname] must be supplied.
83
+     * See [ProfileConfig] for details of each parameter.
84
+     *
85
+     * @param block Optional additional configuration to apply to the [ProfileConfig]
61 86
      */
62 87
     @IrcClientDsl
63
-    fun profile(block: ProfileConfig.() -> Unit) {
64
-        check(profile == null) { "profile may only be specified once" }
65
-        profile = ProfileConfig().apply(block).also { check(it.nickname.isNotEmpty()) { "profile.nickname must be specified" } }
88
+    fun profile(nickname: String? = null, username: String? = null, realName: String? = null, block: (ProfileConfig.() -> Unit)? = null) {
89
+        profile = ProfileConfig().apply {
90
+            nickname?.let { this.nickname = it }
91
+            username?.let { this.username = it }
92
+            realName?.let { this.realName = it }
93
+            block?.let { apply(it) }
94
+        }
66 95
     }
67 96
 
68 97
     /**
@@ -70,7 +99,6 @@ class IrcClientConfigBuilder {
70 99
      */
71 100
     @IrcClientDsl
72 101
     fun sasl(block: SaslConfig.() -> Unit) {
73
-        check(sasl == null) { "sasl may only be specified once" }
74 102
         sasl = SaslConfig().apply(block)
75 103
     }
76 104
 
@@ -143,7 +171,7 @@ class SaslConfig {
143 171
 
144 172
     @IrcClientDsl
145 173
     fun mechanisms(vararg methods: String) {
146
-        with (this.mechanisms) {
174
+        with(this.mechanisms) {
147 175
             clear()
148 176
             addAll(methods)
149 177
         }

+ 73
- 0
src/test/kotlin/com/dmdirc/ktirc/DslTest.kt View File

@@ -26,6 +26,15 @@ internal class IrcClientConfigBuilderTest {
26 26
         }
27 27
     }
28 28
 
29
+    @Test
30
+    fun `throws if empty host is provided`() {
31
+        assertThrows<IllegalStateException> {
32
+            IrcClientConfigBuilder().apply {
33
+                server("")
34
+            }
35
+        }
36
+    }
37
+
29 38
     @Test
30 39
     fun `throws if profile is defined twice`() {
31 40
         assertThrows<IllegalStateException> {
@@ -45,6 +54,15 @@ internal class IrcClientConfigBuilderTest {
45 54
         }
46 55
     }
47 56
 
57
+    @Test
58
+    fun `throws if empty nickname is provided`() {
59
+        assertThrows<IllegalStateException> {
60
+            IrcClientConfigBuilder().apply {
61
+                profile("")
62
+            }
63
+        }
64
+    }
65
+
48 66
     @Test
49 67
     fun `throws if sasl is defined twice`() {
50 68
         assertThrows<IllegalStateException> {
@@ -91,6 +109,35 @@ internal class IrcClientConfigBuilderTest {
91 109
         assertTrue(config.server.useTls)
92 110
     }
93 111
 
112
+    @Test
113
+    fun `applies server settings with convenience function`() {
114
+        val config = IrcClientConfigBuilder().apply {
115
+            profile { nickname = "acidBurn" }
116
+            server("thegibson.com", 1337, true, "h4cktheplan3t")
117
+        }.build()
118
+
119
+        assertEquals("thegibson.com", config.server.host)
120
+        assertEquals(1337, config.server.port)
121
+        assertEquals("h4cktheplan3t", config.server.password)
122
+        assertTrue(config.server.useTls)
123
+    }
124
+
125
+    @Test
126
+    fun `applies server settings with convenience function and block`() {
127
+        val config = IrcClientConfigBuilder().apply {
128
+            profile { nickname = "acidBurn" }
129
+            server("thegibson.com", 1337) {
130
+                password = "h4cktheplan3t"
131
+                useTls = true
132
+            }
133
+        }.build()
134
+
135
+        assertEquals("thegibson.com", config.server.host)
136
+        assertEquals(1337, config.server.port)
137
+        assertEquals("h4cktheplan3t", config.server.password)
138
+        assertTrue(config.server.useTls)
139
+    }
140
+
94 141
     @Test
95 142
     fun `applies profile settings`() {
96 143
         val config = IrcClientConfigBuilder().apply {
@@ -107,6 +154,32 @@ internal class IrcClientConfigBuilderTest {
107 154
         assertEquals("Kate", config.profile.realName)
108 155
     }
109 156
 
157
+    @Test
158
+    fun `applies profile settings with convenience function`() {
159
+        val config = IrcClientConfigBuilder().apply {
160
+            profile("acidBurn", "acidB", "Kate")
161
+            server { host = "thegibson.com" }
162
+        }.build()
163
+
164
+        assertEquals("acidBurn", config.profile.nickname)
165
+        assertEquals("acidB", config.profile.username)
166
+        assertEquals("Kate", config.profile.realName)
167
+    }
168
+
169
+    @Test
170
+    fun `applies profile settings with convenience function and block`() {
171
+        val config = IrcClientConfigBuilder().apply {
172
+            profile("acidBurn", "acidB") {
173
+                realName = "Kate"
174
+            }
175
+            server { host = "thegibson.com" }
176
+        }.build()
177
+
178
+        assertEquals("acidBurn", config.profile.nickname)
179
+        assertEquals("acidB", config.profile.username)
180
+        assertEquals("Kate", config.profile.realName)
181
+    }
182
+
110 183
     @Test
111 184
     fun `applies sasl settings`() {
112 185
         val config = IrcClientConfigBuilder().apply {

Loading…
Cancel
Save