Skip to content

Commit

Permalink
Implement CQL ToTime and rearrange ToDate and ToDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
marlenaMyr committed Jun 8, 2022
1 parent 52fd72b commit a5bced4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
5 changes: 3 additions & 2 deletions modules/cql/src/blaze/elm/quantity.clj
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@
String
(to-quantity [s]
;; (+|-)?#0(.0#)?('<unit>')?
(let [[_ value unit] (re-matches #"(\d+(?:\.\d+)?)\s*('[^']+')?" s)]
(let [[_ value unit] (re-matches #"([+-]?\d+(?:\.\d+)?)\s*('[^']+')?" s)]
(when value
(quantity (p/to-decimal value) (or (str/trim unit "'") "1"))))))
(when-let [value (p/to-decimal value)]
(quantity value (or (str/trim unit "'") "1")))))))


;; 22.30. ToString
Expand Down
40 changes: 34 additions & 6 deletions modules/cql/test/blaze/elm/compiler/type_operators_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[blaze.elm.literal-spec]
[blaze.elm.protocols :as p]
[blaze.elm.quantity :as quantity]
[blaze.elm.quantity-spec]
[blaze.fhir.spec.type.system :as system]
[clojure.spec.test.alpha :as st]
[clojure.test :as test :refer [are deftest is testing]]))
Expand Down Expand Up @@ -459,6 +460,8 @@
(testing "String"
(are [x] (true? (tu/compile-unop elm/converts-to-decimal elm/string x))
(str decimal/min)
"-1"
"0"
"1"
(str decimal/max))

Expand All @@ -477,7 +480,9 @@

(testing "dynamic"
(are [x] (false? (tu/dynamic-compile-eval (elm/converts-to-decimal x)))
#elm/parameter-ref "A"))
#elm/parameter-ref "A")
(are [x] (true? (tu/dynamic-compile-eval (elm/converts-to-decimal x)))
#elm/parameter-ref "1"))

(tu/testing-unary-null elm/converts-to-decimal)

Expand Down Expand Up @@ -515,13 +520,15 @@
(testing "String"
(are [x] (true? (tu/compile-unop elm/converts-to-long elm/string x))
(str Long/MIN_VALUE)
"-1"
"0"
"1"
(str Long/MAX_VALUE))

(are [x] (false? (tu/compile-unop elm/converts-to-long elm/string x))
(str (dec (bigint Long/MIN_VALUE)))
(str (inc (bigint Long/MAX_VALUE)))
"a" ))
"a"))

(testing "Boolean"
(are [x] (true? (tu/compile-unop elm/converts-to-long elm/boolean x))
Expand All @@ -533,7 +540,9 @@

(testing "dynamic"
(are [x] (false? (tu/dynamic-compile-eval (elm/converts-to-long x)))
#elm/parameter-ref "A"))
#elm/parameter-ref "A")
(are [x] (true? (tu/dynamic-compile-eval (elm/converts-to-long x)))
#elm/parameter-ref "1"))

(tu/testing-unary-null elm/converts-to-long)

Expand Down Expand Up @@ -571,6 +580,8 @@
(testing "String"
(are [x] (true? (tu/compile-unop elm/converts-to-integer elm/string x))
(str Integer/MIN_VALUE)
"-1"
"0"
"1"
(str Integer/MAX_VALUE))
(are [x] (false? (tu/compile-unop elm/converts-to-integer elm/string x))
Expand All @@ -588,7 +599,9 @@

(testing "dynamic"
(are [x] (false? (tu/dynamic-compile-eval (elm/converts-to-integer x)))
#elm/parameter-ref "A"))
#elm/parameter-ref "A")
(are [x] (true? (tu/dynamic-compile-eval (elm/converts-to-integer x)))
#elm/parameter-ref "1"))

(tu/testing-unary-null elm/converts-to-integer)

Expand Down Expand Up @@ -631,9 +644,17 @@
(deftest compile-converts-to-quantity-test
(testing "String"
(are [x] (true? (tu/compile-unop elm/converts-to-quantity elm/string x))
"1'm'")
(str decimal/min "'m'")
"-1'm'"
"0'm'"
"1'm'"
(str decimal/max "'m'"))

(are [x] (false? (tu/compile-unop elm/converts-to-quantity elm/string x))
(str (- decimal/min 1e-8M))
(str (+ decimal/max 1e-8M))
(str (- decimal/min 1e-8M) "'m'")
(str (+ decimal/max 1e-8M) "'m'")
""
"a"))

Expand All @@ -647,7 +668,9 @@

(testing "dynamic"
(are [x] (false? (tu/dynamic-compile-eval (elm/converts-to-quantity x)))
#elm/parameter-ref "A"))
#elm/parameter-ref "A")
(are [x] (true? (tu/dynamic-compile-eval (elm/converts-to-quantity x)))
#elm/parameter-ref "1"))

(tu/testing-unary-null elm/converts-to-quantity)

Expand Down Expand Up @@ -1269,6 +1292,7 @@
(are [x res] (p/equal res (core/-eval (tu/compile-unop elm/to-quantity
elm/string x)
{} nil nil))
"-1" (quantity/quantity -1 "1")
"1" (quantity/quantity 1 "1")

"1'm'" (quantity/quantity 1 "m")
Expand All @@ -1281,6 +1305,10 @@

(are [x] (nil? (core/-eval (tu/compile-unop elm/to-quantity elm/string x)
{} nil nil))
(str (- decimal/min 1e-8M))
(str (+ decimal/max 1e-8M))
(str (- decimal/min 1e-8M) "'m'")
(str (+ decimal/max 1e-8M) "'m'")
""
"a"))

Expand Down

0 comments on commit a5bced4

Please sign in to comment.