...

Source file src/cuelang.org/go/internal/core/runtime/extern_test.go

Documentation: cuelang.org/go/internal/core/runtime

     1  // Copyright 2023 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package runtime_test
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"cuelang.org/go/cue"
    23  	"cuelang.org/go/cue/build"
    24  	"cuelang.org/go/cue/cuecontext"
    25  	"cuelang.org/go/cue/errors"
    26  	"cuelang.org/go/cue/token"
    27  	"cuelang.org/go/internal"
    28  	"cuelang.org/go/internal/core/adt"
    29  	"cuelang.org/go/internal/core/runtime"
    30  	"cuelang.org/go/internal/cuetxtar"
    31  	"cuelang.org/go/internal/value"
    32  )
    33  
    34  func Test(t *testing.T) {
    35  	test := cuetxtar.TxTarTest{
    36  		Root: "testdata/",
    37  		Name: "extern",
    38  	}
    39  
    40  	test.Run(t, func(t *cuetxtar.Test) {
    41  		interpreter := &interpreterFake{files: map[string]int{}}
    42  		ctx := cuecontext.New(cuecontext.Interpreter(interpreter))
    43  
    44  		b := t.Instance()
    45  		v := ctx.BuildInstance(b)
    46  		if err := v.Err(); err != nil {
    47  			t.WriteErrors(errors.Promote(err, "test"))
    48  			return
    49  		}
    50  
    51  		fmt.Fprintf(t, "%v\n", v)
    52  	})
    53  }
    54  
    55  type interpreterFake struct {
    56  	files map[string]int
    57  }
    58  
    59  func (i *interpreterFake) Kind() string { return "test" }
    60  
    61  func (i *interpreterFake) NewCompiler(b *build.Instance, r *runtime.Runtime) (runtime.Compiler, errors.Error) {
    62  	switch b.PkgName {
    63  	case "failinit":
    64  		return nil, errors.Newf(token.NoPos, "TEST: fail initialization")
    65  	case "nullinit":
    66  		return nil, nil
    67  	case "scopetest":
    68  		return newCompilerFake(b, r)
    69  	}
    70  	return i, nil
    71  }
    72  
    73  func (i *interpreterFake) Compile(funcName string, _ adt.Value, a *internal.Attr) (adt.Expr, errors.Error) {
    74  	if ok, _ := a.Flag(1, "fail"); ok {
    75  		return nil, errors.Newf(token.NoPos, "TEST: fail compilation")
    76  	}
    77  
    78  	str, ok, err := a.Lookup(1, "err")
    79  	if err != nil {
    80  		return nil, errors.Promote(err, "test")
    81  	}
    82  
    83  	if ok {
    84  		return nil, errors.Newf(token.NoPos, "%s", str)
    85  	}
    86  
    87  	if str, err = a.String(0); err != nil {
    88  		return nil, errors.Promote(err, "test")
    89  	}
    90  
    91  	if _, ok := i.files[str]; !ok {
    92  		i.files[str] = len(i.files) + 1
    93  	}
    94  
    95  	return &adt.Builtin{
    96  		Name:   "impl" + funcName + strconv.Itoa(i.files[str]),
    97  		Params: []adt.Param{{Value: &adt.BasicType{K: adt.IntKind}}},
    98  		Result: adt.IntKind,
    99  	}, nil
   100  }
   101  
   102  type compilerFake struct {
   103  	runtime *runtime.Runtime
   104  	b       *build.Instance
   105  }
   106  
   107  func newCompilerFake(b *build.Instance, r *runtime.Runtime) (runtime.Compiler, errors.Error) {
   108  	return &compilerFake{
   109  		runtime: r,
   110  		b:       b,
   111  	}, nil
   112  }
   113  
   114  func (c *compilerFake) Compile(name string, scope adt.Value, a *internal.Attr) (adt.Expr, errors.Error) {
   115  	typStr, err := a.String(0)
   116  	if err != nil {
   117  		return nil, errors.Promote(err, "test")
   118  	}
   119  
   120  	call := &adt.CallExpr{Fun: &adt.Builtin{
   121  		Result: adt.TopKind,
   122  		Func: func(opctx *adt.OpContext, args []adt.Value) adt.Expr {
   123  			cuectx := (*cue.Context)(c.runtime)
   124  			scope := value.Make(opctx, scope)
   125  
   126  			typ := cuectx.CompileString(typStr, cue.Scope(scope))
   127  			_, ityp := value.ToInternal(typ)
   128  			return ityp
   129  		},
   130  	}}
   131  	return call, nil
   132  }
   133  

View as plain text