From 64a2ca5ad7519cb4d1cc0b90dc48ce82a3209074 Mon Sep 17 00:00:00 2001 From: jlangch Date: Tue, 17 Oct 2023 09:16:42 +0200 Subject: [PATCH] added unit tests --- .../impl/functions/IOFunctionsStreams.java | 8 ++- .../impl/functions/IOFunctionsStreamTest.java | 69 +++++++++++++++++-- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java index 9ca0fa2a2..d5a21c283 100644 --- a/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java +++ b/src/main/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreams.java @@ -794,7 +794,8 @@ else if (Types.isVncJavaObject(args.first(), BufferedWriter.class)) { return args.first(); } else if (Types.isVncJavaObject(args.first(), Writer.class)) { - return new VncJavaObject(args.first()); + final Writer wr = Coerce.toVncJavaObject(args.first(), Writer.class); + return new VncJavaObject(new BufferedWriter(wr)); } else { throw new VncException(String.format( @@ -948,10 +949,13 @@ else if (Types.isVncJavaObject(arg, InputStream.class)) { throw new VncException("Failed to create reader from a :java.io.InputStream", ex); } } + else if (Types.isVncJavaObject(arg, BufferedReader.class)) { + return arg; + } else if (Types.isVncJavaObject(arg, Reader.class)) { try { final Reader rd = Coerce.toVncJavaObject(args.first(), Reader.class); - return new VncJavaObject(rd); + return new VncJavaObject(new BufferedReader(rd)); } catch (VncException ex) { throw ex; diff --git a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java index 339ef6628..0af9ba9ca 100644 --- a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java +++ b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java @@ -41,8 +41,7 @@ public void test_io_buffered_reader() { " (io/delete-file-on-exit file) " + " (io/spit file \"100\n200\" :append false) " + " (try-with [rd (io/buffered-reader file :encoding :utf-8)] " + - " (pr-str [(read-line rd) (read-line rd)])))) " + - ")"; + " (pr-str [(read-line rd) (read-line rd)])))) "; assertEquals("[\"100\" \"200\"]",venice.eval(script)); } @@ -60,10 +59,72 @@ public void test_io_buffered_writer() { " (println wr \"100\") " + " (println wr \"200\")) " + " (try-with [rd (io/buffered-reader file :encoding :utf-8)] " + - " (pr-str [(read-line rd) (read-line rd)])))) " + - ")"; + " (pr-str [(read-line rd) (read-line rd)])))) "; assertEquals("[\"100\" \"200\"]",venice.eval(script)); } + @Test + public void test_io_string_reader() { + final Venice venice = new Venice(); + + final String script1 = + "(try-with [rd (io/string-reader \"1234\")] \n" + + " (pr-str [ (read-char rd) \n" + + " (read-char rd) \n" + + " (read-char rd) ])) "; + + final String script2 = + "(let [rd (io/string-reader \"1\\n2\\n3\\n4\")] \n" + + " (try-with [br (io/buffered-reader rd)] \n" + + " (pr-str [ (read-line br) \n" + + " (read-line br) \n" + + " (read-line br) ]))) "; + + assertEquals("[#\\1 #\\2 #\\3]",venice.eval(script1)); + assertEquals("[\"1\" \"2\" \"3\"]",venice.eval(script2)); + } + + @Test + public void test_io_string_writer() { + final Venice venice = new Venice(); + + final String script = + "(try-with [sw (io/string-writer)] \n" + + " (print sw 100) \n" + + " (print sw \"-\") \n" + + " (print sw 200) \n" + + " (flush sw) \n" + + " @sw) "; + + assertEquals("100-200",venice.eval(script)); + } + + @Test + public void test_io_read_char() { + final Venice venice = new Venice(); + + final String script = + "(try-with [rd (io/string-reader \"1234\")] \n" + + " (pr-str [ (io/read-char rd) \n" + + " (io/read-char rd) \n" + + " (io/read-char rd) ])) "; + + assertEquals("[#\\1 #\\2 #\\3]",venice.eval(script)); + } + + @Test + public void test_io_read_line() { + final Venice venice = new Venice(); + + final String script = + "(let [rd (io/string-reader \"1\\n2\\n3\\n4\")] \n" + + " (try-with [br (io/buffered-reader rd)] \n" + + " (pr-str [ (io/read-line br) \n" + + " (io/read-line br) \n" + + " (io/read-line br) ]))) "; + + assertEquals("[\"1\" \"2\" \"3\"]",venice.eval(script)); + } + }