maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package parser
 20
 21import (
 22	"reflect"
 23	"testing"
 24	"time"
 25)
 26
 27func TestParse(t *testing.T) {
 28	test := func(line string, msg Msg, errDesc string) {
 29		t.Helper()
 30
 31		parsed, err := Parse(line)
 32		if errDesc != "" {
 33			if err == nil {
 34				t.Errorf("Expected an error, got none")
 35				return
 36			}
 37			if err.(MalformedMsg).Desc != errDesc {
 38				t.Errorf("Wrong error desc returned: %v", err.(MalformedMsg).Desc)
 39				return
 40			}
 41		}
 42		if errDesc == "" && err != nil {
 43			t.Errorf("Unexpected error: %v", err)
 44			return
 45		}
 46
 47		if !reflect.DeepEqual(parsed, msg) {
 48			t.Errorf("Wrong Parse result,\n got  %#+v\n want %#+v", parsed, msg)
 49		}
 50	}
 51
 52	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}