mirror of
https://github.com/spf13/viper
synced 2024-11-05 20:57:01 +00:00
adding simple test for BindPFlag
This commit is contained in:
parent
83fd92627c
commit
01b1780288
1 changed files with 44 additions and 0 deletions
|
@ -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"))
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue