MCPcopy Index your code
hub / github.com/coder/coder / TestDuration

Function TestDuration

coderd/httpapi/json_test.go:12–162  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestDuration(t *testing.T) {
13 t.Parallel()
14
15 t.Run("MarshalJSON", func(t *testing.T) {
16 t.Parallel()
17
18 cases := []struct {
19 value time.Duration
20 expected string
21 }{
22 {
23 value: 0,
24 expected: "0s",
25 },
26 {
27 value: 1 * time.Millisecond,
28 expected: "1ms",
29 },
30 {
31 value: 1 * time.Second,
32 expected: "1s",
33 },
34 {
35 value: 1 * time.Minute,
36 expected: "1m0s",
37 },
38 {
39 value: 1 * time.Hour,
40 expected: "1h0m0s",
41 },
42 {
43 value: 1*time.Hour + 1*time.Minute + 1*time.Second + 1*time.Millisecond,
44 expected: "1h1m1.001s",
45 },
46 }
47
48 for _, c := range cases {
49 t.Run(c.expected, func(t *testing.T) {
50 t.Parallel()
51
52 d := httpapi.Duration(c.value)
53 b, err := d.MarshalJSON()
54 require.NoError(t, err)
55 require.Equal(t, `"`+c.expected+`"`, string(b))
56 })
57 }
58 })
59
60 t.Run("UnmarshalJSON", func(t *testing.T) {
61 t.Parallel()
62
63 cases := []struct {
64 value string
65 expected time.Duration
66 }{
67 {
68 value: "0ms",
69 expected: 0,

Callers

nothing calls this directly

Calls 8

MarshalJSONMethod · 0.95
UnmarshalJSONMethod · 0.95
DurationTypeAlias · 0.92
DurationMethod · 0.80
RunMethod · 0.65
EqualMethod · 0.45
ErrorMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected