@@ -17,6 +17,7 @@ limitations under the License.
1717package yaml
1818
1919import (
20+ "bytes"
2021 "encoding/json"
2122 "fmt"
2223 "math"
@@ -26,7 +27,7 @@ import (
2627 "testing"
2728
2829 "github.com/davecgh/go-spew/spew"
29- yaml "gopkg.in/yaml.v2"
30+ "gopkg.in/yaml.v2"
3031)
3132
3233/* Test helper functions */
5960 funcUnmarshalStrict testUnmarshalFunc = func (yamlBytes []byte , obj interface {}) error {
6061 return UnmarshalStrict (yamlBytes , obj )
6162 }
63+
64+ funcDecode testUnmarshalFunc = func (yamlBytes []byte , obj interface {}) error {
65+ return NewDecoder (bytes .NewReader (yamlBytes )).Decode (obj )
66+ }
67+
68+ funcDecodeStrict testUnmarshalFunc = func (yamlBytes []byte , obj interface {}) error {
69+ decoder := NewDecoder (bytes .NewReader (yamlBytes ))
70+ decoder .SetStrict ()
71+ return decoder .Decode (obj )
72+ }
6273)
6374
6475func testUnmarshal (t * testing.T , f testUnmarshalFunc , tests map [string ]unmarshalTestCase ) {
@@ -168,6 +179,43 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ
168179 }
169180}
170181
182+ type testMarshalFunc = func (obj interface {}) ([]byte , error )
183+
184+ var (
185+ funcMarshal testMarshalFunc = func (obj interface {}) ([]byte , error ) {
186+ return Marshal (obj )
187+ }
188+ funcEncode testMarshalFunc = func (obj interface {}) ([]byte , error ) {
189+ var buf bytes.Buffer
190+ encoder := NewEncoder (& buf )
191+ if err := encoder .Encode (obj ); err != nil {
192+ return nil , err
193+ }
194+ return buf .Bytes (), nil
195+ }
196+ )
197+
198+ type marshalTestCase struct {
199+ obj interface {}
200+ encoded []byte
201+ }
202+
203+ func testMarshal (t * testing.T , f testMarshalFunc , tests map [string ]marshalTestCase ) {
204+ for testName , test := range tests {
205+ t .Run (testName , func (t * testing.T ) {
206+ y , err := f (test .obj )
207+ if err != nil {
208+ t .Errorf ("error marshaling YAML: %v" , err )
209+ }
210+
211+ if ! reflect .DeepEqual (y , test .encoded ) {
212+ t .Errorf ("marshal YAML was unsuccessful, expected: %#v, got: %#v" ,
213+ string (test .encoded ), string (y ))
214+ }
215+ })
216+ }
217+ }
218+
171219/* Start tests */
172220
173221type MarshalTest struct {
@@ -180,18 +228,20 @@ type MarshalTest struct {
180228
181229func TestMarshal (t * testing.T ) {
182230 f32String := strconv .FormatFloat (math .MaxFloat32 , 'g' , - 1 , 32 )
183- s := MarshalTest {"a" , math .MaxInt64 , math .MaxFloat32 }
184- e := []byte (fmt .Sprintf ("A: a\n B: %d\n C: %s\n " , math .MaxInt64 , f32String ))
185-
186- y , err := Marshal (s )
187- if err != nil {
188- t .Errorf ("error marshaling YAML: %v" , err )
231+ tests := map [string ]marshalTestCase {
232+ "max" : {
233+ obj : MarshalTest {"a" , math .MaxInt64 , math .MaxFloat32 },
234+ encoded : []byte (fmt .Sprintf ("A: a\n B: %d\n C: %s\n " , math .MaxInt64 , f32String )),
235+ },
189236 }
190237
191- if ! reflect .DeepEqual (y , e ) {
192- t .Errorf ("marshal YAML was unsuccessful, expected: %#v, got: %#v" ,
193- string (e ), string (y ))
194- }
238+ t .Run ("Marshal" , func (t * testing.T ) {
239+ testMarshal (t , funcMarshal , tests )
240+ })
241+
242+ t .Run ("Encode" , func (t * testing.T ) {
243+ testMarshal (t , funcEncode , tests )
244+ })
195245}
196246
197247type UnmarshalUntaggedStruct struct {
@@ -550,6 +600,14 @@ func TestUnmarshal(t *testing.T) {
550600 t .Run ("UnmarshalStrict" , func (t * testing.T ) {
551601 testUnmarshal (t , funcUnmarshalStrict , tests )
552602 })
603+
604+ t .Run ("Decode" , func (t * testing.T ) {
605+ testUnmarshal (t , funcDecode , tests )
606+ })
607+
608+ t .Run ("DecodeStrict" , func (t * testing.T ) {
609+ testUnmarshal (t , funcDecodeStrict , tests )
610+ })
553611}
554612
555613func TestUnmarshalStrictFails (t * testing.T ) {
@@ -618,6 +676,19 @@ func TestUnmarshalStrictFails(t *testing.T) {
618676 }
619677 testUnmarshal (t , funcUnmarshalStrict , failTests )
620678 })
679+
680+ t .Run ("Decode" , func (t * testing.T ) {
681+ testUnmarshal (t , funcDecode , tests )
682+ })
683+
684+ t .Run ("DecodeStrict" , func (t * testing.T ) {
685+ failTests := map [string ]unmarshalTestCase {}
686+ for name , test := range tests {
687+ test .err = fatalErrorsType
688+ failTests [name ] = test
689+ }
690+ testUnmarshal (t , funcDecodeStrict , failTests )
691+ })
621692}
622693
623694func TestYAMLToJSON (t * testing.T ) {
0 commit comments