Skip to content

Commit

Permalink
Give each test case a name
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Jun 6, 2023
1 parent 82689a2 commit 0d60bc2
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions stdlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,239 +50,277 @@ func TestStdlibAdapterUsage(t *testing.T) {
}

func TestStdLibAdapter(t *testing.T) {
var testcases = []struct {
for _, tt := range []struct {
name string
input string
regexp *regexp.Regexp
prefix string
joinPrefix bool
want string
}{
{
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello world`,
want: `ts="2009/01/23 01:23:23.123123" caller=/a/b/c/d.go:23 msg="hello world"`,
},
{
name: "StdlibRegexpFull with simple line",
regexp: StdlibRegexpFull,
input: `hello`,
want: `msg=hello`,
},
{
name: "StdlibRegexpFull with date",
regexp: StdlibRegexpFull,
input: `2009/01/23: hello`,
want: `ts=2009/01/23 msg=hello`,
},
{
name: "StdlibRegexpFull with datetime",
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23: hello`,
want: `ts="2009/01/23 01:23:23" msg=hello`,
},
{
name: "StdlibRegexpFull with time",
regexp: StdlibRegexpFull,
input: `01:23:23: hello`,
want: `ts=01:23:23 msg=hello`,
},
{
name: "StdlibRegexpFull with datetime.ms",
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23.123123: hello`,
want: `ts="2009/01/23 01:23:23.123123" msg=hello`,
},
{
name: "StdlibRegexpFull with datetime.ms, caller",
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello`,
want: `ts="2009/01/23 01:23:23.123123" caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with datetime.ms, caller, space in msgr",
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello world`,
want: `ts="2009/01/23 01:23:23.123123" caller=/a/b/c/d.go:23 msg="hello world"`,
},
{
name: "StdlibRegexpFull with time.ms, caller",
regexp: StdlibRegexpFull,
input: `01:23:23.123123 /a/b/c/d.go:23: hello`,
want: `ts=01:23:23.123123 caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with datetime, caller",
regexp: StdlibRegexpFull,
input: `2009/01/23 01:23:23 /a/b/c/d.go:23: hello`,
want: `ts="2009/01/23 01:23:23" caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with date, caller",
regexp: StdlibRegexpFull,
input: `2009/01/23 /a/b/c/d.go:23: hello`,
want: `ts=2009/01/23 caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with caller",
regexp: StdlibRegexpFull,
input: `/a/b/c/d.go:23: hello`,
want: `caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix",
regexp: StdlibRegexpFull,
input: `some prefix hello`,
prefix: "some prefix ",
want: `msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, date",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23: hello`,
prefix: "some prefix ",
want: `ts=2009/01/23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, datetime",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23: hello`,
prefix: "some prefix ",
want: `ts="2009/01/23 01:23:23" msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, time",
regexp: StdlibRegexpFull,
input: `some prefix 01:23:23: hello`,
prefix: "some prefix ",
want: `ts=01:23:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, datetime.ms",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23.123123: hello`,
prefix: "some prefix ",
want: `ts="2009/01/23 01:23:23.123123" msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, datetime.ms, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
want: `ts="2009/01/23 01:23:23.123123" caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, time.ms, caller",
regexp: StdlibRegexpFull,
input: `some prefix 01:23:23.123123 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
want: `ts=01:23:23.123123 caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, datetime, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
want: `ts="2009/01/23 01:23:23" caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, date, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
want: `ts=2009/01/23 caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with prefix, caller",
regexp: StdlibRegexpFull,
input: `some prefix /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
want: `caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with caller, prefix",
regexp: StdlibRegexpFull,
input: `/a/b/c/d.go:23: some prefix hello`,
prefix: "some prefix ",
want: `caller=/a/b/c/d.go:23 msg=hello`,
},
{
name: "StdlibRegexpFull with join prefix",
regexp: StdlibRegexpFull,
input: `some prefix hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts=2009/01/23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, datetime",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts="2009/01/23 01:23:23" msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, time",
regexp: StdlibRegexpFull,
input: `some prefix 01:23:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts=01:23:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, datetime.ms",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23.123123: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts="2009/01/23 01:23:23.123123" msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, datetime.ms, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts="2009/01/23 01:23:23.123123" caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, time.ms, caller",
regexp: StdlibRegexpFull,
input: `some prefix 01:23:23.123123 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts=01:23:23.123123 caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, datetime, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 01:23:23 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts="2009/01/23 01:23:23" caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, date, caller",
regexp: StdlibRegexpFull,
input: `some prefix 2009/01/23 /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `ts=2009/01/23 caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with join prefix, caller",
regexp: StdlibRegexpFull,
input: `some prefix /a/b/c/d.go:23: hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpFull with caller, join prefix",
regexp: StdlibRegexpFull,
input: `/a/b/c/d.go:23: some prefix hello`,
prefix: "some prefix ",
joinPrefix: true,
want: `caller=/a/b/c/d.go:23 msg="some prefix hello"`,
},
{
name: "StdlibRegexpDefault with error message contain colons",
regexp: StdlibRegexpDefault,
input: `error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:`,
want: `msg="error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:"`,
},
{
name: "StdlibRegexpDefault with datetime, error message contain colons",
regexp: StdlibRegexpDefault,
input: `2023/04/28 07:28:46 error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:`,
want: `ts="2023/04/28 07:28:46" msg="error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:"`,
},
{
name: "StdlibRegexpDefault with datetime, caller, error message contain colons",
regexp: StdlibRegexpDefault,
input: `2023/04/28 07:28:46 /a/b/c/d.go:23: error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:`,
want: `ts="2023/04/28 07:28:46" msg="/a/b/c/d.go:23: error encoding and sending metric family: write tcp 127.0.0.1:9182->127.0.0.1:60125: wsasend:"`,
},
{
name: "StdlibRegexpDefault with datetime.ms, caller",
regexp: StdlibRegexpDefault,
input: `2009/01/23 01:23:23.123123 /a/b/c/d.go:23: hello`,
want: `ts="2009/01/23 01:23:23.123123" msg="/a/b/c/d.go:23: hello"`,
},
{
name: "StdlibRegexpDefault with 1:9182f",
regexp: StdlibRegexpDefault,
input: `1:9182f`,
want: `msg=1:9182f`,
},
}
for _, tt := range testcases {
t.Run(tt.input, func(t *testing.T) {
} {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
adapter := NewStdlibAdapter(NewLogfmtLogger(&buf), StdlibRegexp(tt.regexp), Prefix(tt.prefix, tt.joinPrefix))
fmt.Fprint(adapter, tt.input)
Expand Down

0 comments on commit 0d60bc2

Please sign in to comment.