add tests

This commit is contained in:
Michiel Borkent 2019-08-09 23:08:49 +02:00
parent 5c83e135d9
commit 9a0a4db7fd
5 changed files with 80 additions and 29 deletions

View file

@ -81,17 +81,31 @@ Functions are written using the reader tag `#f`. Currently up to three
arguments are supported. arguments are supported.
``` shellsession ``` shellsession
$ echo '3' | bb '(#f (+ %1 %2 %3) 1 2 *in*)' $ echo '3' | bb '(#f(+ %1 %2 %3) 1 2 *in*)'
6 6
``` ```
Regexes are written using the reader tag `#r`. Regexes are written using the reader tag `#r`.
``` shellsession ``` shellsession
$ ls | bb --raw '(filterv #f (re-find #r "reflection" %) *in*)' $ ls | bb --raw '(filterv #f(re-find #r "reflection" %) *in*)'
["reflection.json"] ["reflection.json"]
``` ```
Find the line numbers where the word Clojure occurs using a case insensitive regex:
``` shellsession
$ cat /tmp/test.txt
foo
Clojure is nice
bar
when you're nice to clojure
$ cat /tmp/test.txt | bb --raw '(map-indexed #f[%1 %2] *in*))' | \
bb '(keep #f(when (re-find #r"(?i)clojure" (second %)) (first %)) *in*)'
(1 3)
```
## Test ## Test
Test on the JVM: Test on the JVM:

View file

@ -1,19 +1,16 @@
(ns babashka.interpreter (ns babashka.interpreter
{:no-doc true} {:no-doc true}
(:refer-clojure :exclude [comparator]) (:refer-clojure :exclude [comparator])
(:require [clojure.walk :refer [postwalk]])) (:require [clojure.walk :refer [postwalk]]
[clojure.string :as str]
(defn safe-nth [x n] [clojure.set :as set]))
(try (nth x n)
(catch Exception _e
nil)))
(def syms '(= < <= >= + +' - * / (def syms '(= < <= >= + +' - * /
aget alength apply assoc assoc-in aget alength apply assoc assoc-in
bit-set bit-shift-left bit-shift-right bit-xor boolean boolean? booleans boolean-array butlast bit-set bit-shift-left bit-shift-right bit-xor boolean boolean? booleans boolean-array butlast
char char? conj cons contains? count char char? conj cons contains? count
dec dec' decimal? dedupe dissoc distinct disj drop dec dec' decimal? dedupe dissoc distinct disj drop
eduction every? eduction even? every?
get get
first float? floats fnil first float? floats fnil
identity inc int-array iterate identity inc int-array iterate
@ -21,18 +18,22 @@
filter filterv find filter filterv find
last line-seq last line-seq
keep keep-indexed keys keep keep-indexed keys
map map-indexed mapcat merge merge-with munge map mapv map-indexed mapcat merge merge-with munge
name newline not= num name newline not= num
neg? nth nthrest neg? nth nthrest
odd?
peek pos? peek pos?
re-seq re-find re-pattern rest reverse re-seq re-find re-pattern rest reverse
safe-nth set? sequential? some? str set? sequential? some? str
take take-last take-nth tree-seq type take take-last take-nth tree-seq type
unchecked-inc-int unchecked-long unchecked-negate unchecked-remainder-int unchecked-inc-int unchecked-long unchecked-negate unchecked-remainder-int
unchecked-subtract-int unsigned-bit-shift-right unchecked-float unchecked-subtract-int unsigned-bit-shift-right unchecked-float
vals vec vector? vals vec vector?
rand-int rand-nth range reduce reduced? remove rand-int rand-nth range reduce reduced? remove
set seq seq? shuffle simple-symbol? sort sort-by subs)) second set seq seq? shuffle simple-symbol? sort sort-by subs
set/difference set/join
str/join str/starts-with? str/ends-with?
zero?))
;; TODO: ;; TODO:
#_(def all-syms #_(def all-syms
@ -59,29 +60,41 @@
(if-let [f (first expr)] (if-let [f (first expr)]
(if-let [v (var-lookup f)] (if-let [v (var-lookup f)]
(apply-fn v in (rest expr)) (apply-fn v in (rest expr))
(if (ifn? f) (cond
(or (= 'if f) (= 'when f))
(let [[_if cond then else] expr]
(if (interpret cond in)
(interpret then in)
(interpret else in)))
;; bb/fn passed as higher order fn, still needs input
(-> f meta :bb/fn)
(apply-fn (f in) in (rest expr))
(ifn? f)
(apply-fn f in (rest expr)) (apply-fn f in (rest expr))
nil)) :else nil))
expr) expr)
:else ;; bb/fn passed as higher order fn, still needs input
expr)) (-> expr meta :bb/fn)
(expr in)
:else expr))
(defn read-fn [form] (defn read-fn [form]
^:bb/fn ^:bb/fn
(fn [& [x y z]] (fn [in]
(postwalk (fn [elt] (fn [& [x y z]]
(case elt (interpret (postwalk (fn [elt]
% x (case elt
%1 x % x
%2 y %1 x
%3 z %2 y
(interpret elt x))) form))) %3 z
elt)) form) in))))
(defn read-regex [form] (defn read-regex [form]
(re-pattern form)) (re-pattern form))
(defn apply-fn [f in args] (defn apply-fn [f in args]
(let [args (map #(interpret % in) args)] (let [args (mapv #(interpret % in) args)]
(apply f args))) (apply f args)))
;;;; Scratch ;;;; Scratch

View file

@ -42,7 +42,7 @@
expr (read-edn expr) expr (read-edn expr)
in (slurp *in*) in (slurp *in*)
in (if raw in (if raw
(str/split in #"\s") (str/split in #"\n")
(read-edn (format "[%s]" in))) (read-edn (format "[%s]" in)))
in (if (= 1 (count in)) (first in) in)] in (if (= 1 (count in)) (first in) in)]
(prn (i/interpret expr in)))))) (prn (i/interpret expr in))))))

View file

@ -1,6 +1,30 @@
(ns babashka.main-test (ns babashka.main-test
(:require (:require
[clojure.test :as test :refer [deftest is testing]] [clojure.test :as test :refer [deftest is testing]]
[babashka.test-utils :refer [bb]])) [babashka.test-utils :as test-utils]
[clojure.edn :as edn]))
(deftest main-test) (defn bb [input & args]
(edn/read-string (apply test-utils/bb (str input) (map str args))))
(deftest main-test
(testing "if and when"
(is (= 1 (bb 0 '(if (zero? *in*) 1 2))))
(is (= 2 (bb 1 '(if (zero? *in*) 1 2))))
(is (= 1 (bb 0 '(when (zero? *in*) 1))))
(is (nil? (bb 1 '(when (zero? *in*) 1)))))
(testing "fn"
(is (= 2 (bb 1 "(#f(+ 1 %) *in*)")))
(is (= [1 2 3] (bb nil "(map #f(+ 1 %) [0 1 2])")))
(is (bb 1 "(#f (when (odd? *in*) *in*) 1)")))
(testing "map"
(is (= [1 2 3] (bb nil '(map inc [0 1 2])))))
(testing "keep"
(is (= [false true false] (bb nil '(keep odd? [0 1 2])))))
(testing "..."
(is (= '(1 3)
(->
(bb "foo\n Clojure is nice. \nbar\n If you're nice to clojure. "
"--raw"
"(map-indexed #f[%1 %2] *in*)")
(bb "(keep #f(when (re-find #r\"(?i)clojure\" (second %)) (first %)) *in*)"))))))

View file

@ -17,7 +17,7 @@
{:in input}))))) {:in input})))))
(def bb (def bb
(case (System/getenv "BB_TEST_ENV") (case (System/getenv "BABASHKA_TEST_ENV")
"jvm" #'bb-jvm "jvm" #'bb-jvm
"native" #'bb-native "native" #'bb-native
#'bb-jvm)) #'bb-jvm))