Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT committed Aug 16, 2022
1 parent 925d2a9 commit 79f40d1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/go/cmd/api_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,34 @@ func TestRecursiveAliasDefinitions(t *testing.T) {
})
}
}

func TestAliasDiagnostics(t *testing.T) {
review, err := createReview(filepath.Clean("testdata/test_alias_diagnostics"))
require.NoError(t, err)
require.Equal(t, "Go", review.Language)
require.Equal(t, "test_alias_diagnostics", review.Name)
require.Equal(t, 6, len(review.Diagnostics))
for _, diagnostic := range review.Diagnostics {
if diagnostic.TargetID == "test_alias_diagnostics.WidgetValue" {
require.Equal(t, DiagnosticLevelInfo, diagnostic.Level)
require.Equal(t, aliasFor+"internal.WidgetValue", diagnostic.Text)
} else {
require.Equal(t, "test_alias_diagnostics.Widget", diagnostic.TargetID)
switch diagnostic.Level {
case DiagnosticLevelInfo:
require.Equal(t, aliasFor+"internal.Widget", diagnostic.Text)
case DiagnosticLevelError:
switch txt := diagnostic.Text; txt {
case missingAliasFor + "WidgetProperties":
case missingAliasFor + "WidgetPropertiesP":
case missingAliasFor + "WidgetThings":
case missingAliasFor + "WidgetThingsP":
default:
t.Fatalf("unexpected diagnostic text %s", txt)
}
default:
t.Fatalf("unexpected diagnostic level %d", diagnostic.Level)
}
}
}
}
5 changes: 5 additions & 0 deletions src/go/cmd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func NewModule(dir string) (*Module, error) {
m := Module{Name: filepath.Base(dir), packages: map[string]*Pkg{}}

baseImportPath := path.Dir(mf.Module.Mod.Path) + "/"
if baseImportPath == "./" {
// this is a relative path in the tests, so remove this prefix.
// if not, then the package name added below won't match the imported packages.
baseImportPath = ""
}
err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
if !indexTestdata && strings.Contains(path, "testdata") {
Expand Down
1 change: 1 addition & 0 deletions src/go/cmd/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// diagnostic messages
const (
aliasFor = "Alias for "
missingAliasFor = "missing alias for nested type "
embedsUnexportedStruct = "Anonymously embeds unexported struct "
sealedInterface = "Applications can't implement this interface"
)
Expand Down
20 changes: 20 additions & 0 deletions src/go/cmd/testdata/test_alias_diagnostics/internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package internal

type Widget struct {
OK bool
Value WidgetValue
MissingScalar WidgetProperties
MissingScalarP *WidgetPropertiesP
MissingSlice []WidgetThings
MissingSliceP []*WidgetThingsP
}

type WidgetProperties struct{}

type WidgetPropertiesP struct{}

type WidgetThings struct{}

type WidgetThingsP struct{}

type WidgetValue struct{}
9 changes: 9 additions & 0 deletions src/go/cmd/testdata/test_alias_diagnostics/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test_alias_diagnostics

import (
"test_alias_diagnostics/internal"
)

type Widget = internal.Widget

type WidgetValue = internal.WidgetValue

0 comments on commit 79f40d1

Please sign in to comment.