github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/errors_test.go (about)

     1  package expressions
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/test/count"
     7  )
     8  
     9  func TestTrimCodeInErrMsg(t *testing.T) {
    10  	tests := []struct {
    11  		Code  string
    12  		Pos   int
    13  		Width int
    14  	}{
    15  		{
    16  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    17  			Pos:   62,
    18  			Width: 100,
    19  		},
    20  		{
    21  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    22  			Pos:   1,
    23  			Width: 10,
    24  		},
    25  		{
    26  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    27  			Pos:   5,
    28  			Width: 10,
    29  		},
    30  		{
    31  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    32  			Pos:   10,
    33  			Width: 10,
    34  		},
    35  		{
    36  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    37  			Pos:   14,
    38  			Width: 10,
    39  		},
    40  		{
    41  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    42  			Pos:   15,
    43  			Width: 10,
    44  		},
    45  		{
    46  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    47  			Pos:   16,
    48  			Width: 10,
    49  		},
    50  		{
    51  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    52  			Pos:   20,
    53  			Width: 10,
    54  		},
    55  		{
    56  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    57  			Pos:   25,
    58  			Width: 10,
    59  		},
    60  		{
    61  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    62  			Pos:   30,
    63  			Width: 10,
    64  		},
    65  		{
    66  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    67  			Pos:   35,
    68  			Width: 10,
    69  		},
    70  		{
    71  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    72  			Pos:   40,
    73  			Width: 10,
    74  		},
    75  		{
    76  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    77  			Pos:   45,
    78  			Width: 10,
    79  		},
    80  		{
    81  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    82  			Pos:   50,
    83  			Width: 10,
    84  		},
    85  		{
    86  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    87  			Pos:   55,
    88  			Width: 10,
    89  		},
    90  		{
    91  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    92  			Pos:   60,
    93  			Width: 10,
    94  		},
    95  		{
    96  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    97  			Pos:   61,
    98  			Width: 10,
    99  		},
   100  		{
   101  			Code:  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
   102  			Pos:   62,
   103  			Width: 10,
   104  		},
   105  	}
   106  
   107  	count.Tests(t, len(tests))
   108  
   109  	for i, test := range tests {
   110  		code := []rune(test.Code)
   111  		pos := test.Pos - 1
   112  		rr, ri := _cropCodeInErrMsg(code, test.Pos, test.Width)
   113  		expected := code[pos]
   114  		actual := rr[ri-1]
   115  		if expected != actual {
   116  			t.Errorf("error message incorrect in test %d", i)
   117  			t.Logf("  Code:     '%s'", test.Code)
   118  			t.Logf("  Pos:       %d, '%s' (%d)", test.Pos, string(test.Code[pos]), test.Code[pos])
   119  			t.Logf("  Return:   '%s' (%d)", string(rr), ri)
   120  			t.Logf("  expected: '%s' (%d)", string([]rune{expected}), expected)
   121  			t.Logf("  actual:   '%s' (%d)", string([]rune{actual}), actual)
   122  		}
   123  	}
   124  }