Explorar el Código

Make sure ISUPPORT outputs are sorted and tested

tags/v0.9.0
Daniel Oaks hace 7 años
padre
commit
ffabd26653
Se han modificado 2 ficheros con 70 adiciones y 21 borrados
  1. 24
    21
      irc/isupport.go
  2. 46
    0
      irc/isupport_test.go

+ 24
- 21
irc/isupport.go Ver fichero

@@ -4,6 +4,7 @@
4 4
 package irc
5 5
 
6 6
 import "fmt"
7
+import "sort"
7 8
 
8 9
 const isupportSupportedString = "are supported by this server"
9 10
 
@@ -41,9 +42,7 @@ func getTokenString(name string, value *string) string {
41 42
 
42 43
 // GetDifference returns the difference between two token lists.
43 44
 func (il *ISupportList) GetDifference(newil *ISupportList) [][]string {
44
-	replies := make([][]string, 0)
45
-	var length int     // Length of the current cache
46
-	var cache []string // Token list cache
45
+	var outTokens sort.StringSlice
47 46
 
48 47
 	// append removed tokens
49 48
 	for name := range il.Tokens {
@@ -54,32 +53,29 @@ func (il *ISupportList) GetDifference(newil *ISupportList) [][]string {
54 53
 
55 54
 		token := fmt.Sprintf("-%s", name)
56 55
 
57
-		if len(token)+length <= maxLastArgLength {
58
-			// account for the space separating tokens
59
-			if len(cache) > 0 {
60
-				length++
61
-			}
62
-			cache = append(cache, token)
63
-			length += len(token)
64
-		}
65
-
66
-		if len(cache) == 13 || len(token)+length >= maxLastArgLength {
67
-			cache = append(cache, isupportSupportedString)
68
-			replies = append(replies, cache)
69
-			cache = make([]string, 0)
70
-			length = 0
71
-		}
56
+		outTokens = append(outTokens, token)
72 57
 	}
73 58
 
74 59
 	// append added tokens
75 60
 	for name, value := range newil.Tokens {
76 61
 		newval, exists := il.Tokens[name]
77
-		if exists && *value == *newval {
62
+		if exists && ((value == nil && newval == nil) || (value != nil && newval != nil && *value == *newval)) {
78 63
 			continue
79 64
 		}
80 65
 
81 66
 		token := getTokenString(name, value)
82 67
 
68
+		outTokens = append(outTokens, token)
69
+	}
70
+
71
+	sort.Sort(outTokens)
72
+
73
+	// create output list
74
+	replies := make([][]string, 0)
75
+	var length int     // Length of the current cache
76
+	var cache []string // Token list cache
77
+
78
+	for _, token := range outTokens {
83 79
 		if len(token)+length <= maxLastArgLength {
84 80
 			// account for the space separating tokens
85 81
 			if len(cache) > 0 {
@@ -111,8 +107,15 @@ func (il *ISupportList) RegenerateCachedReply() {
111 107
 	var length int     // Length of the current cache
112 108
 	var cache []string // Token list cache
113 109
 
114
-	for name, value := range il.Tokens {
115
-		token := getTokenString(name, value)
110
+	// make sure we get a sorted list of tokens, needed for tests and looks nice
111
+	var tokens sort.StringSlice
112
+	for name := range il.Tokens {
113
+		tokens = append(tokens, name)
114
+	}
115
+	sort.Sort(tokens)
116
+
117
+	for _, name := range tokens {
118
+		token := getTokenString(name, il.Tokens[name])
116 119
 
117 120
 		if len(token)+length <= maxLastArgLength {
118 121
 			// account for the space separating tokens

+ 46
- 0
irc/isupport_test.go Ver fichero

@@ -0,0 +1,46 @@
1
+// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+package irc
5
+
6
+import (
7
+	"reflect"
8
+	"testing"
9
+)
10
+
11
+func TestISUPPORT(t *testing.T) {
12
+	// create first list
13
+	tList1 := NewISupportList()
14
+	tList1.Add("SASL", "yes")
15
+	tList1.Add("CASEMAPPING", "rfc1459-strict")
16
+	tList1.Add("INVEX", "i")
17
+	tList1.AddNoValue("EXTBAN")
18
+	tList1.Add("RANDKILL", "whenever")
19
+	tList1.RegenerateCachedReply()
20
+
21
+	expected := [][]string{{"CASEMAPPING=rfc1459-strict", "EXTBAN", "INVEX=i", "RANDKILL=whenever", "SASL=yes", "are supported by this server"}}
22
+	if !reflect.DeepEqual(tList1.CachedReply, expected) {
23
+		t.Error("tList1's cached reply does not match expected cached reply")
24
+	}
25
+
26
+	// create second list
27
+	tList2 := NewISupportList()
28
+	tList2.Add("SASL", "yes")
29
+	tList2.Add("CASEMAPPING", "ascii")
30
+	tList2.AddNoValue("INVEX")
31
+	tList2.Add("EXTBAN", "TestBah")
32
+	tList2.AddNoValue("STABLEKILL")
33
+	tList2.RegenerateCachedReply()
34
+
35
+	expected = [][]string{{"CASEMAPPING=ascii", "EXTBAN=TestBah", "INVEX", "SASL=yes", "STABLEKILL", "are supported by this server"}}
36
+	if !reflect.DeepEqual(tList2.CachedReply, expected) {
37
+		t.Error("tList2's cached reply does not match expected cached reply")
38
+	}
39
+
40
+	// compare lists
41
+	actual := tList1.GetDifference(tList2)
42
+	expected = [][]string{{"-RANDKILL", "CASEMAPPING=ascii", "EXTBAN=TestBah", "INVEX", "STABLEKILL", "are supported by this server"}}
43
+	if !reflect.DeepEqual(actual, expected) {
44
+		t.Error("difference reply does not match expected difference reply")
45
+	}
46
+}

Loading…
Cancelar
Guardar