From bc7cf8799f98b902672cd2f198e3b1b2d0592d7a Mon Sep 17 00:00:00 2001 From: jlangch Date: Sat, 30 Mar 2024 16:31:32 +0100 Subject: [PATCH] added unit tests --- .../venice/modules/TraceModuleTest.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/test/java/com/github/jlangch/venice/modules/TraceModuleTest.java b/src/test/java/com/github/jlangch/venice/modules/TraceModuleTest.java index 5abd04de5..4491d11f7 100644 --- a/src/test/java/com/github/jlangch/venice/modules/TraceModuleTest.java +++ b/src/test/java/com/github/jlangch/venice/modules/TraceModuleTest.java @@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Map; @@ -32,7 +33,9 @@ import com.github.jlangch.venice.Parameters; import com.github.jlangch.venice.Venice; +import com.github.jlangch.venice.VncException; import com.github.jlangch.venice.impl.util.StringUtil; +import com.github.jlangch.venice.util.CapturingPrintStream; public class TraceModuleTest { @@ -87,6 +90,68 @@ public void test_trace() { assertNull(venice.eval(script3, params)); } + @Test + public void test_trace_nested() { + final CapturingPrintStream cps = new CapturingPrintStream(); + + final Venice venice = new Venice(); + final Map params = Parameters.of("*out*", cps); + + final String script1 = + "(do \n" + + " (load-module :trace ['trace :as 't]) \n" + + " \n" + + " (defn foo [x] (+ x 2)) \n" + + " (defn bar [x] (foo x)) \n" + + " \n" + + " (t/trace-var +) \n" + + " (t/trace-var foo) \n" + + " (t/trace-var bar) \n" + + " \n" + + " (bar 5)) "; + + assertEquals(7L, venice.eval(script1, params)); + assertEquals( + "TRACE t00: (user/bar 5)\n" + + "TRACE t00: | (user/foo 5)\n" + + "TRACE t00: | | (core/+ 5 2)\n" + + "TRACE t00: | | | => 7\n" + + "TRACE t00: | | => 7\n" + + "TRACE t00: | => 7\n", + cps.getOutput().replaceAll("t[0-9]+:", "t00:")); + } + + @Test + public void test_trace_exceptipn() { + final CapturingPrintStream cps = new CapturingPrintStream(); + + final Venice venice = new Venice(); + final Map params = Parameters.of("*out*", cps); + + final String script1 = + "(do \n" + + " (load-module :trace ['trace :as 't]) \n" + + " \n" + + " (defn foo [x] (/ x 0)) \n" + + " (defn bar [x] (foo x)) \n" + + " \n" + + " (t/trace-var /) \n" + + " (t/trace-var foo) \n" + + " (t/trace-var bar) \n" + + " \n" + + " (bar 5)) "; + + assertThrows(VncException.class, () -> venice.eval(script1, params)); + assertEquals( + "TRACE t00: (user/bar 5)\n" + + "TRACE t00: | (user/foo 5)\n" + + "TRACE t00: | | (core// 5 0)\n" + + "TRACE t00: | | | => com.github.jlangch.venice.VncException: / by zero\n" + + "TRACE t00: | | => com.github.jlangch.venice.VncException: / by zero\n" + + "TRACE t00: | => com.github.jlangch.venice.VncException: / by zero\n", + cps.getOutput().replaceAll("t[0-9]+:", "t00:")); + } + @Test public void test_traceable() { final Venice venice = new Venice();