Skip to content

Commit

Permalink
Merge pull request #3 from armosec/middleware
Browse files Browse the repository at this point in the history
compareAndUpdate
  • Loading branch information
avrahams authored Jul 10, 2023
2 parents 1c361a9 + 6c14e81 commit 968cd9a
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ func SaveExpected(t *testing.T, fileName string, i interface{}) {
assert.False(t, true, "update expected is true, set to false and rerun test")
}

func CompareAndUpdate[T any](t *testing.T, actual T, expectedBytes []byte, expectedFileName string, update bool, compareOptions ...cmp.Option) {
expected := loadJson[T](expectedBytes)
diff := cmp.Diff(expected, actual, compareOptions...)
assert.Empty(t, diff, "expected to have no diff")
if update && diff != "" {
SaveExpected(t, expectedFileName, actual)
}
assert.False(t, update, "update expected is true, set to false and rerun test")
}

func loadJson[T any](jsonBytes []byte) T {
var obj T
if err := json.Unmarshal(jsonBytes, &obj); err != nil {
panic(err)
}
return obj
}

func CompareOrUpdate[T any](actual T, expectedBytes []byte, expectedFileName string, t *testing.T, update bool) {
if !Equal(t, actual, expectedBytes) && update {
SaveExpected(t, expectedFileName, actual)
Expand Down

0 comments on commit 968cd9a

Please sign in to comment.