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

     1  package sliceflag
     2  
     3  import (
     4  	"flag"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestStringFlag_implements(t *testing.T) {
    10  	var raw interface{}
    11  	raw = new(StringFlag)
    12  	if _, ok := raw.(flag.Value); !ok {
    13  		t.Fatalf("StringFlag should be a Value")
    14  	}
    15  }
    16  
    17  // TestStringFlagSet tests for setting the same flag more than once on the CLI
    18  // like: blah -flag foo -flag bar
    19  func TestStringFlagSet(t *testing.T) {
    20  	sv := new(StringFlag)
    21  	err := sv.Set("foo")
    22  	if err != nil {
    23  		t.Fatalf("err: %s", err)
    24  	}
    25  
    26  	err = sv.Set("bar")
    27  	if err != nil {
    28  		t.Fatalf("err: %s", err)
    29  	}
    30  
    31  	expected := []string{"foo", "bar"}
    32  	if !reflect.DeepEqual([]string(*sv), expected) {
    33  		t.Fatalf("Bad: %#v", sv)
    34  	}
    35  }
    36  
    37  // TestMultiStringFlag tests for setting the same flag using a comma-separated
    38  // list of items like: blah -flag=foo,bar
    39  func TestMultiStringFlag(t *testing.T) {
    40  	sv := new(StringFlag)
    41  	err := sv.Set("chocolate,vanilla")
    42  	if err != nil {
    43  		t.Fatalf("err :%s", err)
    44  	}
    45  
    46  	expected := []string{"chocolate", "vanilla"}
    47  	if !reflect.DeepEqual([]string(*sv), expected) {
    48  		t.Fatalf("Expected: %#v, found: %#v", expected, sv)
    49  	}
    50  }