This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-01
Channels
Is it possible (or good idea in the first place) to try and catch Clojure compiler exceptions (in tests)? This doesn't seem to work, the exception is not caught:
(eval '(try
xxx
(catch Exception e
e)))
(as opposed to (/ 1 0)
— that one is caught, as it's runtime)
It worked 👍
Do you think it's a good/valid approach to testing macros when they expected to blow up on invalid input?
I think a better approach is to isolate the data part of the macro. I often start writing my macros using the following form:
(defn my-macro* [args]
;; actual logic
)
(defmacro my-macro [& args]
(my-macro* args))
It makes life a lot easier.In many cases, you can just test my-macro*
. It makes writing macros much easier.
That's true.
Sure, my actual defmacro
is already a one-liner, all logic is in functions. So the actual question is: does it make sense to have an explicit test for when the macro is supplied with unexpected input that it results in a (nice, expected) exception?
If it's a nice and expected exception, then it's probably not a compiler exception.
In that case, it's a check in one of the functions that you control that you used in that macro. So it would make sense to test those functions instead, without any eval
.
That makes sense, thank you.
When using deftest
/ is
how do you approach tests for truthiness? The true?
/`false?` preds are actually only for booleans, so nil
is not recognized as falsey.
is
is itself an assertion of truthiness (technically that is the default condition of is, it actually comes down to a multimethod where you can add custom kinds of assertions)
Ah...
Thanks!
Can someone explain why read-string
fails on a keyword such as ":@id"? That particular combo doesn't seem to be covered in the "weird characters" page. I'm fetching some JSON data and Cheshire will gladly keywordize "@id" into :@id
but then saving/reading that from edn
fails. I can work around that, but wanted to understand what is happening.
; eval (current-form): (read-string (str {(keyword "@id") 1}))
; (err) java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
the set of keywords that the reader will read is significantly smaller than the set of keywords that you can create using something like the clojure.core/keyword
function (which is what cheshire uses)
str is also not the best way to serialize things to be read later, that is pr-str (similar to the diff between println and prn)
Right, I have run into the keyword problem before. Just can't figure out why the @
is not allowed or what the reader is doing with it.
https://clojure.org/reference/reader#_literals has a section on what is allowed in a literal (a readable) keyword
https://clojure.org/reference/reader#_symbols is the section on symbols which is referenced by the keyword section
in particular > Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, ', ?, <, > and = (other characters may be allowed eventually).
there have been extension proposed to allow for printing and reading otherwise unreadable symbols and keywords, but nothing has been actually implemented
specifically here, @
is a token delimiter in the reader, so it's trying to parse ":"
as a symbol or keyword