Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: replace reflect.DeepEqual where possible #7286

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package cmd
import (
"bytes"
"path"
"reflect"
"slices"
"sort"
"testing"

Expand Down Expand Up @@ -129,11 +129,11 @@ func TestCapabilitiesCurrent(t *testing.T) {
t.Fatal("expected success", err)
}

if !reflect.DeepEqual(caps.Features, tc.expFeatures) {
if !slices.Equal(caps.Features, tc.expFeatures) {
t.Errorf("expected features:\n\n%v\n\nbut got:\n\n%v", tc.expFeatures, caps.Features)
}

if !reflect.DeepEqual(caps.FutureKeywords, tc.expFutureKeywords) {
if !slices.Equal(caps.FutureKeywords, tc.expFutureKeywords) {
t.Errorf("expected future keywords:\n\n%v\n\nbut got:\n\n%v", tc.expFutureKeywords, caps.FutureKeywords)
}
})
Expand Down
19 changes: 10 additions & 9 deletions cmd/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -320,7 +321,7 @@ func TestEvalWithOptimize(t *testing.T) {
files := map[string]string{
"test.rego": `
package test

default p = false
p if { q }
q if { input.x = data.foo }`,
Expand Down Expand Up @@ -389,7 +390,7 @@ func TestEvalWithOptimizeBundleData(t *testing.T) {
files := map[string]string{
"test.rego": `
package test

default p = false
p if { q }
q if { input.x = data.foo }`,
Expand Down Expand Up @@ -1303,7 +1304,7 @@ func TestEvalDebugTraceJSONOutput(t *testing.T) {
for _, actual := range evals {
if expected.location.Compare(actual.location) == 0 {
found = true
if !reflect.DeepEqual(expected.varBindings, actual.varBindings) {
if !maps.Equal(expected.varBindings, actual.varBindings) {
t.Errorf("Expected var bindings:\n\n\t%+v\n\nGot\n\n\t%+v\n\n", expected.varBindings, actual.varBindings)
}
}
Expand Down Expand Up @@ -2482,7 +2483,7 @@ func TestBundleWithStrictFlag(t *testing.T) {
func TestIfElseIfElseNoBrace(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

p if false
else := 1 if false
else := 2`,
Expand All @@ -2507,7 +2508,7 @@ func TestIfElseIfElseNoBrace(t *testing.T) {
func TestIfElseIfElseBrace(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

p if false
else := 1 if { false }
else := 2`,
Expand All @@ -2532,7 +2533,7 @@ func TestIfElseIfElseBrace(t *testing.T) {
func TestIfElse(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

p if false
else := 1 `,
}
Expand Down Expand Up @@ -2584,7 +2585,7 @@ func TestElseNoIfV0(t *testing.T) {
func TestElseIf(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

p if false
else := x if {
x=2
Expand Down Expand Up @@ -2641,7 +2642,7 @@ func TestElseIfElseV0(t *testing.T) {
func TestUnexpectedElseIfElseErr(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

p if false
else := x if {
x=2
Expand Down Expand Up @@ -2679,7 +2680,7 @@ func TestUnexpectedElseIfElseErr(t *testing.T) {
func TestUnexpectedElseIfErr(t *testing.T) {
files := map[string]string{
"bug.rego": `package bug

q := 1 if false
else := 2 if
`,
Expand Down
30 changes: 15 additions & 15 deletions cmd/refactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cmd

import (
"bytes"
"maps"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/open-policy-agent/opa/v1/ast"
Expand All @@ -23,37 +23,37 @@ func TestDoMoveRenamePackage(t *testing.T) {
note: "v0",
v0Compatible: true,
module: `package lib.foo

# this is a comment
default allow = false

allow {
input.message == "hello" # this is a comment too
}`,
expected: ast.MustParseModuleWithOpts(`package baz.bar

# this is a comment
default allow = false

allow {
input.message == "hello" # this is a comment too
}`, ast.ParserOptions{RegoVersion: ast.RegoV0}),
},
{
note: "v1",
module: `package lib.foo

# this is a comment
default allow = false

allow if {
input.message == "hello" # this is a comment too
}`,
expected: ast.MustParseModuleWithOpts(`package baz.bar

# this is a comment
default allow = false

allow if {
input.message == "hello" # this is a comment too
}`, ast.ParserOptions{RegoVersion: ast.RegoV1}),
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestDoMoveRenamePackage(t *testing.T) {
formatted = format.MustAstWithOpts(tc.expected, format.Opts{RegoVersion: ast.RegoV1})
}

if !reflect.DeepEqual(formatted, buf.Bytes()) {
if !bytes.Equal(formatted, buf.Bytes()) {
t.Fatalf("Expected module:\n%v\n\nGot:\n%v\n", string(formatted), buf.String())
}
})
Expand All @@ -110,17 +110,17 @@ func TestDoMoveOverwriteFile(t *testing.T) {
module: `package lib.foo

import data.x.q

default allow := false

allow {
input.message == "hello"
}
`,
expected: ast.MustParseModuleWithOpts(`package baz.bar

import data.hidden.q

default allow := false

allow {
Expand All @@ -132,17 +132,17 @@ func TestDoMoveOverwriteFile(t *testing.T) {
module: `package lib.foo

import data.x.q

default allow := false

allow if {
input.message == "hello"
}
`,
expected: ast.MustParseModuleWithOpts(`package baz.bar

import data.hidden.q

default allow := false

allow if {
Expand Down Expand Up @@ -202,7 +202,7 @@ func TestParseSrcDstMap(t *testing.T) {

expected := map[string]string{"data.lib.foo": "data.baz.bar", "data": "data.acme"}

if !reflect.DeepEqual(actual, expected) {
if !maps.Equal(actual, expected) {
t.Fatalf("Expected mapping %v but got %v", expected, actual)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"encoding/json"
"fmt"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestInitRuntimeCipherSuites(t *testing.T) {
t.Fatal("Expected error but got nil")
} else if err == nil {
if len(tc.expCipherSuites) > 0 {
if !reflect.DeepEqual(*rt.Params.CipherSuites, tc.expCipherSuites) {
if !slices.Equal(*rt.Params.CipherSuites, tc.expCipherSuites) {
t.Fatalf("expected cipher suites %v but got %v", tc.expCipherSuites, *rt.Params.CipherSuites)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"context"
"fmt"
"io/fs"
"maps"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -755,7 +755,7 @@ func TestCompilerBundleMergeWithBundleRegoVersion(t *testing.T) {

compareRegoVersions(t, tc.expGlobalRegoVersion, result.Manifest.RegoVersion)

if !reflect.DeepEqual(tc.expFileRegoVersions, result.Manifest.FileRegoVersions) {
if !maps.Equal(tc.expFileRegoVersions, result.Manifest.FileRegoVersions) {
t.Fatalf("expected file rego versions to be:\n\n%v\n\nbut got:\n\n%v", tc.expFileRegoVersions, result.Manifest.FileRegoVersions)
}
})
Expand Down
3 changes: 2 additions & 1 deletion internal/bundle/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -67,7 +68,7 @@ func TestGenerateBundleInfoWithFileDir(t *testing.T) {

expBuiltinNames := []string{"eq", "gt"}

if !reflect.DeepEqual(expBuiltinNames, builtinNames) {
if !slices.Equal(expBuiltinNames, builtinNames) {
t.Fatalf("expected builtin names to be %v but got %v", expBuiltinNames, builtinNames)
}
})
Expand Down
4 changes: 2 additions & 2 deletions internal/json/patch/patch_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package patch

import (
"reflect"
"slices"
"testing"

"github.com/open-policy-agent/opa/v1/storage"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestParsePatchPathEscaped(t *testing.T) {
t.Fatalf("Expected ok to be %v but was %v", tc.expectedOK, actualOK)
}

if !reflect.DeepEqual(tc.expectedPath, actualPath) {
if !slices.Equal(tc.expectedPath, actualPath) {
t.Fatalf("Expected path to be %v but was %v", tc.expectedPath, actualPath)
}
})
Expand Down
4 changes: 2 additions & 2 deletions internal/pathwatcher/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package pathwatcher

import (
"path/filepath"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -34,7 +34,7 @@ func TestWatchPaths(t *testing.T) {
for _, p := range paths {
result = append(result, filepath.Clean(strings.TrimPrefix(p, rootDir)))
}
if !reflect.DeepEqual(expected, result) {
if !slices.Equal(expected, result) {
t.Fatalf("Expected %q but got: %q", expected, result)
}
})
Expand Down
3 changes: 2 additions & 1 deletion internal/planner/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -1222,7 +1223,7 @@ func (w *stmtCmpWalker) Visit(x interface{}) (ir.Visitor, error) {
f, ok := x.(*ir.Func)
if ok && s.Name == f.Name {
w.found = true
if !reflect.DeepEqual(s.Path, f.Path) {
if !slices.Equal(s.Path, f.Path) {
return nil, fmt.Errorf("func %v: expected path %v, got %v", f, s.Path, f.Path)
}
}
Expand Down
3 changes: 2 additions & 1 deletion v1/ast/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -1502,7 +1503,7 @@ func TestCheckErrorDetails(t *testing.T) {
}

for _, tc := range tests {
if !reflect.DeepEqual(tc.detail.Lines(), tc.expected) {
if !slices.Equal(tc.detail.Lines(), tc.expected) {
t.Errorf("Expected %v for %v but got: %v", tc.expected, tc.detail, tc.detail.Lines())
}
}
Expand Down
Loading