From e8036e1a2445b59499088b9167d8c1e6176de894 Mon Sep 17 00:00:00 2001 From: Joe Buck Date: Thu, 18 Feb 2016 21:27:02 -0800 Subject: [PATCH] Add test for parsing nested keys from environment variables Add a test where an environment variable with a nested key is set and then retrieved. The nested key uses an underscore in place of a dot to indicate the key hierarchy. --- viper_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/viper_test.go b/viper_test.go index dde1fe0..44c4b4c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -371,6 +371,30 @@ func TestEnv(t *testing.T) { } +func TestEnvNestedKeys(t *testing.T) { + initJSON() + + BindEnv("id") + BindEnv("f", "FOOD") + + // Validate the default value + assert.Equal(t, "fancy", Get("icing.type")) + + os.Setenv("ID", "13") + os.Setenv("FOOD", "apple") + os.Setenv("NAME", "crunk") + os.Setenv("ICING_TYPE", "plain") + + assert.Equal(t, "13", Get("id")) + assert.Equal(t, "apple", Get("f")) + assert.Equal(t, "Cake", Get("name")) + + AutomaticEnv() + + assert.Equal(t, "plain", Get("icing.type")) + assert.Equal(t, "crunk", Get("name")) +} + func TestEnvPrefix(t *testing.T) { initJSON()