utils_test.go 903 字节
package utils

import (
	"github.com/stretchr/testify/assert"
	"testing"
	"time"
)

func TestTimeSpan(t *testing.T) {
	inputs := []struct {
		t string
		e bool
	}{
		{"10:00", false},
		{"10:11", false},
		{"24:00", true},
		{"-1:00", true},
		{"10:60", true},
		{"A:B", true},
	}
	for i := range inputs {
		err := ValidWorkTime(inputs[i].t)
		if inputs[i].e {
			assert.NotNil(t, err)
		} else {
			assert.Nil(t, err)
		}
	}
}

func TestComputeTimeDuration(t *testing.T) {
	assertDuration := func(s string) time.Duration {
		t, _ := time.ParseDuration(s)
		return t
	}
	inputs := []struct {
		t1 string
		t2 string
		ts time.Duration
	}{
		{
			"9:00", "18:00", assertDuration("9h"),
		},
		{
			"23:00", "6:00", assertDuration("7h"),
		},
	}
	for i := range inputs {
		input := inputs[i]
		out, err := ComputeTimeDuration(input.t1, input.t2)
		assert.Nil(t, err)
		assert.Equal(t, input.ts, out)
	}
}