Skip to content

Add tests for when-some, plus one for when-let.#851

Open
alysbrooks wants to merge 1 commit intojank-lang:mainfrom
alysbrooks:alys/add-when-some
Open

Add tests for when-some, plus one for when-let.#851
alysbrooks wants to merge 1 commit intojank-lang:mainfrom
alysbrooks:alys/add-when-some

Conversation

@alysbrooks
Copy link
Copy Markdown
Contributor

when-some and when-let behave very similarly so this actually reuses a large number of tests from when-let. It also adds the opposite case of one of these tests to when-let.

Comment thread test/clojure/core_test/when_let.cljc Outdated
(is (nil? (when-let [x nil] x))))
(testing "basic single-binding tests using seqs"
(is (= '(0 1 2 3 4) (when-let [x (range 5)] x))))
(testing "unlike, when-some, we're looking for not-nil specifically, so false evaluates"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good test but I feel like the testing string is backwards?

Comment thread test/clojure/core_test/when_some.cljc Outdated
(when-var-exists when-some
(deftest test-when-some
(testing "basic single-binding tests using vectors or nil"
(is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4] ] x)))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4] ] x)))
(is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4]] x)))

Comment thread test/clojure/core_test/when_some.cljc Outdated
(deftest test-when-some
(testing "basic single-binding tests using vectors or nil"
(is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4] ] x)))
(is (not (nil? (when-some [x [nil]] x))))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some? is probably better here than (not (nil? ...))

Comment thread test/clojure/core_test/when_some.cljc Outdated
(is (= [0 1 2 3 4] (when-some [x [0 1 2 3 4] ] x)))
(is (not (nil? (when-some [x [nil]] x))))
(is (= [] (when-some [x []] x)))
(is (nil? (when-some [x nil] x))))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to test here that a side-effect doesn't get evaluated since the test condition fails

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is important. You could use an atom for this.

@alysbrooks alysbrooks force-pushed the alys/add-when-some branch from 2f91dca to 3701128 Compare March 8, 2026 05:16
when-some and when-let behave very similarly. This adds the opposite
case of one of these tests to when-let.
@alysbrooks alysbrooks force-pushed the alys/add-when-some branch from 3701128 to 8e3552b Compare March 8, 2026 05:21
@alysbrooks
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I made the updates.

Comment on lines +23 to +26
seq-fn (fn s [] (lazy-seq
(swap! calls inc)
(cons 1 (s))))
s (take 5 (seq-fn))]
Copy link
Copy Markdown
Member

@jeaye jeaye Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't testing that seq is called only once. Even if it's called twice, we're not necessarily going to re-run the whole lazy sequence. This is because when seq is called on a sequence, it just returns the sequence. (take 5 (seq-fn)) gives us a sequence, so calling seq on it doesn't change anything.

I think this is an interesting property to try to test, but I don't know of a cross-dialect way to actually test it. @E-A-Griffin any ideas?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the issue that we're essentially testing seq is called once OR seq caches, but we'd prefer to test the first (or maybe both properties) separately?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is that we're just testing something here which isn't part of any contract of when-some. It's specific to the details of lazy-seq and seq. So we can just remove this bit entirely from this test.

@dgr
Copy link
Copy Markdown
Collaborator

dgr commented Apr 20, 2026

What's the status of this PR? It's been two months since it was created and more than a month since the last comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants