|
1 | 1 | (ns sci.cljd-interop-test |
| 2 | + ;; override fns in the ctx cannot serialize to the native binary, so these |
| 3 | + ;; tests use sci/eval-string (in-process), not tu/eval*, matching the JVM. |
2 | 4 | (:require [clojure.test :refer [deftest is testing]] |
| 5 | + [sci.core :as sci] |
3 | 6 | [sci.test-utils :as tu])) |
4 | 7 |
|
5 | 8 | (deftest instance-method-override-test |
6 | 9 | (testing "instance method dispatches to override fn" |
7 | | - (is (= 3 (tu/eval* "(.foo \"abc\")" |
8 | | - {:classes {'String {:class String :instance-methods {'foo count}}}})))) |
| 10 | + (is (= 3 (sci/eval-string "(.foo \"abc\")" |
| 11 | + {:classes {'String {:class String :instance-methods {'foo count}}}})))) |
9 | 12 | (testing "override receives args" |
10 | | - (is (= "abc!" (tu/eval* "(.foo \"abc\" \"!\")" |
11 | | - {:classes {'String {:class String :instance-methods {'foo str}}}})))) |
| 13 | + (is (= "abc!" (sci/eval-string "(.foo \"abc\" \"!\")" |
| 14 | + {:classes {'String {:class String :instance-methods {'foo str}}}})))) |
12 | 15 | (testing "override body calls a real host method (compiled, tree-shakeable)" |
13 | | - (is (= "ABC" (tu/eval* "(.upper \"abc\")" |
14 | | - {:classes {'String {:class String :instance-methods {'upper (fn [s] (.toUpperCase s))}}}})))) |
| 16 | + (is (= "ABC" (sci/eval-string "(.upper \"abc\")" |
| 17 | + {:classes {'String {:class String :instance-methods {'upper (fn [s] (.toUpperCase s))}}}})))) |
15 | 18 | (testing "unlisted method throws" |
16 | 19 | (is (thrown? cljd.core/ExceptionInfo |
17 | | - (tu/eval* "(.bar \"abc\")" |
18 | | - {:classes {'String {:class String :instance-methods {'foo (fn [_s] :x)}}}}))))) |
| 20 | + (sci/eval-string "(.bar \"abc\")" |
| 21 | + {:classes {'String {:class String :instance-methods {'foo (fn [_s] :x)}}}}))))) |
19 | 22 |
|
20 | 23 | (deftest instance-field-override-test |
21 | 24 | (testing "instance field dispatches to override fn, which reads a real host field" |
22 | | - (is (= 4 (tu/eval* "(.-len \"abcd\")" |
23 | | - {:classes {'String {:class String :instance-fields {'len (fn [s] (.-length s))}}}})))) |
| 25 | + (is (= 4 (sci/eval-string "(.-len \"abcd\")" |
| 26 | + {:classes {'String {:class String :instance-fields {'len (fn [s] (.-length s))}}}})))) |
24 | 27 | (testing "unlisted field throws" |
25 | 28 | (is (thrown? cljd.core/ExceptionInfo |
26 | | - (tu/eval* "(.-nope \"abcd\")" |
27 | | - {:classes {'String {:class String :instance-fields {'len (fn [_] 1)}}}}))))) |
| 29 | + (sci/eval-string "(.-nope \"abcd\")" |
| 30 | + {:classes {'String {:class String :instance-fields {'len (fn [_] 1)}}}}))))) |
28 | 31 |
|
29 | 32 | (deftest static-method-override-test |
30 | 33 | (testing "static method dispatches to override fn" |
31 | | - (is (= :parsed (tu/eval* "(DateTime/parse \"2020\")" |
32 | | - {:classes {'DateTime {:class DateTime |
33 | | - :static-methods {'parse (fn [_ _s] :parsed)}}}}))))) |
| 34 | + (is (= :parsed (sci/eval-string "(DateTime/parse \"2020\")" |
| 35 | + {:classes {'DateTime {:class DateTime |
| 36 | + :static-methods {'parse (fn [_ _s] :parsed)}}}}))))) |
34 | 37 |
|
35 | 38 | (deftest static-field-override-test |
36 | 39 | (testing "static field dispatches to override fn" |
37 | | - (is (= :field (tu/eval* "DateTime/monthsPerYear" |
38 | | - {:classes {'DateTime {:class DateTime |
39 | | - :static-fields {'monthsPerYear (fn [_] :field)}}}}))))) |
| 40 | + (is (= :field (sci/eval-string "DateTime/monthsPerYear" |
| 41 | + {:classes {'DateTime {:class DateTime |
| 42 | + :static-fields {'monthsPerYear (fn [_] :field)}}}}))))) |
40 | 43 |
|
41 | 44 | (deftest named-arg-sugar-test |
42 | 45 | (testing "trailing .name val pairs desugar to keyword args" |
|
77 | 80 |
|
78 | 81 | (deftest string-namespace-require-test |
79 | 82 | (testing "a namespace registered under a string name is requirable and aliasable" |
80 | | - (is (= "hi!" (tu/eval* "(require '[\"my.pkg\" :as p]) (p/shout \"hi\")" |
81 | | - {:namespaces {"my.pkg" {'shout (fn [s] (str s "!"))}}})))) |
| 83 | + (is (= "hi!" (sci/eval-string "(require '[\"my.pkg\" :as p]) (p/shout \"hi\")" |
| 84 | + {:namespaces {"my.pkg" {'shout (fn [s] (str s "!"))}}})))) |
82 | 85 | (testing "string require works in an ns form" |
83 | | - (is (= 42 (tu/eval* "(ns app (:require [\"my.pkg\" :as p])) (p/answer)" |
84 | | - {:namespaces {"my.pkg" {'answer (fn [] 42)}}})))) |
| 86 | + (is (= 42 (sci/eval-string "(ns app (:require [\"my.pkg\" :as p])) (p/answer)" |
| 87 | + {:namespaces {"my.pkg" {'answer (fn [] 42)}}})))) |
85 | 88 | (testing "a string-named and a symbol-named ns coexist in loaded-libs" |
86 | 89 | (is (= [1 2] |
87 | | - (tu/eval* "(require '[\"pkg.one\" :as a] '[reg.two :as b]) [(a/x) (b/y)]" |
88 | | - {:namespaces {"pkg.one" {'x (fn [] 1)} |
89 | | - 'reg.two {'y (fn [] 2)}}})))) |
| 90 | + (sci/eval-string "(require '[\"pkg.one\" :as a] '[reg.two :as b]) [(a/x) (b/y)]" |
| 91 | + {:namespaces {"pkg.one" {'x (fn [] 1)} |
| 92 | + 'reg.two {'y (fn [] 2)}}})))) |
90 | 93 | (testing "requiring the same string ns twice is idempotent" |
91 | | - (is (= 1 (tu/eval* "(require '[\"pkg.one\" :as a]) (require '[\"pkg.one\" :as a]) (a/x)" |
92 | | - {:namespaces {"pkg.one" {'x (fn [] 1)}}}))))) |
| 94 | + (is (= 1 (sci/eval-string "(require '[\"pkg.one\" :as a]) (require '[\"pkg.one\" :as a]) (a/x)" |
| 95 | + {:namespaces {"pkg.one" {'x (fn [] 1)}}}))))) |
0 commit comments