Skip to content

Commit

Permalink
Merge pull request #6 from klauswuestefeld/add-ctx
Browse files Browse the repository at this point in the history
Add ctx based on fn arity
  • Loading branch information
kauwai authored Sep 13, 2024
2 parents 37cf8c6 + 7256f3c commit 2a0348d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
4 changes: 1 addition & 3 deletions test/spread/project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject house.jux/test.spread "2024.09.12"
(defproject house.jux/test.spread "2024.09.13"

:description "Support for highly expressive, two-dimensional tests represented as spreadsheets."
:url "https://github.com/klauswuestefeld/simple-clj/tree/master/test/spread"
Expand All @@ -10,8 +10,6 @@
[ring/ring-core "1.6.2"]
[ring/ring-jetty-adapter "1.6.2"]
[house.jux/biz.command-result "2024.09.05"]
[house.jux/biz.timestamp "2024.09.05"]
[house.jux/biz.user "2024.06.10"]
[house.jux/exceptions "2024.06.10"]
[house.jux/http.api "2024.06.11"]
[house.jux/http.exceptions "2024.06.24"]
Expand Down
55 changes: 33 additions & 22 deletions test/spread/src/house/jux__/test/spread__.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
clojure.stacktrace
[clojure.string :as string]
clojure.walk
[house.jux--.biz.user-- :refer [*user*]]
[house.jux--.biz.command-result-- :refer [*result* get-result set-result reset-result]]
[house.jux--.biz.timestamp-- :refer [*timestamp*]]
[simple.check2 :refer [check]]))

(def previous-query-results (atom nil)) ;; TODO: remove this atom
Expand Down Expand Up @@ -90,7 +88,7 @@
(defn- ->initial-step [step]
(-> step
(assoc-in [:command :user] "nil")
(assoc-in [:command :function] "(fn [state & _ignored] state)")
(assoc-in [:command :function] "identity")
(assoc-in [:command :result] "*")))

(defn- steps [structure]
Expand Down Expand Up @@ -229,29 +227,45 @@
(catch Throwable t
(throw (RuntimeException. (str "Error reading user: " (exception->message t)))))))

(defn- arity [function]
(-> function meta :arglists first count))

(defn- run-fn [fn-var args ctx]
(let [arity (arity fn-var)
args (if (= 3 arity)
(cond-> args
(= 1 (count args)) (conj ::ignored ctx)
(= 2 (count args)) (conj ctx))
args)]
(apply fn-var args)))

(defn- run-query [ctx segment]
(let [form (-> (str "(" segment ")")
compile-string
with-underline) ; The undeline symbol in the form refers to the var created with 'intern' above.
f (first form)
fn-var (if (keyword? f) f (resolve f))
args (eval (vec (rest form)))]
(run-fn fn-var args ctx)))

(def initial-query-line 3)
(defn execute-query-segments
([state segments]
(execute-query-segments state segments initial-query-line))
([state ctx segments]
(execute-query-segments state ctx segments initial-query-line))

([value [segment & next-segments] query-line]
([value ctx [segment & next-segments] query-line]
(if (string/blank? segment)
value
(try
(intern *ns* '_ value) ; Intern is a "def" that works at runtime (def creates the var at compile time). This indirection with this var is required because complex non-clojure objects such as #datascript/DB {...}, when added directy in the form, will give the error: "Can't embed object in code". Also it looks better in the print of the form than a huge state map.
(let [result (-> (str "(" segment ")")
compile-string
with-underline ; The undeline symbol in the form refers to the var created with 'intern' above.
eval)]
(execute-query-segments result next-segments (inc query-line)))
(let [result (run-query ctx segment)]
(execute-query-segments result ctx next-segments (inc query-line)))
(catch Exception e
{::wrapped-exception e
:query-line query-line})))))

(defn- safe-query-result [state user segments]
(binding [*user* (eval-user user)
*timestamp* 1000000]
(execute-query-segments state segments)))
(execute-query-segments state {:user (eval-user user) :timestamp 1000000} segments))

(defn- execute-query [state query-results-line query-column-number [user & segments] expected-result]
(let [column (query-column-idx->column query-column-number)
Expand All @@ -271,21 +285,18 @@

(defn- resolve-command-fn [function-str line]
(try
(eval-string function-str)
(->> function-str read-string resolve)
(catch Exception e
(throw-info! (str "Unable to resolve command '" function-str "' " (exception->message e))
{:line line, :column "B"}))))

(defn- execute-command [state {:keys [function user params result result-coords]}]
(binding [*result* (reset-result)]
(let [command (resolve-command-fn function (:line result-coords))
command (if (string/blank? params)
#(command state)
#(command state (eval-string params)))
(let [command (resolve-command-fn function (:line result-coords))
ctx {:user (eval-user user) :timestamp 1000000}
args (if (string/blank? params) [state] [state (eval-string params)])
new-state (try
(binding [*user* (eval-user user)
*timestamp* 1000000]
(command))
(run-fn command args ctx)
(catch Throwable e
(set-result {::wrapped-exception e})
state))]
Expand Down
11 changes: 5 additions & 6 deletions test/spread/test/fixture/biz.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(ns fixture.biz
(:require [house.jux--.biz.user-- :refer [user]]
[house.jux--.biz.command-result-- :refer [set-result]]))
(:require [house.jux--.biz.command-result-- :refer [set-result]]))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn create-new-profile [state profile]
(let [state (->> (assoc profile :created-by (user))
(defn create-new-profile [state profile {:keys [user]}]
(let [state (->> (assoc profile :created-by user)
(assoc state :profile))]
(set-result "Profile Created")
state))
Expand All @@ -14,12 +13,12 @@
(:profile state))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn update-profile [state profile]
(defn update-profile [state profile {:keys [user]}]
(when (-> profile :name (= "BOOM"))
(throw (RuntimeException. "Invalid name")))
(let [state (-> state
(update :profile merge profile)
(assoc-in [:profile :last-updated-by] (user)))]
(assoc-in [:profile :last-updated-by] user))]
(set-result "Profile Updated")
state))

Expand Down
2 changes: 1 addition & 1 deletion test/spread/test/unit/spread_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:steps
[{:command
{:user "nil",
:function "(fn [state & _ignored] state)",
:function "identity",
:params "",
:result "*",
:result-coords {:line 7, :column "D"}},
Expand Down

0 comments on commit 2a0348d

Please sign in to comment.