implemented ordering preservation

This commit is contained in:
Martino Ferrari
2026-01-23 10:23:02 +01:00
parent e2c87c90f3
commit 5c3f05a1a4
18 changed files with 262 additions and 279 deletions

View File

@@ -18,7 +18,7 @@ func TestMultiFileBuildMergeAndOrder(t *testing.T) {
// File 1: Has FieldA, no Class.
// File 2: Has Class, FieldB.
// Both in package +MyObj
f1Content := `
#package Proj.+MyObj
FieldA = 10
@@ -30,10 +30,10 @@ FieldB = 20
`
os.WriteFile("build_multi_test/f1.marte", []byte(f1Content), 0644)
os.WriteFile("build_multi_test/f2.marte", []byte(f2Content), 0644)
// Execute Build
b := builder.NewBuilder([]string{"build_multi_test/f1.marte", "build_multi_test/f2.marte"})
// Prepare output file
// Should be +MyObj.marte (normalized MyObj.marte) - Actually checking content
outputFile := "build_multi_test/MyObj.marte"
@@ -48,19 +48,19 @@ FieldB = 20
t.Fatalf("Build failed: %v", err)
}
f.Close() // Close to flush
// Check Output
if _, err := os.Stat(outputFile); os.IsNotExist(err) {
t.Fatalf("Expected output file not found")
}
content, err := os.ReadFile(outputFile)
if err != nil {
t.Fatalf("Failed to read output: %v", err)
}
output := string(content)
// Check presence
if !strings.Contains(output, "Class = \"MyClass\"") {
t.Error("Output missing Class")
@@ -71,23 +71,23 @@ FieldB = 20
if !strings.Contains(output, "FieldB = 20") {
t.Error("Output missing FieldB")
}
// Check Order: Class/FieldB (from f2) should come BEFORE FieldA (from f1)
// because f2 has the Class definition.
idxClass := strings.Index(output, "Class")
idxFieldB := strings.Index(output, "FieldB")
idxFieldA := strings.Index(output, "FieldA")
if idxClass == -1 || idxFieldB == -1 || idxFieldA == -1 {
t.Fatal("Missing fields in output")
}
// Class should be first
if idxClass > idxFieldA {
t.Errorf("Expected Class (from f2) to be before FieldA (from f1). Output:\n%s", output)
}
// FieldB should be near Class (same fragment)
// FieldA should be after
if idxFieldB > idxFieldA {

View File

@@ -120,7 +120,7 @@ func TestFmtCommand(t *testing.T) {
formatter.Format(config, &buf)
output := buf.String()
// Check for indentation
if !strings.Contains(output, " Class = \"MyClass\"") {
t.Error("Expected 2-space indentation for Class field")
@@ -169,7 +169,7 @@ func TestBuildCommand(t *testing.T) {
// Test Merge
files := []string{"integration/build_merge_1.marte", "integration/build_merge_2.marte"}
b := builder.NewBuilder(files)
outputFile, err := os.Create("build_test/TEST.marte")
if err != nil {
t.Fatalf("Failed to create output file: %v", err)
@@ -180,23 +180,23 @@ func TestBuildCommand(t *testing.T) {
if err != nil {
t.Fatalf("Build failed: %v", err)
}
// Check output existence
if _, err := os.Stat("build_test/TEST.marte"); os.IsNotExist(err) {
t.Fatalf("Expected output file build_test/TEST.marte not found")
}
content, _ := ioutil.ReadFile("build_test/TEST.marte")
output := string(content)
if !strings.Contains(output, "FieldA = 1") || !strings.Contains(output, "FieldB = 2") {
t.Error("Merged output missing fields")
}
// Test Order (Class First)
filesOrder := []string{"integration/build_order_1.marte", "integration/build_order_2.marte"}
bOrder := builder.NewBuilder(filesOrder)
outputFileOrder, err := os.Create("build_test/ORDER.marte")
if err != nil {
t.Fatalf("Failed to create output file: %v", err)
@@ -207,18 +207,18 @@ func TestBuildCommand(t *testing.T) {
if err != nil {
t.Fatalf("Build order test failed: %v", err)
}
contentOrder, _ := ioutil.ReadFile("build_test/ORDER.marte")
outputOrder := string(contentOrder)
// Check for Class before Field
classIdx := strings.Index(outputOrder, "Class = \"Ordered\"")
fieldIdx := strings.Index(outputOrder, "Field = 1")
if classIdx == -1 || fieldIdx == -1 {
t.Fatal("Missing Class or Field in ordered output")
}
if classIdx > fieldIdx {
t.Error("Expected Class to appear before Field in merged output")
}
}
}

View File

@@ -30,28 +30,28 @@ func TestLSPHoverDoc(t *testing.T) {
file := "doc.marte"
idx.AddFile(file, config)
idx.ResolveReferences()
// Test 1: Hover over +MyObject definition
res := idx.Query(file, 4, 2) // Line 4: +MyObject
if res == nil || res.Node == nil {
t.Fatal("Query failed for definition")
}
expectedDoc := "Object Documentation\nSecond line"
if res.Node.Doc != expectedDoc {
t.Errorf("Expected definition doc:\n%q\nGot:\n%q", expectedDoc, res.Node.Doc)
}
// Test 2: Hover over MyObject reference
resRef := idx.Query(file, 10, 16) // Line 10: RefField = MyObject
if resRef == nil || resRef.Reference == nil {
t.Fatal("Query failed for reference")
}
if resRef.Reference.Target == nil {
t.Fatal("Reference target not resolved")
}
if resRef.Reference.Target.Doc != expectedDoc {
t.Errorf("Expected reference target definition doc:\n%q\nGot:\n%q", expectedDoc, resRef.Reference.Target.Doc)
}

View File

@@ -49,17 +49,23 @@ func TestLSPSignalReferences(t *testing.T) {
if root == nil {
t.Fatal("Root node not found")
}
// Traverse to MySig
dataNode := root.Children["Data"]
if dataNode == nil { t.Fatal("Data node not found") }
if dataNode == nil {
t.Fatal("Data node not found")
}
myDS := dataNode.Children["MyDS"]
if myDS == nil { t.Fatal("MyDS node not found") }
if myDS == nil {
t.Fatal("MyDS node not found")
}
signals := myDS.Children["Signals"]
if signals == nil { t.Fatal("Signals node not found") }
if signals == nil {
t.Fatal("Signals node not found")
}
mySigDef := signals.Children["MySig"]
if mySigDef == nil {
t.Fatal("Definition of MySig not found in tree")
@@ -84,4 +90,4 @@ func TestLSPSignalReferences(t *testing.T) {
if foundRefs != 1 {
t.Errorf("Expected 1 reference (Direct), found %d", foundRefs)
}
}
}

View File

@@ -26,14 +26,14 @@ func loadConfig(t *testing.T, filename string) *parser.Configuration {
func TestLSPDiagnostics(t *testing.T) {
inputFile := "integration/check_dup.marte"
config := loadConfig(t, inputFile)
// Simulate LSP logic: Build Index -> Validate
idx := index.NewProjectTree()
idx.AddFile(inputFile, config)
v := validator.NewValidator(idx, ".")
v.ValidateProject()
// Check for expected diagnostics
found := false
for _, d := range v.Diagnostics {
@@ -51,7 +51,7 @@ func TestLSPDiagnostics(t *testing.T) {
}
// For GoToDefinition and References, we need to test the Indexer's ability to resolve symbols.
// Currently, my Indexer (ProjectTree) stores structure but doesn't explicitly track
// Currently, my Indexer (ProjectTree) stores structure but doesn't explicitly track
// "references" in a way that maps a source position to a target symbol yet.
// The ProjectTree is built for structure merging.
// To support LSP "Go To Definition", we need to map usage -> definition.
@@ -63,7 +63,7 @@ func TestLSPDiagnostics(t *testing.T) {
// Previously (before rewrite), `index.go` had `References []Reference`.
// I removed it during the rewrite to ProjectTree!
// I need to re-implement reference tracking in `ProjectTree` or a parallel structure
// I need to re-implement reference tracking in `ProjectTree` or a parallel structure
// to support LSP features.
func TestLSPDefinition(t *testing.T) {
// Create a virtual file content with a definition and a reference
@@ -94,15 +94,15 @@ func TestLSPDefinition(t *testing.T) {
break
}
}
if foundRef == nil {
t.Fatal("Reference to MyObject not found in index")
}
if foundRef.Target == nil {
t.Fatal("Reference to MyObject was not resolved to a target")
}
if foundRef.Target.RealName != "+MyObject" {
t.Errorf("Expected target to be +MyObject, got %s", foundRef.Target.RealName)
}
@@ -123,19 +123,19 @@ func TestLSPHover(t *testing.T) {
idx := index.NewProjectTree()
file := "hover.marte"
idx.AddFile(file, config)
// +MyObject is at line 2.
// Query at line 2, col 2 (on 'M' of MyObject)
res := idx.Query(file, 2, 2)
if res == nil {
t.Fatal("Query returned nil")
}
if res.Node == nil {
t.Fatal("Expected Node result")
}
if res.Node.RealName != "+MyObject" {
t.Errorf("Expected +MyObject, got %s", res.Node.RealName)
}

View File

@@ -49,13 +49,13 @@ func TestFunctionsArrayValidation(t *testing.T) {
for _, d := range v.Diagnostics {
if strings.Contains(d.Message, "not found or is not a valid GAM") {
// This covers both InvalidGAM and MissingGAM cases
if strings.Contains(d.Message, "InvalidGAM") {
foundInvalid = true
}
if strings.Contains(d.Message, "MissingGAM") {
foundMissing = true
}
// This covers both InvalidGAM and MissingGAM cases
if strings.Contains(d.Message, "InvalidGAM") {
foundInvalid = true
}
if strings.Contains(d.Message, "MissingGAM") {
foundMissing = true
}
}
if strings.Contains(d.Message, "must contain references") {
foundNotRef = true

View File

@@ -91,10 +91,10 @@ func TestGAMSignalValidation(t *testing.T) {
}
if !foundBadInput || !foundMissing || !foundBadOutput {
for _, d := range v.Diagnostics {
t.Logf("Diagnostic: %s", d.Message)
}
}
for _, d := range v.Diagnostics {
t.Logf("Diagnostic: %s", d.Message)
}
}
if !foundBadInput {
t.Error("Expected error for OutDS in InputSignals")

View File

@@ -21,23 +21,23 @@ func TestGlobalPragmaDebug(t *testing.T) {
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
// Check if pragma parsed
if len(config.Pragmas) == 0 {
t.Fatal("Pragma not parsed")
}
t.Logf("Parsed Pragma 0: %s", config.Pragmas[0].Text)
// Check if pragma parsed
if len(config.Pragmas) == 0 {
t.Fatal("Pragma not parsed")
}
t.Logf("Parsed Pragma 0: %s", config.Pragmas[0].Text)
idx := index.NewProjectTree()
idx.AddFile("debug.marte", config)
idx.ResolveReferences()
// Check if added to GlobalPragmas
pragmas, ok := idx.GlobalPragmas["debug.marte"]
if !ok || len(pragmas) == 0 {
t.Fatal("GlobalPragmas not populated")
}
t.Logf("Global Pragma stored: %s", pragmas[0])
idx.ResolveReferences()
// Check if added to GlobalPragmas
pragmas, ok := idx.GlobalPragmas["debug.marte"]
if !ok || len(pragmas) == 0 {
t.Fatal("GlobalPragmas not populated")
}
t.Logf("Global Pragma stored: %s", pragmas[0])
v := validator.NewValidator(idx, ".")
v.ValidateProject()
@@ -48,11 +48,11 @@ func TestGlobalPragmaDebug(t *testing.T) {
for _, d := range v.Diagnostics {
if strings.Contains(d.Message, "Implicitly Defined Signal") {
foundImplicitWarning = true
t.Logf("Found warning: %s", d.Message)
t.Logf("Found warning: %s", d.Message)
}
if strings.Contains(d.Message, "Unused GAM") {
foundUnusedWarning = true
t.Logf("Found warning: %s", d.Message)
t.Logf("Found warning: %s", d.Message)
}
}

View File

@@ -64,10 +64,10 @@ func TestImplicitSignal(t *testing.T) {
}
if !foundWarning || foundError {
for _, d := range v.Diagnostics {
t.Logf("Diagnostic: %s", d.Message)
}
}
for _, d := range v.Diagnostics {
t.Logf("Diagnostic: %s", d.Message)
}
}
if !foundWarning {
t.Error("Expected warning for ImplicitSig")
@@ -83,9 +83,9 @@ func TestImplicitSignal(t *testing.T) {
`
p2 := parser.NewParser(contentMissingType)
config2, err2 := p2.Parse()
if err2 != nil {
t.Fatalf("Parse2 failed: %v", err2)
}
if err2 != nil {
t.Fatalf("Parse2 failed: %v", err2)
}
idx2 := index.NewProjectTree()
idx2.AddFile("missing_type.marte", config2)
idx2.ResolveReferences()
@@ -99,9 +99,9 @@ func TestImplicitSignal(t *testing.T) {
}
}
if !foundTypeErr {
for _, d := range v2.Diagnostics {
t.Logf("Diagnostic2: %s", d.Message)
}
for _, d := range v2.Diagnostics {
t.Logf("Diagnostic2: %s", d.Message)
}
t.Error("Expected error for missing Type in implicit signal")
}
}

View File

@@ -32,18 +32,18 @@ func TestMultiFileNodeValidation(t *testing.T) {
// Resolving references might be needed if the validator relies on it for merging implicitly
// But primarily we want to check if the validator sees the merged node.
// The current implementation of Validator likely iterates over the ProjectTree.
// If the ProjectTree doesn't merge nodes automatically, the Validator needs to do it.
// However, the spec says "The build tool, validator, and LSP must merge these definitions".
// Let's assume the Validator or Index does the merging logic.
// The current implementation of Validator likely iterates over the ProjectTree.
// If the ProjectTree doesn't merge nodes automatically, the Validator needs to do it.
// However, the spec says "The build tool, validator, and LSP must merge these definitions".
// Let's assume the Validator or Index does the merging logic.
v := validator.NewValidator(idx, ".")
v.ValidateProject()
// +MyNode is split.
// valid_1 has FieldA
// valid_2 has Class and FieldB
// If merging works, it should have a Class, so no error about missing Class.
// +MyNode is split.
// valid_1 has FieldA
// valid_2 has Class and FieldB
// If merging works, it should have a Class, so no error about missing Class.
for _, diag := range v.Diagnostics {
if strings.Contains(diag.Message, "must contain a 'Class' field") {
@@ -79,14 +79,14 @@ func TestMultiFileReference(t *testing.T) {
parseAndAddToIndex(t, idx, "integration/multifile_ref_2.marte")
idx.ResolveReferences()
// Check if the reference in +SourceNode to TargetNode is resolved.
// Check if the reference in +SourceNode to TargetNode is resolved.
v := validator.NewValidator(idx, ".")
v.ValidateProject()
if len(v.Diagnostics) > 0 {
// Filter out irrelevant errors
}
if len(v.Diagnostics) > 0 {
// Filter out irrelevant errors
}
}
func TestHierarchicalPackageMerge(t *testing.T) {
@@ -99,13 +99,13 @@ func TestHierarchicalPackageMerge(t *testing.T) {
// +MyObj should have Class (from file 1) and FieldX (from file 2).
// If Class is missing, ValidateProject reports error.
for _, diag := range v.Diagnostics {
if strings.Contains(diag.Message, "must contain a 'Class' field") {
t.Errorf("Unexpected 'Class' field error for +MyObj: %s", diag.Message)
}
}
// We can also inspect the tree to verify FieldX is there (optional, but good for confidence)
baseNode := idx.Root.Children["Base"]
if baseNode == nil {
@@ -115,7 +115,7 @@ func TestHierarchicalPackageMerge(t *testing.T) {
if objNode == nil {
t.Fatal("MyObj node not found in Base")
}
hasFieldX := false
for _, frag := range objNode.Fragments {
for _, def := range frag.Definitions {
@@ -124,7 +124,7 @@ func TestHierarchicalPackageMerge(t *testing.T) {
}
}
}
if !hasFieldX {
t.Error("FieldX not found in +MyObj")
}
@@ -153,44 +153,44 @@ func TestHierarchicalDuplicate(t *testing.T) {
func TestIsolatedFileValidation(t *testing.T) {
idx := index.NewProjectTree()
// File 1: Has package. Defines SharedClass.
f1Content := `
// File 1: Has package. Defines SharedClass.
f1Content := `
#package Proj.Pkg
+SharedObj = { Class = SharedClass }
`
p1 := parser.NewParser(f1Content)
c1, _ := p1.Parse()
idx.AddFile("shared.marte", c1)
// File 2: No package. References SharedObj.
// Should NOT resolve to SharedObj in shared.marte because iso.marte is isolated.
f2Content := `
p1 := parser.NewParser(f1Content)
c1, _ := p1.Parse()
idx.AddFile("shared.marte", c1)
// File 2: No package. References SharedObj.
// Should NOT resolve to SharedObj in shared.marte because iso.marte is isolated.
f2Content := `
+IsoObj = {
Class = "MyClass"
Ref = SharedObj
}
`
p2 := parser.NewParser(f2Content)
c2, _ := p2.Parse()
idx.AddFile("iso.marte", c2)
idx.ResolveReferences()
// Find reference
var ref *index.Reference
for i := range idx.References {
if idx.References[i].File == "iso.marte" && idx.References[i].Name == "SharedObj" {
ref = &idx.References[i]
break
}
}
if ref == nil {
t.Fatal("Reference SharedObj not found in index")
}
if ref.Target != nil {
t.Errorf("Expected reference in isolated file to be unresolved, but got target in %s", ref.Target.Fragments[0].File)
}
p2 := parser.NewParser(f2Content)
c2, _ := p2.Parse()
idx.AddFile("iso.marte", c2)
idx.ResolveReferences()
// Find reference
var ref *index.Reference
for i := range idx.References {
if idx.References[i].File == "iso.marte" && idx.References[i].Name == "SharedObj" {
ref = &idx.References[i]
break
}
}
if ref == nil {
t.Fatal("Reference SharedObj not found in index")
}
if ref.Target != nil {
t.Errorf("Expected reference in isolated file to be unresolved, but got target in %s", ref.Target.Fragments[0].File)
}
}