github.com/govau/cf-common@v0.0.7/env/opts_test.go (about) 1 package env 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 ) 8 9 func TestWithOSLookup(t *testing.T) { 10 osEnv := map[string]string{ 11 fmt.Sprintf("%s_%s", t.Name(), "A"): "a", 12 } 13 14 tests := []struct { 15 testName string 16 name string 17 def string 18 want string 19 }{ 20 { 21 testName: "exists", 22 name: fmt.Sprintf("%s_%s", t.Name(), "A"), 23 want: "a", 24 }, 25 { 26 testName: "not exists", 27 name: fmt.Sprintf("%s_%s", t.Name(), "Z"), 28 def: "z", 29 want: "z", 30 }, 31 } 32 for _, tt := range tests { 33 t.Run(tt.testName, func(t *testing.T) { 34 for k, v := range osEnv { 35 os.Setenv(k, v) 36 defer os.Unsetenv(k) 37 } 38 vs := NewVarSet(WithOSLookup()) 39 if got := vs.String(tt.name, tt.def); got != tt.want { 40 t.Errorf("String(%q, %q) = %q, want %q", tt.name, tt.def, got, tt.want) 41 } 42 }) 43 } 44 } 45 46 func TestWithMapLookup(t *testing.T) { 47 const lookupName = "A" 48 49 tests := []struct { 50 name string 51 m map[string]string 52 want string 53 }{ 54 {name: "exists", m: map[string]string{"A": "a"}, want: "a"}, 55 {name: "not exists", m: map[string]string{"Z": "z"}, want: ""}, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 vs := NewVarSet(WithMapLookup(tt.m)) 60 if got := vs.String(lookupName, ""); got != tt.want { 61 t.Errorf("String(%q) = %q, want %q", lookupName, got, tt.want) 62 } 63 }) 64 } 65 }