Preserve Gotlin semantics across packages
This commit is contained in:
parent
5b30e49486
commit
acb42a5702
10 changed files with 525 additions and 34 deletions
64
internal/lang/metadata_test.go
Normal file
64
internal/lang/metadata_test.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package lang
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestImportedGotlinClassesPreserveReferenceSemantics(t *testing.T) {
|
||||
library, err := Parse(`package platform
|
||||
class Lifecycle { fun stopping(): Boolean { return false } }
|
||||
fun createLifecycle(): Lifecycle { return Lifecycle() }`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata, err := BuildPackageMetadata(library, "example/platform")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
consumer, err := Parse(`package service
|
||||
import platform "example/platform"
|
||||
class Worker(val lifecycle: platform.Lifecycle) {
|
||||
fun stopped(): Boolean { return lifecycle.stopping() }
|
||||
}
|
||||
fun create(): platform.Lifecycle { return platform.createLifecycle() }`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
semantic, diagnostics := AnalyzeWithMetadata(consumer, []*PackageMetadata{metadata})
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatalf("diagnostics: %#v", diagnostics)
|
||||
}
|
||||
fieldType := semantic.ClassInfo["Worker"].Fields["lifecycle"].Type
|
||||
if _, ok := fieldType.(ImportedClassType); !ok {
|
||||
t.Fatalf("type = %#v", fieldType)
|
||||
}
|
||||
output, err := GenerateGoWithMetadata(consumer, []*PackageMetadata{metadata})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, expected := range []string{"lifecycle *platform.Lifecycle", "func create() *platform.Lifecycle", "platform.CreateLifecycle()"} {
|
||||
if !strings.Contains(string(output), expected) {
|
||||
t.Fatalf("missing %q:\n%s", expected, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportedGotlinClassRejectsExplicitPointer(t *testing.T) {
|
||||
library, err := Parse(`package platform class Lifecycle`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
metadata, err := BuildPackageMetadata(library, "example/platform")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
consumer, err := Parse(`package service import platform "example/platform" fun use(value: *platform.Lifecycle) {}`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, diagnostics := AnalyzeWithMetadata(consumer, []*PackageMetadata{metadata})
|
||||
if len(diagnostics) == 0 || !strings.Contains(diagnostics[0].Message, "already reference-valued") {
|
||||
t.Fatalf("diagnostics: %#v", diagnostics)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue