-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from grafana/fix-translate
thema: Fix Translate() to same version
- Loading branch information
Showing
4 changed files
with
1,536 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.