update: context set

This commit is contained in:
Lucas F. 2026-01-04 12:29:36 -03:00
parent 164e68e94c
commit e5305f85c9
2 changed files with 110 additions and 75 deletions

View file

@ -3,74 +3,33 @@ const testing = std.testing;
const Context = @import("context.zig").Context;
const Value = @import("context.zig").Value;
test "context com arena" {
test "context set amigável e get com ponto" {
const allocator = testing.allocator;
var ctx = Context.init(allocator);
defer ctx.deinit();
try ctx.set("nome", Value{ .string = "Lucas" }); // sem dupe!
try ctx.set("idade", Value{ .int = 30 });
try ctx.set("nome", "Lucas");
try ctx.set("idade", 30);
try ctx.set("ativo", true);
try ctx.set("preco", 99.99);
try ctx.set("vazio", "");
// struct
const Person = struct { nome: []const u8, idade: i64 };
const p = Person{ .nome = "Ana", .idade = 25 };
try ctx.set("user", p);
//
// // list
const numeros = [_]i64{ 1, 2, 3 };
try ctx.set("lista", numeros);
// acesso
try testing.expectEqualStrings("Lucas", ctx.get("nome").?.string);
try testing.expectEqual(@as(i64, 30), ctx.get("idade").?.int);
}
test "context todos os tipos" {
const allocator = testing.allocator;
var ctx = Context.init(allocator);
defer ctx.deinit();
// null
try ctx.set("nulo", Value{ .null = {} });
try testing.expectEqual(ctx.get("nulo").?, .null);
// bool
try ctx.set("verdadeiro", Value{ .bool = true });
try ctx.set("falso", Value{ .bool = false });
try testing.expect(ctx.get("verdadeiro").?.bool == true);
try testing.expect(ctx.get("falso").?.bool == false);
// int
try ctx.set("idade", Value{ .int = 30 });
try testing.expect(ctx.get("idade").?.int == 30);
// float
try ctx.set("preco", Value{ .float = 99.99 });
try testing.expectApproxEqAbs(@as(f64, 99.99), ctx.get("preco").?.float, 0.001);
// string
try ctx.set("nome", Value{ .string = "Lucas" });
try testing.expectEqualStrings("Lucas", ctx.get("nome").?.string);
// list
var list = try allocator.alloc(Value, 3);
defer allocator.free(list);
list[0] = Value{ .int = 1 };
list[1] = Value{ .int = 2 };
list[2] = Value{ .int = 3 };
try ctx.set("numeros", Value{ .list = list });
const numeros = ctx.get("numeros").?.list;
try testing.expectEqual(@as(usize, 3), numeros.len);
try testing.expect(numeros[0].int == 1);
try testing.expect(numeros[1].int == 2);
try testing.expect(numeros[2].int == 3);
// dict
var dict = std.StringHashMapUnmanaged(Value){};
try dict.put(ctx.allocator(), "chave1", Value{ .string = "valor1" });
try dict.put(ctx.allocator(), "chave2", Value{ .int = 42 });
try ctx.set("config", Value{ .dict = dict });
const config = ctx.get("config").?.dict;
try testing.expectEqualStrings("valor1", config.get("chave1").?.string);
try testing.expect(config.get("chave2").?.int == 42);
}
test "failback" {
const allocator = testing.allocator;
var ctx = Context.init(allocator);
defer ctx.deinit();
try testing.expectEqual(ctx.get("nome"), null);
try testing.expectEqualStrings("Ana", ctx.get("user.nome").?.string);
try testing.expect(ctx.get("user.idade").?.int == 25);
try testing.expect(ctx.get("lista.1").?.int == 2);
try testing.expect(ctx.get("vazio").?.string.len == 0);
try testing.expect(ctx.get("preco").?.float == 99.99);
try testing.expect(ctx.get("ativo").?.bool == true);
}