forked from jank-lang/clojure-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cljc
More file actions
52 lines (50 loc) · 2.26 KB
/
eval.cljc
File metadata and controls
52 lines (50 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(ns clojure.core-test.eval
(:require [clojure.test :as t :refer [are deftest is testing]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p]
#?(:cljs [cljs.js]))) ; need this for eval support in CLJS
(when-var-exists eval
(def x 42)
(deftest test-eval
#?(:cljs nil
:default (testing "Strings, numbers, characters, true, false, nil and keywords evaluate to themselves."
(are [expected form] (= expected (eval form))
;; lots of Clojure objects just evaluate to themselves
1 1
0 0
-1 -1
1.0 1.0
1N 1N
1.0M 1.0M
1/2 1/2
"a string" "a string"
"(+ 1 2)" "(+ 1 2)" ; strings are just evaluated as strings
\x \x
true true
false false
nil nil
:a-keyword :a-keyword)))
#?(:cljs nil
:default (testing "Symbol resolution"
;; namespace qualified
(is (= 42 (eval 'clojure.core-test.eval/x)))))
#?(:cljs nil
:default (testing "Vectors, Maps, Sets"
;; basic literal collections
(is (= [:a :b] (eval [:a :b])))
(is (= {:a :b} (eval {:a :b})))
(is (= #{:a :b} (eval #{:a :b})))
;; collections with embedded symbols
(is (= [:a :b 42] (eval [:a :b 'clojure.core-test.eval/x])))))
#?(:cljs nil
:default (testing "Lists, function application, and macros"
;; empty list evaluates to itself
(is (= '() (eval '())))
;; function calls
(is (= 5 (eval '(+ 2 3))))
(is (= 6 (eval '(* 2 3))))
;; macros
(is (= 42 (eval '(or false clojure.core-test.eval/x))))
(is (= 42 (eval '(and (+ 2 3) clojure.core-test.eval/x))))
;; special forms
(is (= 43 (eval '(let [y 43] (or false y)))))
(is (= 43 (eval '(loop [y 0] (if (= y 43) y (recur (inc y)))))))))))