1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package parser2021import (22 "reflect"23 "testing"24 "time"25)2627func TestParse(t *testing.T) {28 test := func(line string, msg Msg, errDesc string) {29 t.Helper()3031 parsed, err := Parse(line)32 if errDesc != "" {33 if err == nil {34 t.Errorf("Expected an error, got none")35 return36 }37 if err.(MalformedMsg).Desc != errDesc {38 t.Errorf("Wrong error desc returned: %v", err.(MalformedMsg).Desc)39 return40 }41 }42 if errDesc == "" && err != nil {43 t.Errorf("Unexpected error: %v", err)44 return45 }4647 if !reflect.DeepEqual(parsed, msg) {48 t.Errorf("Wrong Parse result,\n got %#+v\n want %#+v", parsed, msg)49 }50 }5152 test("2006-01-02T15:04:05.000Z module: hello\t", Msg{53 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),54 Module: "module",55 Message: "hello",56 Context: map[string]interface{}{},57 }, "")58 test("2006-01-02T15:04:05.000Z module: hello: whatever\t", Msg{59 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),60 Module: "module",61 Message: "hello: whatever",62 Context: map[string]interface{}{},63 }, "")64 test("2006-01-02T15:04:05.000Z module: hello: whatever\t{\"a\":1}", Msg{65 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),66 Module: "module",67 Message: "hello: whatever",68 Context: map[string]interface{}{69 "a": float64(1),70 },71 }, "")72 test("2006-01-02T15:04:05.000Z module: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{73 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),74 Module: "module",75 Message: "hello: whatever",76 Context: map[string]interface{}{77 "a": float64(1),78 "b": "bbb",79 },80 }, "")81 test("2006-01-02T15:04:05.000Z [debug] module: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{82 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),83 Debug: true,84 Module: "module",85 Message: "hello: whatever",86 Context: map[string]interface{}{87 "a": float64(1),88 "b": "bbb",89 },90 }, "")91 test("2006-01-02T15:04:05.000Z [debug] oink oink: hello: whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{92 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),93 Debug: true,94 Message: "oink oink: hello: whatever",95 Context: map[string]interface{}{96 "a": float64(1),97 "b": "bbb",98 },99 }, "")100 test("2006-01-02T15:04:05.000Z [debug] whatever\t{\"a\":1,\"b\":\"bbb\"}", Msg{101 Stamp: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC),102 Debug: true,103 Message: "whatever",104 Context: map[string]interface{}{105 "a": float64(1),106 "b": "bbb",107 },108 }, "")109 test("module: hello\t", Msg{}, "timestamp parse")110 test("hello\t", Msg{}, "missing a timestamp")111 test("2006-01-02T15:04:05.000Z module: hello", Msg{}, "missing a tab separator")112 test("2006-01-02T15:04:05.000Z [BROKEN FORMATTING: json: wtf lol omg]: hello map[stringasdasd]", Msg{}, "missing a tab separator")113}