From 967e04580768ace2dafae07dcf762348eeb4392a Mon Sep 17 00:00:00 2001 From: Lilleman Date: Sat, 15 Oct 2022 23:06:06 +0200 Subject: [PATCH] feat: tests and stuff --- go.mod | 8 ++++++ go.sum | 15 ++++++++++ main_test.go | 77 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 2e07cd3..a6b16f8 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module gitea.larvit.se/pwrpln/go_log go 1.19 + +require github.com/stretchr/testify v1.8.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5164829 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test.go b/main_test.go index e212bf4..00b88c5 100644 --- a/main_test.go +++ b/main_test.go @@ -2,35 +2,68 @@ package main import ( "testing" - "time" + + "github.com/stretchr/testify/assert" ) func TestDefault(t *testing.T) { + stdout := "" + stderr := "" testLog := Log{} testLog.SetDefaultValues() - testLog.Context = []Metadata{ - { - Name: "foo", - Value: "bar", - }, - { - Name: "lur", - Value: "pelle", - }, + testLog.Stdout = func(msg string) { + stdout += msg } - testLog.Info("bosse") - testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}}) + testLog.Stderr = func(msg string) { + stderr += msg + } + testLog.Warn("oh noes") + testLog.Info("einfoo") + testLog.Verbose("moar moar") + testLog.Debug("deboug") + + // Only info should be processed due to default log limit being info + assert.Equal(t, "[\x1b[32mInf\x1b[0m] einfoo\n", stdout[20:]) + assert.Equal(t, "[\x1b[33mWrn\x1b[0m] oh noes\n", stderr[20:]) } -func TestWoo(t *testing.T) { - loc, _ := time.LoadLocation("UTC") - log := Log{ - Context: []Metadata{}, // Will be prepended to metadata on all log entries - MinLogLvl: Info, // Minimal log level to output - Fmt: DefaultFmt, // Log message formatter - Stderr: DefaultStderr, // Log message outputter for Debug, Verbose and Info - Stdout: DefaultStdout, // Log message outputter for Warning and Error - TimeLocation: loc, // Timestamp location/time zone setting +func TestError(t *testing.T) { + stderr := "" + testLog := Log{} + testLog.SetDefaultValues() + testLog.Stderr = func(msg string) { + stderr += msg } - log.Info("wat") + testLog.Error("lureri") + assert.Equal(t, "[\x1b[31mErr\x1b[0m] lureri\n", stderr[20:]) } + +// func TestMetadata(t *testing.T) { +// testLog := Log{} +// testLog.SetDefaultValues() +// testLog.Context = []Metadata{ +// { +// Name: "foo", +// Value: "bar", +// }, +// { +// Name: "lur", +// Value: "pelle", +// }, +// } +// testLog.Info("bosse") +// testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}}) +// } + +// func TestWoo(t *testing.T) { +// loc, _ := time.LoadLocation("UTC") +// log := Log{ +// Context: []Metadata{}, // Will be prepended to metadata on all log entries +// MinLogLvl: Info, // Minimal log level to output +// Fmt: DefaultFmt, // Log message formatter +// Stderr: DefaultStderr, // Log message outputter for Debug, Verbose and Info +// Stdout: DefaultStdout, // Log message outputter for Warning and Error +// TimeLocation: loc, // Timestamp location/time zone setting +// } +// log.Info("wat") +// }