Skip to content

Commit

Permalink
Some unit testing in ircclient package
Browse files Browse the repository at this point in the history
  • Loading branch information
cfindlayisme committed May 24, 2024
1 parent 68623c2 commit d32dc47
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ircclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Loop() {
words := strings.Split(message, " ")

if strings.HasPrefix(message, "PING") {
returnPong(message)
ReturnPong(ircConnection, message)
} else if len(words) >= 2 && words[1] == "PRIVMSG" {
processPrivmsg(words)
} else {
Expand Down
6 changes: 3 additions & 3 deletions ircclient/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func SetMode(channel string, mode string) error {
}

func SetTopic(channel string, topic string) error {
cleanTopic := cleanMessage(topic)
cleanTopic := CleanMessage(topic)
_, err := fmt.Fprintf(ircConnection, "TOPIC "+channel+" "+cleanTopic+"\r\n")
return err
}
Expand All @@ -49,14 +49,14 @@ func Quote(command string) error {
}

func SendMessage(target string, message string) error {
ircMessage := cleanMessage(message)
ircMessage := CleanMessage(message)

_, err := fmt.Fprintf(ircConnection, "PRIVMSG "+target+" :"+ircMessage+"\r\n")
return err
}

func SendNotice(target string, message string) error {
ircMessage := cleanMessage(message)
ircMessage := CleanMessage(message)

_, err := fmt.Fprintf(ircConnection, "NOTICE "+target+" :"+ircMessage+"\r\n")
return err
Expand Down
7 changes: 4 additions & 3 deletions ircclient/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package ircclient
import (
"fmt"
"log"
"net"
"strings"

"github.com/cfindlayisme/wmb/model"
"github.com/cfindlayisme/wmb/webhook"
)

func returnPong(message string) {
func ReturnPong(connection net.Conn, message string) {
pongMessage := strings.Replace(message, "PING", "PONG", 1)
fmt.Fprintf(ircConnection, pongMessage+"\r\n")
fmt.Fprintf(connection, pongMessage+"\r\n")
log.Println("PONG returned to server PING")
}

func cleanMessage(message string) string {
func CleanMessage(message string) string {
// Strip newlines to prevent chaining of commands, ie, QUIT to the end
message = strings.ReplaceAll(message, "\n", "")
message = strings.ReplaceAll(message, "\r", "")
Expand Down
44 changes: 44 additions & 0 deletions ircclient/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ircclient_test

import (
"bufio"
"net"
"strings"
"testing"

"github.com/cfindlayisme/wmb/ircclient"
"github.com/stretchr/testify/require"
)

func TestReturnPong(t *testing.T) {
// Create a pair of connected, in-memory network connections
conn1, conn2 := net.Pipe()

// Call the function in a goroutine, because it will block until conn2 is read
go ircclient.ReturnPong(conn1, "PING :tmi.twitch.tv")

// Read the data from conn2
reader := bufio.NewReader(conn2)
data, err := reader.ReadString('\n')
require.NoError(t, err, "Error reading from connection")

// Remove the trailing newline
data = strings.TrimSuffix(data, "\n")

// Check if the data is as expected
require.Equal(t, "PONG :tmi.twitch.tv\r", data, "The data should be 'PONG :tmi.twitch.tv'")
}

func TestCleanMessage(t *testing.T) {
// Test case with newlines and carriage returns
input := "Hello\nWorld\r\n"
expected := "HelloWorld"
result := ircclient.CleanMessage(input)
require.Equal(t, expected, result, "The message should be cleaned")

// Test case with no newlines or carriage returns
input = "Hello World"
expected = "Hello World"
result = ircclient.CleanMessage(input)
require.Equal(t, expected, result, "The message should be unchanged")
}

0 comments on commit d32dc47

Please sign in to comment.