feat(protocol): semantic validator for AST
Aggiunge validatore semantico per AST Strategy: arity check su verbi logici/comparatori/data, whitelist indicatori (sma, rsi, atr, macd, realized_vol) e feature OHLCV. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
|
||||
from multi_swarm.protocol.parser import parse_strategy
|
||||
from multi_swarm.protocol.validator import ValidationError, validate_strategy
|
||||
|
||||
|
||||
def test_valid_strategy_passes() -> None:
|
||||
src = "(strategy (when (gt (indicator rsi 14) 70.0) (entry-short)))"
|
||||
ast = parse_strategy(src)
|
||||
validate_strategy(ast) # no exception
|
||||
|
||||
|
||||
def test_indicator_unknown_name_fails() -> None:
|
||||
src = "(strategy (when (gt (indicator wibble 14) 70.0) (entry-short)))"
|
||||
ast = parse_strategy(src)
|
||||
with pytest.raises(ValidationError, match="unknown indicator"):
|
||||
validate_strategy(ast)
|
||||
|
||||
|
||||
def test_indicator_wrong_arity_fails() -> None:
|
||||
src = "(strategy (when (gt (indicator rsi) 70.0) (entry-short)))"
|
||||
ast = parse_strategy(src)
|
||||
with pytest.raises(ValidationError):
|
||||
validate_strategy(ast)
|
||||
|
||||
|
||||
def test_comparator_wrong_arity_fails() -> None:
|
||||
src = "(strategy (when (gt 1.0) (entry-long)))"
|
||||
ast = parse_strategy(src)
|
||||
with pytest.raises(ValidationError):
|
||||
validate_strategy(ast)
|
||||
|
||||
|
||||
def test_feature_unknown_column_fails() -> None:
|
||||
src = "(strategy (when (gt (feature wibble) 100.0) (entry-long)))"
|
||||
ast = parse_strategy(src)
|
||||
with pytest.raises(ValidationError, match="unknown feature"):
|
||||
validate_strategy(ast)
|
||||
Reference in New Issue
Block a user