jolt/test/chez/_io.janet
Yogthos b38a7dd007 Chez Phase 2 (inc V): java.io.File + slurp/spit/flush/file-seq (jolt-yyud)
A File is a path-backed jfile record: (instance? java.io.File f) is true,
str/slurp coerce it to its path, and the File method surface (getName/
getPath/exists/isDirectory/isFile/listFiles/getParent) dispatches through
record-method-dispatch. slurp/spit/flush run over Chez's filesystem
primitives; file-seq's dir primitives (__file?/__dir?/__list-dir) and the
overlay's File branch (.isDirectory/.listFiles, which emit to jolt-host-call)
are jfile-aware. clojure.java.io/file + as-file are def-var!'d natively.

New host/chez/io.ss, a Chez-native implementation -- the seed's
clojure.java.io (io.clj) is a Janet-backed shim over janet.*/janet.file, so
it can't be reused. The analyzer resolves io/file because the seed ctx has
clojure.java.io loaded; only a runtime def-var! is needed. type/instance-
check/str-render/jolt-host-call are extended via the set!-wrap pattern (type
also re-def-var!'d since the var cell captured the old value).

Reader/StringReader-coupled io (io/reader, line-seq over a file, .toURL,
slurp over a reader) deferred to jolt-at0a.

Parity 2176 -> 2191, 0 new divergences. New test/chez/_io.janet 20/20.
2026-06-19 10:02:12 -04:00

65 lines
3.6 KiB
Text

# jolt-yyud — clojure.java.io/file + java.io.File interop + slurp/spit/flush/
# file-seq on Chez. A File is a path-backed jfile record (instance? java.io.File,
# str -> path, the File method surface). io.clj is a Janet-backed shim (janet.*),
# so this is a Chez-native implementation, not a port of it. Reader/StringReader-
# coupled cases (line-seq, slurp over a reader, toURL) are deferred to jolt-at0a.
# Oracle = build/jolt.
#
# janet test/chez/_io.janet
(def jolt-bin (or (os/getenv "JOLT_BIN") "bin/jolt-chez"))
(defn io [body] (string "(do (require (quote [clojure.java.io :as io])) " body ")"))
(def cases
[# --- File construction + method surface ---
[(io "(str (io/file \"/a/b\"))") "/a/b"]
[(io "(str (io/file \"/a\" \"b\"))") "/a/b"]
[(io "(.getName (io/file \"/a/b/c.txt\"))") "c.txt"]
[(io "(.getPath (io/file \"/a\" \"b\"))") "/a/b"]
[(io "(.isDirectory (io/file \"docs\"))") "true"]
[(io "(.isFile (io/file \"project.janet\"))") "true"]
[(io "(.isFile (io/file \"docs\"))") "false"]
[(io "(.exists (io/file \"/no/such/path/xyz\"))") "false"]
[(io "(.exists (io/file \"project.janet\"))") "true"]
# --- instance? + type ---
[(io "(instance? java.io.File (io/file \"/a/b\"))") "true"]
[(io "(instance? java.io.File \"/a/b\")") "false"]
[(io "(str (type (io/file \"/a\")))") ":jolt/file"]
# --- file-seq ---
[(io "(every? (fn [f] (instance? java.io.File f)) (file-seq (io/file \"docs\")))") "true"]
[(io "(pos? (count (filter (fn [f] (.isFile f)) (file-seq (io/file \"docs\")))))") "true"]
["(do (require (quote [clojure.string :as s])) (boolean (some (fn [p] (s/ends-with? p \"project.janet\")) (file-seq \".\"))))" "true"]
# --- slurp / spit / flush ---
["(string? (slurp \"project.janet\"))" "true"]
["(do (spit \"/tmp/jolt-io-test.txt\" \"hello\") (slurp \"/tmp/jolt-io-test.txt\"))" "hello"]
["(do (spit \"/tmp/jolt-io-test.txt\" \"a\") (spit \"/tmp/jolt-io-test.txt\" \"b\" :append true) (slurp \"/tmp/jolt-io-test.txt\"))" "ab"]
["(flush)" ""]
[(io "(string? (slurp (io/file \"project.janet\")))") "true"]])
(defn run-capture [bin expr]
(def proc (os/spawn [bin "-e" expr] :p {:out :pipe :err :pipe}))
(def out (ev/read (proc :out) 0x100000))
(def err (ev/read (proc :err) 0x100000))
(def code (os/proc-wait proc))
(def lines (filter (fn [l] (not (empty? l)))
(string/split "\n" (string/trim (if out (string out) "")))))
[code (if (empty? lines) "" (last lines)) (string/trim (if err (string err) ""))])
(var pass 0)
(def fails @[])
(each [expr expected] cases
(def [ocode oracle _] (run-capture "build/jolt" expr))
(def [code got err] (run-capture jolt-bin expr))
(cond
(not= ocode 0) (array/push fails [expr (string "ORACLE FAILED exit " ocode)])
(not= oracle expected) (array/push fails [expr (string "ORACLE MISMATCH want `" expected "` got `" oracle "`")])
(not= code 0) (array/push fails [expr (string "exit " code "; err: " err)])
(= got expected) (++ pass)
(array/push fails [expr (string "want `" expected "`, got `" got "`")])))
(printf "\n_io parity [%s]: %d/%d passed" jolt-bin pass (length cases))
(when (> (length fails) 0)
(printf "%d FAIL(s):" (length fails))
(each [e m] fails (printf " FAIL %s\n %s" e m)))
(flush)
(os/exit (if (empty? fails) 0 1))