Back

macroexpand-all (clj)

(source)

function

(macroexpand-all form)
Recursively performs all possible macroexpansions in form.

Examples

metabase/metabase
(ns ^:mb/once metabase.util.i18n-test
  (:require
   [clojure.test :refer :all]
   [clojure.walk :as walk]
   [metabase.test :as mt]
   [metabase.util.i18n :as i18n]))

(deftest ^:parallel validate-number-of-args-test
  (testing "`trs` and `tru` should validate that the are being called with the correct number of args\n"
    (testing "not enough args"
      (is (thrown?
           clojure.lang.Compiler$CompilerException
           (walk/macroexpand-all `(i18n/trs "{0} {1}" 0))))
      (is (thrown-with-msg?
           AssertionError
           #"expects 2 args, got 1"
           (#'i18n/validate-number-of-args "{0} {1}" [0]))))

    (testing "too many args"
      (is (thrown?
           clojure.lang.Compiler$CompilerException
           (walk/macroexpand-all `(i18n/trs "{0} {1}" 0 1 2))))
      (is (thrown-with-msg?
           AssertionError
           #"expects 2 args, got 3"
           (#'i18n/validate-number-of-args "{0} {1}" [0 1 2]))))

    (testing "Missing format specifiers (e.g. {1} but no {0})"
      (testing "num args match num specifiers"
        (is (thrown?
             clojure.lang.Compiler$CompilerException
             (walk/macroexpand-all `(i18n/trs "{1}" 0))))
        (is (thrown-with-msg?
             AssertionError
             #"missing some \{\} placeholders\. Expected \{0\}, \{1\}"
             (#'i18n/validate-number-of-args "{1}" [0]))))

      (testing "num args match num specifiers if none were missing"
        (is (thrown?
             clojure.lang.Compiler$CompilerException
             (walk/macroexpand-all `(i18n/trs "{1}" 0 1))))
        (is (thrown-with-msg?
             AssertionError
             #"missing some \{\} placeholders\. Expected \{0\}, \{1\}"
             (#'i18n/validate-number-of-args "{1}" [0 1]))))))

  (testing "The number of args is still validated if the first argument is a `str` form"
      (is (thrown?
           clojure.lang.Compiler$CompilerException
           (walk/macroexpand-all `(i18n/trs (~'str "{0}" "{1}") 0))))
      (is (thrown-with-msg?
           AssertionError
           #"expects 2 args, got 1"
           (#'i18n/validate-number-of-args '(str "{0}" "{1}") [0]))))

  (testing "`trsn` and `trun` should validate that they are being called with at most one arg\n"
    (is (thrown?
         clojure.lang.Compiler$CompilerException
         (walk/macroexpand-all `(i18n/trsn "{1}" "{1}" n))))
    (is (thrown-with-msg?
         AssertionError
         #"only supports a single \{0\} placeholder"
         (#'i18n/validate-n "{1}" "{1}")))

    (is (thrown?
         clojure.lang.Compiler$CompilerException
         (walk/macroexpand-all `(i18n/trsn "{0} {1}" "{0} {1}" n))))
    (is (thrown-with-msg?
         AssertionError
         #"only supports a single \{0\} placeholder"
         (#'i18n/validate-n "{0} {1}" "{0} {1}")))))
clojure/core.typed
(deftest keywordize-keys-test
  (is-tc-e #(keywordize-keys {"a" 1 "b" 2})
           :requires [[clojure.walk :refer [keywordize-keys]]]))

(deftest macroexpand-all-test
  (is-tc-e #(macroexpand-all '(-> c (+ 3) (* 2)))
           :requires [[clojure.walk :refer [macroexpand-all]]]))
typedclojure/typedclojure
(deftest keywordize-keys-test
  (is-tc-e #(keywordize-keys {"a" 1 "b" 2})
           :requires [[clojure.walk :refer [keywordize-keys]]]))

(deftest macroexpand-all-test
  (is-tc-e #(macroexpand-all '(-> c (+ 3) (* 2)))
           :requires [[clojure.walk :refer [macroexpand-all]]]))
pallet/ritz
(ns ritz.repl-utils.macroexpand
  (:require
   [clojure.walk :as walk]))

(defmacro macroexpand-all [form]
  `(walk/macroexpand ~form))
generateme/fastmath
^{:nextjournal.clerk/visibility :hide-ns
  :nextjournal.clerk/toc true}
(ns core
  {:clj-kondo/config '{:config-in-call {utils/table {:ignore [:unresolved-symbol]}}}}
  (:require [fastmath.core :as m]
            [nextjournal.clerk :as clerk]
            [clojure.walk :as walk]
            [utils :as u]))

(clerk/code (walk/macroexpand-all '(m/mevalpoly -2.0 2.5 1.0 0.0 7.0 3.0)))