lib/log+test: wire printfln family onto fmt {n}-parser
Add lprintfln, printfln, lfatalf, fatalf — Hare-shape funcs over the
bb10ee7 fmt.fprintfln + fatalf scaffolding. Logger vtable grows by
one slot (printfln); std and silent loggers both wire the slot in
ensureinit. fatalf composes printfln + os.exit(255) like the existing
fatal arm.
Format-string param is named `format` rather than Hare's `fmt`. With
`use fmt;` at the top, naming the param `fmt: str` shadows the module
ref in body lookups — fmt.fprintfln in the body resolves to the str
param and emits CALL through str.ptr. Silent runtime crash. Filed as
task #19. Rename is reversible after #19.
Tests: 5 new scenarios — basic lprintfln + global dispatch + silent
no-op + indexed `{1} {0}` + modifier `{:5}`. Fatalf arms left TODO
pending the subprocess fixture (same shape as the existing fatal
TODO).
This commit is contained in:
@@ -165,10 +165,110 @@ fn streq(a: str, b: str) bool = {
|
||||
if (!streq(memio.string(&mem2), "second\n")) { fail(); };
|
||||
};
|
||||
|
||||
// TODO subprocess: log.fatal / log.lfatal exit(255). Verifying them
|
||||
// needs a fork+wait fixture so the parent can assert WEXITSTATUS ==
|
||||
// 255 without the test process itself terminating. lib/os doesn't
|
||||
// ship process spawning yet; revisit when that lands.
|
||||
// ---- lprintfln: {n}-placeholder render into a memio sink -------------
|
||||
|
||||
@test fn lprintflnbasic() void = {
|
||||
let buf: [32]u8;
|
||||
let mem: memio.state;
|
||||
let s: io.stream;
|
||||
memio.fixed(&mem, &s, buf[0:32]);
|
||||
|
||||
let sl: log.stdlogger;
|
||||
log.new(&sl, &s);
|
||||
|
||||
log.lprintfln(&sl.logger, "x={} y={}", 42i64, "hi");
|
||||
|
||||
if (!streq(memio.string(&mem), "x=42 y=hi\n")) { fail(); };
|
||||
};
|
||||
|
||||
// ---- printfln through global: setlogger then dispatch ---------------
|
||||
|
||||
// Mirrors [[setloggerswap]] but exercises the format-string path:
|
||||
// install sl1 as global, printfln writes there; mem2 stays empty.
|
||||
@test fn printflnglobal() void = {
|
||||
let buf1: [32]u8;
|
||||
let mem1: memio.state;
|
||||
let s1: io.stream;
|
||||
memio.fixed(&mem1, &s1, buf1[0:32]);
|
||||
let sl1: log.stdlogger;
|
||||
log.new(&sl1, &s1);
|
||||
|
||||
let buf2: [32]u8;
|
||||
let mem2: memio.state;
|
||||
let s2: io.stream;
|
||||
memio.fixed(&mem2, &s2, buf2[0:32]);
|
||||
let sl2: log.stdlogger;
|
||||
log.new(&sl2, &s2);
|
||||
|
||||
log.setlogger(&sl1.logger);
|
||||
log.printfln("v={}", 7i64);
|
||||
if (!streq(memio.string(&mem1), "v=7\n")) { fail(); };
|
||||
if (mem2.pos != 0) { fail(); };
|
||||
};
|
||||
|
||||
// ---- silent.printfln writes nothing ---------------------------------
|
||||
|
||||
// The silent logger's printfln callback discards args without touching
|
||||
// fmt or any sink. Same pattern as [[silentwritesnothing]] for the
|
||||
// bare-args path.
|
||||
@test fn silentignoresprintfln() void = {
|
||||
let buf: [16]u8;
|
||||
let mem: memio.state;
|
||||
let s: io.stream;
|
||||
memio.fixed(&mem, &s, buf[0:16]);
|
||||
|
||||
let sl: log.stdlogger;
|
||||
log.new(&sl, &s); // triggers log.ensureinit; populates log.silent
|
||||
|
||||
if (log.silent == nil) { fail(); };
|
||||
|
||||
log.lprintfln(log.silent, "ignored={}", 1i64);
|
||||
|
||||
if (mem.pos != 0) { fail(); };
|
||||
};
|
||||
|
||||
// ---- lprintfln: indexed {N} placeholder across log → fmt seam -------
|
||||
|
||||
// The fmt parser proper is covered in fmttest; this pins that log's
|
||||
// variadic forwarding propagates argv order so indexed placeholders
|
||||
// resolve correctly.
|
||||
@test fn lprintflnindexed() void = {
|
||||
let buf: [16]u8;
|
||||
let mem: memio.state;
|
||||
let s: io.stream;
|
||||
memio.fixed(&mem, &s, buf[0:16]);
|
||||
|
||||
let sl: log.stdlogger;
|
||||
log.new(&sl, &s);
|
||||
|
||||
log.lprintfln(&sl.logger, "{1} {0}", "a", "b");
|
||||
|
||||
if (!streq(memio.string(&mem), "b a\n")) { fail(); };
|
||||
};
|
||||
|
||||
// ---- lprintfln: {:mods} modifier across log → fmt seam --------------
|
||||
|
||||
// Pins that mod-bearing placeholders flow through log's forwarding
|
||||
// intact (parser proper covered in fmttest).
|
||||
@test fn lprintflnmods() void = {
|
||||
let buf: [16]u8;
|
||||
let mem: memio.state;
|
||||
let s: io.stream;
|
||||
memio.fixed(&mem, &s, buf[0:16]);
|
||||
|
||||
let sl: log.stdlogger;
|
||||
log.new(&sl, &s);
|
||||
|
||||
log.lprintfln(&sl.logger, "{:5}", 42i64);
|
||||
|
||||
if (!streq(memio.string(&mem), " 42\n")) { fail(); };
|
||||
};
|
||||
|
||||
// TODO subprocess: log.fatal / log.lfatal / log.fatalf / log.lfatalf
|
||||
// exit(255). Verifying them needs a fork+wait fixture so the parent
|
||||
// can assert WEXITSTATUS == 255 without the test process itself
|
||||
// terminating. lib/os doesn't ship process spawning yet; revisit
|
||||
// when that lands.
|
||||
|
||||
export fn main() i32 = {
|
||||
signalled = 1; defaultwiredtoglobal();
|
||||
@@ -177,5 +277,10 @@ export fn main() i32 = {
|
||||
signalled = 4; lprintlnempty();
|
||||
signalled = 5; silentwritesnothing();
|
||||
signalled = 6; setloggerswap();
|
||||
signalled = 7; lprintflnbasic();
|
||||
signalled = 8; printflnglobal();
|
||||
signalled = 9; silentignoresprintfln();
|
||||
signalled = 10; lprintflnindexed();
|
||||
signalled = 11; lprintflnmods();
|
||||
return 0;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user