Added more test utilities for client_test.go, added NAMES and MODE handling for channels

This commit is contained in:
2018-06-25 10:58:14 +02:00
parent 65e4385b71
commit 6256f6a6fe
6 changed files with 237 additions and 50 deletions
+32
View File
@@ -0,0 +1,32 @@
package irctest
import (
"errors"
"strings"
"testing"
"git.aiterp.net/gisle/irc"
)
// AssertUserlist compares the userlist to a list of prefixed nicks
func AssertUserlist(t *testing.T, channel *irc.Channel, assertedOrder ...string) error {
users := channel.UserList().Users()
order := make([]string, 0, len(users))
for _, user := range users {
order = append(order, user.PrefixedNick)
}
orderA := strings.Join(order, ", ")
orderB := strings.Join(assertedOrder, ", ")
if orderA != orderB {
t.Logf("Userlist: %s", orderA)
t.Logf("Asserted: %s", orderB)
t.Fail()
return errors.New("Userlists does not match")
}
return nil
}
+57 -43
View File
@@ -49,52 +49,64 @@ func (interaction *Interaction) Listen() (addr string, err error) {
for i := 0; i < len(lines); i++ {
line := lines[i]
switch line.Kind {
case 'S':
{
_, err := conn.Write(append([]byte(line.Data), '\r', '\n'))
if err != nil {
interaction.Failure = &InteractionFailure{
Index: i, NetErr: err,
if line.Data != "" {
switch line.Kind {
case 'S':
{
_, err := conn.Write(append([]byte(line.Data), '\r', '\n'))
if err != nil {
interaction.Failure = &InteractionFailure{
Index: i, NetErr: err,
}
return
}
}
case 'C':
{
conn.SetReadDeadline(time.Now().Add(time.Second))
input, err := reader.ReadString('\n')
if err != nil {
interaction.Failure = &InteractionFailure{
Index: i, NetErr: err,
}
return
}
input = strings.Replace(input, "\r", "", -1)
input = strings.Replace(input, "\n", "", 1)
match := line.Data
success := false
if strings.HasSuffix(match, "*") {
success = strings.HasPrefix(input, match[:len(match)-1])
} else {
success = match == input
}
interaction.Log = append(interaction.Log, input)
if !success {
if !interaction.Strict {
i--
continue
}
interaction.Failure = &InteractionFailure{
Index: i, Result: input,
}
return
}
return
}
}
case 'C':
{
conn.SetReadDeadline(time.Now().Add(time.Second))
input, err := reader.ReadString('\n')
if err != nil {
interaction.Failure = &InteractionFailure{
Index: i, NetErr: err,
}
return
}
input = strings.Replace(input, "\r", "", -1)
input = strings.Replace(input, "\n", "", 1)
}
match := line.Data
success := false
if strings.HasSuffix(match, "*") {
success = strings.HasPrefix(input, match[:len(match)-1])
} else {
success = match == input
}
interaction.Log = append(interaction.Log, input)
if !success {
if !interaction.Strict {
i--
continue
}
interaction.Failure = &InteractionFailure{
Index: i, Result: input,
}
return
if line.Callback != nil {
err := line.Callback()
if err != nil {
interaction.Failure = &InteractionFailure{
Index: i, CBErr: err,
}
return
}
}
}
@@ -114,11 +126,13 @@ type InteractionFailure struct {
Index int
Result string
NetErr error
CBErr error
}
// InteractionLine is part of an interaction, whether it is a line
// that is sent to a client or a line expected from a client.
type InteractionLine struct {
Kind byte
Data string
Kind byte
Data string
Callback func() error
}