github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/config/indentOption_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	yaml "gopkg.in/yaml.v2"
     9  
    10  	"github.com/yoheimuta/protolint/internal/linter/config"
    11  )
    12  
    13  func TestIndentOption_UnmarshalYAML(t *testing.T) {
    14  	for _, test := range []struct {
    15  		name             string
    16  		inputConfig      []byte
    17  		wantIndentOption config.IndentOption
    18  		wantExistErr     bool
    19  	}{
    20  		{
    21  			name: "not found supported style",
    22  			inputConfig: []byte(`
    23  style: 4-space
    24  `),
    25  			wantExistErr: true,
    26  		},
    27  		{
    28  			name: "style: tab",
    29  			inputConfig: []byte(`
    30  style: tab
    31  `),
    32  			wantIndentOption: config.IndentOption{
    33  				Style: "\t",
    34  			},
    35  		},
    36  		{
    37  			name: "style: 4",
    38  			inputConfig: []byte(`
    39  style: 4
    40  `),
    41  			wantIndentOption: config.IndentOption{
    42  				Style: strings.Repeat(" ", 4),
    43  			},
    44  		},
    45  		{
    46  			name: "style: 2",
    47  			inputConfig: []byte(`
    48  style: 2
    49  `),
    50  			wantIndentOption: config.IndentOption{
    51  				Style: strings.Repeat(" ", 2),
    52  			},
    53  		},
    54  		{
    55  			name: "not found supported newline",
    56  			inputConfig: []byte(`
    57  style: tab
    58  newline: linefeed
    59  `),
    60  			wantExistErr: true,
    61  		},
    62  		{
    63  			name: "newline: \n",
    64  			inputConfig: []byte(`
    65  newline: "\n"
    66  `),
    67  			wantIndentOption: config.IndentOption{
    68  				Newline: "\n",
    69  			},
    70  		},
    71  		{
    72  			name: "newline: \r",
    73  			inputConfig: []byte(`
    74  newline: "\r"
    75  `),
    76  			wantIndentOption: config.IndentOption{
    77  				Newline: "\r",
    78  			},
    79  		},
    80  		{
    81  			name: "newline: \r\n",
    82  			inputConfig: []byte(`
    83  newline: "\r\n"
    84  `),
    85  			wantIndentOption: config.IndentOption{
    86  				Newline: "\r\n",
    87  			},
    88  		},
    89  		{
    90  			name: "support not_insert_newline",
    91  			inputConfig: []byte(`
    92  not_insert_newline: true
    93  `),
    94  			wantIndentOption: config.IndentOption{
    95  				NotInsertNewline: true,
    96  			},
    97  		},
    98  	} {
    99  		test := test
   100  		t.Run(test.name, func(t *testing.T) {
   101  			var got config.IndentOption
   102  
   103  			err := yaml.UnmarshalStrict(test.inputConfig, &got)
   104  			if test.wantExistErr {
   105  				if err == nil {
   106  					t.Errorf("got err nil, but want err")
   107  				}
   108  				return
   109  			}
   110  			if err != nil {
   111  				t.Errorf("got err %v, but want nil", err)
   112  				return
   113  			}
   114  
   115  			if !reflect.DeepEqual(got, test.wantIndentOption) {
   116  				t.Errorf("got %v, but want %v", got, test.wantIndentOption)
   117  			}
   118  		})
   119  	}
   120  }