github.com/theliebeskind/genfig@v0.1.5-alpha/parsers/dotenv_test.go (about) 1 package parsers_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 . "github.com/theliebeskind/genfig/parsers" 10 ) 11 12 const ( 13 complexDotenv = ` 14 # this is a comment 15 A=a b 16 C_D=1 17 C_E=2 18 F=[2,"3","g"] 19 G=0.5 20 ` 21 ) 22 23 var ( 24 complexDotenvResult = map[string]interface{}{"a": "a b", "c": map[string]interface{}{"d": int64(1), "e": int64(2)}, "f": []interface{}{float64(2), "3", "g"}, "g": 0.5} 25 ) 26 27 func Test_Dotenv(t *testing.T) { 28 type args struct { 29 data []byte 30 } 31 tests := []struct { 32 name string 33 args args 34 want map[string]interface{} 35 wantErr bool 36 }{ 37 {"empty data", args{}, nil, true}, 38 {"invalid data", args{[]byte("foobar´?")}, nil, true}, 39 {"invalid key", args{[]byte("fooba@=12")}, nil, true}, 40 {"valid dotenv", args{[]byte("A=1")}, map[string]interface{}{"a": int64(1)}, false}, 41 {"also valid dotenv", args{[]byte("A: 1")}, map[string]interface{}{"a": int64(1)}, false}, 42 {"valid nested dotenv", args{[]byte("A_B=1")}, map[string]interface{}{"a": map[string]interface{}{"b": int64(1)}}, false}, 43 {"another valid nested dotenv", args{[]byte("A-B=1")}, map[string]interface{}{"a": map[string]interface{}{"b": int64(1)}}, false}, 44 {"double occurency map on basic", args{[]byte("A=1\nA_A=2")}, nil, true}, 45 {"double occurency basic on map", args{[]byte("A_A=2\nA=1")}, nil, true}, 46 {"nested double occurency", args{[]byte("A_A_A=2\nA_A=1")}, nil, true}, 47 {"complex dotenv", args{[]byte(complexDotenv)}, complexDotenvResult, false}, 48 } 49 s := DotenvStrategy{} 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 got, err := s.Parse(tt.args.data) 53 if tt.wantErr { 54 require.Error(t, err) 55 } else { 56 require.NoError(t, err) 57 } 58 assert.Equal(t, tt.want, got) 59 }) 60 } 61 }