Skip to content

Commit

Permalink
Merge pull request #144 from grafana/fix-translate
Browse files Browse the repository at this point in the history
thema: Fix Translate() to same version
  • Loading branch information
sam boyer committed May 11, 2023
2 parents baeb17a + fc2c99e commit 9a5baae
Show file tree
Hide file tree
Showing 4 changed files with 1,536 additions and 2 deletions.
80 changes: 80 additions & 0 deletions instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package thema

import (
"cuelang.org/go/cue"
"encoding/json"
"fmt"
"io"
"strings"
"testing"

"github.com/grafana/thema/internal/txtartest/vanilla"

"cuelang.org/go/cue/cuecontext"
"github.com/stretchr/testify/require"
)

func TestInstance_Translate(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/lineage",
Name: "core/instance/translate",
}

ctx := cuecontext.New()
rt := NewRuntime(ctx)

test.Run(t, func(tc *vanilla.Test) {
if !tc.HasTag("multiversion") {
return
}

lin, lerr := bindTxtarLineage(tc, rt)
require.NoError(tc, lerr)

for sch := lin.First(); sch != nil; sch = sch.Successor() {
for _, example := range sch.Examples() {
for sch := lin.First(); sch != nil; sch = sch.Successor() {
to := sch.Version()
tinst, lacunas := example.Translate(to)
require.NotNil(t, tinst)

result := tinst.Underlying()
require.True(t, result.Exists())
require.NoError(t, result.Err())

writeGolden(tc, to, example, result, lacunas)
}
}
}
})
}

func writeGolden(tc *vanilla.Test, to SyntacticVersion, example *Instance, result cue.Value, lacunas TranslationLacunas) {
tc.Helper()

fromStr := example.Schema().Version().String()
toStr := to.String()

exName := example.name
tName := strings.Replace(tc.Name(), tc.Name()+"/", "", -1)
wName := fmt.Sprintf("%s-%s-%s->%s.json", tName, fromStr, exName, toStr)

w := tc.Writer(wName)

// From (example)
marshalAndWrite(tc, w, example.Underlying())
// To (result)
marshalAndWrite(tc, w, result)
// Lacunas
marshalAndWrite(tc, w, lacunas)
}

func marshalAndWrite(tc *vanilla.Test, w io.Writer, any interface{}) {
tc.Helper()

bytes, err := json.Marshal(any)
require.NoError(tc, err)

_, err = w.Write(append(bytes, '\n'))
require.NoError(tc, err)
}
2 changes: 1 addition & 1 deletion testdata/internal/basic.cue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ basic: #Lineage & {
from: [1, 1]
input: _
result: {
renamed: input.init
renamed: input.renamed
all: input.all
if (input.optional != _|_) {
optional: input.optional
Expand Down

0 comments on commit 9a5baae

Please sign in to comment.