Add tests for when-some, plus one for when-let.#851
Add tests for when-some, plus one for when-let.#851alysbrooks wants to merge 1 commit intojank-lang:mainfrom
Conversation
600b193 to
2f91dca
Compare
| (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" |
There was a problem hiding this comment.
This is a good test but I feel like the testing string is backwards?
| (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))) |
There was a problem hiding this comment.
| (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))) |
| (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)))) |
There was a problem hiding this comment.
some? is probably better here than (not (nil? ...))
| (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)))) |
There was a problem hiding this comment.
Probably better to test here that a side-effect doesn't get evaluated since the test condition fails
There was a problem hiding this comment.
Yes, this is important. You could use an atom for this.
2f91dca to
3701128
Compare
when-some and when-let behave very similarly. This adds the opposite case of one of these tests to when-let.
3701128 to
8e3552b
Compare
|
Thanks for the feedback! I made the updates. |
| seq-fn (fn s [] (lazy-seq | ||
| (swap! calls inc) | ||
| (cons 1 (s)))) | ||
| s (take 5 (seq-fn))] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
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. |
when-someandwhen-letbehave very similarly so this actually reuses a large number of tests fromwhen-let. It also adds the opposite case of one of these tests to when-let.