识别Clojure宏中的命名空间符号

时间:2017-01-29 13:14:00

标签: clojure

我正在处理一个宏either,它执行一些代码转换,在文件epicea.antivalue.core中定义。除此之外,它应该识别符号make的使用,就好像该符号绑定在命名空间epicea.antivalue.core中一样。但理想情况下,符号make应该是未绑定的。我的代码可在此处找到:https://github.com/jonasseglare/epicea-antivalue/tree/stackoverlow-question

要识别符号make,我这里是epicea.antivalue.core的一些代码:

(def make ::make)

(defn evals-to-keyword [kwd]
  (fn [x]
    (try
      (and (symbol? x)
           (= kwd (eval x)))
      (catch Throwable e
        false))))

;; Function to test if we are referring to a symbol 'make' in the namespace of this file.
(def make-sym? (evals-to-keyword ::make))

但不幸的是,我需要这条线     (def make :: make) 为了它的工作。

我的问题反映在单元测试代码中     epicea.antivalue.core-test.clj 哪个目前失败

(ns epicea.antivalue.core-test
  (:import [epicea.antivalue AntivalueException])
  (:require [clojure.test :refer :all]
            [epicea.tag.core :as tag]
            [epicea.antivalue.core :as av]))

(deftest contextual-make
  ;; The call to 'av/make' should only work inside the 'av/either' macro
  ;; These tests PASS
  (is (= 4 (av/either (av/make false 9 3) 4)))
  (is (= 9 (av/either (av/make true 9 3) 4)))

  ;; This test PASSES, as we would expect.
  (is (try
        (eval '(println epicea.antivalue.core/some-unbound-symbol))
        false
        (catch Throwable e
          true)))

  ;; But outside the 'av/either' macro, 'av/make' should be unbound.
  ;; This test FAILS, but I don't want it to fail. How can we achieve that?
  (is (try
        (eval '(println epicea.antivalue.core/make))
        false
        (catch Throwable e
          true))))

所以我的问题是:如何在make-sym?名称空间中定义符号make的情况下实现函数epicea.antivalue.core?换句话说,如何通过仅编辑源文件epicea.antivalue.core

来使单元测试通过

0 个答案:

没有答案