From a71f9e1edad8841cfc009a7c5036083c1b2e90df Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Wed, 4 Dec 2024 18:30:22 +0800 Subject: [PATCH] add unit test Signed-off-by: Lixia (Sylvia) Lei --- internal/trace/text_formatter_test.go | 99 +++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 internal/trace/text_formatter_test.go diff --git a/internal/trace/text_formatter_test.go b/internal/trace/text_formatter_test.go new file mode 100644 index 000000000..c7ac5b9c1 --- /dev/null +++ b/internal/trace/text_formatter_test.go @@ -0,0 +1,99 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package trace + +import ( + "reflect" + "testing" + "time" + + "github.com/sirupsen/logrus" +) + +func TestTextFormatter_Format(t *testing.T) { + tests := []struct { + name string + f *TextFormatter + entry *logrus.Entry + want []byte + wantErr bool + }{ + { + name: "debug log entry", + f: &TextFormatter{}, + entry: &logrus.Entry{ + Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), + Level: logrus.DebugLevel, + Message: "test debug", + Data: logrus.Fields{}, + }, + want: []byte("[2024-12-01T23:30:01.000000055Z][DEBUG]: test debug\n\n\n"), + wantErr: false, + }, + { + name: "info log entry", + f: &TextFormatter{}, + entry: &logrus.Entry{ + Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), + Level: logrus.InfoLevel, + Message: "test info", + Data: logrus.Fields{}, + }, + want: []byte("[2024-12-01T23:30:01.000000055Z][INFO]: test info\n\n\n"), + wantErr: false, + }, + { + name: "warning log entry with data fields", + f: &TextFormatter{}, + entry: &logrus.Entry{ + Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), + Level: logrus.WarnLevel, + Message: "test warning with fields", + Data: logrus.Fields{ + "testkey": "testval", + }, + }, + want: []byte("[2024-12-01T23:30:01.000000055Z][WARNING]: test warning with fields\n[Data]:\n testkey=testval\n\n\n"), + wantErr: false, + }, + { + name: "error log entry with data fields", + f: &TextFormatter{}, + entry: &logrus.Entry{ + Time: time.Date(2024, time.December, 1, 23, 30, 1, 55, time.UTC), + Level: logrus.ErrorLevel, + Message: "test warning with fields", + Data: logrus.Fields{ + "testkey": 123, + }, + }, + want: []byte("[2024-12-01T23:30:01.000000055Z][ERROR]: test warning with fields\n[Data]:\n testkey=123\n\n\n"), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.f.Format(tt.entry) + if (err != nil) != tt.wantErr { + t.Errorf("TextFormatter.Format() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("TextFormatter.Format() = %s, want %s", got, tt.want) + } + }) + } +}