mirror of
https://codeberg.org/scip/yadu.git
synced 2025-12-16 20:21:00 +01:00
187 lines
3.0 KiB
Go
187 lines
3.0 KiB
Go
package yadu_test
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/tlinden/yadu"
|
|
)
|
|
|
|
type body string
|
|
|
|
type Ammo struct {
|
|
Forweapon string
|
|
Impact int
|
|
Cost int
|
|
Range float32
|
|
}
|
|
|
|
type Enemy struct {
|
|
Alive bool
|
|
Health int
|
|
Name string
|
|
Body body `yaml:"-"`
|
|
Ammo []Ammo
|
|
}
|
|
|
|
type Tests struct {
|
|
name string
|
|
want string
|
|
negate bool
|
|
opts yadu.Options
|
|
with slog.Attr
|
|
haswith bool
|
|
}
|
|
|
|
const testTimeFormat = "03:04.05"
|
|
|
|
var tests = []Tests{
|
|
{
|
|
name: "has-railgun",
|
|
want: "forweapon: Railgun",
|
|
negate: false,
|
|
},
|
|
{
|
|
name: "has-ammo",
|
|
want: "ammo:",
|
|
negate: false,
|
|
},
|
|
{
|
|
name: "has-alive",
|
|
want: "alive: true",
|
|
negate: false,
|
|
},
|
|
{
|
|
name: "has-no-body",
|
|
want: "body:",
|
|
negate: true,
|
|
},
|
|
{
|
|
name: "has-time",
|
|
want: time.Now().Format(yadu.DefaultTimeFormat),
|
|
negate: false,
|
|
},
|
|
{
|
|
name: "has-no-time",
|
|
want: time.Now().Format(yadu.DefaultTimeFormat),
|
|
opts: yadu.Options{
|
|
ReplaceAttr: removeTime,
|
|
},
|
|
negate: true,
|
|
},
|
|
{
|
|
name: "has-custom-time",
|
|
want: time.Now().Format(testTimeFormat),
|
|
opts: yadu.Options{
|
|
TimeFormat: testTimeFormat,
|
|
},
|
|
negate: false,
|
|
},
|
|
{
|
|
name: "with-group",
|
|
want: "pid:",
|
|
negate: false,
|
|
with: slog.Group("program_info",
|
|
slog.Int("pid", 1923),
|
|
slog.Bool("alive", true),
|
|
),
|
|
haswith: true,
|
|
},
|
|
{
|
|
name: "has-debug",
|
|
want: "DEBUG",
|
|
negate: false,
|
|
opts: yadu.Options{
|
|
Level: slog.LevelDebug,
|
|
},
|
|
},
|
|
{
|
|
name: "has-warn",
|
|
want: "WARN",
|
|
negate: false,
|
|
opts: yadu.Options{
|
|
Level: slog.LevelWarn,
|
|
},
|
|
},
|
|
{
|
|
name: "has-error",
|
|
want: "ERROR",
|
|
negate: false,
|
|
opts: yadu.Options{
|
|
Level: slog.LevelError,
|
|
},
|
|
},
|
|
{
|
|
// check if output is NOT colored when disabling it
|
|
name: "disable-color",
|
|
want: "\x1b[0m",
|
|
negate: true,
|
|
opts: yadu.Options{
|
|
NoColor: true,
|
|
},
|
|
},
|
|
{
|
|
// check if output is colored
|
|
name: "enable-color",
|
|
want: "\x1b[0m",
|
|
negate: false,
|
|
},
|
|
}
|
|
|
|
func GetEnemy() *Enemy {
|
|
return &Enemy{Alive: true, Health: 10, Name: "Bodo", Body: "body\nbody\n",
|
|
Ammo: []Ammo{{Forweapon: "Railgun", Range: 400, Impact: 100, Cost: 100000}},
|
|
}
|
|
|
|
}
|
|
|
|
func removeTime(_ []string, a slog.Attr) slog.Attr {
|
|
if a.Key == slog.TimeKey {
|
|
return slog.Attr{}
|
|
}
|
|
return a
|
|
}
|
|
|
|
func Test(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tt := range tests {
|
|
var buf bytes.Buffer
|
|
|
|
logger := slog.New(yadu.NewHandler(&buf, &tt.opts))
|
|
|
|
if !tt.with.Equal(slog.Attr{}) {
|
|
logger = logger.With(tt.with)
|
|
}
|
|
|
|
if !tt.opts.NoColor {
|
|
color.NoColor = false
|
|
}
|
|
|
|
slog.SetDefault(logger)
|
|
|
|
switch tt.opts.Level {
|
|
case slog.LevelDebug:
|
|
logger.Debug("attack", "enemy", GetEnemy())
|
|
case slog.LevelWarn:
|
|
logger.Warn("attack", "enemy", GetEnemy())
|
|
case slog.LevelError:
|
|
logger.Error("attack", "enemy", GetEnemy())
|
|
default:
|
|
logger.Info("attack", "enemy", GetEnemy())
|
|
}
|
|
|
|
got := buf.String()
|
|
|
|
if strings.Contains(got, tt.want) == tt.negate {
|
|
t.Errorf("test %s failed.\n want:\n%s\n got: %s\n", tt.name, tt.want, got)
|
|
}
|
|
|
|
buf.Reset()
|
|
}
|
|
}
|