Przeglądaj źródła

Store channel modes a bit nicer.

Groundwork for #5
tags/v0.5.0
Chris Smith 5 lat temu
rodzic
commit
bbef1acb43

+ 2
- 0
CHANGELOG Wyświetl plik

5
  * Improved some documentation
5
  * Improved some documentation
6
  * Added ServerConnecting and ServerDisconnected events
6
  * Added ServerConnecting and ServerDisconnected events
7
  * Server status now starts as Disconnected rather than Connecting
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
 v0.4.0
11
 v0.4.0
10
 
12
 

+ 1
- 0
src/main/kotlin/com/dmdirc/ktirc/messages/ISupportProcessor.kt Wyświetl plik

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

+ 5
- 2
src/main/kotlin/com/dmdirc/ktirc/model/ServerState.kt Wyświetl plik

57
     operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
57
     operator fun <T : Any> get(feature: ServerFeature<T>) = features.getOrDefault(feature, feature.default) as? T? ?: feature.default
58
 
58
 
59
     internal operator fun set(feature: ServerFeature<*>, value: Any) {
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
         features[feature] = value
64
         features[feature] = value
62
     }
65
     }
63
 
66
 
93
     /** The maximum number of channels a client may join. */
96
     /** The maximum number of channels a client may join. */
94
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
97
     object MaximumChannels : ServerFeature<Int>("MAXCHANNELS", Int::class) // TODO: CHANLIMIT also exists
95
     /** The modes supported in channels. */
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
     /** The maximum length of a channel name, defaulting to 200. */
100
     /** The maximum length of a channel name, defaulting to 200. */
98
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
101
     object MaximumChannelNameLength : ServerFeature<Int>("CHANNELLEN", Int::class, 200)
99
     /** Whether or not the server supports extended who. */
102
     /** Whether or not the server supports extended who. */

+ 3
- 4
src/test/kotlin/com/dmdirc/ktirc/events/ServerStateHandlerTest.kt Wyświetl plik

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

+ 15
- 3
src/test/kotlin/com/dmdirc/ktirc/messages/ISupportProcessorTest.kt Wyświetl plik

29
     @Test
29
     @Test
30
     fun `ISupportProcessor handles string arguments`() {
30
     fun `ISupportProcessor handles string arguments`() {
31
         val events = processor.process(IrcMessage(emptyMap(), "server.com".toByteArray(), "005",
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
     @Test
49
     @Test
40
                 listOf("nickname", "-CHANMODES", "are supported blah blah").map { it.toByteArray() }))
52
                 listOf("nickname", "-CHANMODES", "are supported blah blah").map { it.toByteArray() }))
41
 
53
 
42
         val oldFeatures = ServerFeatureMap()
54
         val oldFeatures = ServerFeatureMap()
43
-        oldFeatures[ServerFeature.ChannelModes] = "abc"
55
+        oldFeatures[ServerFeature.ChannelModes] = arrayOf("abc")
44
         oldFeatures.setAll(events[0].serverFeatures)
56
         oldFeatures.setAll(events[0].serverFeatures)
45
         assertNull(oldFeatures[ServerFeature.ChannelModes])
57
         assertNull(oldFeatures[ServerFeature.ChannelModes])
46
     }
58
     }

+ 2
- 2
src/test/kotlin/com/dmdirc/ktirc/model/ServerFeatureMapTest.kt Wyświetl plik

46
         val featureMap1 = ServerFeatureMap()
46
         val featureMap1 = ServerFeatureMap()
47
         val featureMap2 = ServerFeatureMap()
47
         val featureMap2 = ServerFeatureMap()
48
         featureMap2[ServerFeature.WhoxSupport] = true
48
         featureMap2[ServerFeature.WhoxSupport] = true
49
-        featureMap2[ServerFeature.ChannelModes] = "abc"
49
+        featureMap2[ServerFeature.ChannelModes] = arrayOf("abc", "def")
50
         featureMap1.setAll(featureMap2)
50
         featureMap1.setAll(featureMap2)
51
 
51
 
52
         assertEquals(true, featureMap1[ServerFeature.WhoxSupport])
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
 

Ładowanie…
Anuluj
Zapisz