Skip to content

Commit e52575e

Browse files
authored
Merge pull request #1517 from sogaiu/add-assertf
Add assertf and use in boot.janet. Address #1516
2 parents 5e443cd + 10994cb commit e52575e

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/boot/boot.janet

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
,v
155155
(,error ,(if err err (string/format "assert failure in %j" x))))))
156156

157+
(defmacro assertf
158+
"Convenience macro that combines `assert` and `string/format`."
159+
[x & args]
160+
~(as-macro ,assert ,x (,string/format ,;args)))
161+
157162
(defmacro defdyn
158163
``Define an alias for a keyword that is used as a dynamic binding. The
159164
alias is a normal, lexically scoped binding that can be used instead of
@@ -3934,7 +3939,7 @@
39343939
(defn make-sig []
39353940
(ffi/signature :default real-ret-type ;computed-type-args))
39363941
(defn make-ptr []
3937-
(assert (ffi/lookup (if lazy (llib) lib) raw-symbol) (string "failed to find ffi symbol " raw-symbol)))
3942+
(assertf (ffi/lookup (if lazy (llib) lib) raw-symbol) "failed to find ffi symbol %v" raw-symbol))
39383943
(if lazy
39393944
~(defn ,alias ,;meta [,;formal-args]
39403945
(,ffi/call (,(delay (make-ptr))) (,(delay (make-sig))) ,;formal-args))
@@ -4111,7 +4116,7 @@
41114116
"Get the manifest for a give installed bundle"
41124117
[bundle-name]
41134118
(def name (get-manifest-filename bundle-name))
4114-
(assert (fexists name) (string "no bundle " bundle-name " found"))
4119+
(assertf (fexists name) "no bundle %v found" bundle-name)
41154120
(parse (slurp name)))
41164121

41174122
(defn- get-bundle-module
@@ -4254,11 +4259,9 @@
42544259
(def missing (seq [d :in deps :when (not (bundle/installed? d))] (string d)))
42554260
(when (next missing) (errorf "missing dependencies %s" (string/join missing ", "))))
42564261
(def bundle-name (get config :name default-bundle-name))
4257-
(assert bundle-name (errorf "unable to infer bundle name for %v, use :name argument" path))
4258-
(assert (not (string/check-set "\\/" bundle-name))
4259-
(string "bundle name "
4260-
bundle-name
4261-
" cannot contain path separators"))
4262+
(assertf bundle-name "unable to infer bundle name for %v, use :name argument" path)
4263+
(assertf (not (string/check-set "\\/" bundle-name))
4264+
"bundle name %v cannot contain path separators" bundle-name)
42624265
(assert (next bundle-name) "cannot use empty bundle-name")
42634266
(assert (not (fexists (get-manifest-filename bundle-name)))
42644267
"bundle is already installed")
@@ -4310,7 +4313,7 @@
43104313
(var i 0)
43114314
(def man (bundle/manifest bundle-name))
43124315
(def files (get man :files @[]))
4313-
(assert (os/mkdir dest-dir) (string "could not create directory " dest-dir " (or it already exists)"))
4316+
(assertf (os/mkdir dest-dir) "could not create directory %v (or it already exists)" dest-dir)
43144317
(def s (sep))
43154318
(os/mkdir (string dest-dir s "bundle"))
43164319
(def install-hook (string dest-dir s "bundle" s "init.janet"))

test/suite-boot.janet

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,4 +986,14 @@
986986
(assert (deep= (get (dyn 'a) :source-form) source))
987987
(setdyn *debug* nil)
988988

989+
# issue #1516
990+
(assert (assertf true) "assertf 1 argument")
991+
(assert (assertf true "fun message") "assertf 2 arguments")
992+
(assert (assertf true "%s message" "mystery") "assertf 3 arguments")
993+
(assert (assertf (not nil) "%s message" "ordinary") "assertf not nil")
994+
(assert-error "assertf error 1" (assertf false))
995+
(assert-error "assertf error 2" (assertf false "fun message"))
996+
(assert-error "assertf error 3" (assertf false "%s message" "mystery"))
997+
(assert-error "assertf error 4" (assertf nil "%s %s" "alice" "bob"))
998+
989999
(end-suite)

0 commit comments

Comments
 (0)