@@ -150,25 +150,30 @@ func Test_camelCaseToUpperSnakeCase(t *testing.T) {
150150
151151func  Test_getName (t  * testing.T ) {
152152	tests  :=  []struct  {
153- 		tag     string 
154- 		tg      string 
155- 		field   string 
156- 		name    string 
157- 		secret   bool 
153+ 		tag       string 
154+ 		tg        string 
155+ 		field     string 
156+ 		name      string 
157+ 		options   options 
158158	}{
159- 		{"t1" , "" , "Name" , "NAME" , false },
160- 		{"t2" , "" , "MyName" , "MY_NAME" , false },
161- 		{"t3" , "NaME" , "Name" , "NaME" , false },
162- 		{"t4" , "NaME,secret" , "Name" , "NaME" , true },
163- 		{"t5" , ",secret" , "Name" , "NAME" , true },
164- 		{"t6" , "NameWith,Comma" , "Name" , "NameWith,Comma" , false },
165- 		{"t7" , "NameWith,Comma,secret" , "Name" , "NameWith,Comma" , true },
159+ 		{"t1" , "" , "Name" , "NAME" , options {optional : false , secret : false }},
160+ 		{"t2" , "" , "MyName" , "MY_NAME" , options {optional : false , secret : false }},
161+ 		{"t3" , "NaME" , "Name" , "NaME" , options {optional : false , secret : false }},
162+ 		{"t4" , "NaME,optional" , "Name" , "NaME" , options {optional : true , secret : false }},
163+ 		{"t5" , "NaME,secret" , "Name" , "NaME" , options {optional : false , secret : true }},
164+ 		{"t6" , ",optional" , "Name" , "NAME" , options {optional : true , secret : false }},
165+ 		{"t7" , ",secret" , "Name" , "NAME" , options {optional : false , secret : true }},
166+ 		{"t8" , ",optional,secret" , "Name" , "NAME" , options {optional : true , secret : true }},
167+ 		{"t9" , ",secret,optional" , "Name" , "NAME" , options {optional : true , secret : true }},
168+ 		{"t10" , "NameWith,Comma" , "Name" , "NameWith,Comma" , options {optional : false , secret : false }},
169+ 		{"t11" , "NameWith,Comma,optional" , "Name" , "NameWith,Comma" , options {optional : true , secret : false }},
170+ 		{"t12" , "NameWith,Comma,optional,secret" , "Name" , "NameWith,Comma" , options {optional : true , secret : true }},
166171	}
167172
168173	for  _ , test  :=  range  tests  {
169- 		name , secret  :=  getName (test .tg , test .field )
174+ 		name , options  :=  getName (test .tg , test .field )
170175		assert .Equal (t , test .name , name , test .tag )
171- 		assert .Equal (t , test .secret ,  secret , test .tag )
176+ 		assert .Equal (t , test .options ,  options , test .tag )
172177	}
173178}
174179
@@ -207,7 +212,18 @@ func mockLookup2(name string) (string, bool) {
207212
208213func  mockLookup3 (name  string ) (string , bool ) {
209214	data  :=  map [string ]string {
210- 		"PORT" : "a8080" ,
215+ 		"HOST" : "localhost" ,
216+ 		"PORT" : "a8080" , // invalid `int` 
217+ 		"URL" :  "http://example.com" ,
218+ 	}
219+ 	value , ok  :=  data [name ]
220+ 	return  value , ok 
221+ }
222+ 
223+ func  mockLookup4 (name  string ) (string , bool ) {
224+ 	data  :=  map [string ]string {
225+ 		"PORT" : "8080" ,
226+ 		"URL" :  "http://example.com" ,
211227	}
212228	value , ok  :=  data [name ]
213229	return  value , ok 
@@ -235,6 +251,12 @@ type Config3 struct {
235251	Embedded 
236252}
237253
254+ type  Config4  struct  {
255+ 	Host  string  `env:",optional"` 
256+ 	Port  int 
257+ 	Embedded 
258+ }
259+ 
238260func  TestLoader_Load (t  * testing.T ) {
239261	l  :=  NewWithLookup ("" , mockLookup , nil )
240262
@@ -267,12 +289,22 @@ func TestLoader_Load(t *testing.T) {
267289	var  cfg3  Config1 
268290	l  =  NewWithLookup ("" , mockLookup3 , nil )
269291	err  =  l .Load (& cfg3 )
270- 	assert .NotNil (t , err )
292+ 	assert .EqualError (t , err ,  "error reading  \" Port \" : strconv.ParseInt: parsing  \" a8080 \" : invalid syntax" 
271293
272294	var  cfg4  Config3 
273295	l  =  NewWithLookup ("" , mockLookup3 , nil )
274296	err  =  l .Load (& cfg4 )
275- 	assert .NotNil (t , err )
297+ 	assert .EqualError (t , err , "error reading \" Port\" : strconv.ParseInt: parsing \" a8080\" : invalid syntax" )
298+ 
299+ 	var  cfg5  Config1 
300+ 	l  =  NewWithLookup ("T_" , mockLookup4 , nil )
301+ 	err  =  l .Load (& cfg5 )
302+ 	assert .EqualError (t , err , "missing required environment variable \" T_HOST\" " )
303+ 
304+ 	var  cfg6  Config4 
305+ 	l  =  NewWithLookup ("" , mockLookup4 , nil )
306+ 	err  =  l .Load (& cfg6 )
307+ 	assert .Nil (t , err )
276308}
277309
278310func  TestNew (t  * testing.T ) {
0 commit comments