github.com/govau/cf-common@v0.0.7/env/env_test.go (about) 1 package env 2 3 import ( 4 "bytes" 5 "errors" 6 "reflect" 7 "testing" 8 ) 9 10 func TestString(t *testing.T) { 11 lookup := func(name string) (string, bool) { 12 switch name { 13 case "A": 14 return "a", true 15 default: 16 return "", false 17 } 18 } 19 tests := []struct { 20 testName, name, def, want string 21 }{ 22 {"exists, no default given", "A", "", "a"}, 23 {"exists, default given", "A", "a", "a"}, 24 {"not exists, no default given", "Z", "", ""}, 25 {"not exists, default given", "Z", "z", "z"}, 26 } 27 for _, tt := range tests { 28 t.Run(tt.testName, func(t *testing.T) { 29 v := VarSet{[]Lookup{lookup}} 30 got := v.String(tt.name, tt.def) 31 if got != tt.want { 32 t.Errorf("String(%q, %q) = %q, want %q", tt.name, tt.def, got, tt.want) 33 } 34 }) 35 } 36 } 37 38 func TestMustString(t *testing.T) { 39 lookup := func(name string) (string, bool) { 40 switch name { 41 case "A": 42 return "a", true 43 default: 44 return "", false 45 } 46 } 47 tests := []struct { 48 testName, name, want string 49 err error 50 }{ 51 {"exists", "A", "a", nil}, 52 {"not exists", "Z", "", NewVarNotFoundErr("Z")}, 53 } 54 for _, tt := range tests { 55 t.Run(tt.testName, func(t *testing.T) { 56 defer func() { 57 err := recover() 58 switch err { 59 case nil: 60 if tt.err != nil { 61 t.Fatalf("got no panic error, want %v", tt.err) 62 } 63 default: 64 if tt.err == nil { 65 t.Fatalf("got panic error %v, want none", err) 66 } else if !reflect.DeepEqual(err, tt.err) { 67 t.Fatalf("got panic error %v, want %v", err, tt.err) 68 } 69 } 70 }() 71 v := VarSet{[]Lookup{lookup}} 72 got := v.MustString(tt.name) 73 if got != tt.want { 74 t.Errorf("MustString(%q) = %q, want %q", tt.name, got, tt.want) 75 } 76 }) 77 } 78 } 79 80 func TestBool(t *testing.T) { 81 lookup := func(name string) (string, bool) { 82 switch name { 83 case "A": 84 return "true", true 85 case "B": 86 return "1", true 87 case "C": 88 return "false", true 89 case "D": 90 return "no", true 91 default: 92 return "", false 93 } 94 } 95 tests := []struct { 96 testName, name string 97 want, err bool 98 }{ 99 {"exists, value ok (1)", "A", true, false}, 100 {"exists, value ok (2)", "B", true, false}, 101 {"exists, value ok (3)", "C", false, false}, 102 {"exists, value not ok", "D", false, true}, 103 {"not exists", "Z", false, false}, 104 } 105 for _, tt := range tests { 106 t.Run(tt.testName, func(t *testing.T) { 107 v := VarSet{[]Lookup{lookup}} 108 got, err := v.Bool(tt.name) 109 if tt.err && err == nil { 110 t.Fatalf("got no error, want one") 111 } else if !tt.err && err != nil { 112 t.Fatalf("got error %v, want none", err) 113 } 114 if got != tt.want { 115 t.Errorf("Bool(%q) = %v, want %v", tt.name, got, tt.want) 116 } 117 }) 118 } 119 } 120 121 func TestMustBool(t *testing.T) { 122 lookup := func(name string) (string, bool) { 123 switch name { 124 case "A": 125 return "true", true 126 case "B": 127 return "1", true 128 case "C": 129 return "TRUE", true 130 case "D": 131 return "false", true 132 case "Y": 133 return "no", true 134 default: 135 return "", false 136 } 137 } 138 tests := []struct { 139 testName, name string 140 want bool 141 err error 142 }{ 143 {"exists (1)", "A", true, nil}, 144 {"exists (2)", "B", true, nil}, 145 {"exists (3)", "C", true, nil}, 146 {"exists (4)", "D", false, nil}, 147 {"not exists", "Z", false, nil}, 148 {"not valid", "Y", false, NewVarNotParsableErr("Y")}, 149 } 150 for _, tt := range tests { 151 t.Run(tt.testName, func(t *testing.T) { 152 defer func() { 153 err := recover() 154 switch err { 155 case nil: 156 if tt.err != nil { 157 t.Fatalf("got no panic error, want %v", tt.err) 158 } 159 default: 160 if tt.err == nil { 161 t.Fatalf("got panic error %v, want none", err) 162 } else if !reflect.DeepEqual(err, tt.err) { 163 t.Fatalf("got panic error %v, want %v", err, tt.err) 164 } 165 } 166 }() 167 v := VarSet{[]Lookup{lookup}} 168 got := v.MustBool(tt.name) 169 if got != tt.want { 170 t.Errorf("MustBool(%q) = %v, want %v", tt.name, got, tt.want) 171 } 172 }) 173 } 174 } 175 176 func TestMustHexEncodedByteArray(t *testing.T) { 177 lookup := func(name string) (string, bool) { 178 switch name { 179 case "A": 180 return "61", true // a 181 case "APPLE": 182 return "6170706c65", true // apple 183 case "PARTIAL": 184 return "6", true 185 default: 186 return "", false 187 } 188 } 189 tests := []struct { 190 testName string 191 name string 192 len int 193 want []byte 194 err error 195 }{ 196 {"exists (1)", "A", 1, []byte("a"), nil}, 197 {"exists (2)", "APPLE", 5, []byte("apple"), nil}, 198 {"not exists", "Z", 1, nil, NewVarNotFoundErr("Z")}, 199 {"cannot decode", "PARTIAL", 1, nil, NewVarNotParsableErr("PARTIAL")}, 200 {"wrong length", "APPLE", 42, nil, NewVarNotParsableErr("APPLE")}, 201 } 202 for _, tt := range tests { 203 t.Run(tt.testName, func(t *testing.T) { 204 defer func() { 205 err := recover() 206 switch err { 207 case nil: 208 if tt.err != nil { 209 t.Fatalf("got no panic error, want %v", tt.err) 210 } 211 default: 212 if tt.err == nil { 213 t.Fatalf("got panic error %v, want none", err) 214 } else if !reflect.DeepEqual(err, tt.err) { 215 t.Fatalf("got panic error %v, want %v", err, tt.err) 216 } 217 } 218 }() 219 v := VarSet{[]Lookup{lookup}} 220 got := v.MustHexEncodedByteArray(tt.name, tt.len) 221 if !bytes.Equal(got, tt.want) { 222 t.Errorf("MustHexEncodedByteArray(%q) = %q, want %q", tt.name, got, tt.want) 223 } 224 }) 225 } 226 } 227 228 func TestIsVarNotFound(t *testing.T) { 229 tests := []struct { 230 name string 231 err error 232 want bool 233 }{ 234 {"true", NewVarNotFoundErr("A"), true}, 235 {"false", errors.New("other"), false}, 236 } 237 for _, tt := range tests { 238 t.Run(tt.name, func(t *testing.T) { 239 if got := IsVarNotFound(tt.err); got != tt.want { 240 t.Errorf("IsVarNotFound(%v) = %v, want %v", tt.err, got, tt.want) 241 } 242 }) 243 } 244 } 245 246 func TestIsVarNotParsable(t *testing.T) { 247 tests := []struct { 248 name string 249 err error 250 want bool 251 }{ 252 {"true", NewVarNotParsableErr("A"), true}, 253 {"false", errors.New("other"), false}, 254 } 255 for _, tt := range tests { 256 t.Run(tt.name, func(t *testing.T) { 257 if got := IsVarNotParsable(tt.err); got != tt.want { 258 t.Errorf("IsVarNotParsable(%v) = %v, want %v", tt.err, got, tt.want) 259 } 260 }) 261 } 262 }