package yamlutil

import (
	"bytes"
	"testing"

	yaml "gopkg.in/yaml.v2"
)

func TestBlob(t *testing.T) {
	testCases := []struct {
		descr string
		in    Blob
		exp   string
	}{
		{
			descr: "empty",
			in:    Blob(""),
			exp:   `""`,
		},
		{
			descr: "zero",
			in:    Blob{0},
			exp:   "AA==",
		},
		{
			descr: "zeros",
			in:    Blob{0, 0, 0},
			exp:   "AAAA",
		},
		{
			descr: "foo",
			in:    Blob("foo"),
			exp:   "Zm9v",
		},
	}

	for _, test := range testCases {
		t.Run(test.descr, func(t *testing.T) {
			out, err := yaml.Marshal(test.in)
			if err != nil {
				t.Fatalf("error marshaling %q: %v", test.in, err)
			} else if test.exp+"\n" != string(out) {
				t.Fatalf("marshal exp:%q got:%q", test.exp+"\n", out)
			}

			var blob Blob
			if err := yaml.Unmarshal(out, &blob); err != nil {
				t.Fatalf("error unmarshaling %q: %v", out, err)
			} else if !bytes.Equal([]byte(blob), []byte(test.in)) {
				t.Fatalf("unmarshal exp:%q got:%q", test.in, blob)
			}
		})
	}
}