beginners 2025-11-26

How should you write a test that uses and ?

(deftest test-name
  (is (and ... ...)))
This works but it's not as useful because when the test fails it just shows false instead of the values

This is with clojure.test

Just make N separate tests.

Oh right of course lol, thank you!

πŸ‘ 1

The problem is with or though:)

πŸ₯² 1

it might be helpful to use the 2-arity of clojure.test/is to leave a hint: (is (or (foo user) (bar user)) "user lacking...")

☝️ 1

For the and case, something like this:

(deftest test-name
  (let [x 2
        y 4]
    (is (even? x))
    (is (even? y))
    (is (> x y))))

(Just in case β€œN separate tests” might be interpreted as β€œN separate deftests”.)

πŸ‘ 1

Yes, yes! Just separate test cases.

πŸ‘ 1

I'm extremely confused on why this is the case. An expression eval's to false, but then if i print it (or use it in a test) it becomes true?

The way it's shown makes it seem as if it's literally check (...). Meaning, those are two independent forms. There's no (def ...) around them that would bind the value of (...) to the name check.

Ah, is it in a let?

Oh sorry I should have clarified, this is inside a let binding

Nothing stands out for me. I'd inspect your REPL and your test environments, if they are different. Maybe there's some Java property that differs, maybe some dynamic binding, maybe you reset some var in your REPL - could be anything. Also, just in case - if example-task-generators is a vector, I'd use subvec instead of that (list ...). If it's not a vector, maybe a combination of drop + take would be better to avoid the repetition and having to manually construct a list.

example-task-generator is a vector yes, but the tasks/match-task-generator-with-day function returns a filter which would be a list, so to compare the two i need to make a list right

(= [1 2 3]
   '(1 2 3))
;; => true

Oh right, because theyd both be a collection

@p-himik I took the check expression outside of the let, and ran clj ... on a print statement of it and it prints to true, which means my repl might be a bit broken, ill give it a restart and see if something changes

A quick note on tests β€” I avoid let in my tests if I can. I wrote this test today:

(def term
  '{:tag :term,
    :attrs {},
    :content
    ({:tag :termDesc,
      :attrs {},
      :content
      ({:tag :termCode, :attrs {}, :content ("A0MNA")}
       {:tag :termExtendedName,
        :attrs {},
        :content ("Red Sea houndfish (as animal)")}
       {:tag :termScopeNote,
        :attrs {},
        :content
        ("Live animal of the taxonomic species Tylosurus choram [...]")})})})

(deftest parse-term
  (is (= (mtx/parse-term term)
         {:foodex2.term/code "A0MNA"
          :foodex2.term/name "Red Sea houndfish (as animal)"
          :foodex2.term/note "Live animal of the taxonomic species Tylosurus choram [...]"})))
Since there's no local bindings, it's easy to evaluate expressions inside the test.

That's a good point yeah, it's been annoying to copy paste the let values in order to run the repl on parts of it.

🎯 1

There are tools for that - no need to copy-paste. E.g. https://github.com/borkdude/deflet.

🀯 1