Added schema validation and schema db
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -29,8 +28,8 @@ FieldA = 10
|
||||
Class = "MyClass"
|
||||
FieldB = 20
|
||||
`
|
||||
ioutil.WriteFile("build_multi_test/f1.marte", []byte(f1Content), 0644)
|
||||
ioutil.WriteFile("build_multi_test/f2.marte", []byte(f2Content), 0644)
|
||||
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"})
|
||||
@@ -55,7 +54,7 @@ FieldB = 20
|
||||
t.Fatalf("Expected output file not found")
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(outputFile)
|
||||
content, err := os.ReadFile(outputFile)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read output: %v", err)
|
||||
}
|
||||
|
||||
@@ -169,7 +169,14 @@ func TestBuildCommand(t *testing.T) {
|
||||
// Test Merge
|
||||
files := []string{"integration/build_merge_1.marte", "integration/build_merge_2.marte"}
|
||||
b := builder.NewBuilder(files)
|
||||
err := b.Build("build_test")
|
||||
|
||||
outputFile, err := os.Create("build_test/TEST.marte")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create output file: %v", err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
err = b.Build(outputFile)
|
||||
if err != nil {
|
||||
t.Fatalf("Build failed: %v", err)
|
||||
}
|
||||
@@ -189,12 +196,19 @@ func TestBuildCommand(t *testing.T) {
|
||||
// Test Order (Class First)
|
||||
filesOrder := []string{"integration/build_order_1.marte", "integration/build_order_2.marte"}
|
||||
bOrder := builder.NewBuilder(filesOrder)
|
||||
err = bOrder.Build("build_test")
|
||||
|
||||
outputFileOrder, err := os.Create("build_test/ORDER.marte")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create output file: %v", err)
|
||||
}
|
||||
defer outputFileOrder.Close()
|
||||
|
||||
err = bOrder.Build(outputFileOrder)
|
||||
if err != nil {
|
||||
t.Fatalf("Build order test failed: %v", err)
|
||||
}
|
||||
|
||||
contentOrder, _ := ioutil.ReadFile("build_test/TEST.marte")
|
||||
contentOrder, _ := ioutil.ReadFile("build_test/ORDER.marte")
|
||||
outputOrder := string(contentOrder)
|
||||
|
||||
// Check for Class before Field
|
||||
|
||||
85
test/validator_components_test.go
Normal file
85
test/validator_components_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func TestPIDGAMValidation(t *testing.T) {
|
||||
// PIDGAM requires Kp, Ki, Kd
|
||||
content := `
|
||||
+MyPID = {
|
||||
Class = PIDGAM
|
||||
Kp = 1.0
|
||||
// Missing Ki
|
||||
// Missing Kd
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("pid.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
foundKi := false
|
||||
foundKd := false
|
||||
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Ki'") {
|
||||
foundKi = true
|
||||
}
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Kd'") {
|
||||
foundKd = true
|
||||
}
|
||||
}
|
||||
|
||||
if !foundKi {
|
||||
t.Error("Expected error for missing 'Ki' in PIDGAM")
|
||||
}
|
||||
if !foundKd {
|
||||
t.Error("Expected error for missing 'Kd' in PIDGAM")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileDataSourceValidation(t *testing.T) {
|
||||
// FileDataSource requires Filename
|
||||
content := `
|
||||
+MyFile = {
|
||||
Class = FileDataSource
|
||||
// Missing Filename
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("file.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Filename'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for missing 'Filename' in FileDataSource")
|
||||
}
|
||||
}
|
||||
85
test/validator_db_test.go
Normal file
85
test/validator_db_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func TestRealTimeApplicationValidation(t *testing.T) {
|
||||
// RealTimeApplication requires Functions, Data, States
|
||||
content := `
|
||||
+App = {
|
||||
Class = RealTimeApplication
|
||||
+Functions = {}
|
||||
// Missing Data
|
||||
// Missing States
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("app.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
missingData := false
|
||||
missingStates := false
|
||||
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Data'") {
|
||||
missingData = true
|
||||
}
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'States'") {
|
||||
missingStates = true
|
||||
}
|
||||
}
|
||||
|
||||
if !missingData {
|
||||
t.Error("Expected error for missing 'Data' field in RealTimeApplication")
|
||||
}
|
||||
if !missingStates {
|
||||
t.Error("Expected error for missing 'States' field in RealTimeApplication")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGAMSchedulerValidation(t *testing.T) {
|
||||
// GAMScheduler requires TimingDataSource (reference)
|
||||
content := `
|
||||
+Scheduler = {
|
||||
Class = GAMScheduler
|
||||
// Missing TimingDataSource
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("scheduler.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'TimingDataSource'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for missing 'TimingDataSource' in GAMScheduler")
|
||||
}
|
||||
}
|
||||
77
test/validator_extra_test.go
Normal file
77
test/validator_extra_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func TestSDNSubscriberValidation(t *testing.T) {
|
||||
// SDNSubscriber requires Address and Port
|
||||
content := `
|
||||
+MySDN = {
|
||||
Class = SDNSubscriber
|
||||
Address = "239.0.0.1"
|
||||
// Missing Port
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("sdn.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Port'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for missing 'Port' in SDNSubscriber")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileWriterValidation(t *testing.T) {
|
||||
// FileWriter requires Filename
|
||||
content := `
|
||||
+MyWriter = {
|
||||
Class = FileWriter
|
||||
// Missing Filename
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("writer.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'Filename'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for missing 'Filename' in FileWriter")
|
||||
}
|
||||
}
|
||||
138
test/validator_schema_test.go
Normal file
138
test/validator_schema_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||
"github.com/marte-dev/marte-dev-tools/internal/validator"
|
||||
)
|
||||
|
||||
func TestSchemaValidationMandatory(t *testing.T) {
|
||||
// StateMachine requires "States"
|
||||
content := `
|
||||
+MySM = {
|
||||
Class = StateMachine
|
||||
// Missing States
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("test.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'States'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for missing mandatory field 'States', but found none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaValidationType(t *testing.T) {
|
||||
// OrderedClass: First (int), Second (string)
|
||||
content := `
|
||||
+Obj = {
|
||||
Class = OrderedClass
|
||||
First = "WrongType"
|
||||
Second = "Correct"
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("test.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Field 'First' expects type 'int'") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for wrong type in field 'First', but found none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaValidationOrder(t *testing.T) {
|
||||
// OrderedClass: First, Second (ordered=true)
|
||||
content := `
|
||||
+Obj = {
|
||||
Class = OrderedClass
|
||||
Second = "Correct"
|
||||
First = 1
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("test.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
found := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Field 'First' is out of order") {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
t.Error("Expected error for out-of-order fields, but found none")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaValidationMandatoryNode(t *testing.T) {
|
||||
// StateMachine requires "States" which is usually a node (+States or $States)
|
||||
content := `
|
||||
+MySM = {
|
||||
Class = StateMachine
|
||||
+States = {}
|
||||
}
|
||||
`
|
||||
p := parser.NewParser(content)
|
||||
config, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
|
||||
idx := index.NewProjectTree()
|
||||
idx.AddFile("test.marte", config)
|
||||
|
||||
v := validator.NewValidator(idx)
|
||||
v.ValidateProject()
|
||||
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Missing mandatory field 'States'") {
|
||||
t.Error("Reported missing mandatory field 'States' despite +States being present as a child node")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user