-
Notifications
You must be signed in to change notification settings - Fork 37
Add tests for eval
#868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for eval
#868
Changes from 1 commit
a23df41
6cb2d90
83b7694
91741c8
559eece
fc4b57f
9dde1b9
2d2ded0
d27419a
ff3ae15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 '()))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about an infinite seq?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are you trying to test with that? That it doesn't get realized?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, eval'ing an infinite range returns a What are you wanting to test with that?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interestingly, eval'ing a fixed range returns a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep.
That's the type of
So, eval clearly doesn't realize the seq, from the behavior. That's a great test case to have.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, how do I test for not being realized, exactly? Is it just that I get something back at all, because if it tried to realize the infinite sequence it would wait until the full sequence was realized and finally throw and OOM exception?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. If we get it back and continue running the test, we can know that it wasn't realized.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I'll add this. |
||
| ;; 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))))))))))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that both of these are macros and not special forms. That's not to say this is wrong, it's just not exactly testing what's commented.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm. I relied on https://clojure.org/reference/special_forms for the list of special forms. And the doc strings for both and |
||
Uh oh!
There was an error while loading. Please reload this page.