2017-07-26 08:02:35 +02:00
|
|
|
|
// Copyright (c) 2017 Euan Kemp
|
2017-08-17 10:23:24 +02:00
|
|
|
|
// Copyright (c) 2017 Daniel Oaks
|
2017-07-26 08:02:35 +02:00
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestCasefoldChannel(t *testing.T) {
|
|
|
|
|
type channelTest struct {
|
|
|
|
|
channel string
|
|
|
|
|
folded string
|
|
|
|
|
err bool
|
|
|
|
|
}
|
|
|
|
|
testCases := []channelTest{
|
|
|
|
|
{
|
|
|
|
|
channel: "#foo",
|
|
|
|
|
folded: "#foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#rfc1459[noncompliant]",
|
|
|
|
|
folded: "#rfc1459[noncompliant]",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#{[]}",
|
|
|
|
|
folded: "#{[]}",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#FOO",
|
|
|
|
|
folded: "#foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#bang!",
|
|
|
|
|
folded: "#bang!",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "#",
|
|
|
|
|
folded: "#",
|
|
|
|
|
},
|
2018-12-06 12:46:11 +01:00
|
|
|
|
{
|
|
|
|
|
channel: "##",
|
|
|
|
|
folded: "##",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
channel: "##Ubuntu",
|
|
|
|
|
folded: "##ubuntu",
|
|
|
|
|
},
|
2018-12-06 04:35:36 +01:00
|
|
|
|
{
|
|
|
|
|
channel: "#中文频道",
|
|
|
|
|
folded: "#中文频道",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// Hebrew; it's up to the client to display this right-to-left, including the #
|
|
|
|
|
channel: "#שלום",
|
|
|
|
|
folded: "#שלום",
|
|
|
|
|
},
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, errCase := range []string{
|
|
|
|
|
"", "#*starpower", "# NASA", "#interro?", "OOF#", "foo",
|
2018-12-06 04:35:36 +01:00
|
|
|
|
// bidi violation mixing latin and hebrew characters:
|
|
|
|
|
"#shalomעליכם",
|
2017-07-26 08:02:35 +02:00
|
|
|
|
} {
|
|
|
|
|
testCases = append(testCases, channelTest{channel: errCase, err: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, tt := range testCases {
|
|
|
|
|
t.Run(fmt.Sprintf("case %d: %s", i, tt.channel), func(t *testing.T) {
|
|
|
|
|
res, err := CasefoldChannel(tt.channel)
|
2017-08-17 10:23:24 +02:00
|
|
|
|
if tt.err && err == nil {
|
|
|
|
|
t.Errorf("expected error when casefolding [%s], but did not receive one", tt.channel)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !tt.err && err != nil {
|
|
|
|
|
t.Errorf("unexpected error while casefolding [%s]: %s", tt.channel, err.Error())
|
2017-07-26 08:02:35 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if tt.folded != res {
|
2017-08-17 10:23:24 +02:00
|
|
|
|
t.Errorf("expected [%v] to be [%v]", res, tt.folded)
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCasefoldName(t *testing.T) {
|
|
|
|
|
type nameTest struct {
|
|
|
|
|
name string
|
|
|
|
|
folded string
|
|
|
|
|
err bool
|
|
|
|
|
}
|
|
|
|
|
testCases := []nameTest{
|
|
|
|
|
{
|
|
|
|
|
name: "foo",
|
|
|
|
|
folded: "foo",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "FOO",
|
|
|
|
|
folded: "foo",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, errCase := range []string{
|
|
|
|
|
"", "#", "foo,bar", "star*man*junior", "lo7t?",
|
|
|
|
|
"f.l", "excited!nick", "foo@bar", ":trail",
|
|
|
|
|
"~o", "&o", "@o", "%h", "+v", "-m",
|
|
|
|
|
} {
|
|
|
|
|
testCases = append(testCases, nameTest{name: errCase, err: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, tt := range testCases {
|
|
|
|
|
t.Run(fmt.Sprintf("case %d: %s", i, tt.name), func(t *testing.T) {
|
|
|
|
|
res, err := CasefoldName(tt.name)
|
2017-08-17 10:23:24 +02:00
|
|
|
|
if tt.err && err == nil {
|
|
|
|
|
t.Errorf("expected error when casefolding [%s], but did not receive one", tt.name)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !tt.err && err != nil {
|
|
|
|
|
t.Errorf("unexpected error while casefolding [%s]: %s", tt.name, err.Error())
|
2017-07-26 08:02:35 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if tt.folded != res {
|
2017-08-17 10:23:24 +02:00
|
|
|
|
t.Errorf("expected [%v] to be [%v]", res, tt.folded)
|
2017-07-26 08:02:35 +02:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|