github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/foreachref/format_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package foreachref_test
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/gitbundle/modules/git/foreachref"
    12  
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestFormat_Flag(t *testing.T) {
    17  	tests := []struct {
    18  		name string
    19  
    20  		givenFormat foreachref.Format
    21  
    22  		wantFlag string
    23  	}{
    24  		{
    25  			name: "references are delimited by dual null chars",
    26  
    27  			// no reference fields requested
    28  			givenFormat: foreachref.NewFormat(),
    29  
    30  			// only a reference delimiter field in --format
    31  			wantFlag: "%00%00",
    32  		},
    33  
    34  		{
    35  			name: "a field is a space-separated key-value pair",
    36  
    37  			givenFormat: foreachref.NewFormat("refname:short"),
    38  
    39  			// only a reference delimiter field
    40  			wantFlag: "refname:short %(refname:short)%00%00",
    41  		},
    42  
    43  		{
    44  			name: "fields are separated by a null char field-delimiter",
    45  
    46  			givenFormat: foreachref.NewFormat("refname:short", "author"),
    47  
    48  			wantFlag: "refname:short %(refname:short)%00author %(author)%00%00",
    49  		},
    50  
    51  		{
    52  			name: "multiple fields",
    53  
    54  			givenFormat: foreachref.NewFormat("refname:short", "objecttype", "objectname"),
    55  
    56  			wantFlag: "refname:short %(refname:short)%00objecttype %(objecttype)%00objectname %(objectname)%00%00",
    57  		},
    58  	}
    59  
    60  	for _, test := range tests {
    61  		tc := test // don't close over loop variable
    62  		t.Run(tc.name, func(t *testing.T) {
    63  			gotFlag := tc.givenFormat.Flag()
    64  
    65  			require.Equal(t, tc.wantFlag, gotFlag, "unexpected for-each-ref --format string. wanted: '%s', got: '%s'", tc.wantFlag, gotFlag)
    66  		})
    67  	}
    68  }