From 01b1780288d1dcf3451e48bce510daae1ff5033f Mon Sep 17 00:00:00 2001 From: Chris Hamant Date: Thu, 9 Oct 2014 15:48:55 -0400 Subject: [PATCH] adding simple test for BindPFlag --- viper_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/viper_test.go b/viper_test.go index 9818138..3a9a764 100644 --- a/viper_test.go +++ b/viper_test.go @@ -7,11 +7,13 @@ package viper import ( "bytes" + "fmt" "os" "sort" "testing" "time" + "github.com/spf13/pflag" "github.com/stretchr/testify/assert" ) @@ -51,6 +53,27 @@ var jsonExample = []byte(`{ } }`) +//stubs for PFlag Values +type stringValue string + +func newStringValue(val string, p *string) *stringValue { + *p = val + return (*stringValue)(p) +} + +func (s *stringValue) Set(val string) error { + *s = stringValue(val) + return nil +} + +func (s *stringValue) Type() string { + return "string" +} + +func (s *stringValue) String() string { + return fmt.Sprintf("%s", *s) +} + func TestBasics(t *testing.T) { SetConfigFile("/tmp/config.yaml") assert.Equal(t, "/tmp/config.yaml", getConfigFile()) @@ -202,3 +225,24 @@ func TestMarshal(t *testing.T) { } assert.Equal(t, &C, &config{Name: "Steve", Port: 1234}) } + +func TestBindPFlag(t *testing.T) { + var testString = "testing" + var testValue = newStringValue(testString, &testString) + + flag := &pflag.Flag{ + Name: "testflag", + Value: testValue, + Changed: false, + } + + BindPFlag("testvalue", flag) + + assert.Equal(t, testString, Get("testvalue")) + + flag.Value.Set("testing_mutate") + flag.Changed = true //hack for pflag usage + + assert.Equal(t, "testing_mutate", Get("testvalue")) + +}