aboutsummaryrefslogtreecommitdiffstats
path: root/modules/util/time_str_test.go
blob: 8d1de51c8e646633d3f9be3f1d45ebc4bfe16146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2024 Gitea. All rights reserved.
// SPDX-License-Identifier: MIT

package util

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestTimeStr(t *testing.T) {
	t.Run("Parse", func(t *testing.T) {
		// Test TimeEstimateParse
		tests := []struct {
			input  string
			output int64
			err    bool
		}{
			{"1h", 3600, false},
			{"1m", 60, false},
			{"1s", 1, false},
			{"1h 1m 1s", 3600 + 60 + 1, false},
			{"1d1x", 0, true},
		}
		for _, test := range tests {
			t.Run(test.input, func(t *testing.T) {
				output, err := TimeEstimateParse(test.input)
				if test.err {
					assert.Error(t, err)
				} else {
					assert.NoError(t, err)
				}
				assert.Equal(t, test.output, output)
			})
		}
	})
	t.Run("String", func(t *testing.T) {
		tests := []struct {
			input  int64
			output string
		}{
			{3600, "1h"},
			{60, "1m"},
			{1, "1s"},
			{3600 + 1, "1h 1s"},
		}
		for _, test := range tests {
			t.Run(test.output, func(t *testing.T) {
				output := TimeEstimateString(test.input)
				assert.Equal(t, test.output, output)
			})
		}
	})
}