MCPcopy
hub / github.com/spf13/viper / TestUnmarshal

Function TestUnmarshal

viper_test.go:854–895  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

852}
853
854func TestUnmarshal(t *testing.T) {
855 v := New()
856 v.SetDefault("port", 1313)
857 v.Set("name", "Steve")
858 v.Set("duration", "1s1ms")
859 v.Set("modes", []int{1, 2, 3})
860
861 type config struct {
862 Port int
863 Name string
864 Duration time.Duration
865 Modes []int
866 }
867
868 var C config
869 require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
870
871 assert.Equal(
872 t,
873 &config{
874 Name: "Steve",
875 Port: 1313,
876 Duration: time.Second + time.Millisecond,
877 Modes: []int{1, 2, 3},
878 },
879 &C,
880 )
881
882 v.Set("port", 1234)
883 require.NoError(t, v.Unmarshal(&C), "unable to decode into struct")
884
885 assert.Equal(
886 t,
887 &config{
888 Name: "Steve",
889 Port: 1234,
890 Duration: time.Second + time.Millisecond,
891 Modes: []int{1, 2, 3},
892 },
893 &C,
894 )
895}
896
897func TestUnmarshalWithDefaultDecodeHook(t *testing.T) {
898 opt := mapstructure.ComposeDecodeHookFunc(

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
SetDefaultMethod · 0.80
UnmarshalMethod · 0.80
SetMethod · 0.45

Tested by

no test coverage detected