Path: blob/main/components/common-go/util/duration_test.go
2498 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package util_test56import (7"encoding/json"8"fmt"9"testing"10"time"1112"github.com/google/go-cmp/cmp"1314"github.com/gitpod-io/gitpod/common-go/util"15)1617func TestUnmarshalJSON(t *testing.T) {18type carrier struct {19T util.Duration `json:"t"`20}2122tests := []struct {23Input string24Expected *carrier25Error string26}{27{"{\"t\": \"10m\"}", &carrier{T: util.Duration(10 * time.Minute)}, ""},28{"{\"t\": \"doesntParse\"}", nil, "time: invalid duration \"doesntParse\""},29}3031for i, test := range tests {32t.Run(fmt.Sprintf("%03d", i), func(t *testing.T) {33var res carrier34err := json.Unmarshal([]byte(test.Input), &res)35if err != nil {36msg := err.Error()37if msg != test.Error {38t.Errorf("unexpected error \"%s\": expected \"%s\"", msg, test.Error)39}40return41} else if test.Error != "" {42t.Errorf("expected error but saw none")43}4445if diff := cmp.Diff(test.Expected, &res); diff != "" {46t.Errorf("unexpected result (-want +got):\n%s", diff)47}48})49}50}515253