|
1 | 1 | (ns cljs.async-await-test |
| 2 | + (:refer-global :only [Date Promise]) |
2 | 3 | (:require [clojure.test :refer [deftest is async]] |
3 | | - [cljs.core :as cc :refer [await] :rename {await aw}]) |
| 4 | + [cljs.core :as cc :refer [await] :rename {await aw}] |
| 5 | + [goog.object :as gobj]) |
4 | 6 | (:require-macros [cljs.macro-test.macros :refer [await!] :as macros])) |
5 | 7 |
|
6 | 8 | (defn ^:async foo [n] |
|
192 | 194 | (let [x (await (js/Promise.resolve 1))] x))) |
193 | 195 | b3 (case :foo :foo (case :foo :foo (await (js/Promise.resolve 1)))) |
194 | 196 | b4 (int ;; wrapped in int to avoid false positive warning: |
195 | | - ;; all arguments must be numbers, got [number |
196 | | - ;; ignore] instead |
197 | | - (try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 ))) |
| 197 | + ;; all arguments must be numbers, got [number |
| 198 | + ;; ignore] instead |
| 199 | + (try (throw (throw (await (js/Promise.resolve 1)))) (catch :default _ 1 ))) |
198 | 200 | a (atom 0) |
199 | 201 | b5 (do (swap! a inc) (swap! a inc) |
200 | 202 | ;; do with single expr, wrapped in identity to avoid merging with upper do |
|
229 | 231 | (is (= 1 f)) |
230 | 232 | (done)) |
231 | 233 | (catch :default _ (is false))))) |
| 234 | + |
| 235 | +(deftest await-with-ctor |
| 236 | + (async done |
| 237 | + (let [f (^:async fn [] (Date. (await (Promise/resolve 0))))] |
| 238 | + (is (= (Date. 0) (await (f))))) |
| 239 | + (done))) |
| 240 | + |
| 241 | +(deftest await-with-literals |
| 242 | + (async done |
| 243 | + (let [objf (^:async fn [] #js {:foo (await (Promise/resolve "bar"))})] |
| 244 | + (is (gobj/equals #js {:foo "bar"} (await (objf))))) |
| 245 | + (let [arrayf (^:async fn [] #js [0 (await (Promise/resolve 1 )) 2])] |
| 246 | + (is (= [0 1 2] (seq (await (arrayf)))))) |
| 247 | + (let [listf (^:async fn [] (list 0 1 2))] |
| 248 | + (is (= '(0 1 2) (await (listf))))) |
| 249 | + (let [vectorf (^:async fn [] [0 (await (Promise/resolve 1 )) 2])] |
| 250 | + (is (= [0 1 2] (await (vectorf))))) |
| 251 | + (let [mapf (^:async fn [] {:foo (await (Promise/resolve :bar))})] |
| 252 | + (is (= {:foo :bar} (await (mapf))))) |
| 253 | + (let [setf (^:async fn [] #{:foo (await (Promise/resolve :bar)) :baz})] |
| 254 | + (is (= #{:foo :bar :baz} (await (setf))))) |
| 255 | + (done))) |
| 256 | + |
| 257 | +(deftest await-with-if-test |
| 258 | + (async done |
| 259 | + (let [f (^:async fn [] (if (await (Promise/resolve true)) :success :fail))] |
| 260 | + (is (= :success (await (f))))) |
| 261 | + (done))) |
| 262 | + |
| 263 | +(defn ^:async async-destructure |
| 264 | + [{:keys [foo bar] |
| 265 | + :or {bar (await (Promise/resolve "hello!"))}}] |
| 266 | + [foo bar]) |
| 267 | + |
| 268 | +(deftest await-in-async-destructure |
| 269 | + (async done |
| 270 | + (let [res (await (async-destructure {:foo 1}))] |
| 271 | + (is (= [1 "hello!"] res))) |
| 272 | + (done))) |
| 273 | + |
| 274 | +(comment |
| 275 | + (clojure.test/run-tests) |
| 276 | + ) |
0 commit comments