github.com/coveo/gotemplate@v2.7.7+incompatible/yaml/yaml_test.go (about) 1 package yaml 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/coveo/gotemplate/collections" 9 ) 10 11 func Test_list_String(t *testing.T) { 12 t.Parallel() 13 14 tests := []struct { 15 name string 16 l yamlList 17 want string 18 }{ 19 {"Nil", nil, "[]\n"}, 20 {"Empty List", yamlList{}, "[]\n"}, 21 {"List of int", yamlList{1, 2, 3}, collections.UnIndent(` 22 - 1 23 - 2 24 - 3 25 `)[1:]}, 26 {"List of string", strFixture, collections.UnIndent(` 27 - Hello 28 - World, 29 - I'm 30 - Foo 31 - Bar! 32 `)[1:]}, 33 } 34 for _, tt := range tests { 35 t.Run(tt.name, func(t *testing.T) { 36 if got := tt.l.String(); got != tt.want { 37 t.Errorf("yamlList.String():\ngot:\n%v\nwant:\n%v", got, tt.want) 38 } 39 }) 40 } 41 } 42 43 func Test_dict_String(t *testing.T) { 44 t.Parallel() 45 46 tests := []struct { 47 name string 48 d yamlDict 49 want string 50 }{ 51 {"nil", nil, "{}\n"}, 52 {"Map", dictFixture, collections.UnIndent(` 53 float: 1.23 54 int: 123 55 list: 56 - 1 57 - two 58 listInt: 59 - 1 60 - 2 61 - 3 62 map: 63 sub1: 1 64 sub2: two 65 mapInt: 66 "1": 1 67 "2": two 68 string: Foo bar 69 `)[1:]}, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 if got := tt.d.String(); got != tt.want { 74 t.Errorf("yamlDict.String():\ngot:\n%v\nwant:\n%v", got, tt.want) 75 } 76 }) 77 } 78 } 79 80 func TestUnmarshal(t *testing.T) { 81 t.Parallel() 82 83 tests := []struct { 84 name string 85 yaml string 86 want interface{} 87 }{ 88 {"nil", "{}\n", yamlDict{}}, 89 {"Map", fmt.Sprint(dictFixture), dictFixture}, 90 } 91 for _, tt := range tests { 92 t.Run(tt.name, func(t *testing.T) { 93 var out interface{} 94 err := Unmarshal([]byte(tt.yaml), &out) 95 if err == nil && !reflect.DeepEqual(out, tt.want) { 96 t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want) 97 } 98 }) 99 } 100 } 101 102 func TestUnmarshalWithError(t *testing.T) { 103 t.Parallel() 104 105 tests := []struct { 106 name string 107 yaml string 108 }{ 109 {"Error", "Invalid"}, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 var out map[string]interface{} 114 err := Unmarshal([]byte(tt.yaml), &out) 115 if err == nil { 116 t.Errorf("Unmarshal() expected error") 117 } 118 }) 119 } 120 } 121 122 func TestUnmarshalStrict(t *testing.T) { 123 t.Parallel() 124 125 tests := []struct { 126 name string 127 yaml string 128 want interface{} 129 wantErr bool 130 }{ 131 {"nil", "{}\n", map[string]interface{}{}, false}, 132 {"Map", fmt.Sprint(dictFixture), dictFixture.Native(), false}, 133 {"Error", "Invalid", nil, true}, 134 } 135 for _, tt := range tests { 136 t.Run(tt.name, func(t *testing.T) { 137 var out map[string]interface{} 138 err := UnmarshalStrict([]byte(tt.yaml), &out) 139 if (err != nil) != tt.wantErr { 140 t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) 141 } 142 if err == nil && !reflect.DeepEqual(out, tt.want) { 143 t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want) 144 } 145 }) 146 } 147 }