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

     1  package eclint_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"gitlab.com/greut/eclint"
     9  )
    10  
    11  func TestReadLines(t *testing.T) {
    12  	tests := []struct {
    13  		Name     string
    14  		File     []byte
    15  		LineFunc eclint.LineFunc
    16  	}{
    17  		{
    18  			Name: "Empty file",
    19  			File: []byte(""),
    20  			LineFunc: func(i int, line []byte, _ bool) error {
    21  				if i != 0 || len(line) > 0 {
    22  					return fmt.Errorf("more than one line found (%d), or non empty line %q", i, line)
    23  				}
    24  
    25  				return nil
    26  			},
    27  		}, {
    28  			Name: "crlf",
    29  			File: []byte("\r\n\r\n"),
    30  			LineFunc: func(i int, line []byte, _ bool) error {
    31  				if i > 1 || len(line) > 2 {
    32  					return fmt.Errorf("more than two lines found (%d), or non empty line %q", i, line)
    33  				}
    34  
    35  				return nil
    36  			},
    37  		}, {
    38  			Name: "cr",
    39  			File: []byte("\r\r"),
    40  			LineFunc: func(i int, line []byte, _ bool) error {
    41  				if i > 1 || len(line) > 2 {
    42  					return fmt.Errorf("more than two lines found (%d), or non empty line %q", i, line)
    43  				}
    44  
    45  				return nil
    46  			},
    47  		}, {
    48  			Name: "lf",
    49  			File: []byte("\n\n"),
    50  			LineFunc: func(i int, line []byte, _ bool) error {
    51  				if i > 1 || len(line) > 2 {
    52  					return fmt.Errorf("more than two lines found (%d), or non empty line %q", i, line)
    53  				}
    54  
    55  				return nil
    56  			},
    57  		},
    58  	}
    59  
    60  	for _, tc := range tests {
    61  		tc := tc
    62  
    63  		t.Run(tc.Name, func(t *testing.T) {
    64  			t.Parallel()
    65  
    66  			r := bytes.NewReader(tc.File)
    67  
    68  			errs := eclint.ReadLines(r, -1, tc.LineFunc)
    69  			if len(errs) > 0 {
    70  				t.Errorf("no errors were expected, got some. %s", errs[0])
    71  
    72  				return
    73  			}
    74  		})
    75  	}
    76  }