PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/cinc/test/analyzer/jvm.clj

http://github.com/remleduff/CinC
Clojure | 47 lines | 40 code | 7 blank | 0 comment | 21 complexity | 7bc1eb2b941f3eaa0ba21058c3aa4d72 MD5 | raw file
  1. (ns cinc.test.analyzer.jvm
  2. (:refer-clojure :exclude [macroexpand-1])
  3. (:require [clojure.test :refer :all]
  4. [cinc.analyzer :refer [analyze]]
  5. [cinc.analyzer.jvm :refer [macroexpand-1]]))
  6. (defmacro ast [form]
  7. `(analyze '~form {:context :expr}))
  8. (defmacro mexpand [form]
  9. `(macroexpand-1 '~form {:context :expr}))
  10. (deftest macroexpander-test
  11. (is (= (list '. (list 'clojure.core/identity java.lang.Object) 'toString)
  12. (mexpand (.toString Object))))
  13. (is (= (list '. java.lang.Integer '(parseInt "2")) (mexpand (Integer/parseInt "2")))))
  14. (deftest analyzer-test
  15. (is (= :monitor-enter (-> (ast (monitor-enter 1)) :op)))
  16. (is (= :monitor-exit (-> (ast (monitor-exit 1)) :op)))
  17. (is (= :import (-> (ast (clojure.core/import* Integer)) :op)))
  18. (let [r-ast (ast (reify
  19. Object (toString [this] "")
  20. Appendable (append [this ^char x] this)))]
  21. (is (= :reify (-> r-ast :op)))
  22. (is (= #{Appendable clojure.lang.IObj} (-> r-ast :interfaces)))
  23. (is (= '#{toString append} (->> r-ast :methods (mapv :name) set))))
  24. (let [dt-ast (ast (deftype* x user.x [a b]
  25. :implements [Appendable]
  26. (append [this ^char x] this)))]
  27. (is (= :deftype (-> dt-ast :op)))
  28. (is (= '[a b] (->> dt-ast :fields (mapv :name))))
  29. (is (= '[append] (->> dt-ast :methods (mapv :name))))
  30. (is (= 'user.x (-> dt-ast :class-name))))
  31. (let [c-ast (ast (case* 1 0 0 :number {2 [2 :two] 3 [3 :three]} :compact :int))]
  32. (is (= :number (-> c-ast :default :form)))
  33. (is (= #{2 3} (->> c-ast :tests (mapv (comp :form :expr :test)) set)))
  34. (is (= #{:three :two} (->> c-ast :thens (mapv (comp :form :then)) set)))
  35. (is (= 3 (-> c-ast :high)))
  36. (is (= :int (-> c-ast :test-type)))
  37. (is (= :compact (-> c-ast :switch-type)))
  38. (is (= 2 (-> c-ast :low)))
  39. (is (= 0 (-> c-ast :shift)))
  40. (is (= 0 (-> c-ast :mask)))))