Skip to content

Commit

Permalink
Added another package that the bot can use and wrote a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Bragg committed Dec 25, 2024
1 parent 58a6e92 commit 18870c2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cmd/bot/bot.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
// Initializes and runs the Discord bot
package main

import "fmt"

import "rsc.io/quote"
import (
"fmt"
"log"
"github.com/faulteh/nap-and-go/pkg/greetings"
"rsc.io/quote"
)

func main() {
// Set properties of the predefined Logger, including
// the log entry prefix and a flag to disable printing
// the time, source file, and line number.
log.SetPrefix("bot: ")
log.SetFlags(0)

fmt.Println(quote.Go())

// A slice of names.
names := []string{"Ardenne", "Sam", "Liz"}

// Request greeting messages for the names.
messages, err := greetings.Hellos(names)

// If an error was returned, print it to the console and exit the program.
if err != nil {
log.Fatal(err)
}

fmt.Println(messages)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
rsc.io/sampler v1.3.0 // indirect
)

replace github.com/faulteh/nap-and-go/pkg => ./pkg
52 changes: 52 additions & 0 deletions pkg/greetings/greetings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Package greetings contains a single function, Hello, which takes a name as input and returns a greeting.
package greetings

import (
"errors"
"fmt"
"math/rand"
)

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
// If no name was given, return an error with a message.
if name == "" {
return "", errors.New("empty name")
}

// Return a greeting that embeds the name in a message.
message := fmt.Sprintf(randomFormat(), name)
return message, nil
}

// Hellos returns a map that associates each of the named people
// with a greeting message.
func Hellos(names []string) (map[string]string, error) {
// A map to associate names with messages.
messages := make(map[string]string)
// Loop through the received slice of names, calling
// the Hello function to get a message for each name.
for _, name := range names {
message, err := Hello(name)
if err != nil {
return nil, err
}
// In the map, associate the retrieved message with
// the name.
messages[name] = message
}
return messages, nil
}

func randomFormat() string {
// A slice of message formats.
formats := []string{
"Hi, %v. Welcome!",
"Great to see you, %v!",
"Hail, %v! Well met!",
}

// Return a randomly selected message format by specifying
// a random index for the slice of formats.
return formats[rand.Intn(len(formats))]
}
26 changes: 26 additions & 0 deletions pkg/greetings/greetings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package greetings

import (
"testing"
"regexp"
)

// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestHelloName(t *testing.T) {
name := "Ardenne"
want := regexp.MustCompile(`\b` + name + `\b`)
msg, err := Hello("Ardenne")
if !want.MatchString(msg) || err != nil {
t.Fatalf(`Hello("Ardenne") = %q, %v, want match for %#q, nil`, msg, err, want)
}
}

// TestHelloEmpty calls greetings.Hello with an empty string,
// checking for an error.
func TestHelloEmpty(t *testing.T) {
msg, err := Hello("")
if msg != "" || err == nil {
t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
}
}

0 comments on commit 18870c2

Please sign in to comment.