Added my first golang test. Moved func out of interface value for easier testing.
This commit is contained in:
@@ -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
|
||||
|
||||
26
bridge/slack/helpers_test.go
Normal file
26
bridge/slack/helpers_test.go
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user