fix: computeTrend division by zero, add tests for trend and currency parsing, add username to settings
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -170,4 +171,57 @@ func TestSanitizeDisplayName(t *testing.T) {
|
||||
if got := SanitizeDisplayName("Ali\x00ce"); got != "Alice" {
|
||||
t.Errorf("SanitizeDisplayName control: got %q", got)
|
||||
}
|
||||
if got := SanitizeDisplayName(""); got != "" {
|
||||
t.Errorf("SanitizeDisplayName empty: got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCurrencyInput(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
want string
|
||||
ok bool
|
||||
}{
|
||||
{"$ USD", "$ USD", true},
|
||||
{"T Toman", "T Toman", true},
|
||||
{"IRR Rial", "IRR Rial", true},
|
||||
{"€ EUR", "€ EUR", true},
|
||||
{"", "", false},
|
||||
{"$", "", false},
|
||||
{" ", "", false},
|
||||
{"$$$ TooLongCode", "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, errMsg := parseCurrencyInput(c.input)
|
||||
if c.ok && (got != c.want || errMsg != "") {
|
||||
t.Errorf("parseCurrencyInput(%q) = (%q, %q), want (%q, \"\")", c.input, got, errMsg, c.want)
|
||||
}
|
||||
if !c.ok && errMsg == "" {
|
||||
t.Errorf("parseCurrencyInput(%q) = (%q, \"\"), want error", c.input, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeTrendFromAvgs(t *testing.T) {
|
||||
cases := []struct {
|
||||
recent, older float64
|
||||
wantPts int
|
||||
wantDirSub string
|
||||
}{
|
||||
{0, 0, 5, "stable"},
|
||||
{100, 0, 15, "new"},
|
||||
{0, 100, 0, "-100"},
|
||||
{121, 100, 15, "+21"},
|
||||
{100, 100, 5, "stable"},
|
||||
{100, 130, 0, "-23"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
pts, dir := computeTrendFromAvgs(c.recent, c.older)
|
||||
if pts != c.wantPts {
|
||||
t.Errorf("computeTrendFromAvgs(%.0f, %.0f) pts = %d, want %d", c.recent, c.older, pts, c.wantPts)
|
||||
}
|
||||
if !strings.Contains(dir, c.wantDirSub) {
|
||||
t.Errorf("computeTrendFromAvgs(%.0f, %.0f) dir = %q, want containing %q", c.recent, c.older, dir, c.wantDirSub)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user