bot: Fixed handling of action messages, and added NPC support.
the build was successful

This commit is contained in:
2018-11-25 19:36:45 +01:00
parent 9a0292c2fa
commit e949a980fb
3 changed files with 90 additions and 12 deletions
+23
View File
@@ -0,0 +1,23 @@
package util
import (
"strings"
)
// RemoveNPCAttribution removes NPC attribution.
func RemoveNPCAttribution(str string) string {
lastOpen := strings.LastIndex(str, "(")
lastClose := strings.LastIndex(str, ")")
ending := len(str) - 1
if lastOpen > 0 && str[lastOpen-1] != '(' && (lastClose == -1 || lastClose == ending) {
cutoff := lastOpen
for cutoff > 1 && str[cutoff-1] == ' ' {
cutoff--
}
return str[:cutoff]
}
return str
}
+44
View File
@@ -0,0 +1,44 @@
package util_test
import (
"fmt"
"testing"
"git.aiterp.net/rpdata/logbot3/internal/util"
)
func TestRemoveNPCAttribution(t *testing.T) {
table := []struct {
Input string
Output string
}{
{
Input: "Character does stuff. (Player_Name)",
Output: "Character does stuff.",
},
{
Input: "Text (with some clarification) about stuff.",
Output: "Text (with some clarification) about stuff.",
},
{
Input: "Text without anything.",
Output: "Text without anything.",
},
{
Input: "((*Correction))",
Output: "((*Correction))",
},
}
for i, row := range table {
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
result := util.RemoveNPCAttribution(row.Input)
if result != row.Output {
t.Errorf("Row %d failed", i)
t.Errorf("Expected: %#+v", row.Output)
t.Errorf("Result: %#+v", result)
}
})
}
}