Skip to content

Commit

Permalink
internal: remove some deprecated type/method usages
Browse files Browse the repository at this point in the history
Motivated by:

    - cuetdtest/M.Runtime already existing, but nevertheless a lot of
      avoidable calls to cuetdtest/M.UpdateRuntime

cuetdtest:

    - Add M.Context and express M.Runtime as a wrapper around M.Context
    - Remove M.UpdateRuntime

Because M.Context returns a cue.Context, this creates an import cycle,
caused by cue/types_test.go, cue/decode_test.go and
cue/attribute_test.go being in package cue.

    - Move cue/types_test.go, cue/decode_test.go and
      cue_attribute_test.go to package cue_test in order to break new
      import cycle.
    - Similary, subsume/{export|value}_test.go move to subsume_test

This gives the opportunity to rework these tests (especially
types_test.go) to:

    - Use the M.Context method, and cue.Context instead of cue.Runtime
    - Stop using deprecated methods and types

Some types tests relied on access to private fields, methods, functions,
and vars.

    - Add cue/export_test.go to carefully lift a few private fields etc
      to public during tests.

Updates #2480.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I6c597d6bd4bec219cec454373285852cce31b9c3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197685
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
cuematthew committed Jul 18, 2024
1 parent 719893f commit e1f552f
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 478 deletions.
29 changes: 15 additions & 14 deletions cue/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cue
package cue_test

import (
"fmt"
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/cuetdtest"
)
Expand Down Expand Up @@ -46,57 +47,57 @@ func TestAttributes(t *testing.T) {
`

testCases := []struct {
flags AttrKind
flags cue.AttrKind
path string
out string
}{{
flags: FieldAttr,
flags: cue.FieldAttr,
path: "a.a",
out: "[@foo(a,b,c=1)]",
}, {
flags: FieldAttr,
flags: cue.FieldAttr,
path: "a.b",
out: "[@bar(a,b,c,d=1) @foo(a,,d=1)]",
}, {
flags: DeclAttr,
flags: cue.DeclAttr,
path: "b",
out: "[@embed(foo)]",
}, {
flags: FieldAttr,
flags: cue.FieldAttr,
path: "b",
out: "[@field(foo)]",
}, {
flags: ValueAttr,
flags: cue.ValueAttr,
path: "b",
out: "[@field(foo) @embed(foo)]",
}, {
flags: ValueAttr,
flags: cue.ValueAttr,
path: "c1",
out: "[@step(1)]",
}, {
flags: DeclAttr,
flags: cue.DeclAttr,
path: "c2",
out: "[@step(2a)]",
}, {
flags: FieldAttr,
flags: cue.FieldAttr,
path: "c2",
out: "[@step(2b)]",
}, {
flags: DeclAttr,
flags: cue.DeclAttr,
path: "",
out: "[@step(2c)]",
}, {
flags: ValueAttr | FieldAttr,
flags: cue.ValueAttr | cue.FieldAttr,
path: "c3",
out: "[@step(3)]",
}, {
flags: ValueAttr | FieldAttr,
flags: cue.ValueAttr | cue.FieldAttr,
path: "c4",
out: "[]",
}}
for _, tc := range testCases {
cuetdtest.FullMatrix.Run(t, tc.path, func(t *cuetdtest.M) {
v := getValue(t, config).LookupPath(ParsePath(tc.path))
v := getValue(t, config).LookupPath(cue.ParsePath(tc.path))
a := v.Attributes(tc.flags)
got := fmt.Sprint(a)
if got != tc.out {
Expand Down
7 changes: 4 additions & 3 deletions cue/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package cue
package cue_test

import (
"fmt"
"reflect"
"testing"
"time"

"cuelang.org/go/cue"
"cuelang.org/go/internal/cuetdtest"
"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -260,14 +261,14 @@ func TestDecodeIntoCUEValue(t *testing.T) {
// This test doesn't fit within the table used by TestDecode
// because cue values aren't easily comparable with cmp.Diff.
var st struct {
X Value `json:"x"`
X cue.Value `json:"x"`
}
err := getValue(t, `x: string`).Decode(&st)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(fmt.Sprint(st.X), "string"))

// Check we can decode into a top level value.
var v Value
var v cue.Value
err = getValue(t, `int`).Decode(&v)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(fmt.Sprint(v), "int"))
Expand Down
34 changes: 34 additions & 0 deletions cue/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cue

import "cuelang.org/go/internal/core/adt"

// Export the private Value.v field for testing purposes. But do so as
// a function, not a method, so that we don't run the risk of
// accidentally extending the set of types Value may inhabit during
// tests.

func ValueVertex(v Value) *adt.Vertex { return v.v }

var (
ValueCtx = Value.ctx

AnyDefinition = anyDefinition

DebugStr = debugStr

PathToStrings = pathToStrings
)
15 changes: 3 additions & 12 deletions cue/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestLookupPath(t *testing.T) {
r := &cue.Runtime{}
ctx := cuecontext.New()

testCases := []struct {
in string
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestLookupPath(t *testing.T) {
}}
for _, tc := range testCases {
t.Run(tc.path.String(), func(t *testing.T) {
v := compileT(t, r, tc.in)
v := mustCompile(t, ctx, tc.in)

v = v.LookupPath(tc.path)

Expand All @@ -123,7 +123,7 @@ func TestLookupPath(t *testing.T) {
return
}

w := compileT(t, r, tc.out)
w := mustCompile(t, ctx, tc.out)

if k, d := diff.Diff(v, w); k != diff.Identity {
b := &bytes.Buffer{}
Expand All @@ -134,15 +134,6 @@ func TestLookupPath(t *testing.T) {
}
}

func compileT(t *testing.T, r *cue.Runtime, s string) cue.Value {
t.Helper()
inst, err := r.Compile("", s)
if err != nil {
t.Fatal(err)
}
return inst.Value()
}

func TestHidden(t *testing.T) {
in := `
-- cue.mod/module.cue --
Expand Down
Loading

0 comments on commit e1f552f

Please sign in to comment.