Skip to content

Commit

Permalink
refactored chestsheet examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Oct 17, 2023
1 parent fe04b95 commit d5804c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public VncVal apply(final VncList args) {
" (let [file (io/temp-file \"test-\", \".txt\")] \n" +
" (io/delete-file-on-exit file) \n" +
" (io/spit file \"123456789\" :append true) \n" +
" (try-with [rd (io/buffered-reader (io/file-in-stream file) :utf-8)] \n" +
" (try-with [rd (io/buffered-reader file :encoding :utf-8)] \n" +
" (io/slurp-reader rd))) \n" +
")")
.seeAlso(
Expand Down Expand Up @@ -738,10 +738,10 @@ else if (Types.isVncByteBuffer(content)) {
"`io/spit-writer` for stream output available!")
.examples(
"(do \n" +
" (let [file (io/temp-file \"test-\", \".txt\") \n" +
" os (io/file-out-stream file)] \n" +
" (io/delete-file-on-exit file) \n" +
" (try-with [wr (io/buffered-writer os :utf-8)] \n" +
" (let [file (io/temp-file \"test-\", \".txt\") \n" +
" os (io/file-out-stream file)] \n" +
" (io/delete-file-on-exit file) \n" +
" (try-with [wr (io/buffered-writer os :encoding :utf-8)] \n" +
" (io/spit-writer wr \"123456789\" :flush true)))) ")
.seeAlso("io/spit-stream", "io/spit")
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,12 @@ public VncVal apply(final VncList args) {
"(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" +
"f may be a: \n\n" +
" * `java.io.File`, e.g: `(io/file \"/temp/foo.json\")` \n" +
" * `java.nio.file.Path` \n" +
" * `java.io.OutputStream` \n" +
" * `java.io.Writer` \n" +
"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/buffered-writer` supports load paths. See the `loadpath/paths` " +
Expand Down Expand Up @@ -850,12 +854,12 @@ public VncVal apply(final VncList args) {
.doc(
"Create a `java.io.Reader` from f. \n\n" +
"f may be a: \n\n" +
" * string file path, e.g: \"/temp/foo.json\" \n" +
" * string \n" +
" * bytebuffer \n" +
" * `java.io.File`, e.g: `(io/file \"/temp/foo.json\")` \n" +
" * `java.nio.file.Path` \n" +
" * `java.io.InputStream` \n" +
" * `java.io.Reader` \n" +
" * `java.nio.file.Path` \n" +
" * `java.net.URL` \n" +
" * `java.net.URI` \n\n" +
"Options: \n\n" +
Expand Down Expand Up @@ -890,6 +894,13 @@ public VncVal apply(final VncList args) {

final ILoadPaths loadpaths = ThreadContext.getInterceptor().getLoadPaths();

if (Types.isVncString(arg)) {
return new VncJavaObject(
new BufferedReader(
new StringReader(
Coerce.toVncString(arg).getValue())));
}

final File file = convertToFile(arg);
if (file != null) {
try {
Expand All @@ -915,11 +926,6 @@ public VncVal apply(final VncList args) {
throw new VncException("Failed to create reader from the file " + file.getPath(), ex);
}
}
else if (Types.isVncString(arg)) {
return new VncJavaObject(
new StringReader(
Coerce.toVncString(arg).getValue()));
}
else if (Types.isVncByteBuffer(arg)) {
try {
final VncByteBuffer buf = (VncByteBuffer)arg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class IOFunctionsStreamTest {
public void test_io_buffered_reader() {
final Venice venice = new Venice();

final String script =
final String script1 =
"(do \n" +
" (import :java.io.FileInputStream) \n" +
" (let [file (io/temp-file \"test-\", \".txt\")] \n" +
Expand All @@ -43,7 +43,13 @@ public void test_io_buffered_reader() {
" (try-with [rd (io/buffered-reader file :encoding :utf-8)] \n" +
" (pr-str [(read-line rd) (read-line rd)])))) ";

assertEquals("[\"100\" \"200\"]",venice.eval(script));
final String script2 =
"(try-with [rd (io/buffered-reader \"100\n200\")] \n" +
" (pr-str [(read-line rd) (read-line rd)])) ";

assertEquals("[\"100\" \"200\"]",venice.eval(script1));

assertEquals("[\"100\" \"200\"]",venice.eval(script2));
}

@Test
Expand Down

0 comments on commit d5804c7

Please sign in to comment.