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 Jul 5, 2024
1 parent cd0165e commit 5207c87
Show file tree
Hide file tree
Showing 3 changed files with 35 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,11 +17,21 @@
#{"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)}
})

(def supported-methods
Expand Down
23 changes: 23 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

0 comments on commit 5207c87

Please sign in to comment.