github.com/DominikUrban/viper@v0.0.0-20220730150717-aaf74638bd32/internal/encoding/yaml/codec_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestCodec_Encode(t *testing.T) {
     9  	codec := Codec{}
    10  
    11  	b, err := codec.Encode(data)
    12  	if err != nil {
    13  		t.Fatal(err)
    14  	}
    15  
    16  	if encoded != string(b) {
    17  		t.Fatalf("decoded value does not match the expected one\nactual:   %#v\nexpected: %#v", string(b), encoded)
    18  	}
    19  }
    20  
    21  func TestCodec_Decode(t *testing.T) {
    22  	t.Run("OK", func(t *testing.T) {
    23  		codec := Codec{}
    24  
    25  		v := map[string]interface{}{}
    26  
    27  		err := codec.Decode([]byte(original), v)
    28  		if err != nil {
    29  			t.Fatal(err)
    30  		}
    31  
    32  		if !reflect.DeepEqual(decoded, v) {
    33  			t.Fatalf("decoded value does not match the expected one\nactual:   %#v\nexpected: %#v", v, decoded)
    34  		}
    35  	})
    36  
    37  	t.Run("InvalidData", func(t *testing.T) {
    38  		codec := Codec{}
    39  
    40  		v := map[string]interface{}{}
    41  
    42  		err := codec.Decode([]byte(`invalid data`), v)
    43  		if err == nil {
    44  			t.Fatal("expected decoding to fail")
    45  		}
    46  
    47  		t.Logf("decoding failed as expected: %s", err)
    48  	})
    49  }