Skip to content

Commit

Permalink
Add Equals function for QualValue. Closes #646
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre authored Sep 14, 2023
1 parent c0b7ab2 commit 66e7199
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
1 change: 0 additions & 1 deletion grpc/proto/qual.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proto
import "log"

func (x *Qual) Equals(other *Qual) bool {
//log.Printf("[TRACE] me %s, other %s", x.String(), other.String())
return x.String() == other.String()
}

Expand Down
5 changes: 5 additions & 0 deletions grpc/proto/qual_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package proto

func (x *QualValue) Equals(other *QualValue) bool {
return x.String() == other.String()
}
99 changes: 99 additions & 0 deletions grpc/proto/qual_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package proto

import (
"testing"
"time"
)

type qualValuesEqualTest struct {
q1 *QualValue
q2 *QualValue
expected bool
}

var testCasesQualValuesEquals = map[string]qualValuesEqualTest{
"same string": {
&QualValue{Value: &QualValue_StringValue{StringValue: "a"}},
&QualValue{Value: &QualValue_StringValue{StringValue: "a"}},
true,
},
"diff string": {
&QualValue{Value: &QualValue_StringValue{StringValue: "a"}},
&QualValue{Value: &QualValue_StringValue{StringValue: "b"}},
false,
},
"same int64": {
&QualValue{Value: &QualValue_Int64Value{Int64Value: 100}},
&QualValue{Value: &QualValue_Int64Value{Int64Value: 100}},
true,
},
"diff int64": {
&QualValue{Value: &QualValue_Int64Value{Int64Value: 100}},
&QualValue{Value: &QualValue_Int64Value{Int64Value: 1000}},
false,
},
"same double": {
&QualValue{Value: &QualValue_DoubleValue{DoubleValue: 100}},
&QualValue{Value: &QualValue_DoubleValue{DoubleValue: 100}},
true,
},
"diff double": {
&QualValue{Value: &QualValue_DoubleValue{DoubleValue: 100.5}},
&QualValue{Value: &QualValue_DoubleValue{DoubleValue: 100.6}},
false,
},
"same inet": {
&QualValue{Value: toInet("192.168.0.1")},
&QualValue{Value: toInet("192.168.0.1")},
true,
},
"diff inet": {
&QualValue{Value: toInet("192.168.0.1")},
&QualValue{Value: toInet("192.168.10.1")},
false,
},
"same bool": {
&QualValue{Value: &QualValue_BoolValue{BoolValue: true}},
&QualValue{Value: &QualValue_BoolValue{BoolValue: true}},
true,
},
"diff bool": {
&QualValue{Value: &QualValue_BoolValue{BoolValue: true}},
&QualValue{Value: &QualValue_BoolValue{BoolValue: false}},
false,
},
"same timestamp": {
getTimestampValue(now),
getTimestampValue(now),
true,
},
"diff timestamp": {
getTimestampValue(now),
getTimestampValue(now.Add(1 * time.Minute)),
false,
},
"same list": {
toStringList("a", "b"),
toStringList("a", "b"),
true,
},
"diff list": {
toStringList("a", "b"),
toStringList("a", "c"),
false,
},
"diff types": {
&QualValue{Value: &QualValue_StringValue{StringValue: "a"}},
&QualValue{Value: &QualValue_Int64Value{Int64Value: 100}},
false,
},
}

func TestQualValuesEquals(t *testing.T) {
for name, test := range testCasesQualValuesEquals {
result := test.q1.Equals(test.q2)
if result != test.expected {
t.Errorf("Test: '%s' FAILED : \nexpected:\n %v \ngot:\n %v\n", name, test.expected, result)
}
}
}

0 comments on commit 66e7199

Please sign in to comment.