github.com/theliebeskind/genfig@v0.1.5-alpha/writers/config_test.go (about) 1 package writers_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/theliebeskind/genfig/writers" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 "github.com/theliebeskind/genfig/models" 12 "github.com/theliebeskind/genfig/util" 13 ) 14 15 func Test_WriteConfig(t *testing.T) { 16 tests := []struct { 17 name string 18 config map[string]interface{} 19 def map[string]interface{} 20 contains []string 21 wantErr bool 22 }{ 23 {"empty", map[string]interface{}{}, nil, []string{}, false}, 24 {"simple string", map[string]interface{}{"a": "b"}, nil, []string{"A: \"b\""}, false}, 25 {"simple string with default", map[string]interface{}{"a": "b"}, map[string]interface{}{"a": "def"}, []string{"A: \"b\""}, false}, 26 {"simple int", map[string]interface{}{"a": 1}, nil, []string{"A: 1"}, false}, 27 {"simple bool", map[string]interface{}{"a": true}, nil, []string{"A: true"}, false}, 28 {"int array", map[string]interface{}{"a": []int{1, 2, 3}}, nil, []string{"A: []int"}, false}, 29 {"empy interface array", map[string]interface{}{"a": []interface{}{}}, nil, []string{"A: []interface {}{}"}, false}, 30 {"mixed interface array", map[string]interface{}{"a": []interface{}{1, ""}}, nil, []string{"A: []interface {}{1, \"\"}"}, false}, 31 {"int interface array", map[string]interface{}{"a": []interface{}{1, 2}}, nil, []string{"A: []int{1, 2}"}, false}, 32 {"string interface array", map[string]interface{}{"a": []interface{}{"a", "b"}}, nil, []string{"A: []string"}, false}, 33 {"map", map[string]interface{}{"a": map[string]interface{}{"b": 1}}, nil, []string{"A: ConfigA{", "B: 1"}, false}, 34 {"map with interface key", map[string]interface{}{"a": map[interface{}]interface{}{"b": 1}}, nil, []string{"A: ConfigA{", "B: 1"}, false}, 35 {"map of map", map[string]interface{}{"a": map[string]interface{}{"b": map[string]interface{}{"c": 1}}}, nil, []string{"A: ConfigA{", "B: ConfigAB{", "C: 1"}, false}, 36 } 37 for _, tt := range tests { 38 t.Run(tt.name, func(t *testing.T) { 39 s := &strings.Builder{} 40 def := tt.def 41 if def == nil { 42 def = tt.config 43 } 44 err := writers.WriteConfig(s, models.SchemaMap{ 45 "ConfigA": models.Schema{}, 46 "ConfigAB": models.Schema{}, 47 "ConfigABC": models.Schema{}, 48 }, tt.config, def, "test") 49 if tt.wantErr { 50 require.Error(t, err) 51 return 52 } 53 got := s.String() 54 require.NoError(t, err) 55 for _, c := range tt.contains { 56 assert.Contains(t, got, c) 57 } 58 }) 59 } 60 } 61 62 func Benchmark_WriteConfigValue(b *testing.B) { 63 w := util.NoopWriter{} 64 s := models.SchemaMap{ 65 "ConfigA": models.Schema{}, 66 "ConfigAB": models.Schema{}, 67 "ConfigABC": models.Schema{}, 68 "ConfigABD": models.Schema{}, 69 "ConfigABE": models.Schema{}, 70 } 71 m := map[string]interface{}{"a": map[interface{}]interface{}{"b": map[string]interface{}{"c": []interface{}{1}, "d": "s", "e": 1}}} 72 e := map[string]interface{}{} 73 for n := 0; n < b.N; n++ { 74 writers.WriteConfigValue(w, "Config", m, e, s, 0) 75 } 76 }