-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_equal.go
42 lines (36 loc) · 1.02 KB
/
data_equal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package check
import (
"context"
"fmt"
"net/http"
"reflect"
)
// UnexpectedDataValueError is returned when a check fails.
type UnexpectedDataValueError struct {
// DataID is the ID of the data.
DataID string
// Expected is the expected value.
Expected interface{}
// Actual is the actual value.
Actual interface{}
}
// Error returns an error string.
func (e *UnexpectedDataValueError) Error() string {
return fmt.Sprintf("unexpected data value for %s: expected %v, got %v", e.DataID, e.Expected, e.Actual)
}
// DataEqualChecker is used to check whether or not the value stored in the context data is equal to the given value
type DataEqualChecker struct {
DataID string
Value interface{}
}
// Check performs the DataEqual check
func (c *DataEqualChecker) Check(ctx context.Context, response *http.Response) error {
if exp, got := c.Value, DataIDFromContext(ctx, c.DataID); !reflect.DeepEqual(exp, got) {
return &UnexpectedDataValueError{
DataID: c.DataID,
Expected: exp,
Actual: got,
}
}
return nil
}