Improved scoping
This commit is contained in:
65
test/scoping_test.go
Normal file
65
test/scoping_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/marte-community/marte-dev-tools/internal/index"
|
||||
"github.com/marte-community/marte-dev-tools/internal/parser"
|
||||
)
|
||||
|
||||
func TestNameScoping(t *testing.T) {
|
||||
// App1 = { A = { Data = 10 } B = { Ref = A } }
|
||||
// App2 = { C = { Data = 10 } A = { Data = 12 } D = { Ref = A } }
|
||||
|
||||
content := `
|
||||
+App1 = {
|
||||
Class = App
|
||||
+A = { Class = Node Data = 10 }
|
||||
+B = { Class = Node Ref = A }
|
||||
}
|
||||
+App2 = {
|
||||
Class = App
|
||||
+C = { Class = Node Data = 10 }
|
||||
+A = { Class = Node Data = 12 }
|
||||
+D = { Class = Node Ref = A }
|
||||
}
|
||||
`
|
||||
pt := index.NewProjectTree()
|
||||
p := parser.NewParser(content)
|
||||
cfg, err := p.Parse()
|
||||
if err != nil { t.Fatal(err) }
|
||||
pt.AddFile("main.marte", cfg)
|
||||
|
||||
pt.ResolveReferences()
|
||||
|
||||
// Helper to find ref target
|
||||
findRefTarget := func(refName string, containerName string) *index.ProjectNode {
|
||||
for _, ref := range pt.References {
|
||||
if ref.Name == refName {
|
||||
container := pt.GetNodeContaining(ref.File, ref.Position)
|
||||
if container != nil && container.RealName == containerName {
|
||||
return ref.Target
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
targetB := findRefTarget("A", "+B")
|
||||
if targetB == nil {
|
||||
t.Fatal("Could not find reference A in +B")
|
||||
}
|
||||
// Check if targetB is App1.A
|
||||
if targetB.Parent == nil || targetB.Parent.RealName != "+App1" {
|
||||
t.Errorf("App1.B.Ref resolved to wrong target: %v (Parent %v)", targetB.RealName, targetB.Parent.RealName)
|
||||
}
|
||||
|
||||
targetD := findRefTarget("A", "+D")
|
||||
if targetD == nil {
|
||||
t.Fatal("Could not find reference A in +D")
|
||||
}
|
||||
// Check if targetD is App2.A
|
||||
if targetD.Parent == nil || targetD.Parent.RealName != "+App2" {
|
||||
t.Errorf("App2.D.Ref resolved to wrong target: %v (Parent %v)", targetD.RealName, targetD.Parent.RealName)
|
||||
}
|
||||
}
|
||||
101
test/validator_variable_usage_test.go
Normal file
101
test/validator_variable_usage_test.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marte-community/marte-dev-tools/internal/index"
|
||||
"github.com/marte-community/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-community/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func TestVariableValidation(t *testing.T) {
|
||||
// Need a schema that enforces strict types to test usage validation.
|
||||
// We can use built-in types or rely on Variable Definition validation.
|
||||
|
||||
// Test Case 1: Variable Definition Mismatch
|
||||
contentDef := `
|
||||
#var Positive: uint = -5
|
||||
`
|
||||
pt := index.NewProjectTree()
|
||||
p := parser.NewParser(contentDef)
|
||||
cfg, err := p.Parse()
|
||||
if err != nil { t.Fatal(err) }
|
||||
pt.AddFile("def.marte", cfg)
|
||||
|
||||
v := validator.NewValidator(pt, ".")
|
||||
v.CheckVariables()
|
||||
|
||||
foundError := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Variable 'Positive' value mismatch") {
|
||||
foundError = true
|
||||
}
|
||||
}
|
||||
if !foundError {
|
||||
t.Error("Expected error for invalid variable definition")
|
||||
}
|
||||
|
||||
// Test Case 2: Variable Usage Mismatch
|
||||
// We need a class with specific field type.
|
||||
// PIDGAM.Kp is float | int.
|
||||
// Let's use string variable.
|
||||
contentUsage := `
|
||||
#var MyStr: string = "hello"
|
||||
+MyPID = {
|
||||
Class = PIDGAM
|
||||
Kp = $MyStr
|
||||
Ki = 0.0
|
||||
Kd = 0.0
|
||||
}
|
||||
`
|
||||
pt2 := index.NewProjectTree()
|
||||
p2 := parser.NewParser(contentUsage)
|
||||
cfg2, err := p2.Parse()
|
||||
if err != nil { t.Fatal(err) }
|
||||
pt2.AddFile("usage.marte", cfg2)
|
||||
|
||||
v2 := validator.NewValidator(pt2, ".")
|
||||
v2.ValidateProject() // Should run CUE validation on nodes
|
||||
|
||||
foundUsageError := false
|
||||
for _, d := range v2.Diagnostics {
|
||||
// Schema validation error
|
||||
if strings.Contains(d.Message, "Schema Validation Error") &&
|
||||
(strings.Contains(d.Message, "conflicting values") || strings.Contains(d.Message, "mismatched types")) {
|
||||
foundUsageError = true
|
||||
}
|
||||
}
|
||||
|
||||
if !foundUsageError {
|
||||
t.Error("Expected error for invalid variable usage in PIDGAM.Kp")
|
||||
for _, d := range v2.Diagnostics {
|
||||
t.Logf("Diag: %s", d.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// Test Case 3: Valid Usage
|
||||
contentValid := `
|
||||
#var MyGain: float = 1.5
|
||||
+MyPID = {
|
||||
Class = PIDGAM
|
||||
Kp = $MyGain
|
||||
Ki = 0.0
|
||||
Kd = 0.0
|
||||
}
|
||||
`
|
||||
pt3 := index.NewProjectTree()
|
||||
p3 := parser.NewParser(contentValid)
|
||||
cfg3, err := p3.Parse()
|
||||
if err != nil { t.Fatal(err) }
|
||||
pt3.AddFile("valid.marte", cfg3)
|
||||
|
||||
v3 := validator.NewValidator(pt3, ".")
|
||||
v3.ValidateProject()
|
||||
|
||||
for _, d := range v3.Diagnostics {
|
||||
if strings.Contains(d.Message, "Schema Validation Error") {
|
||||
t.Errorf("Unexpected schema error: %s", d.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user