mirror of https://github.com/spf13/viper.git
adding simple test for BindPFlag
This commit is contained in:
parent
83fd92627c
commit
01b1780288
|
@ -7,11 +7,13 @@ package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestBasics(t *testing.T) {
|
||||||
SetConfigFile("/tmp/config.yaml")
|
SetConfigFile("/tmp/config.yaml")
|
||||||
assert.Equal(t, "/tmp/config.yaml", getConfigFile())
|
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})
|
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"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue