/timestamp, /theme: Fix rendering, add tests
authorAndrey Petrov <andrey.petrov@shazow.net>
Fri, 22 Mar 2019 19:27:00 +0000 (15:27 -0400)
committerAndrey Petrov <andrey.petrov@shazow.net>
Fri, 22 Mar 2019 19:27:00 +0000 (15:27 -0400)
chat/message/theme.go
chat/message/user.go
chat/message/user_test.go

index 54886a4f36e390d5ef8a5b876cdf7320f1d4e3d0..37eb7246d53bb41b6ccdf7dbfc66bd7e81354e90 100644 (file)
@@ -174,6 +174,9 @@ var Themes []Theme
 // Default theme to use
 var DefaultTheme *Theme
 
+// MonoTheme is a simple theme without colors, useful for testing and bots.
+var MonoTheme *Theme
+
 func allColors256() *Palette {
        colors := []uint8{}
        var i uint8
@@ -225,6 +228,7 @@ func init() {
        }
 
        DefaultTheme = &Themes[0]
+       MonoTheme = &Themes[3]
 
        /* Some debug helpers for your convenience:
 
index faea90c11e926467b936a4d6db5e68a9d44bb9c1..3bfca1be9fe870d2d7cc68b126379bef1beb8361 100644 (file)
@@ -182,7 +182,7 @@ func (u *User) render(m Message) string {
                } else {
                        ts = ts.UTC()
                }
-               return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat) + "  " + out + Newline)
+               return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat)) + "  " + out + Newline
        }
        return out + Newline
 }
@@ -238,6 +238,7 @@ func init() {
        DefaultUserConfig = UserConfig{
                Bell:  true,
                Quiet: false,
+               Theme: DefaultTheme,
        }
 
        // TODO: Seed random?
index f70e6747ea02f50d05931f861fef376e83981ff2..de8b6dcf33c89a96abdc7c0dbdf6a7b092301591 100644 (file)
@@ -1,6 +1,7 @@
 package message
 
 import (
+       "math/rand"
        "reflect"
        "testing"
 )
@@ -10,6 +11,11 @@ func TestMakeUser(t *testing.T) {
 
        s := &MockScreen{}
        u := NewUserScreen(SimpleID("foo"), s)
+
+       cfg := u.Config()
+       cfg.Theme = MonoTheme // Mono
+       u.SetConfig(cfg)
+
        m := NewAnnounceMsg("hello")
 
        defer u.Close()
@@ -22,3 +28,33 @@ func TestMakeUser(t *testing.T) {
                t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
        }
 }
+
+func TestRenderTimestamp(t *testing.T) {
+       var actual, expected []byte
+
+       // Reset seed for username color
+       rand.Seed(1)
+       s := &MockScreen{}
+       u := NewUserScreen(SimpleID("foo"), s)
+
+       cfg := u.Config()
+       timefmt := "AA:BB"
+       cfg.Timeformat = &timefmt
+       u.SetConfig(cfg)
+
+       if got, want := cfg.Theme.Timestamp("foo"), `\e[38;05;245mfoo`+Reset; got != want {
+               t.Errorf("Wrong timestamp formatting:\n got: %q\nwant: %q", got, want)
+       }
+
+       m := NewPublicMsg("hello", u)
+
+       defer u.Close()
+       u.Send(m)
+       u.HandleMsg(u.ConsumeOne())
+
+       s.Read(&actual)
+       expected = []byte(`\e[38;05;245mAA:BB` + Reset + `  [\e[38;05;88mfoo\e[0m] hello` + Newline)
+       if !reflect.DeepEqual(actual, expected) {
+               t.Errorf("Wrong screen output:\n Got: `%q`;\nWant: `%q`", actual, expected)
+       }
+}