package main

import (
	"testing"
	"time"

	"gopkg.in/yaml.v2"
)

func TestDurationYAML(t *testing.T) {
	cases := map[string]time.Duration{
		"":           0,
		"1s":         time.Second,
		"1m1s":       time.Minute + time.Second,
		"1h1s":       time.Hour + time.Second,
		"1d":         24 * time.Hour,
		"1w":         7 * 24 * time.Hour,
		"1w2d3h4m5s": 7*24*time.Hour + 2*24*time.Hour + 3*time.Hour + 4*time.Minute + 5*time.Second,
	}

	for name, wantd := range cases {
		want := wantd
		t.Run(name, func(t *testing.T) {
			var d1, d2 duration
			if err := yaml.Unmarshal([]byte(name), &d1); err != nil {
				t.Error(err)
			} else if time.Duration(d1) != want {
				t.Error(d1)
			} else if b, err := yaml.Marshal(d1); err != nil {
				t.Error(err)
			} else if err := yaml.Unmarshal(b, &d2); err != nil {
				t.Error(err)
			} else if d1 != d2 {
				t.Error(d1, d2)
			}
		})
	}
}
