Files
maddy/pkg/logparser/parse_test.go
fox.cpp bf188e454f Move most code from the repo root into subdirectories
The intention is to keep to repo root clean while the list of packages
is slowly growing.

Additionally, a bunch of small (~30 LoC) files in the repo root is
merged into a single maddy.go file, for the same reason.

Most of the internal code is moved into the internal/ directory. Go
toolchain will make it impossible to import these packages from external
applications.

Some packages are renamed and moved into the pkg/ directory in the root.
According to https://github.com/golang-standards/project-layout this is
the de-facto standard to place "library code that's ok to use by
external applications" in.

To clearly define the purpose of top-level directories, README.md files
are added to each.
2019-12-06 01:35:12 +03:00

96 lines
2.8 KiB
Go

package parser
import (
"reflect"
"testing"
"time"
)
func TestParse(t *testing.T) {
test := func(line string, msg Msg, errDesc string) {
t.Helper()
parsed, err := Parse(line)
if errDesc != "" {
if err == nil {
t.Errorf("Expected an error, got none")
return
}
if err.(MalformedMsg).Desc != errDesc {
t.Errorf("Wrong error desc returned: %v", err.(MalformedMsg).Desc)
return
}
}
if errDesc == "" && err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if !reflect.DeepEqual(parsed, msg) {
t.Errorf("Wrong Parse result,\n got %#+v\n want %#+v", parsed, msg)
}
}
test("2006-01-02T15:04:05.000Z module: hello\t", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Module: "module",
Message: "hello",
Context: map[string]interface{}{},
}, "")
test("2006-01-02T15:04:05.000Z module: hello: whatever\t", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Module: "module",
Message: "hello: whatever",
Context: map[string]interface{}{},
}, "")
test("2006-01-02T15:04:05.000Z module: hello: whatever\t{\"a\":1}", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Module: "module",
Message: "hello: whatever",
Context: map[string]interface{}{
"a": float64(1),
},
}, "")
test("2006-01-02T15:04:05.000Z module: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Module: "module",
Message: "hello: whatever",
Context: map[string]interface{}{
"a": float64(1),
"b": "bbb",
},
}, "")
test("2006-01-02T15:04:05.000Z [debug] module: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Debug: true,
Module: "module",
Message: "hello: whatever",
Context: map[string]interface{}{
"a": float64(1),
"b": "bbb",
},
}, "")
test("2006-01-02T15:04:05.000Z [debug] oink oink: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Debug: true,
Message: "oink oink: hello: whatever",
Context: map[string]interface{}{
"a": float64(1),
"b": "bbb",
},
}, "")
test("2006-01-02T15:04:05.000Z [debug] whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{
Stamp: time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
Debug: true,
Message: "whatever",
Context: map[string]interface{}{
"a": float64(1),
"b": "bbb",
},
}, "")
test("module: hello\t", Msg{}, "timestamp parse")
test("hello\t", Msg{}, "missing a timestamp")
test("2006-01-02T15:04:05.000Z module: hello", Msg{}, "missing a tab separator")
test("2006-01-02T15:04:05.000Z [BROKEN FORMATTING: json: wtf lol omg]: hello map[stringasdasd]", Msg{}, "missing a tab separator")
}