From abad9d7db9e0bce9d8ffc8f5e72963472a705474 Mon Sep 17 00:00:00 2001 From: sogaiu <983021772@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:43:00 +0900 Subject: [PATCH 1/2] Add assertf and use in boot.janet. Address #1516 --- src/boot/boot.janet | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 3337f6fa..6b205625 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -154,6 +154,11 @@ ,v (,error ,(if err err (string/format "assert failure in %j" x)))))) +(defmacro assertf + "Convenience macro that combines `assert` and `string/format`." + [x & args] + ~(as-macro ,assert ,x (,string/format ,;args))) + (defmacro defdyn ``Define an alias for a keyword that is used as a dynamic binding. The alias is a normal, lexically scoped binding that can be used instead of @@ -3934,7 +3939,7 @@ (defn make-sig [] (ffi/signature :default real-ret-type ;computed-type-args)) (defn make-ptr [] - (assert (ffi/lookup (if lazy (llib) lib) raw-symbol) (string "failed to find ffi symbol " raw-symbol))) + (assertf (ffi/lookup (if lazy (llib) lib) raw-symbol) "failed to find ffi symbol %v" raw-symbol)) (if lazy ~(defn ,alias ,;meta [,;formal-args] (,ffi/call (,(delay (make-ptr))) (,(delay (make-sig))) ,;formal-args)) @@ -4111,7 +4116,7 @@ "Get the manifest for a give installed bundle" [bundle-name] (def name (get-manifest-filename bundle-name)) - (assert (fexists name) (string "no bundle " bundle-name " found")) + (assertf (fexists name) "no bundle %v found" bundle-name) (parse (slurp name))) (defn- get-bundle-module @@ -4254,11 +4259,9 @@ (def missing (seq [d :in deps :when (not (bundle/installed? d))] (string d))) (when (next missing) (errorf "missing dependencies %s" (string/join missing ", ")))) (def bundle-name (get config :name default-bundle-name)) - (assert bundle-name (errorf "unable to infer bundle name for %v, use :name argument" path)) - (assert (not (string/check-set "\\/" bundle-name)) - (string "bundle name " - bundle-name - " cannot contain path separators")) + (assertf bundle-name "unable to infer bundle name for %v, use :name argument" path) + (assertf (not (string/check-set "\\/" bundle-name)) + "bundle name %v cannot contain path separators" bundle-name) (assert (next bundle-name) "cannot use empty bundle-name") (assert (not (fexists (get-manifest-filename bundle-name))) "bundle is already installed") @@ -4310,7 +4313,7 @@ (var i 0) (def man (bundle/manifest bundle-name)) (def files (get man :files @[])) - (assert (os/mkdir dest-dir) (string "could not create directory " dest-dir " (or it already exists)")) + (assertf (os/mkdir dest-dir) "could not create directory %v (or it already exists)" dest-dir) (def s (sep)) (os/mkdir (string dest-dir s "bundle")) (def install-hook (string dest-dir s "bundle" s "init.janet")) From 10994cbc6a19b3dfb3e4ca851965411342f75402 Mon Sep 17 00:00:00 2001 From: sogaiu <983021772@users.noreply.github.com> Date: Wed, 30 Oct 2024 23:41:31 +0900 Subject: [PATCH 2/2] Add some tests for assertf --- test/suite-boot.janet | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/suite-boot.janet b/test/suite-boot.janet index 5b2361d4..ab829107 100644 --- a/test/suite-boot.janet +++ b/test/suite-boot.janet @@ -986,4 +986,14 @@ (assert (deep= (get (dyn 'a) :source-form) source)) (setdyn *debug* nil) +# issue #1516 +(assert (assertf true) "assertf 1 argument") +(assert (assertf true "fun message") "assertf 2 arguments") +(assert (assertf true "%s message" "mystery") "assertf 3 arguments") +(assert (assertf (not nil) "%s message" "ordinary") "assertf not nil") +(assert-error "assertf error 1" (assertf false)) +(assert-error "assertf error 2" (assertf false "fun message")) +(assert-error "assertf error 3" (assertf false "%s message" "mystery")) +(assert-error "assertf error 4" (assertf nil "%s %s" "alice" "bob")) + (end-suite)