Skip to content

Commit 38d59c3

Browse files
mengskysamaDivjot Arora
authored and
Divjot Arora
committed
Fix primitive.Regex Equal method (#318)
1 parent 4daf0f2 commit 38d59c3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

bson/primitive/primitive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (rp Regex) String() string {
7070

7171
// Equal compares rp to rp2 and returns true is the are equal.
7272
func (rp Regex) Equal(rp2 Regex) bool {
73-
return rp.Pattern == rp2.Pattern && rp.Options == rp.Options
73+
return rp.Pattern == rp2.Pattern && rp.Options == rp2.Options
7474
}
7575

7676
// IsZero returns if rp is the empty Regex

bson/primitive/primitive_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,23 @@ func TestPrimitiveIsZero(t *testing.T) {
5858
})
5959
}
6060
}
61+
62+
func TestRegexCompare(t *testing.T) {
63+
testcases := []struct {
64+
name string
65+
r1 Regex
66+
r2 Regex
67+
eq bool
68+
}{
69+
{"equal", Regex{Pattern: "foo1", Options: "bar1"}, Regex{Pattern: "foo1", Options: "bar1"}, true},
70+
{"not equal", Regex{Pattern: "foo1", Options: "bar1"}, Regex{Pattern: "foo2", Options: "bar2"}, false},
71+
{"not equal", Regex{Pattern: "foo1", Options: "bar1"}, Regex{Pattern: "foo1", Options: "bar2"}, false},
72+
{"not equal", Regex{Pattern: "foo1", Options: "bar1"}, Regex{Pattern: "foo2", Options: "bar1"}, false},
73+
}
74+
75+
for _, tc := range testcases {
76+
t.Run(tc.name, func(t *testing.T) {
77+
require.True(t, tc.r1.Equal(tc.r2) == tc.eq)
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)