github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/internal/config/config_map_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  func TestFindEntry(t *testing.T) {
    12  	tests := []struct {
    13  		name    string
    14  		key     string
    15  		output  string
    16  		wantErr bool
    17  	}{
    18  		{
    19  			name:   "find key",
    20  			key:    "valid",
    21  			output: "present",
    22  		},
    23  		{
    24  			name:    "find key that is not present",
    25  			key:     "invalid",
    26  			wantErr: true,
    27  		},
    28  		{
    29  			name:   "find key with blank value",
    30  			key:    "blank",
    31  			output: "",
    32  		},
    33  		{
    34  			name:   "find key that has same content as a value",
    35  			key:    "same",
    36  			output: "logical",
    37  		},
    38  	}
    39  
    40  	for _, tt := range tests {
    41  		cm := ConfigMap{Root: testYaml()}
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			out, err := cm.FindEntry(tt.key)
    44  			if tt.wantErr {
    45  				assert.EqualError(t, err, "not found")
    46  				return
    47  			}
    48  			assert.NoError(t, err)
    49  			fmt.Println(out)
    50  			assert.Equal(t, tt.output, out.ValueNode.Value)
    51  		})
    52  	}
    53  }
    54  
    55  func testYaml() *yaml.Node {
    56  	var root yaml.Node
    57  	var data = `
    58  valid: present
    59  erroneous: same
    60  blank:
    61  same: logical
    62  `
    63  	_ = yaml.Unmarshal([]byte(data), &root)
    64  	return root.Content[0]
    65  }