Implemented regex validation for variables
This commit is contained in:
@@ -137,7 +137,7 @@ func (l *Lexer) NextToken() Token {
|
||||
return l.emit(TokenLBracket)
|
||||
case ']':
|
||||
return l.emit(TokenRBracket)
|
||||
case '&', '?', '!', '<', '>', '*', '(', ')':
|
||||
case '&', '?', '!', '<', '>', '*', '(', ')', '~', '%', '^':
|
||||
return l.emit(TokenSymbol)
|
||||
case '"':
|
||||
return l.lexString()
|
||||
|
||||
@@ -296,6 +296,12 @@ func (p *Parser) parseVariableDefinition(startTok Token) (Definition, bool) {
|
||||
break
|
||||
}
|
||||
if t.Type == TokenEqual {
|
||||
if p.peekN(1).Type == TokenSymbol && p.peekN(1).Value == "~" {
|
||||
p.next()
|
||||
p.next()
|
||||
typeTokens = append(typeTokens, Token{Type: TokenSymbol, Value: "=~", Position: t.Position})
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
typeTokens = append(typeTokens, p.next())
|
||||
|
||||
53
test/regex_variable_test.go
Normal file
53
test/regex_variable_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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 TestRegexVariable(t *testing.T) {
|
||||
content := `
|
||||
#var IP: string & =~"^[0-9.]+$" = "127.0.0.1"
|
||||
#var BadIP: string & =~"^[0-9.]+$" = "abc"
|
||||
|
||||
+Obj = {
|
||||
IP = $IP
|
||||
}
|
||||
`
|
||||
// Test Validator
|
||||
pt := index.NewProjectTree()
|
||||
p := parser.NewParser(content)
|
||||
cfg, err := p.Parse()
|
||||
if err != nil {
|
||||
t.Fatalf("Parse failed: %v", err)
|
||||
}
|
||||
pt.AddFile("regex.marte", cfg)
|
||||
|
||||
v := validator.NewValidator(pt, ".")
|
||||
v.CheckVariables()
|
||||
|
||||
foundError := false
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Variable 'BadIP' value mismatch") {
|
||||
foundError = true
|
||||
}
|
||||
}
|
||||
|
||||
if !foundError {
|
||||
t.Error("Expected error for BadIP")
|
||||
for _, d := range v.Diagnostics {
|
||||
t.Logf("Diag: %s", d.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// Test valid variable
|
||||
for _, d := range v.Diagnostics {
|
||||
if strings.Contains(d.Message, "Variable 'IP' value mismatch") {
|
||||
t.Error("Unexpected error for IP")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user