Improve docs and coverage of GenerateQuote
MODIFY
discordutil/message.go
discordutil/message.go
@@ -125,18 +125,19 @@ func SendMessageAsFile(session *discordgo.Session, message string, channel strin
}
}
-// Generates a Quote using the given Input. The `messageAfterQuote` will be
-// appended after the quote in case it is not empty.
+// GenerateQuote formats a message quote using the given Input. The
+// `messageAfterQuote` will be appended after the quote in case it is not
+// empty.
func GenerateQuote(message, author string, time discordgo.Timestamp, attachments []*discordgo.MessageAttachment, messageAfterQuote string) (string, error) {
messageTime, parseError := time.Parse()
if parseError != nil {
return "", parseError
}
- // All quotes should be UTC.
+ // All quotes should be UTC in order to not confuse quote-readers.
messageTimeUTC := messageTime.UTC()
-
quotedMessage := strings.ReplaceAll(message, "\n", "\n> ")
+
if len(attachments) > 0 {
var attachmentsAsText string
for index, attachment := range attachments {
@@ -147,17 +148,17 @@ func GenerateQuote(message, author string, time discordgo.Timestamp, attachments
}
}
+ //If the quoted message ends with a "useless" quote-line-prefix
+ //we simply "reuse" that line to not add unnecessary newlines.
if strings.HasSuffix(quotedMessage, "> ") {
quotedMessage = quotedMessage + attachmentsAsText
} else {
quotedMessage = quotedMessage + "\n> " + attachmentsAsText
}
}
- quotedMessage = fmt.Sprintf("> **%s** %s UTC:\n> %s\n", author, times.TimeToString(&messageTimeUTC), quotedMessage)
- currentContent := strings.TrimSpace(messageAfterQuote)
- if currentContent != "" {
- quotedMessage = quotedMessage + currentContent
- }
- return quotedMessage, nil
+ return fmt.Sprintf("> **%s** %s UTC:\n> %s\n%s", author,
+ times.TimeToString(&messageTimeUTC), quotedMessage,
+ strings.TrimSpace(messageAfterQuote)),
+ nil
}

MODIFY
discordutil/message_test.go
discordutil/message_test.go
@@ -200,6 +200,7 @@ func TestGenerateQuote(t *testing.T) {
message string
author string
time discordgo.Timestamp
+ attachments []*discordgo.MessageAttachment
messageAfterQuote string
}
tests := []struct {
@@ -318,17 +319,55 @@ func TestGenerateQuote(t *testing.T) {
},
want: "",
wantErr: true,
+ }, {
+ name: "single line plus single attachment, no sender message",
+ args: args{
+ message: "f",
+ author: "author",
+ attachments: []*discordgo.MessageAttachment{
+ {
+ URL: "https://download/this.zip",
+ },
+ },
+ time: discordgo.Timestamp("2019-10-28T21:30:57.003000+00:00"),
+ messageAfterQuote: "",
+ },
+ want: "> **author** 21:30:57 UTC:\n" +
+ "> f\n" +
+ "> https://download/this.zip\n",
+ wantErr: false,
+ }, {
+ name: "single line plus two attachment, no sender message",
+ args: args{
+ message: "f",
+ author: "author",
+ attachments: []*discordgo.MessageAttachment{
+ {
+ URL: "https://download/this.zip",
+ },
+ {
+ URL: "https://download/thistoo.zip",
+ },
+ },
+ time: discordgo.Timestamp("2019-10-28T21:30:57.003000+00:00"),
+ messageAfterQuote: "",
+ },
+ want: "> **author** 21:30:57 UTC:\n" +
+ "> f\n" +
+ "> https://download/this.zip\n" +
+ "> https://download/thistoo.zip\n",
+ wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got, err := GenerateQuote(tt.args.message, tt.args.author, tt.args.time, nil, tt.args.messageAfterQuote)
+ got, err := GenerateQuote(tt.args.message, tt.args.author, tt.args.time, tt.args.attachments, tt.args.messageAfterQuote)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateQuote() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
- t.Errorf("GenerateQuote() got = %v, want %v", got, tt.want)
+ t.Errorf("GenerateQuote() got = '%v', want '%v'", got, tt.want)
}
})
}

Improve docs and coverage of GenerateQuote