gitlab.com/greut/eclint@v0.5.2-0.20240402114752-14681fe6e0bf/print_test.go (about)

     1  package eclint_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"testing"
     8  
     9  	"gitlab.com/greut/eclint"
    10  )
    11  
    12  func TestPrintErrors(t *testing.T) {
    13  	tests := []struct {
    14  		Name      string
    15  		HasOutput bool
    16  		Errors    []error
    17  	}{
    18  		{
    19  			Name:      "no errors",
    20  			HasOutput: false,
    21  			Errors:    []error{},
    22  		}, {
    23  			Name:      "simple error",
    24  			HasOutput: true,
    25  			Errors: []error{
    26  				errors.New("random error"),
    27  			},
    28  		}, {
    29  			Name:      "validation error",
    30  			HasOutput: true,
    31  			Errors: []error{
    32  				eclint.ValidationError{},
    33  			},
    34  		}, {
    35  			Name:      "complete validation error",
    36  			HasOutput: true,
    37  			Errors: []error{
    38  				eclint.ValidationError{
    39  					Line:     []byte("Hello"),
    40  					Index:    1,
    41  					Position: 2,
    42  				},
    43  			},
    44  		},
    45  	}
    46  
    47  	ctx := context.TODO()
    48  
    49  	for _, tc := range tests {
    50  		tc := tc
    51  
    52  		// Test the nominal case
    53  		t.Run(tc.Name, func(t *testing.T) {
    54  			t.Parallel()
    55  
    56  			buf := bytes.NewBuffer(make([]byte, 0, 1024))
    57  			opt := &eclint.Option{
    58  				Stdout: buf,
    59  			}
    60  
    61  			err := eclint.PrintErrors(ctx, opt, tc.Name, tc.Errors)
    62  			if err != nil {
    63  				t.Error("no errors were expected")
    64  			}
    65  
    66  			outputLength := buf.Len()
    67  			if (outputLength > 0) != tc.HasOutput {
    68  				t.Errorf("unexpected output length got %d, wanted %v", outputLength, tc.HasOutput)
    69  			}
    70  		})
    71  	}
    72  }