Skip to content

Commit

Permalink
Add most basic = tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-3 committed Feb 10, 2025
1 parent 3075e42 commit 2e896ac
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/clojure/core_test/equal.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(ns clojure.core-test.equal
(:require [clojure.test :as t]))

;; Doesn't test (yet):
;; * Java collections for JVM Clojure
;; * clojure.lang.PersistentQueue
;; * ##Inf and some other special forms
;; * Records

(t/deftest two-scalars
(t/are [in ex] (= in ex)
nil nil
true true
false false
\a \a
"green" "green"
:eggs :eggs
:and/ham :and/ham
'one-fish 'one-fish
'two/fish 'two/fish
42 42
84/2 42
42 84/2
3.14 3.14
3.141592M 3.141592M
(float 0.5) (double 0.5)))

(t/deftest two-scalars-unequal
(t/are [in ex] (not (= in ex))
nil false
true false
\a \b
"yellow" "purple"
:hello :goodbye
:my/hello 'my/hello
:my/hello :your/hello
'one-fish 'two-fish
'red/fish 'red/coral
2 2.0
1M 1
(float 0.1) (double 0.1)
##NaN ##NaN))

(t/deftest string-not-list-of-chars
(t/is (not (= "hello" '(\h \e \l \l \o)))))

(t/deftest collections
(t/are [in ex] (= in ex)
[] '()
'() []
[1 2 3] '(1 2 3)
'(1 2 3) [1 2 3]
[0 1 2] (range 3)
'(0 1 2) (range 3)
{:a 1 "b" :2 3 \c \d 4} {"b" :2 \d 4 3 \c :a 1}
#{:a \b "c"} #{\b "c" :a}
;; https://clojure.org/guides/equality notes that sometimes collections with ##NaN are =
(list ##NaN) (list ##NaN)))

(t/deftest collections-unequal
(t/are [in ex] (not (= in ex))
nil '()
nil []
nil #{}
'() #{}
'() {}
[] #{}
[] {}
#{} {}
[1 2 3] #{1 2 3}
'(1 2 3) #{1 2 3}
{:a 1 "b" \c} {:a "1" "b" \c}
#{:a \b "c"} #{\b "d" :a}
[1 2 3] [3 2 1]
'(1 2 3) '(3 2 1)
[\a \b \c] {0 \a 1 \b 2 \c}
[\a ##NaN] [\a ##NaN]
#{1.0 2.0 ##NaN} #{1.0 2.0 ##NaN}))

(t/deftest sorted-collections
(t/are [in ex] (= in ex)
{:b 14 :c 15 :a 13} (sorted-map :a 13 :b 14 :c 15)
(sorted-map-by < 13 :a 14 :b 15 :c) (sorted-map-by > 13 :a 14 :b 15 :c)
#{6 4 2} (sorted-set 4 2 6)
(sorted-set-by > 4 2 6) (sorted-set-by < 4 2 6)))

(t/deftest nested-collections
(t/are [in ex] (= in ex)
{#{} ['()]} {#{} ['()]}
{:just '(:a {:plain [:simple #{:tailor}]})} {:just '(:a {:plain [:simple #{:tailor}]})}
[1 '(2 3 [4])] (list 1 [2 3 '(4)])))

(t/deftest regex
;; Value-equal regex are NOT =, only identical?
(t/is (not (= #"my regex" #"my regex")))
(t/is (let [r #"my regex"
r' r]
(= r r'))))

(t/deftest functions
;; identical? functions are =, but no other functions
(t/is (not (= #(+ 2 %) #(+ 2 %))))
(t/is (let [f #(+ 2 %)
f' f]
(= f f'))))

0 comments on commit 2e896ac

Please # to comment.