cuelang.org/go@v0.10.1/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  type M struct {
    28  	*testing.T
    29  
    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) Context() *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.Context())
    53  }
    54  
    55  const DefaultVersion = "v2"
    56  
    57  type Matrix []M
    58  
    59  var FullMatrix Matrix = []M{{
    60  	name:    DefaultVersion,
    61  	version: internal.DefaultVersion,
    62  }, {
    63  	name:     "v3",
    64  	fallback: "v2",
    65  	version:  internal.DevVersion,
    66  	Flags:    cuedebug.Config{Sharing: true},
    67  }, {
    68  	name:     "v3-noshare",
    69  	fallback: "v2",
    70  	version:  internal.DevVersion,
    71  }}
    72  
    73  var SmallMatrix Matrix = FullMatrix[:2]
    74  
    75  var DefaultOnlyMatrix Matrix = FullMatrix[:1]
    76  
    77  // Run runs a test with the given name f for each configuration in the matrix.
    78  func (m Matrix) Run(t *testing.T, name string, f func(t *M)) {
    79  	t.Run(name, func(t *testing.T) {
    80  		m.Do(t, f)
    81  	})
    82  }
    83  
    84  // Do runs f for each configuration in the matrix.
    85  func (m Matrix) Do(t *testing.T, f func(t *M)) {
    86  	for _, c := range m {
    87  		t.Run(c.name, func(t *testing.T) {
    88  			c.T = t
    89  			f(&c)
    90  		})
    91  	}
    92  }
    93  
    94  func (t *M) TODO_V3() {
    95  	if t.version == internal.DevVersion {
    96  		t.Skip("Skipping v3")
    97  	}
    98  }
    99  
   100  func (t *M) TODO_Sharing() {
   101  	if t.Flags.Sharing {
   102  		t.Skip("Skipping v3 with sharing")
   103  	}
   104  }
   105  
   106  func (t *M) TODO_NoSharing() {
   107  	if t.version == internal.DevVersion && !t.Flags.Sharing {
   108  		t.Skip("Skipping v3 without sharing")
   109  	}
   110  }