Added my first golang test. Moved func out of interface value for easier testing.

This commit is contained in:
Patrick Connolly
2018-11-15 22:28:12 +08:00
parent 5605032873
commit 762aa3a41c
2 changed files with 36 additions and 8 deletions

View File

@@ -269,16 +269,18 @@ var (
topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`)
)
func (b *Bslack) extractTopicOrPurpose(text string) (string, string) {
func extractTopicOrPurpose(text string) (string, string) {
r := topicOrPurposeRE.FindStringSubmatch(text)
action, updateType, extracted := r[2], r[3], r[4]
switch action {
case "set":
return updateType, extracted
case "cleared":
return updateType, ""
if len(r) >= 5 {
action, updateType, extracted := r[2], r[3], r[4]
switch action {
case "set":
return updateType, extracted
case "cleared":
return updateType, ""
}
}
return "", ""
return "unknown", ""
}
// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users

View File

@@ -0,0 +1,26 @@
package bslack
import (
"testing"
)
func TestExtractTopicOrPurpose(t *testing.T) {
tables := []struct{
input string
changeType string
output string
}{
{"@someone set channel topic: one liner", "topic", "one liner"},
{"@someone set channel purpose: one liner", "purpose", "one liner"},
{"@someone set channel topic: multi\nliner", "topic", "multi\nliner"},
{"@someone cleared channel topic", "topic", ""},
{"some unmatched message", "unknown", ""},
}
for _, table := range tables {
changeType, output := extractTopicOrPurpose(table.input)
if changeType != table.changeType || output != table.output {
t.Error()
}
}
}