Skip to content

Commit

Permalink
Add initial string and JSON conversion functions
Browse files Browse the repository at this point in the history
Upgrade cljs-bean when using shadow-cljs to get :keywordize-keys
functionality.
  • Loading branch information
jonsmock committed Oct 8, 2024
1 parent f075857 commit 11e551d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"node_modules/@lonocloud/resolve-deps/src"]

:dependencies [[funcool/promesa "8.0.450"]
[cljs-bean "1.8.0"]]
[cljs-bean "1.9.0"]]

:builds
{:dctest
Expand Down
12 changes: 11 additions & 1 deletion src/dctest/expressions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;; Licensed under EPL 2.0

(ns dctest.expressions
(:require [cljs-bean.core :refer [->clj]]
(:require [cljs-bean.core :refer [->clj ->js]]
[clojure.edn :as edn]
[clojure.pprint :refer [pprint]]
[clojure.string :as S]
Expand All @@ -17,12 +17,22 @@
#{"env" "process" "step"})

(def stdlib
;; {name {:arity number :fn (fn [context & args] ..)}}
{
;; Test status functions
"always" {:arity 0 :fn (constantly true)}
"success" {:arity 0 :fn #(not (get-in % [:state :failed]))}
"failure" {:arity 0 :fn #(boolean (get-in % [:state :failed]))}

;; Conversion functions
"fromJSON" {:arity 1 :fn #(->clj (js/JSON.parse %2) :keywordize-keys false)}
"toJSON" {:arity 1 :fn #(js/JSON.stringify (->js %2))}

;; String functions
"contains" {:arity 2 :fn #(S/includes? %2 %3)}
"startsWith" {:arity 2 :fn #(S/starts-with? %2 %3)}
"endsWith" {:arity 2 :fn #(S/ends-with? %2 %3)}

;; Error functions
"throw" {:arity 1 :fn #(throw (ex-info %2 {}))}
})
Expand Down
32 changes: 32 additions & 0 deletions test/dctest/expressions_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,29 @@
false {:failed false}
true {:failed true})

;; Conversion functions
(are [expected expr] (= expected (expr/read-eval {} expr))
"{}" "toJSON({})"
{} "fromJSON(\"{}\")"

;; roundtrip
1 "fromJSON(toJSON(1))"
{"a" 5} "fromJSON(toJSON({\"a\": 5}))"
"{\"a\":5}" "toJSON(fromJSON(toJSON({\"a\": 5})))"
)

;; String functions
(are [expected expr] (= expected (expr/read-eval {} expr))
true "contains('Hello World', 'World')"
false "contains('Hello Worl', 'World')"

true "startsWith('Hello World', 'Hello')"
false "startsWith('Hello World', 'World')"

false "endsWith('Hello World', 'Hello')"
true "endsWith('Hello World', 'World')"
)

;; Check function names/args
(are [text] (thrown-with-msg? js/Error #"Unchecked errors"
(expr/read-eval {:state {:failed false}} text))
Expand Down Expand Up @@ -207,6 +230,15 @@
;; count
3 "[1, 2, 3].count()")

;; Nested properties and methods with function calls
(are [expected text] (= expected (expr/read-eval {:env {"foo" 3}} text))
"3" "env.foo.toString()"
3 "fromJSON(toJSON(env)).foo"
"3" "env.foo.toString()"
"3" "env['foo'].toString()"
3 "fromJSON(env.toString()).foo"
"3" "fromJSON(env.toString()).foo.toString()")

;; Coerce keywords to strings inside context
(is (= 123
(expr/read-eval {:env {:a {:b 123}}} "env.a.b")))
Expand Down

0 comments on commit 11e551d

Please sign in to comment.