From 430caf9bf268e0bbed73c32c697f523e721bd2c4 Mon Sep 17 00:00:00 2001 From: Ryan Mulligan Date: Fri, 20 Dec 2013 18:51:49 -0800 Subject: [PATCH] add doc-from-ns-form to bultitude.core --- src/bultitude/core.clj | 10 ++++++++++ test/bultitude/core_test.clj | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/bultitude/core.clj b/src/bultitude/core.clj index aca440d..c7e6704 100644 --- a/src/bultitude/core.clj +++ b/src/bultitude/core.clj @@ -123,3 +123,13 @@ (.replace \- \_) (.replace \. \/)) ".clj")) + +(defn doc-from-ns-form + "Extract the docstring from a given ns form without evaluating the form. The docstring returned should be the return value of (:doc (meta namespace-symbol)) if the ns-form were to be evaluated." + [ns-form] + (let [meta-docstring (:doc (meta (second ns-form))) + references (next (next ns-form)) + docstring (when (string? (first references)) (first references)) + references (if docstring (next references) references) + attribute-docstring (:doc (when (map? (first references)) (first references)))] + (or attribute-docstring docstring meta-docstring))) diff --git a/test/bultitude/core_test.clj b/test/bultitude/core_test.clj index 16eb157..6af0561 100644 --- a/test/bultitude/core_test.clj +++ b/test/bultitude/core_test.clj @@ -22,3 +22,36 @@ (is (= #{'bulti-tude.test} (set (namespaces-on-classpath :prefix "bulti-tude")))))) + +(defn test-doc-from-ns-form-helper + [docstring ns-form] + (eval ns-form) + (is (= docstring + (:doc (meta *ns*)) + (doc-from-ns-form ns-form)))) + +(deftest doc-from-ns-form-test + (testing "doc-from-ns-form" + (let [callee-ns-name (ns-name *ns*)] + (test-doc-from-ns-form-helper + nil + '(ns no-doc-namespace-name)) + (test-doc-from-ns-form-helper + "Docstring" + '(ns regular-doc-namespace-name "Docstring")) + (test-doc-from-ns-form-helper + "Attribute-Docstring" + '(ns attribute-doc-namepsace-name {:doc "Attribute-Docstring"})) + (test-doc-from-ns-form-helper + "Meta-Docstring" + '(ns ^{:doc "Meta-Docstring"} meta-doc-namespace-name)) + (test-doc-from-ns-form-helper + "Docstring" + '(ns ^{:doc "Meta-Docstring"} meta-and-reg-doc-namespace-name "Docstring")) + (test-doc-from-ns-form-helper + "Attribute-Docstring" + '(ns reg-and-attribute-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})) + (test-doc-from-ns-form-helper + "Attribute-Docstring" + '(ns ^{:doc "Meta-Docstring"} all-doc-namespace-name "Docstring" {:doc "Attribute-Docstring"})) + (in-ns callee-ns-name))))