babashka/test/babashka/impl/repl_test.clj

52 lines
1.5 KiB
Clojure
Raw Normal View History

2019-12-08 11:46:06 +00:00
(ns babashka.impl.repl-test
(:require
[babashka.impl.repl :refer [start-repl!]]
[clojure.string :as str]
[clojure.test :as t :refer [deftest is]]
[sci.core :as sci]
2020-03-28 10:20:39 +00:00
[sci.impl.opts :refer [init]]
[sci.impl.vars :as vars]))
2019-12-08 11:46:06 +00:00
(set! *warn-on-reflection* true)
;; (vars/bindRoot sci/in *in*)
;; (vars/bindRoot sci/out *out*)
(vars/bindRoot sci/err *err*)
2019-12-08 11:46:06 +00:00
(defn repl! []
2020-09-16 13:54:39 +00:00
(start-repl! (init {:bindings {'*command-line-args*
2020-11-18 20:09:26 +00:00
["a" "b" "c"]}})))
2019-12-08 11:46:06 +00:00
(defn assert-repl [expr expected]
(is (str/includes? (sci/with-out-str
(sci/with-in-str (str expr "\n:repl/quit")
2019-12-08 11:46:06 +00:00
(repl!))) expected)))
2020-03-28 10:20:39 +00:00
(defn assert-repl-error [expr expected]
(is (str/includes?
(let [sw (java.io.StringWriter.)]
(sci/binding [sci/out (java.io.StringWriter.)
sci/err sw]
(sci/with-in-str (str expr "\n:repl/quit")
(repl!)))
(str sw)) expected)))
2019-12-08 11:46:06 +00:00
(deftest repl-test
(assert-repl "1" "1")
(assert-repl "[1 2 3]" "[1 2 3]")
(assert-repl "()" "()")
2019-12-08 11:46:06 +00:00
(assert-repl "(+ 1 2 3)" "6")
(assert-repl "(defn foo [] (+ 1 2 3)) (foo)" "6")
(assert-repl "(defn foo [] (+ 1 2 3)) (foo)" "6")
(assert-repl "1\n(inc *1)" "2")
(assert-repl "1\n(dec *1)(+ *2 *2)" "2")
(assert-repl "1\n(dec *1)(+ *2 *2)" "2")
2020-03-28 10:20:39 +00:00
(assert-repl "*command-line-args*" "[\"a\" \"b\" \"c\"]")
2020-04-21 21:38:16 +00:00
(assert-repl-error "(+ 1 nil)" "NullPointerException")
(assert-repl-error "(/ 1 0) (pst 1)" "Divide by zero\n\tclojure.lang.Numbers"))
2019-12-08 11:46:06 +00:00
;;;; Scratch
(comment
)