This repository has been archived on 2025-03-19. You can view files and clone it, but cannot push or open issues or pull requests.
gowaybarplug/updater_test.go
2024-06-11 15:30:57 -06:00

43 lines
749 B
Go

package gowaybarplug
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type ChBuf struct {
Ch chan bool
Buff bytes.Buffer
}
func NewChBuf() *ChBuf {
return &ChBuf{
Ch: make(chan bool, 100),
}
}
func (c *ChBuf) Write(data []byte) (int, error) {
n, err := c.Buff.Write(data)
c.Ch <- err == nil
return n, err
}
func (c *ChBuf) WaitForBuffer(deadline time.Duration) {
select {
case <-c.Ch:
case <-time.After(deadline):
}
}
func TestNewUpdater(t *testing.T) {
buff := NewChBuf()
u := NewUpdater().OutputTo(buff)
u.Status <- &Status{Text: "test"}
buff.WaitForBuffer(2 * time.Second)
result, err := buff.Buff.ReadString('\n')
assert.NoError(t, err)
assert.Equal(t, result, "{\"text\":\"test\"}\n")
}