From 36673563d6f0937417ffc99ab16b535d0d67dd4c Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Wed, 4 Dec 2024 09:21:37 -0500 Subject: [PATCH 1/2] Support the optional `attr-map?` on the `ns` macro --- CHANGELOG.md | 3 +++ src/basilisp/core.lpy | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2658e8e3..96e44469 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + * Added support for the optional `attr-map?` on the `ns` macro (#1159) + ### Fixed * Fix a bug where `#` characters were not legal in keywords and symbols (#1149) * Fix a bug where seqs were not considered valid input for matching clauses of the `case` macro (#1148) diff --git a/src/basilisp/core.lpy b/src/basilisp/core.lpy index 85d8a257..fff5fb61 100644 --- a/src/basilisp/core.lpy +++ b/src/basilisp/core.lpy @@ -5241,10 +5241,14 @@ You may include an optional docstring for the namespace to describe its purpose. The docstring will be applied as the ``:doc`` key on the namespace metadata map. + An optional map of metadata can be given after the docstring. If provided, this + metadata will be merged into the metadata for the resulting namespace. + Example:: (ns my.namespace \"My namespace with code\" + {:meta-key 1} (:refer-basilisp :exclude [get]) (:require [basilisp.string :as str]) @@ -5273,18 +5277,26 @@ (when-not (and (symbol? name) (nil? (namespace name))) (throw (ex-info "Namespace name must be a non-namespaced symbol" {:name name}))) - (let [doc (when (string? (first opts)) - (first opts)) - opts (if doc (rest opts) opts) - opts (reduce* (fn [m opt] - (let [opt-name (first opt) - options (rest opt)] - (when-not (keyword? opt-name) - (throw (ex-info "Namespace option must be a keyword" - {:option opt-name}))) - (assoc m opt-name (vec options)))) - {} - opts) + (let [doc (when (string? (first opts)) + (first opts)) + opts (if doc (rest opts) opts) + attr-map (when (map? (first opts)) + (first opts)) + metadata (merge (->> (meta name) + (remove #(= (namespace (key %)) "basilisp.lang.reader")) + (apply hash-map)) + {:doc doc} + attr-map) + opts (if attr-map (rest opts) opts) + opts (reduce* (fn [m opt] + (let [opt-name (first opt) + options (rest opt)] + (when-not (keyword? opt-name) + (throw (ex-info "Namespace option must be a keyword" + {:option opt-name}))) + (assoc m opt-name (vec options)))) + {} + opts) refer-filters (when-let [filters (or (:refer-basilisp opts) (:refer-clojure opts))] @@ -5300,8 +5312,8 @@ `(^:use-var-indirection do (in-ns (quote ~name)) (swap! *loaded-libs* conj (quote ~name)) - ~(when doc - `(alter-meta! (the-ns (quote ~name)) assoc :doc ~doc)) + ~(when metadata + `(reset-meta! (the-ns (quote ~name)) ~metadata)) (refer-basilisp ~@refer-filters) ~requires ~uses From c78081ef32f1631e5fff8befb219c76837701b94 Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Wed, 4 Dec 2024 10:03:11 -0500 Subject: [PATCH 2/2] Ok --- tests/basilisp/testrunner_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/basilisp/testrunner_test.py b/tests/basilisp/testrunner_test.py index 1939cfb2..ae6577a9 100644 --- a/tests/basilisp/testrunner_test.py +++ b/tests/basilisp/testrunner_test.py @@ -10,6 +10,7 @@ class TestTestrunner: @pytest.fixture def run_result(self, pytester: pytest.Pytester) -> pytest.RunResult: + runtime.Namespace.remove(sym.symbol("test-testrunner")) code = """ (ns test-testrunner (:require @@ -222,7 +223,7 @@ def test_fixtures(pytester: pytest.Pytester): [ ("error-during-setup", ":once", 2, 0, 0), ("error-during-setup", ":each", 2, 0, 0), - ("error-during-teardown", ":once", 3, 0, 0), + ("error-during-teardown", ":once", 1, 1, 1), ("error-during-teardown", ":each", 2, 1, 1), ], ) @@ -234,6 +235,7 @@ def test_fixtures_with_errors( passes: int, failures: int, ): + runtime.Namespace.remove(sym.symbol("test-fixtures-with-errors")) code = f""" (ns test-fixtures-with-errors (:require