github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/errors/errors_test.go (about)

     1  package errors_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/tooploox/oya/pkg/errors"
     8  	tu "github.com/tooploox/oya/testutil"
     9  )
    10  
    11  func TestSnippet_Format(t *testing.T) {
    12  	cases := []struct {
    13  		desc     string
    14  		snippet  errors.Snippet
    15  		line     uint
    16  		col      uint
    17  		expected string
    18  	}{
    19  		{
    20  			desc:     "empty snippet",
    21  			snippet:  errors.Snippet{},
    22  			line:     1,
    23  			col:      1,
    24  			expected: "",
    25  		},
    26  		{
    27  			desc: "one line snippet",
    28  			snippet: errors.Snippet{
    29  				LineOffset: 0,
    30  				Lines: []string{
    31  					"line1",
    32  				},
    33  			},
    34  			line:     1,
    35  			col:      1,
    36  			expected: "> 1| line1\n     ^\n",
    37  		},
    38  		{
    39  			desc: "multi-line snippet",
    40  			snippet: errors.Snippet{
    41  				LineOffset: 0,
    42  				Lines: []string{
    43  					"line1",
    44  					"line2",
    45  					"line3",
    46  				},
    47  			},
    48  			line:     3,
    49  			col:      1,
    50  			expected: "  2| line2\n> 3| line3\n     ^\n",
    51  		},
    52  		{
    53  			desc: "two digit line number",
    54  			snippet: errors.Snippet{
    55  				LineOffset: 8,
    56  				Lines: []string{
    57  					"line9",
    58  					"line10",
    59  					"line11",
    60  				},
    61  			},
    62  			line:     10,
    63  			col:      2,
    64  			expected: "   9| line9\n> 10| line10\n       ^\n",
    65  		},
    66  	}
    67  	for _, tc := range cases {
    68  		var out bytes.Buffer
    69  		tc.snippet.Print(&out, tc.line, tc.col)
    70  		result := out.String()
    71  		tu.AssertEqual(t, tc.expected, result)
    72  	}
    73  }