-
Notifications
You must be signed in to change notification settings - Fork 37
Add tests for apply
#870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add tests for apply
#870
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
341a284
Add tests for `apply`
dgr ae6c72e
Fix: don't depend on order of map entries
dgr 2a53783
Fix comment and remove trailing white space
dgr fcf87d1
New test: apply `apply` recursively
dgr 44d7857
Spell "sequence" correctly
dgr d50b368
Test whether an infinite sequence arg is realized
dgr 76334de
Finish comment
dgr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| (ns clojure.core-test.apply | ||
| (:require [clojure.test :as t :refer [are deftest is testing]] | ||
| [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists] :as p])) | ||
|
|
||
| (when-var-exists apply | ||
| (deftest test-apply | ||
| (is (= 0 (apply + nil))) ; Apply + with no args | ||
| (is (= 0 (apply + '()))) | ||
| (is (= 0 (apply + []))) | ||
| (is (= 0 (apply + {}))) ; map is a sequence of map entries | ||
| (is (= 0 (apply + #{}))) | ||
| (is (= 0 (apply + ""))) ; string is a sequence of characters | ||
|
|
||
| (is (= 1 (apply + 1 nil))) ; Apply + with 1 arg and empty sequence | ||
| (is (= 1 (apply + 1 '()))) | ||
| (is (= 1 (apply + 1 []))) | ||
| (is (= 1 (apply + 1 {}))) | ||
| (is (= 1 (apply + 1 #{}))) | ||
| (is (= 1 (apply + 1 ""))) | ||
|
|
||
| (is (= 1 (apply + '(1)))) ; Zero non-sequence arg | ||
| (is (= 3 (apply + 1 '(2)))) ; One non-sequential arg | ||
| (is (= 6 (apply + 1 2 '(3)))) ; Two non-sequential args | ||
| (is (= 10 (apply + 1 2 3 '(4)))) ; Three non-sequential args | ||
| (is (= 15 (apply + 1 2 3 4 '(5)))) ; Four non-sequential args | ||
| (is (= 45 (apply + (range 10)))) ; All args as sequential | ||
|
dgr marked this conversation as resolved.
|
||
| (is (= 10 (apply + 1 [2 3 4]))) | ||
| (is (= 10 (apply + 1 #{2 3 4}))) ; A set works but order not guaranteed | ||
| (is (= #{[:a 1] [:b 2]} ; Can't guarantee order of map entries | ||
| (apply conj #{} {:a 1, :b 2}))) | ||
| (is (= [\a \b \c] ; String is sequence of chars | ||
| (apply conj [] "abc"))) | ||
|
|
||
| ;; Try various IFn things | ||
| (is (= 1 (apply {:a 1} [:a]))) ; apply map to key | ||
| (is (= 1 (apply :a [{:a 1}]))) ; apply keyword to map | ||
| (is (= 2 (apply [0 1 2 3 4] [2]))) ; apply vector to index | ||
| (is (p/thrown? (apply 2 [[0 1 2 3 4]]))) ; but numbers don't implement IFn | ||
| (is (= :a (apply #{:a :b :c} [:a]))) ; apply set to element | ||
| (is (= :a (apply :a [#{:a :b :c}]))) ; apply keyword to set | ||
|
|
||
| ;; apply recrusively | ||
| (is (= 10 (apply apply + [1 2 [3 4]]))) | ||
|
|
||
| ;; validate that `apply` doesn't try to further evaluate its | ||
| ;; arguments. If the infinite range is realized, we would expect | ||
| ;; an OOM Exception at some point. | ||
| (is (= 3 (count (apply conj [] [1 2 (range)])))))) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.