Skip to content

Add tests for ffirst, fnext, last, nfirst, and nnext #86

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 3 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/clojure/core_test/ffirst.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns clojure.core-test.ffirst
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/ffirst
(deftest test-ffirst
(testing "common"
(is (= nil (ffirst '())))
(is (= nil (ffirst [])))
(is (= nil (ffirst {})))
(is (= nil (ffirst #{})))
(is (= nil (ffirst nil)))
(is (= :a (ffirst {:a :b})))
(is (= 0 (ffirst [[0 1] [2 3]])))
(is (= 0 (ffirst '([0 1] [2 3]))))
(is (= 0 (ffirst (repeat (range)))))
(is (= 0 (ffirst [(range)])))
(is (= \a (ffirst ["ab" "cd"])))
(is (= \a (ffirst ["abcd"])))
(is (= \a (ffirst #{"abcd"}))))
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be good to cover a happy case for infinite seqs. For example, two come to mind:

  • (ffirst (repeat (range)))
  • (ffirst [(range)])

Same with nfirst.

Copy link
Contributor Author

@rafonsecad rafonsecad Jun 13, 2025

Choose a reason for hiding this comment

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

@jeaye for ffirst those cases are ok, but nfirst would work with a finite range

(nfirst (repeat (range 0 10))) ;; (1 2 3 4 5 6 7 8 9)

(nfirst (repeat (range))) ; is equal to (next (range))

Copy link
Member

Choose a reason for hiding this comment

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

Yep, do whichever tweaks you need for nfirst. 🙂


(testing "exceptions"
#?@(:clj
[(is (thrown? Exception (ffirst (range 0 10))))
(is (thrown? Exception (ffirst (range)))) ; infinite lazy seq
(is (thrown? Exception (ffirst [:a :b :c])))
(is (thrown? Exception (ffirst '(:a :b :c))))]
:cljs
[(is (thrown? js/Error (ffirst (range 0 10))))
(is (thrown? js/Error (ffirst (range)))) ; infinite lazy seq
(is (thrown? js/Error (ffirst [:a :b :c])))
(is (thrown? js/Error (ffirst '(:a :b :c))))]))))
32 changes: 32 additions & 0 deletions test/clojure/core_test/fnext.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns clojure.core-test.fnext
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/fnext
(deftest test-fnext
(testing "section name"
(is (= nil (fnext '())))
(is (= nil (fnext [])))
(is (= nil (fnext {})))
(is (= nil (fnext #{})))
(is (= nil (fnext nil)))
(is (= nil (fnext "")))
(is (= nil (fnext "a")))
(is (= nil (fnext {:a :b})))
(is (= :b (fnext [:a :b])))
(is (= 1 (fnext (range 0 10))))
(is (= 1 (fnext (range)))) ; infinite lazy seq
(is (= [2 3] (fnext [[0 1] [2 3]])))
(is (= '(2 3) (fnext '([0 1] [2 3]))))
(is (= \b (fnext "abcd")))
(is (= "cd" (fnext ["ab" "cd"])))
(is (= nil (fnext ["abcd"])))
(is (= nil (fnext #{"abcd"}))))

(testing "exceptions"
#?@(:clj
[(is (thrown? Exception (fnext 0)))
(is (thrown? Exception (fnext \a )))]
:cljs
[(is (thrown? js/Error (fnext 0)))]))))
24 changes: 24 additions & 0 deletions test/clojure/core_test/last.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns clojure.core-test.last
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/last
(deftest test-last
(testing "common"
(is (= 9 (last (range 0 10))))
(is (= :c (last [:a :b :c])))
(is (= :c (last '(:a :b :c))))
(is (= \d (last "abcd")))
(is (= \a (last "a")))
(is (= nil (last '())))
(is (= nil (last [])))
(is (= nil (last #{})))
(is (= nil (last nil))))

(testing "exceptions"
#?@(:clj
[(is (thrown? Exception (last \a)))
(is (thrown? Exception (last 0)))]
:cljs
[(is (thrown? js/Error (last 0)))]))))
33 changes: 33 additions & 0 deletions test/clojure/core_test/nfirst.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns clojure.core-test.nfirst
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/nfirst
(deftest test-nfirst
(testing "common"
(is (= nil (nfirst '())))
(is (= nil (nfirst [])))
(is (= nil (nfirst {})))
(is (= nil (nfirst #{})))
(is (= nil (nfirst nil)))
(is (= nil (nfirst "")))
(is (= '(:b) (nfirst {:a :b})))
(is (= '(1) (nfirst [[0 1] [2 3]])))
(is (= '(1) (nfirst '([0 1] [2 3]))))
(is (= '(1 2 3 4) (nfirst (repeat (range 0 5)))))
(is (= '(\b) (nfirst ["ab" "cd"])))
(is (= '(\b \c \d) (nfirst ["abcd"])))
(is (= '(\b \c \d) (nfirst #{"abcd"}))))

(testing "exceptions"
#?@(:clj
[(is (thrown? Exception (nfirst (range 0 10))))
(is (thrown? Exception (nfirst (range)))) ; infinite lazy seq
(is (thrown? Exception (nfirst [:a :b :c])))
(is (thrown? Exception (nfirst '(:a :b :c))))]
:cljs
[(is (thrown? js/Error (nfirst (range 0 10))))
(is (thrown? js/Error (nfirst (range)))) ; infinite lazy seq
(is (thrown? js/Error (nfirst [:a :b :c])))
(is (thrown? js/Error (nfirst '(:a :b :c))))]))))
16 changes: 16 additions & 0 deletions test/clojure/core_test/nnext.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns clojure.core-test.nnext
(:require clojure.core
[clojure.test :as t :refer [deftest testing is are]]
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))

(when-var-exists clojure.core/nnext
(deftest test-nnext
(testing "common"
(is (= '(2 3 4 5 6 7 8 9) (nnext (range 0 10))))
(is (= 2 (first (nnext (range))))) ; infinite lazy seq
(is (= '(3) (nnext [1 2 3])))
(is (= '([:c 3] [:d 4]) (nnext {:a 1, :b 2, :c 3, :d 4})))
(is (nil? (nnext nil)))
(is (nil? (nnext '())))
(is (nil? (nnext [])))
(is (nil? (nnext #{}))))))