From d059af3b0212f4eef19589c6dd275d839913018d Mon Sep 17 00:00:00 2001 From: jlangch Date: Tue, 17 Oct 2023 08:50:16 +0200 Subject: [PATCH] refactoring --- .../docgen/cheatsheet/section/IoSection.java | 2 - .../impl/functions/IOFunctionsStreams.java | 160 ++----------- .../com/github/jlangch/venice/jsonl.venice | 8 +- .../functions/IOFunctionsSpitSlurpTest.java | 215 ++++++++++++++++++ .../impl/functions/IOFunctionsStreamTest.java | 69 ++++++ .../impl/functions/IOFunctionsTest.java | 179 --------------- 6 files changed, 313 insertions(+), 320 deletions(-) create mode 100644 src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurpTest.java create mode 100644 src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java diff --git a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/section/IoSection.java b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/section/IoSection.java index 8b8b3b989..ada43d319 100644 --- a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/section/IoSection.java +++ b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/section/IoSection.java @@ -100,8 +100,6 @@ public DocSection section() { all.addSection(rd_wr); rd_wr.addItem(diBuilder.getDocItem("io/buffered-reader")); rd_wr.addItem(diBuilder.getDocItem("io/buffered-writer")); - rd_wr.addItem(diBuilder.getDocItem("io/reader")); - rd_wr.addItem(diBuilder.getDocItem("io/writer")); rd_wr.addItem(diBuilder.getDocItem("io/string-reader")); rd_wr.addItem(diBuilder.getDocItem("io/string-writer")); rd_wr.addItem(diBuilder.getDocItem("io/flush")); 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 f1c06b21a..9ca0fa2a2 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 @@ -706,71 +706,6 @@ public VncVal apply(final VncList args) { private static final long serialVersionUID = -1848883965231344442L; }; - public static VncFunction io_buffered_reader = - new VncFunction( - "io/buffered-reader", - VncFunction - .meta() - .arglists( - "(io/buffered-reader is encoding?)", - "(io/buffered-reader rdr)") - .doc( - "Creates a `java.io.BufferedReader` from a `java.io.InputStream` is with optional " + - "encoding (defaults to :utf-8), from a `java.io.Reader` or from a string.\n\n" + - "Note: The caller is responsible for closing the reader!") - .examples( - "(let [data (bytebuf [108 105 110 101 32 49 10 108 105 110 101 32 50]) \n" + - " is (io/bytebuf-in-stream data)] \n" + - " (try-with [rd (io/buffered-reader is :utf-8)] \n" + - " (println (read-line rd)) \n" + - " (println (read-line rd)))) ", - "(try-with [rd (io/buffered-reader \"1\\n2\\n3\\n4\")] \n" + - " (println (read-line rd)) \n" + - " (println (read-line rd))) ") - .seeAlso( - "io/reader", "io/string-reader", "io/buffered-writer") - .build() - ) { - @Override - public VncVal apply(final VncList args) { - ArityExceptions.assertArity(this, args, 1, 2); - - if (Types.isVncString(args.first())) { - return new VncJavaObject( - new BufferedReader( - new StringReader(((VncString)args.first()).getValue()))); - } - else if (Types.isVncJavaObject(args.first())) { - final Object delegate = ((VncJavaObject)args.first()).getDelegate(); - if (delegate instanceof InputStream) { - try { - final InputStream is = (InputStream)delegate; - final Charset charset = CharsetUtil.charset(args.second()); - - return new VncJavaObject( - new BufferedReader( - new InputStreamReader(is, charset))); - } - catch (Exception ex) { - throw new VncException(ex.getMessage(), ex); - } - } - else if (delegate instanceof BufferedReader) { - return args.first(); - } - else if (delegate instanceof Reader) { - return new VncJavaObject(new BufferedReader((Reader)delegate)); - } - } - - throw new VncException(String.format( - "Function 'io/buffered-reader' requires a :java.io.InputStream, " + - "a :java.io.Reader, or a string. %s as is not allowed!", - Types.getType(args.first()))); - } - - private static final long serialVersionUID = -1848883965231344442L; - }; public static VncFunction io_buffered_writer = new VncFunction( @@ -778,76 +713,22 @@ else if (delegate instanceof Reader) { VncFunction .meta() .arglists( - "(io/buffered-writer os encoding?)", - "(io/buffered-writer wr)") - .doc( - "Creates a `java.io.BufferedWriter` from a `java.io.OutputStream` os with optional " + - "encoding (defaults to :utf-8) or from a `java.io.Writer`.\n\n" + - "Note: The caller is responsible for closing the writer!") - .examples() - .seeAlso( - "io/writer", "io/string-writer", "io/buffered-reader") - .build() - ) { - @Override - public VncVal apply(final VncList args) { - ArityExceptions.assertArity(this, args, 1, 2); - - if (Types.isVncJavaObject(args.first())) { - final Object delegate = ((VncJavaObject)args.first()).getDelegate(); - if (delegate instanceof OutputStream) { - try { - final OutputStream os = (OutputStream)delegate; - final Charset charset = CharsetUtil.charset(args.second()); - - return new VncJavaObject( - new BufferedWriter( - new OutputStreamWriter(os, charset))); - } - catch (Exception ex) { - throw new VncException(ex.getMessage(), ex); - } - } - else if (delegate instanceof BufferedWriter) { - return args.first(); - } - else if (delegate instanceof Writer) { - return new VncJavaObject(new BufferedWriter((Writer)delegate)); - } - } - - throw new VncException(String.format( - "Function 'io/buffered-writer' requires a :java.io.OutputStream " + - "or a :java.io.Writer. %s as is not allowed!", - Types.getType(args.first()))); - } - - private static final long serialVersionUID = -1848883965231344442L; - }; - - - public static VncFunction io_writer = - new VncFunction( - "io/writer", - VncFunction - .meta() - .arglists( - "(io/writer f & options)" ) + "(io/buffered-writer f & options)" ) .doc( "Creates a `java.io.Writer` for f.\n\n" + "f may be a file or a string (file path). " + "Options: \n\n" + "| :append true/false | e.g.: `:append true`, defaults to false |\n" + "| :encoding enc | e.g.: `:encoding :utf-8`, defaults to :utf-8 |\n\n" + - "`io/writer` supports load paths. See the `loadpath/paths` " + + "`io/buffered-writer` supports load paths. See the `loadpath/paths` " + "doc for a description of the *load path* feature.") .seeAlso( - "str", "io/string-writer", "io/buffered-writer", "io/reader") + "println", "io/string-writer", "io/buffered-reader") .build() ) { @Override public VncVal apply(final VncList args) { - ArityExceptions.assertMinArity(this, args, 2); + ArityExceptions.assertMinArity(this, args, 1); sandboxFunctionCallValidation(); @@ -909,12 +790,15 @@ else if (Types.isVncJavaObject(args.first(), OutputStream.class)) { ex); } } + else if (Types.isVncJavaObject(args.first(), BufferedWriter.class)) { + return args.first(); + } else if (Types.isVncJavaObject(args.first(), Writer.class)) { return new VncJavaObject(args.first()); } else { throw new VncException(String.format( - "Function 'io/writer' does not allow %s as f", + "Function 'io/buffered-writer' does not allow %s as f", Types.getType(args.first()))); } } @@ -942,7 +826,7 @@ else if (Types.isVncJavaObject(args.first(), Writer.class)) { " (flush sw) \n" + " (println @sw)) ") .seeAlso( - "str", "io/writer", "io/buffered-writer", "io/string-reader") + "println", "io/buffered-writer", "io/buffered-reader") .build() ) { @Override @@ -955,13 +839,13 @@ public VncVal apply(final VncList args) { private static final long serialVersionUID = -1848883965231344442L; }; - public static VncFunction io_reader = + public static VncFunction io_buffered_reader = new VncFunction( - "io/reader", + "io/buffered-reader", VncFunction .meta() .arglists( - "(io/reader f & options)" ) + "(io/buffered-reader f & options)" ) .doc( "Create a `java.io.Reader` from f. \n\n" + "f may be a: \n\n" + @@ -975,11 +859,19 @@ public VncVal apply(final VncList args) { " * `java.net.URI` \n\n" + "Options: \n\n" + "| :encoding enc | e.g.: `:encoding :utf-8`, defaults to :utf-8 | \n\n" + - "`io/reader` supports load paths. See the `loadpath/paths` " + + "`io/buffered-reader` supports load paths. See the `loadpath/paths`" + "doc for a description of the *load path* feature. \n\n" + "Note: The caller is responsible for closing the reader!") - .seeAlso( - "io/string-reader", "io/buffered-reader", "io/writer") + .examples( + "(let [data (bytebuf [108 105 110 101 32 49 10 108 105 110 101 32 50])] \n" + + " (try-with [rd (io/buffered-reader data :encoding :utf-8)] \n" + + " (println (read-line rd)) \n" + + " (println (read-line rd)))) ", + "(try-with [rd (io/buffered-reader \"1\\n2\\n3\\n4\")] \n" + + " (println (read-line rd)) \n" + + " (println (read-line rd))) ") + .seeAlso( + "read-line", "io/string-reader", "io/buffered-writer") .build() ) { @Override @@ -1070,7 +962,7 @@ else if (Types.isVncJavaObject(arg, Reader.class)) { } else { throw new VncException(String.format( - "Function 'io/reader' does not allow %s as f", + "Function 'io/buffered-reader' does not allow %s as f", Types.getType(args.first()))); } } @@ -1099,7 +991,7 @@ else if (Types.isVncJavaObject(arg, Reader.class)) { " (println (read-line br)) \n" + " (println (read-line br)))) ") .seeAlso( - "io/reader", "io/buffered-reader", "io/string-writer") + "read-line", "io/buffered-reader", "io/string-writer") .build() ) { @Override @@ -1333,10 +1225,8 @@ public static void validateReadableFile(final File file) { .add(io_wrap_os_with_print_writer) .add(io_wrap_is_with_buffered_reader) .add(io_buffered_writer) - .add(io_writer) .add(io_string_writer) .add(io_buffered_reader) - .add(io_reader) .add(io_string_reader) .add(io_capturing_print_stream) .add(io_print) diff --git a/src/main/resources/com/github/jlangch/venice/jsonl.venice b/src/main/resources/com/github/jlangch/venice/jsonl.venice index 75c522923..d6afbd92c 100644 --- a/src/main/resources/com/github/jlangch/venice/jsonl.venice +++ b/src/main/resources/com/github/jlangch/venice/jsonl.venice @@ -178,10 +178,10 @@ slurp [in & options] - (try-with [rd (apply io/buffered-reader in options))] - (loop [line (. rd :readLine) data '()] + (try-with [rd (apply io/buffered-reader in options)] + (loop [line (read-line rd) data '()] (if (some? line) (let [l (str/trim-to-nil line) v (if (nil? l) nil (apply json/read-str l options))] - (recur (. rd :readLine) (if (nil? v) data (conj data v)))) - data))) + (recur (read-line rd) (if (nil? v) data (conj data v)))) + data)))) diff --git a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurpTest.java b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurpTest.java new file mode 100644 index 000000000..b37c189ff --- /dev/null +++ b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsSpitSlurpTest.java @@ -0,0 +1,215 @@ +/* __ __ _ + * \ \ / /__ _ __ (_) ___ ___ + * \ \/ / _ \ '_ \| |/ __/ _ \ + * \ / __/ | | | | (_| __/ + * \/ \___|_| |_|_|\___\___| + * + * + * Copyright 2017-2022 Venice + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jlangch.venice.impl.functions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; + +import org.junit.jupiter.api.Test; + +import com.github.jlangch.venice.Parameters; +import com.github.jlangch.venice.Venice; + + +public class IOFunctionsSpitSlurpTest { + + @Test + public void test_io_spit() { + final Venice venice = new Venice(); + + // with default encoding + try { + final File file = File.createTempFile("spit", ".txt"); + file.deleteOnExit(); + + venice.eval( + "(io/spit file \"123456789\" :append true)", + Parameters.of("file", file.getAbsolutePath())); + + assertEquals( + "123456789", + venice.eval( + "(io/slurp file)", + Parameters.of("file", file.getAbsolutePath()))); + } + catch(Exception ex) { + throw new RuntimeException(ex); + } + + // with UTF-8 encoding + try { + final File file = File.createTempFile("spit", ".txt"); + file.deleteOnExit(); + + venice.eval( + "(io/spit file \"123456789\" :append true :encoding \"UTF-8\")", + Parameters.of("file", file.getAbsolutePath())); + + assertEquals( + "123456789", + venice.eval( + "(io/slurp file :encoding \"UTF-8\")", + Parameters.of("file", file.getAbsolutePath()))); + } + catch(Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + public void test_io_slurp() { + final Venice venice = new Venice(); + + // with default encoding + try { + final File file = File.createTempFile("slurp", ".txt"); + file.deleteOnExit(); + + Files.write(file.toPath(), "123456789".getBytes("UTF-8"), StandardOpenOption.APPEND); + + assertEquals( + "123456789", + venice.eval( + "(io/slurp file)", + Parameters.of("file", file.getAbsolutePath()))); + } + catch(Exception ex) { + throw new RuntimeException(ex); + } + + // with UTF-8 encoding + try { + final File file = File.createTempFile("slurp", ".txt"); + file.deleteOnExit(); + + Files.write(file.toPath(), "123456789".getBytes("UTF-8"), StandardOpenOption.APPEND); + + assertEquals( + "123456789", + venice.eval( + "(io/slurp file :encoding \"UTF-8\")", + Parameters.of("file", file.getAbsolutePath()))); + } + catch(Exception ex) { + throw new RuntimeException(ex); + } + } + + @Test + public void test_io_slurp_stream() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileInputStream) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/spit file \"123456789\" :append true) " + + " (io/delete-file-on-exit file) " + + " (try-with [is (. :FileInputStream :new file)] " + + " (io/slurp-stream is :binary false))) " + + ")"; + + assertEquals("123456789",venice.eval(script)); + } + + @Test + public void test_io_slurp_lines_file() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/spit file \"123\n456\n789\" :append true) " + + " (io/delete-file-on-exit file) " + + " (pr-str (io/slurp-lines file)))) " + + ")"; + + assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); + } + + @Test + public void test_io_slurp_lines_stream_1() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileInputStream) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/spit file \"123\n456\n789\" :append true) " + + " (io/delete-file-on-exit file) " + + " (try-with [is (. :FileInputStream :new file)] " + + " (pr-str (io/slurp-lines is)))) " + + ")"; + + assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); + } + + @Test + public void test_io_slurp_lines_stream_2() { + final Venice venice = new Venice(); + + final String script = + "(str (->> \"1\\n2\\n3\" \n" + + " io/string-in-stream \n" + + " io/slurp-lines))"; + + assertEquals("(1 2 3)", venice.eval(script)); + } + + @Test + public void test_io_slurp_lines_reader() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileReader) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/spit file \"123\n456\n789\" :append true) " + + " (io/delete-file-on-exit file) " + + " (try-with [rd (. :FileReader :new file)] " + + " (pr-str (io/slurp-lines rd)))) " + + ")"; + + assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); + } + + @Test + public void test_io_spit_stream() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileOutputStream) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/delete-file-on-exit file) " + + " (try-with [is (. :FileOutputStream :new file)] " + + " (io/spit-stream is \"123456789\" :flush true)) " + + " (io/slurp file :binary false)) " + + ")"; + + assertEquals("123456789",venice.eval(script)); + } + +} 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 new file mode 100644 index 000000000..339ef6628 --- /dev/null +++ b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsStreamTest.java @@ -0,0 +1,69 @@ +/* __ __ _ + * \ \ / /__ _ __ (_) ___ ___ + * \ \/ / _ \ '_ \| |/ __/ _ \ + * \ / __/ | | | | (_| __/ + * \/ \___|_| |_|_|\___\___| + * + * + * Copyright 2017-2022 Venice + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.jlangch.venice.impl.functions; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import com.github.jlangch.venice.Venice; + + +public class IOFunctionsStreamTest { + + @Test + public void test_io_buffered_reader() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileInputStream) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (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)])))) " + + ")"; + + assertEquals("[\"100\" \"200\"]",venice.eval(script)); + } + + @Test + public void test_io_buffered_writer() { + final Venice venice = new Venice(); + + final String script = + "(do " + + " (import :java.io.FileInputStream) " + + " (let [file (io/temp-file \"test-\", \".txt\")] " + + " (io/delete-file-on-exit file) " + + " (try-with [wr (io/buffered-writer file)] " + + " (println wr \"100\") " + + " (println wr \"200\")) " + + " (try-with [rd (io/buffered-reader file :encoding :utf-8)] " + + " (pr-str [(read-line rd) (read-line rd)])))) " + + ")"; + + assertEquals("[\"100\" \"200\"]",venice.eval(script)); + } + +} diff --git a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsTest.java b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsTest.java index 229629f73..24c206669 100644 --- a/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsTest.java +++ b/src/test/java/com/github/jlangch/venice/impl/functions/IOFunctionsTest.java @@ -28,8 +28,6 @@ import java.io.File; import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; import org.junit.jupiter.api.Test; @@ -431,183 +429,6 @@ public void test_io_tmp_dir() { assertTrue((Boolean)venice.eval("(io/file? (io/tmp-dir))")); } - @Test - public void test_io_spit() { - final Venice venice = new Venice(); - - // with default encoding - try { - final File file = File.createTempFile("spit", ".txt"); - file.deleteOnExit(); - - venice.eval( - "(io/spit file \"123456789\" :append true)", - Parameters.of("file", file.getAbsolutePath())); - - assertEquals( - "123456789", - venice.eval( - "(io/slurp file)", - Parameters.of("file", file.getAbsolutePath()))); - } - catch(Exception ex) { - throw new RuntimeException(ex); - } - - // with UTF-8 encoding - try { - final File file = File.createTempFile("spit", ".txt"); - file.deleteOnExit(); - - venice.eval( - "(io/spit file \"123456789\" :append true :encoding \"UTF-8\")", - Parameters.of("file", file.getAbsolutePath())); - - assertEquals( - "123456789", - venice.eval( - "(io/slurp file :encoding \"UTF-8\")", - Parameters.of("file", file.getAbsolutePath()))); - } - catch(Exception ex) { - throw new RuntimeException(ex); - } - } - - @Test - public void test_io_slurp() { - final Venice venice = new Venice(); - - // with default encoding - try { - final File file = File.createTempFile("slurp", ".txt"); - file.deleteOnExit(); - - Files.write(file.toPath(), "123456789".getBytes("UTF-8"), StandardOpenOption.APPEND); - - assertEquals( - "123456789", - venice.eval( - "(io/slurp file)", - Parameters.of("file", file.getAbsolutePath()))); - } - catch(Exception ex) { - throw new RuntimeException(ex); - } - - // with UTF-8 encoding - try { - final File file = File.createTempFile("slurp", ".txt"); - file.deleteOnExit(); - - Files.write(file.toPath(), "123456789".getBytes("UTF-8"), StandardOpenOption.APPEND); - - assertEquals( - "123456789", - venice.eval( - "(io/slurp file :encoding \"UTF-8\")", - Parameters.of("file", file.getAbsolutePath()))); - } - catch(Exception ex) { - throw new RuntimeException(ex); - } - } - - @Test - public void test_io_slurp_stream() { - final Venice venice = new Venice(); - - final String script = - "(do " + - " (import :java.io.FileInputStream) " + - " (let [file (io/temp-file \"test-\", \".txt\")] " + - " (io/spit file \"123456789\" :append true) " + - " (io/delete-file-on-exit file) " + - " (try-with [is (. :FileInputStream :new file)] " + - " (io/slurp-stream is :binary false))) " + - ")"; - - assertEquals("123456789",venice.eval(script)); - } - - @Test - public void test_io_slurp_lines_file() { - final Venice venice = new Venice(); - - final String script = - "(do " + - " (let [file (io/temp-file \"test-\", \".txt\")] " + - " (io/spit file \"123\n456\n789\" :append true) " + - " (io/delete-file-on-exit file) " + - " (pr-str (io/slurp-lines file)))) " + - ")"; - - assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); - } - - @Test - public void test_io_slurp_lines_stream_1() { - final Venice venice = new Venice(); - - final String script = - "(do " + - " (import :java.io.FileInputStream) " + - " (let [file (io/temp-file \"test-\", \".txt\")] " + - " (io/spit file \"123\n456\n789\" :append true) " + - " (io/delete-file-on-exit file) " + - " (try-with [is (. :FileInputStream :new file)] " + - " (pr-str (io/slurp-lines is)))) " + - ")"; - - assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); - } - - @Test - public void test_io_slurp_lines_stream_2() { - final Venice venice = new Venice(); - - final String script = - "(str (->> \"1\\n2\\n3\" \n" + - " io/string-in-stream \n" + - " io/slurp-lines))"; - - assertEquals("(1 2 3)", venice.eval(script)); - } - - @Test - public void test_io_slurp_lines_reader() { - final Venice venice = new Venice(); - - final String script = - "(do " + - " (import :java.io.FileReader) " + - " (let [file (io/temp-file \"test-\", \".txt\")] " + - " (io/spit file \"123\n456\n789\" :append true) " + - " (io/delete-file-on-exit file) " + - " (try-with [rd (. :FileReader :new file)] " + - " (pr-str (io/slurp-lines rd)))) " + - ")"; - - assertEquals("(\"123\" \"456\" \"789\")", venice.eval(script)); - } - - @Test - public void test_io_spit_stream() { - final Venice venice = new Venice(); - - final String script = - "(do " + - " (import :java.io.FileOutputStream) " + - " (let [file (io/temp-file \"test-\", \".txt\")] " + - " (io/delete-file-on-exit file) " + - " (try-with [is (. :FileOutputStream :new file)] " + - " (io/spit-stream is \"123456789\" :flush true)) " + - " (io/slurp file :binary false)) " + - ")"; - - assertEquals("123456789",venice.eval(script)); - } - @Test public void test_io_user_dir() { final Venice venice = new Venice();