Skip to content

Commit 57c7bc2

Browse files
authored
fix(configtype): Add Equal() method to Duration (#1059)
`cmp.Diff` doesn't like it otherwise.
1 parent 6b3d831 commit 57c7bc2

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

configtype/duration.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ func (d *Duration) MarshalJSON() ([]byte, error) {
3939
func (d *Duration) Duration() time.Duration {
4040
return d.duration
4141
}
42+
43+
func (d Duration) Equal(other Duration) bool {
44+
return d.duration == other.duration
45+
}

configtype/duration_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package configtype
1+
package configtype_test
22

33
import (
44
"encoding/json"
55
"testing"
66
"time"
7+
8+
"github.com/cloudquery/plugin-sdk/v4/configtype"
9+
"github.com/google/go-cmp/cmp"
710
)
811

912
func TestDuration(t *testing.T) {
@@ -16,7 +19,7 @@ func TestDuration(t *testing.T) {
1619
{"-50m30s", -50*time.Minute - 30*time.Second},
1720
}
1821
for _, tc := range cases {
19-
var d Duration
22+
var d configtype.Duration
2023
err := json.Unmarshal([]byte(`"`+tc.give+`"`), &d)
2124
if err != nil {
2225
t.Fatalf("error calling Unmarshal(%q): %v", tc.give, err)
@@ -26,3 +29,26 @@ func TestDuration(t *testing.T) {
2629
}
2730
}
2831
}
32+
33+
func TestComparability(t *testing.T) {
34+
cases := []struct {
35+
give configtype.Duration
36+
compare configtype.Duration
37+
equal bool
38+
}{
39+
{configtype.NewDuration(0), configtype.NewDuration(0), true},
40+
{configtype.NewDuration(0), configtype.NewDuration(1), false},
41+
}
42+
for _, tc := range cases {
43+
if (tc.give == tc.compare) != tc.equal {
44+
t.Errorf("comparing %v and %v should be %v", tc.give, tc.compare, tc.equal)
45+
}
46+
47+
diff := cmp.Diff(tc.give, tc.compare)
48+
if tc.equal && diff != "" {
49+
t.Errorf("comparing %v and %v should be equal, but diff is %s", tc.give, tc.compare, diff)
50+
} else if !tc.equal && diff == "" {
51+
t.Errorf("comparing %v and %v should not be equal, but diff is empty", tc.give, tc.compare)
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)