@@ -56,6 +56,11 @@ local function strip_ansi(s)
5656 return s :gsub (" \27 %[%d+m" , " " )
5757end
5858
59+ -- Check that a log line ends with the expected message
60+ local function ends_with_msg (s , msg )
61+ return strip_ansi (s ):sub (- # msg - 1 ) == msg .. " \n "
62+ end
63+
5964
6065-- ── Suite: level filtering ────────────────────────────────────────────────────
6166
486491end
487492
488493do
489- -- custom tostr is called for each argument
494+ -- custom tostr is called for each argument in multi-arg calls
490495 reset_log ()
491496 log .usecolor = false
492497 local calls = {}
502507 assert_true (" custom tostr return value appears in output" , captured [1 ]:find (" x x" ))
503508end
504509
510+ do
511+ -- single non-string arg goes through tostr
512+ reset_log ()
513+ log .usecolor = false
514+ local received
515+ log .tostr = function (v )
516+ received = v ; return " <" .. tostring (v ) .. " >"
517+ end
518+
519+ capture_start ()
520+ log .info (42 )
521+ capture_stop ()
522+
523+ assert_eq (" tostr receives the non-string value" , received , 42 )
524+ assert_true (" single non-string arg formatted by tostr" , ends_with_msg (captured [1 ], " <42>" ))
525+ end
526+
527+ do
528+ -- single string arg bypasses tostr and equals the message exactly
529+ reset_log ()
530+ log .usecolor = false
531+ local called = false
532+ log .tostr = function (v )
533+ called = true ; return tostring (v )
534+ end
535+
536+ capture_start ()
537+ log .info (" plain string" )
538+ capture_stop ()
539+
540+ assert_false (" tostr not called for single string arg" , called )
541+ assert_true (" single string arg equals message exactly" , ends_with_msg (captured [1 ], " plain string" ))
542+ end
543+
505544do
506545 -- custom tostr bypasses number rounding
507546 log .tostr = function (v ) return tostring (v ) end
520559 local inst = log { tostr = function (v ) return " T:" .. tostring (v ) end , usecolor = false }
521560
522561 capture_start ()
523- inst .info (" hello" )
562+ inst .info (" hello" , " world " )
524563 capture_stop ()
525564 assert_true (" instance tostr wraps value" , captured [1 ]:find (" T:hello" ))
526565
527566 capture_start ()
528- log .info (" hello" )
567+ log .info (" hello" , " world " )
529568 capture_stop ()
530569 assert_false (" global logger unaffected by instance tostr" , captured [1 ]:find (" T:hello" ))
531570end
538577 log .usecolor = false
539578
540579 capture_start ()
541- inst .info (" hello" )
580+ inst .info (" hello" , " world " )
542581 capture_stop ()
543582
544583 assert_true (" instance inherits tostr from global logger" , captured [1 ]:find (" G:hello" ))
551590 local inst = log { tostr = function (v ) return " I:" .. tostring (v ) end , usecolor = false }
552591
553592 capture_start ()
554- inst .info (" hello" )
593+ inst .info (" hello" , " world " )
555594 capture_stop ()
556595 assert_true (" instance tostr overrides global tostr" , captured [1 ]:find (" I:hello" ))
557596 assert_false (" global tostr not used when instance tostr set" , captured [1 ]:find (" G:hello" ))
0 commit comments