-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added another package that the bot can use and wrote a test
- Loading branch information
Scott Bragg
committed
Dec 25, 2024
1 parent
58a6e92
commit 18870c2
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |