cuelang.org/go@v0.13.0/internal/cuetdtest/matrix.go (about)

     1  // Copyright 2024 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  //     https://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 cuetdtest
    16  
    17  import (
    18  	"testing"
    19  
    20  	"cuelang.org/go/cue"
    21  	"cuelang.org/go/cue/cuecontext"
    22  	"cuelang.org/go/internal"
    23  	"cuelang.org/go/internal/core/runtime"
    24  	"cuelang.org/go/internal/cuedebug"
    25  )
    26  
    27  // M represents a point in the evaluation matrix of possible
    28  // runtime configurations.
    29  type M struct {
    30  	// Flags is public to allow tests to customise e.g. logging.
    31  	Flags cuedebug.Config
    32  
    33  	name     string
    34  	fallback string
    35  	version  internal.EvaluatorVersion
    36  }
    37  
    38  func (t *M) Name() string     { return t.name }
    39  func (t *M) Fallback() string { return t.fallback }
    40  func (t *M) IsDefault() bool  { return t.name == DefaultVersion }
    41  
    42  func (t *M) CueContext() *cue.Context {
    43  	ctx := cuecontext.New()
    44  	r := (*runtime.Runtime)(ctx)
    45  	r.SetVersion(t.version)
    46  	r.SetDebugOptions(&t.Flags)
    47  	return ctx
    48  }
    49  
    50  // Runtime creates a runtime that is configured according to the matrix.
    51  func (t *M) Runtime() *runtime.Runtime {
    52  	return (*runtime.Runtime)(t.CueContext())
    53  }
    54  
    55  // TODO(mvdan): the default should now be evalv3.
    56  const DefaultVersion = "v2"
    57  
    58  type Matrix []M
    59  
    60  var FullMatrix Matrix = []M{{
    61  	name:    DefaultVersion,
    62  	version: internal.EvalV2,
    63  }, {
    64  	name:     "v3",
    65  	fallback: "v2",
    66  	version:  internal.EvalV3,
    67  	Flags:    cuedebug.Config{Sharing: true},
    68  }, {
    69  	name:     "v3-noshare",
    70  	fallback: "v2",
    71  	version:  internal.EvalV3,
    72  }}
    73  
    74  var SmallMatrix Matrix = FullMatrix[:2]
    75  
    76  var DefaultOnlyMatrix Matrix = FullMatrix[:1]
    77  
    78  var DevOnlyMatrix Matrix = FullMatrix[1:2]
    79  
    80  // Run runs a subtest with the given name that
    81  // invokes a further subtest for each configuration in the matrix.
    82  func (m Matrix) Run(t *testing.T, name string, f func(t *testing.T, m *M)) {
    83  	t.Run(name, func(t *testing.T) {
    84  		m.Do(t, f)
    85  	})
    86  }
    87  
    88  // Do runs f in a subtest for each configuration in the matrix.
    89  func (m Matrix) Do(t *testing.T, f func(t *testing.T, m *M)) {
    90  	for _, c := range m {
    91  		t.Run(c.name, func(t *testing.T) {
    92  			f(t, &c)
    93  		})
    94  	}
    95  }
    96  
    97  func (m *M) TODO_V2(t testing.TB) {
    98  	if m.version == internal.EvalV2 {
    99  		t.Skip("Skipping v2")
   100  	}
   101  }
   102  
   103  func (m *M) TODO_V3(t testing.TB) {
   104  	if m.version == internal.EvalV3 {
   105  		t.Skip("Skipping v3")
   106  	}
   107  }
   108  
   109  func (m *M) TODO_Sharing(t testing.TB) {
   110  	if m.Flags.Sharing {
   111  		t.Skip("Skipping v3 with sharing")
   112  	}
   113  }
   114  
   115  func (m *M) TODO_NoSharing(t testing.TB) {
   116  	if m.version == internal.EvalV3 && !m.Flags.Sharing {
   117  		t.Skip("Skipping v3 without sharing")
   118  	}
   119  }