Browse Source

Store channel modes a bit nicer.

Groundwork for #5
tags/v0.5.0
Chris Smith 5 years ago
parent
commit
bbef1acb43

+ 2
- 0
CHANGELOG View File

@@ -5,6 +5,8 @@ vNEXT (in development)
5 5
  * Improved some documentation
6 6
  * Added ServerConnecting and ServerDisconnected events
7 7
  * Server status now starts as Disconnected rather than Connecting
8
+ * CHANMODES feature is now stored as an array, not a single comma-separated string
9
+ * Improved error message when features are of an unexpected type
8 10
 
9 11
 v0.4.0
10 12
 

+ 1
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/ISupportProcessor.kt View File

@@ -64,6 +64,7 @@ internal class ISupportProcessor : MessageProcessor {
64 64
             String::class -> this
65 65
             CaseMapping::class -> CaseMapping.fromName(this)
66 66
             ModePrefixMapping::class -> indexOf(')').let { ModePrefixMapping(substring(1 until it), substring(it + 1)) }
67
+            Array<String>::class -> split(',').toTypedArray()
67 68
             else -> TODO("not implemented")
68 69
         }
69 70
     }

+ 5
- 2
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt View File

@@ -57,7 +57,10 @@ class ServerFeatureMap {
57 57
     operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
58 58
 
59 59
     internal operator fun set(feature: ServerFeature<*>, value: Any) {
60
-        require(feature.type.isInstance(value))
60
+        require(feature.type.isInstance(value)) {
61
+            "Value given for feature ${feature::class} must be type ${feature.type} but was ${value::class}"
62
+        }
63
+
61 64
         features[feature] = value
62 65
     }
63 66
 
@@ -93,7 +96,7 @@ sealed class ServerFeature<T : Any>(val name: String, val type: KClass<T>, val d
93 96
     /** The maximum number of channels a client may join. */
94 97
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
95 98
     /** The modes supported in channels. */
96
-    object ChannelModes : ServerFeature<String>("CHANMODES", String::class)
99
+    object ChannelModes : ServerFeature<Array<String>>("CHANMODES", Array<String>::class)
97 100
     /** The maximum length of a channel name, defaulting to 200. */
98 101
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
99 102
     /** Whether or not the server supports extended who. */

+ 3
- 4
src/test/kotlin/com/dmdirc/ktirc/events/ServerStateHandlerTest.kt View File

@@ -6,8 +6,7 @@ import com.dmdirc.ktirc.model.*
6 6
 import com.nhaarman.mockitokotlin2.doReturn
7 7
 import com.nhaarman.mockitokotlin2.mock
8 8
 import kotlinx.coroutines.runBlocking
9
-import org.junit.jupiter.api.Assertions.assertEquals
10
-import org.junit.jupiter.api.Assertions.assertTrue
9
+import org.junit.jupiter.api.Assertions.*
11 10
 import org.junit.jupiter.api.Test
12 11
 
13 12
 internal class ServerStateHandlerTest {
@@ -97,12 +96,12 @@ internal class ServerStateHandlerTest {
97 96
     @Test
98 97
     fun `ServerStateHandler updates features on features event`() = runBlocking {
99 98
         val features = ServerFeatureMap()
100
-        features[ServerFeature.ChannelModes] = "abc"
99
+        features[ServerFeature.ChannelModes] = arrayOf("abc", "def")
101 100
         features[ServerFeature.WhoxSupport] = true
102 101
 
103 102
         handler.processEvent(ircClient, ServerFeaturesUpdated(TestConstants.time, features))
104 103
 
105
-        assertEquals("abc", serverState.features[ServerFeature.ChannelModes])
104
+        assertArrayEquals(arrayOf("abc", "def"), serverState.features[ServerFeature.ChannelModes])
106 105
         assertEquals(true, serverState.features[ServerFeature.WhoxSupport])
107 106
     }
108 107
 

+ 15
- 3
src/test/kotlin/com/dmdirc/ktirc/messages/ISupportProcessorTest.kt View File

@@ -29,9 +29,21 @@ internal class ISupportProcessorTest {
29 29
     @Test
30 30
     fun `ISupportProcessor handles string arguments`() {
31 31
         val events = processor.process(IrcMessage(emptyMap(), "server.com".toByteArray(), "005",
32
-                listOf("nickname", "CHANMODES=abcd", "are supported blah blah").map { it.toByteArray() }))
32
+                listOf("nickname", "NETWORK=abcd", "are supported blah blah").map { it.toByteArray() }))
33 33
 
34
-        assertEquals("abcd", events[0].serverFeatures[ServerFeature.ChannelModes])
34
+        assertEquals("abcd", events[0].serverFeatures[ServerFeature.Network])
35
+    }
36
+
37
+    @Test
38
+    fun `ISupportProcessor handles string array arguments`() {
39
+        val events = processor.process(IrcMessage(emptyMap(), "server.com".toByteArray(), "005",
40
+                listOf("nickname", "CHANMODES=abcd,efg,,hij", "are supported blah blah").map { it.toByteArray() }))
41
+
42
+        val modes = events[0].serverFeatures[ServerFeature.ChannelModes]!!
43
+        assertEquals("abcd", modes[0])
44
+        assertEquals("efg", modes[1])
45
+        assertEquals("", modes[2])
46
+        assertEquals("hij", modes[3])
35 47
     }
36 48
 
37 49
     @Test
@@ -40,7 +52,7 @@ internal class ISupportProcessorTest {
40 52
                 listOf("nickname", "-CHANMODES", "are supported blah blah").map { it.toByteArray() }))
41 53
 
42 54
         val oldFeatures = ServerFeatureMap()
43
-        oldFeatures[ServerFeature.ChannelModes] = "abc"
55
+        oldFeatures[ServerFeature.ChannelModes] = arrayOf("abc")
44 56
         oldFeatures.setAll(events[0].serverFeatures)
45 57
         assertNull(oldFeatures[ServerFeature.ChannelModes])
46 58
     }

+ 2
- 2
src/test/kotlin/com/dmdirc/ktirc/model/ServerFeatureMapTest.kt View File

@@ -46,11 +46,11 @@ internal class ServerFeatureMapTest {
46 46
         val featureMap1 = ServerFeatureMap()
47 47
         val featureMap2 = ServerFeatureMap()
48 48
         featureMap2[ServerFeature.WhoxSupport] = true
49
-        featureMap2[ServerFeature.ChannelModes] = "abc"
49
+        featureMap2[ServerFeature.ChannelModes] = arrayOf("abc", "def")
50 50
         featureMap1.setAll(featureMap2)
51 51
 
52 52
         assertEquals(true, featureMap1[ServerFeature.WhoxSupport])
53
-        assertEquals("abc", featureMap1[ServerFeature.ChannelModes])
53
+        assertArrayEquals(arrayOf("abc", "def"), featureMap1[ServerFeature.ChannelModes])
54 54
     }
55 55
 
56 56
 

Loading…
Cancel
Save