{"id":16284955,"url":"https://github.com/emlyn/clj-macro-test","last_synced_at":"2025-08-07T09:23:55.100Z","repository":{"id":140013605,"uuid":"223250104","full_name":"emlyn/clj-macro-test","owner":"emlyn","description":"A demonstration of an issue when trying to test macros","archived":false,"fork":false,"pushed_at":"2019-11-21T22:21:50.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-04T04:39:26.222Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emlyn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-21T19:34:16.000Z","updated_at":"2019-11-21T22:21:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"b0650626-71e1-4957-8bbd-efccc86b1e4a","html_url":"https://github.com/emlyn/clj-macro-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/emlyn/clj-macro-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emlyn%2Fclj-macro-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emlyn%2Fclj-macro-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emlyn%2Fclj-macro-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emlyn%2Fclj-macro-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emlyn","download_url":"https://codeload.github.com/emlyn/clj-macro-test/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emlyn%2Fclj-macro-test/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269231989,"owners_count":24382476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T19:21:41.744Z","updated_at":"2025-08-07T09:23:55.049Z","avatar_url":"https://github.com/emlyn.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# macro-test\n\nA demonstration of an issue when trying to test macros\n\n## Usage\n\nThe core namespace contains a macro.\nIt is not important what it is for this demonstration, any macro will do.\nIn this case I copied a simple `infix` macro from [here](https://www.braveclojure.com/writing-macros/#Anatomy_of_a_Macro).\n\nRun the tests with `lein test`. One assertion will fail:\n\n``` shell\n\u003e lein test\n\nlein test macro-test.core-test\n\nlein test :only macro-test.core-test/infix-test\n\nFAIL in (infix-test) (core_test.clj:7)\nThis fails: the macro is not expanded\nexpected: (= (quote (+ 1 1)) (macroexpand (quote (infix (1 + 1)))))\n  actual: (not (= (+ 1 1) (infix (1 + 1))))\n\nRan 1 tests containing 3 assertions.\n1 failures, 0 errors.\nTests failed.\n```\n\nNotice how `macroexpand` did not expand the macro, but just returned the form unchanged.\nLet's try running the test from a repl. First start up a repl with `lein repl`, then:\n\n``` clojure\nmacro-test.core=\u003e (require 'macro-test.core-test)\nnil\nmacro-test.core=\u003e (in-ns 'macro-test.core-test)\n#object[clojure.lang.Namespace 0x4ae0e040 \"macro-test.core-test\"]\nmacro-test.core-test=\u003e (infix-test)\nnil\n```\n\nNothing is printed, which means the test passed. How come?\n\nIt turns out that `clojure.test` calls the test function from a different namespace.\nLet's try doing that:\n\n``` clojure\nmacro-test.core-test=\u003e (in-ns 'user)\n#object[clojure.lang.Namespace 0x58d743 \"user\"]\nuser=\u003e (macro-test.core-test/infix-test)\n\nFAIL in (infix-test) (core_test.clj:7)\nThis fails: the macro is not expanded\nexpected: (= (quote (+ 1 1)) (macroexpand (quote (infix (1 + 1)))))\n  actual: (not (= (+ 1 1) (infix (1 + 1))))\nnil\n```\n\nNow we see the same failure!\nWe can replicate this `macroexpand` behaviour by calling it from the same or different namespaces:\n\n``` clojure\nmacro-test.user=\u003e (macroexpand '(infix (1 + 1)))\n(infix (1 + 1))\nmacro-test.user=\u003e (in-ns 'macro-test.core-test)\n#object[clojure.lang.Namespace 0x4ae0e040 \"macro-test.core-test\"]\nmacro-test.core-test=\u003e (macroexpand '(infix (1 + 1)))\n(+ 1 1)\n```\n\nNow it becomes clearer what's going wrong.\nTrying to macroexpand a macro can only work if there is a macro in scope to expand,\nbut in the `user` namespace, there is no `infix` macro.\n\nWe could work around this by fully qualifying the macro name (and any symbols passed in as arguments),\nlike `(macroexpand '(macro-test.core/infix (1 + 1)))`.\n\nOr we could use a syntax-quote instead: `(macroexpand ``(infix (1 + 1)))`,\nwhich will automatically qualify all symbols.\nAlthough that includes the `+`, so instead of the expected `(+ 1 1)` result,\nwe get `(clojure.core/+ 1 1)`.\n\nBut maybe it would be better if clojure would run the tests in their own namespace\nso that we don't have to use any workarounds?\n\n## License\n\nCopyright © 2019 FIXME\n\nThis program and the accompanying materials are made available under the\nterms of the Eclipse Public License 2.0 which is available at\nhttp://www.eclipse.org/legal/epl-2.0.\n\nThis Source Code may also be made available under the following Secondary\nLicenses when the conditions for such availability set forth in the Eclipse\nPublic License, v. 2.0 are satisfied: GNU General Public License as published by\nthe Free Software Foundation, either version 2 of the License, or (at your\noption) any later version, with the GNU Classpath Exception which is available\nat https://www.gnu.org/software/classpath/license.html.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femlyn%2Fclj-macro-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femlyn%2Fclj-macro-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femlyn%2Fclj-macro-test/lists"}