Skip to content

Commit

Permalink
wit: add test to verify no World-exported types
Browse files Browse the repository at this point in the history
TODO: The Rust source in wit-parser says types are ONLY exported, and NOT imported, yet TypeDefs appear in the imports field.
  • Loading branch information
ydnar committed Sep 25, 2023
1 parent 4a7d987 commit 7174552
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions wit/testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,18 @@ func TestPackageFieldIsNotNil(t *testing.T) {
func TestTypeDefNamesNotNil(t *testing.T) {
err := loadTestdata(func(path string, res *Resolve) error {
t.Run(strings.TrimPrefix(path, testdataDir), func(t *testing.T) {
for i, v := range res.TypeDefs {
switch v.Kind.(type) {
for i, td := range res.TypeDefs {
switch td.Kind.(type) {
case *Record, *Variant, *Enum, *Flags:
default:
continue
}
name := fmt.Sprintf("TypeDefs[%d]", i)
if v.Name != nil {
name += "#" + *v.Name
if td.Name != nil {
name += "#" + *td.Name
}
t.Run(name, func(t *testing.T) {
if v.Name == nil {
if td.Name == nil {
t.Error("Name is nil")
}
})
Expand All @@ -239,3 +239,29 @@ func TestTypeDefNamesNotNil(t *testing.T) {
t.Error(err)
}
}

// TestNoExportedTypeDefs verifies that any [TypeDef] instances in a [World] are
// referenced in Imports, and not Exports.
func TestNoExportedTypeDefs(t *testing.T) {
err := loadTestdata(func(path string, res *Resolve) error {
t.Run(strings.TrimPrefix(path, testdataDir), func(t *testing.T) {
for i, w := range res.Worlds {
if len(w.Imports) == 0 && len(w.Exports) == 0 {
continue
}
name := fmt.Sprintf("Worlds[%d]#%s", i, w.Name)
t.Run(name, func(t *testing.T) {
for name, item := range w.Exports {
if _, ok := item.(*TypeDef); ok {
t.Errorf("found TypeDef in World.Exports: %s", name)
}
}
})
}
})
return nil
})
if err != nil {
t.Error(err)
}
}

0 comments on commit 7174552

Please sign in to comment.