From fddcd4556c01bf7add84a265ee79a41be06647c7 Mon Sep 17 00:00:00 2001 From: Delgan Date: Tue, 26 Nov 2024 12:26:08 +0100 Subject: [PATCH] Add unit test ensuring "lazy" args are called exactly once --- tests/test_opt.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_opt.py b/tests/test_opt.py index 6db8480f..2704103b 100644 --- a/tests/test_opt.py +++ b/tests/test_opt.py @@ -153,6 +153,22 @@ def laziness(): assert writer.read() == "10 => 1: 1\n17 => 4: 1\n20 => 7: 2\n" +def test_lazy_function_executed_only_once(writer): + counter = 0 + + def laziness(): + nonlocal counter + counter += 1 + return counter + + logger.add(writer, level=10, format="{level.name} => {message}") + + logger.opt(lazy=True).info("1: {lazy} {lazy}", lazy=laziness) + logger.opt(lazy=True).info("2: {0} {0}", laziness) + + assert writer.read() == "INFO => 1: 1 1\nINFO => 2: 2 2\n" + + def test_logging_within_lazy_function(writer): logger.add(writer, level=20, format="{message}")