fix: validateIntRange/FloatRange soportan rangos negativos

Bug: "-100-100" se parseaba como ["", "100", "100"] con splitScalar
Fix: Buscar el separador '-' que NO está al inicio del string

Ejemplos que ahora funcionan:
- "-100-100" → min=-100, max=100
- "-50-50" → min=-50, max=50
- "0-100" → min=0, max=100 (sin cambio)

Test añadido para rangos negativos.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
reugenio 2025-12-18 17:35:25 +01:00
parent 0ef5efd8d8
commit 957767d865

View file

@ -119,10 +119,18 @@ pub const ConfigVariable = struct {
_ = self; _ = self;
const parsed = std.fmt.parseInt(i32, value, 10) catch return false; const parsed = std.fmt.parseInt(i32, value, 10) catch return false;
// Parse "min-max" // Parse "min-max" (soporta negativos, ej: "-100-100", "-50-50", "0-100")
var it = std.mem.splitScalar(u8, range, '-'); // Buscar el '-' separador: es el primero que NO está al inicio
const min_str = it.next() orelse return false; const separator_pos = blk: {
const max_str = it.next() orelse return false; const start: usize = if (range.len > 0 and range[0] == '-') 1 else 0;
for (range[start..], start..) |c, i| {
if (c == '-') break :blk i;
}
return false; // No encontró separador
};
const min_str = range[0..separator_pos];
const max_str = range[separator_pos + 1 ..];
const min = std.fmt.parseInt(i32, min_str, 10) catch return false; const min = std.fmt.parseInt(i32, min_str, 10) catch return false;
const max = std.fmt.parseInt(i32, max_str, 10) catch return false; const max = std.fmt.parseInt(i32, max_str, 10) catch return false;
@ -134,10 +142,17 @@ pub const ConfigVariable = struct {
_ = self; _ = self;
const parsed = std.fmt.parseFloat(f32, value) catch return false; const parsed = std.fmt.parseFloat(f32, value) catch return false;
// Parse "min-max" // Parse "min-max" (soporta negativos)
var it = std.mem.splitScalar(u8, range, '-'); const separator_pos = blk: {
const min_str = it.next() orelse return false; const start: usize = if (range.len > 0 and range[0] == '-') 1 else 0;
const max_str = it.next() orelse return false; for (range[start..], start..) |c, i| {
if (c == '-') break :blk i;
}
return false;
};
const min_str = range[0..separator_pos];
const max_str = range[separator_pos + 1 ..];
const min = std.fmt.parseFloat(f32, min_str) catch return false; const min = std.fmt.parseFloat(f32, min_str) catch return false;
const max = std.fmt.parseFloat(f32, max_str) catch return false; const max = std.fmt.parseFloat(f32, max_str) catch return false;
@ -979,6 +994,26 @@ test "ConfigVariable validation - integer range" {
try std.testing.expect(!int_var.validate("abc")); try std.testing.expect(!int_var.validate("abc"));
} }
test "ConfigVariable validation - negative integer range" {
// Rango con mínimo negativo: "-100-100"
const neg_var = ConfigVariable{
.name = "test_neg",
.config_key = "@test_neg",
.var_type = .integer,
.default = "0",
.auto_validate = "-100-100",
.description = "Test negative range",
};
try std.testing.expect(neg_var.validate("0"));
try std.testing.expect(neg_var.validate("-100"));
try std.testing.expect(neg_var.validate("100"));
try std.testing.expect(neg_var.validate("-50"));
try std.testing.expect(neg_var.validate("50"));
try std.testing.expect(!neg_var.validate("-101"));
try std.testing.expect(!neg_var.validate("101"));
}
test "ConfigVariable validation - enum" { test "ConfigVariable validation - enum" {
const enum_var = ConfigVariable{ const enum_var = ConfigVariable{
.name = "test_enum", .name = "test_enum",