github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/helper/flag-kv/flag_json_test.go (about)

     1  package kvflag
     2  
     3  import (
     4  	"flag"
     5  	"path/filepath"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestFlagJSON_impl(t *testing.T) {
    11  	var _ flag.Value = new(FlagJSON)
    12  }
    13  
    14  func TestFlagJSON(t *testing.T) {
    15  	cases := []struct {
    16  		Input   string
    17  		Initial map[string]string
    18  		Output  map[string]string
    19  		Error   bool
    20  	}{
    21  		{
    22  			"basic.json",
    23  			nil,
    24  			map[string]string{"key": "value"},
    25  			false,
    26  		},
    27  
    28  		{
    29  			"basic.json",
    30  			map[string]string{"foo": "bar"},
    31  			map[string]string{"foo": "bar", "key": "value"},
    32  			false,
    33  		},
    34  
    35  		{
    36  			"basic.json",
    37  			map[string]string{"key": "bar"},
    38  			map[string]string{"key": "value"},
    39  			false,
    40  		},
    41  	}
    42  
    43  	for _, tc := range cases {
    44  		f := new(FlagJSON)
    45  		if tc.Initial != nil {
    46  			f = (*FlagJSON)(&tc.Initial)
    47  		}
    48  
    49  		err := f.Set(filepath.Join("./test-fixtures", tc.Input))
    50  		if (err != nil) != tc.Error {
    51  			t.Fatalf("bad error. Input: %#v\n\n%s", tc.Input, err)
    52  		}
    53  
    54  		actual := map[string]string(*f)
    55  		if !reflect.DeepEqual(actual, tc.Output) {
    56  			t.Fatalf("bad: %#v", actual)
    57  		}
    58  	}
    59  }