Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/common-go/util/duration_test.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package util_test
6
7
import (
8
"encoding/json"
9
"fmt"
10
"testing"
11
"time"
12
13
"github.com/google/go-cmp/cmp"
14
15
"github.com/gitpod-io/gitpod/common-go/util"
16
)
17
18
func TestUnmarshalJSON(t *testing.T) {
19
type carrier struct {
20
T util.Duration `json:"t"`
21
}
22
23
tests := []struct {
24
Input string
25
Expected *carrier
26
Error string
27
}{
28
{"{\"t\": \"10m\"}", &carrier{T: util.Duration(10 * time.Minute)}, ""},
29
{"{\"t\": \"doesntParse\"}", nil, "time: invalid duration \"doesntParse\""},
30
}
31
32
for i, test := range tests {
33
t.Run(fmt.Sprintf("%03d", i), func(t *testing.T) {
34
var res carrier
35
err := json.Unmarshal([]byte(test.Input), &res)
36
if err != nil {
37
msg := err.Error()
38
if msg != test.Error {
39
t.Errorf("unexpected error \"%s\": expected \"%s\"", msg, test.Error)
40
}
41
return
42
} else if test.Error != "" {
43
t.Errorf("expected error but saw none")
44
}
45
46
if diff := cmp.Diff(test.Expected, &res); diff != "" {
47
t.Errorf("unexpected result (-want +got):\n%s", diff)
48
}
49
})
50
}
51
}
52
53