Skip to content

Commit

Permalink
Modify txtar tests to add configurable import mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
spinillos committed Apr 20, 2023
1 parent 0a02617 commit 6f2ce59
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
14 changes: 10 additions & 4 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func TestGenerateWithImports(t *testing.T) {
ToDo: map[string]string{
"imports/oneref_verbose": "Figure out how to disambiguate struct literals from the struct-with-braces-and-one-element case",
},
ImportMappers: map[string]func(string) (string, error){
"imports/imports": func(s string) (string, error) {
if s == "example.com/dep" {
return "new", nil
}
return s, nil
},
},
}

ctx := cuecontext.New()
Expand All @@ -57,10 +65,8 @@ func TestGenerateWithImports(t *testing.T) {
}

b, err := cuetsy.Generate(v, cuetsy.Config{
Export: true,
ImportMapper: func(string) (string, error) {
return "", nil
},
Export: true,
ImportMapper: t.ImportMapperFn,
})
if err != nil {
errors.Print(t, err, nil)
Expand Down
12 changes: 12 additions & 0 deletions internal/cuetxtar/cuetxtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type TxTarTest struct {

// ToDo is a map of tests that should be skipped now, but should be fixed.
ToDo map[string]string

ImportMappers map[string]func(string) (string, error)
}

// A Test represents a single test based on a .txtar file.
Expand All @@ -83,6 +85,8 @@ type Test struct {
Dir string

hasGold bool

ImportMapperFn func(s string) (string, error)
}

func (t *Test) Write(b []byte) (n int, err error) {
Expand Down Expand Up @@ -288,6 +292,14 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
t.Skip(msg)
}

if im, ok := x.ImportMappers[testName]; ok {
tc.ImportMapperFn = im
} else {
tc.ImportMapperFn = func(s string) (string, error) {
return "", nil
}
}

f(tc)

update := false
Expand Down
37 changes: 37 additions & 0 deletions testdata/imports/imports.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- cue.mod/module.cue --
module: "example.com"

-- in.cue --
package test

import "example.com/dep"
import "example.com/other"

#Foo: {
x: dep.#Test
y: other.#Test
} @cuetsy(kind="interface")

-- dep/file.cue --
package dep

#Test: {
a: string
} @cuetsy(kind="interface")

-- other/file.cue --
package other

#Test: {
a: string
} @cuetsy(kind="interface")

-- out/gen --

import * as dep from 'new';
import * as other from 'example.com/other';

export interface Foo {
x: dep.Test;
y: other.Test;
}
6 changes: 5 additions & 1 deletion ts/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (f File) String() string {

for _, i := range f.Imports {
b.WriteString(formatInner(EOLNone, 0, i))
b.WriteString("\n\n")
b.WriteString("\n")
}

if len(f.Imports) > 0 {
b.WriteString("\n")
}

for i, n := range f.Nodes {
Expand Down

0 comments on commit 6f2ce59

Please sign in to comment.