Skip to content

Commit 8fbaf81

Browse files
committed
cue: remove duplicate test code
The removed code was a duplicated in builtin_test (package cue_test) and build_test (package cue). It can be removed by moving build_test to package cue_test. Some additional simplifications. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I72682ab5bf5f2cdb4e2f7e3a63950daaab82caf4 Signed-off-by: Marcel van Lohuizen <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/530898 Reviewed-by: Paul Jolly <[email protected]>
1 parent e5c7176 commit 8fbaf81

File tree

4 files changed

+20
-71
lines changed

4 files changed

+20
-71
lines changed

cue/build.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,8 @@ func (r *hiddenRuntime) Build(p *build.Instance) (*Instance, error) {
117117
return r.complete(p, v)
118118
}
119119

120-
// Build creates one Instance for each build.Instance. A returned Instance may
121-
// be incomplete, in which case its Err field is set.
122-
//
123-
// Example:
124-
// inst := cue.Build(load.Instances(args))
125-
//
126-
// Deprecated: use Context.BuildInstances. The use of Instance is being phased
127-
// out.
120+
// Deprecated: use cuecontext.Context.BuildInstances. The use of Instance is
121+
// being phased out.
128122
func Build(instances []*build.Instance) []*Instance {
129123
if len(instances) == 0 {
130124
panic("cue: list of instances must not be empty")

cue/build_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package cue
15+
package cue_test
1616

1717
import (
1818
"fmt"
1919
"testing"
2020

21+
"cuelang.org/go/cue"
2122
"cuelang.org/go/cue/ast"
2223
"cuelang.org/go/cue/build"
24+
"cuelang.org/go/cue/cuecontext"
2325
"cuelang.org/go/cue/token"
2426
"cuelang.org/go/internal/core/debug"
27+
"cuelang.org/go/internal/value"
2528
)
2629

2730
func TestFromExpr(t *testing.T) {
@@ -40,12 +43,12 @@ func TestFromExpr(t *testing.T) {
4043
}}
4144
for _, tc := range testCases {
4245
t.Run("", func(t *testing.T) {
43-
r := &Runtime{}
44-
inst, err := r.CompileExpr(tc.expr)
45-
if err != nil {
46+
r := cuecontext.New()
47+
v := r.BuildExpr(tc.expr)
48+
if err := v.Err(); err != nil {
4649
t.Fatal(err)
4750
}
48-
if got := fmt.Sprint(inst.Value()); got != tc.out {
51+
if got := fmt.Sprint(v); got != tc.out {
4952
t.Errorf("\n got: %v; want %v", got, tc.out)
5053
}
5154
})
@@ -194,13 +197,14 @@ func TestBuild(t *testing.T) {
194197
}}
195198
for _, tc := range testCases {
196199
t.Run("", func(t *testing.T) {
197-
insts := Build(makeInstances(tc.instances))
200+
insts := cue.Build(makeInstances(tc.instances))
198201
var got string
199202
if err := insts[0].Err; err != nil {
200203
got = err.Error()
201204
} else {
202205
cfg := &debug.Config{Compact: true}
203-
got = debug.NodeString(insts[0].index, insts[0].Value().v, cfg)
206+
r, v := value.ToInternal(insts[0].Value())
207+
got = debug.NodeString(r, v, cfg)
204208
}
205209
if got != tc.emit {
206210
t.Errorf("\n got: %s\nwant: %s", got, tc.emit)

cue/builtin_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"testing"
2020

2121
"cuelang.org/go/cue"
22-
"cuelang.org/go/cue/build"
23-
"cuelang.org/go/cue/token"
2422

2523
_ "cuelang.org/go/pkg"
2624
)
@@ -167,52 +165,3 @@ func TestSingleBuiltin(t *testing.T) {
167165
})
168166
}
169167
}
170-
171-
type builder struct {
172-
ctxt *build.Context
173-
imports map[string]*bimport
174-
}
175-
176-
func (b *builder) load(pos token.Pos, path string) *build.Instance {
177-
bi := b.imports[path]
178-
if bi == nil {
179-
return nil
180-
}
181-
return b.build(bi)
182-
}
183-
184-
type bimport struct {
185-
path string // "" means top-level
186-
files []string
187-
}
188-
189-
func makeInstances(insts []*bimport) (instances []*build.Instance) {
190-
b := builder{
191-
ctxt: build.NewContext(),
192-
imports: map[string]*bimport{},
193-
}
194-
for _, bi := range insts {
195-
if bi.path != "" {
196-
b.imports[bi.path] = bi
197-
}
198-
}
199-
for _, bi := range insts {
200-
if bi.path == "" {
201-
instances = append(instances, b.build(bi))
202-
}
203-
}
204-
return
205-
}
206-
207-
func (b *builder) build(bi *bimport) *build.Instance {
208-
path := bi.path
209-
if path == "" {
210-
path = "dir"
211-
}
212-
p := b.ctxt.NewInstance(path, b.load)
213-
for i, f := range bi.files {
214-
_ = p.AddFile(fmt.Sprintf("file%d.cue", i), f)
215-
}
216-
_ = p.Complete()
217-
return p
218-
}

cue/types_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ import (
3333
"cuelang.org/go/internal/core/debug"
3434
)
3535

36-
func getInstance(t *testing.T, body ...string) *Instance {
36+
func getInstance(t *testing.T, body string) *Instance {
3737
t.Helper()
3838

39-
insts := Build(makeInstances([]*bimport{{files: body}}))
40-
if insts[0].Err != nil {
41-
t.Fatalf("unexpected parse error: %v", insts[0].Err)
39+
var r Runtime // TODO: use Context and return Value
40+
41+
inst, err := r.Compile("foo", body)
42+
if err != nil {
43+
t.Fatalf("unexpected parse error: %v", err)
4244
}
43-
return insts[0]
45+
return inst
4446
}
4547

4648
func TestAPI(t *testing.T) {

0 commit comments

Comments
 (0)