Path: blob/main/components/local-app/pkg/prettyprint/prettyprint_test.go
2500 views
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package prettyprint56import (7"bytes"8"testing"910"github.com/google/go-cmp/cmp"11)1213func TestWriterWrite(t *testing.T) {14type R struct {15Foo string `print:"foo"`16Number int `print:"number"`17DiffName string `print:"foobar"`18}19type Expectation struct {20Error string21Out string22}23tests := []struct {24Name string25Expectation Expectation26Format WriterFormat27Field string28Data []R29}{30{31Name: "wide format with data",32Expectation: Expectation{33Out: "FOO NUMBER FOOBAR \nfoo 42 bar \n",34},35Format: WriterFormatWide,36Data: []R{37{Foo: "foo", Number: 42, DiffName: "bar"},38},39},40{41Name: "narrow format with data",42Expectation: Expectation{43Out: "Foo: foo\nNumber: 42\nFoobar: bar\n",44},45Format: WriterFormatNarrow,46Data: []R{47{Foo: "foo", Number: 42, DiffName: "bar"},48},49},50{51Name: "empty",52Expectation: Expectation{53Out: "",54},55Format: WriterFormatNarrow,56Field: "",57Data: nil,58},59{60Name: "empty wide",61Expectation: Expectation{62Out: "FOO NUMBER FOOBAR \n",63},64Format: WriterFormatWide,65},66{67Name: "empty field",68Expectation: Expectation{},69Format: WriterFormatNarrow,70Field: "foo",71Data: nil,72},73{74Name: "empty field wide",75Expectation: Expectation{},76Format: WriterFormatWide,77Field: "foo",78Data: nil,79},80{81Name: "empty field with data",82Expectation: Expectation{},83Format: WriterFormatNarrow,84Field: "foo",85Data: []R{{}},86},87{88Name: "empty field with data wide",89Expectation: Expectation{},90Format: WriterFormatWide,91Field: "foo",92Data: []R{{}},93},94}9596for _, test := range tests {97t.Run(test.Name, func(t *testing.T) {98var act Expectation99100out := bytes.NewBuffer(nil)101w := Writer[R]{102Out: out,103Format: test.Format,104Field: test.Field,105}106err := w.Write(test.Data)107if err != nil {108act.Error = err.Error()109}110act.Out = out.String()111112if diff := cmp.Diff(test.Expectation, act); diff != "" {113t.Errorf("Write() mismatch (-want +got):\n%s", diff)114}115})116}117}118119120