Back
test (clj)
(source)function
(test v)
test [v] finds fn at key :test in var metadata and calls it,
presuming failure will throw exception
Examples
clojure
(ns clojure.test-clojure.server
(:import java.util.Random)
(:require [clojure.test :refer :all])
(:require [clojure.core.server :as s]))
(deftest test-validate-opts
(check-invalid-opts {} "Missing required socket server property :name")
(check-invalid-opts {:name "a" :accept 'clojure.core/+} "Missing required socket server property :port")
(doseq [port [-1 "5" 999999]]
(check-invalid-opts {:name "a" :port port :accept 'clojure.core/+} (str "Invalid socket server port: " port)))
(check-invalid-opts {:name "a" :port 5555} "Missing required socket server property :accept"))
(deftest test-parse-props
(let [thread (create-random-thread)]
(.start thread)
(Thread/sleep 1000)
(try
(is (>= (count
(#'s/parse-props (System/getProperties))) 0))
(finally (.interrupt thread)))))
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(defmacro defequivtest
;; f is the core fn, r is the reducers equivalent, rt is the reducible ->
;; coll transformer
[name [f r rt] fns]
`(deftest ~name
(let [c# (range -100 1000)]
(doseq [fn# ~fns]
(is (= (~f fn# c#)
(~rt (~r fn# c#))))))))
(defequivtest test-map
[map r/map #(into [] %)]
[inc dec #(Math/sqrt (Math/abs %))])
(defequivtest test-mapcat
[mapcat r/mapcat #(into [] %)]
[(fn [x] [x])
(fn [x] [x (inc x)])
(fn [x] [x (inc x) x])])
(deftest test-mapcat-obeys-reduced
(is (= [1 "0" 2 "1" 3]
(->> (concat (range 100) (lazy-seq (throw (Exception. "Too eager"))))
(r/mapcat (juxt inc str))
(r/take 5)
(into [])))))
(defequivtest test-reduce
[reduce r/reduce identity]
[+' *'])
(defequivtest test-filter
[filter r/filter #(into [] %)]
[even? odd? #(< 200 %) identity])
(deftest test-sorted-maps
(let [m (into (sorted-map)
'{1 a, 2 b, 3 c, 4 d})]
(is (= "1a2b3c4d" (reduce-kv str "" m))
"Sorted maps should reduce-kv in sorted order")
(is (= 1 (reduce-kv (fn [acc k v]
(reduced (+ acc k)))
0 m))
"Sorted maps should stop reduction when asked")))
(deftest test-nil
(is (= {:k :v} (reduce-kv assoc {:k :v} nil)))
(is (= 0 (r/fold + nil))))
(deftest test-fold-runtime-exception
(is (thrown? IndexOutOfBoundsException
(let [test-map-count 1234
k-fail (rand-int test-map-count)]
(r/fold (fn ([])
([ret [k v]])
([ret k v] (when (= k k-fail)
(throw (IndexOutOfBoundsException.)))))
(zipmap (range test-map-count) (repeat :dummy)))))))
(deftest test-closed-over-clearing
;; this will throw OutOfMemory without proper reference clearing
(is (number? (reduce + 0 (r/map identity (range 1e8))))))
clojure
(deftest division
(is (= clojure.core// /))
(binding [*ns* *ns*]
(eval '(do (ns foo
(:require [clojure.core :as bar])
(:use [clojure.test]))
(is (= clojure.core// bar//))))))
(deftest Instants
(testing "Instants are read as java.util.Date by default"
(is (= java.util.Date (class #inst "2010-11-12T13:14:15.666"))))
(let [s "#inst \"2010-11-12T13:14:15.666-06:00\""]
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date produces java.util.Date"
(is (= java.util.Date (class (read-string s)))))
(testing "java.util.Date instants round-trips"
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Date instants round-trip throughout the year"
(doseq [month (range 1 13) day (range 1 29) hour (range 1 23)]
(let [s (format "#inst \"2010-%02d-%02dT%02d:14:15.666-06:00\"" month day hour)]
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))))
(testing "java.util.Date handling DST in time zones"
(let [dtz (TimeZone/getDefault)]
(try
;; A timezone with DST in effect during 2010-11-12
(TimeZone/setDefault (TimeZone/getTimeZone "Australia/Sydney"))
(is (= (-> s read-string)
(-> s read-string pr-str read-string)))
(finally (TimeZone/setDefault dtz)))))
(testing "java.util.Date should always print in UTC"
(let [d (read-string s)
pstr (print-str d)
len (.length pstr)]
(is (= (subs pstr (- len 7)) "-00:00\"")))))
(binding [*data-readers* {'inst read-instant-calendar}]
(testing "read-instant-calendar produces java.util.Calendar"
(is (instance? java.util.Calendar (read-string s))))
(testing "java.util.Calendar round-trips"
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Calendar remembers timezone in literal"
(is (= "#inst \"2010-11-12T13:14:15.666-06:00\""
(-> s read-string pr-str)))
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Calendar preserves milliseconds"
(is (= 666 (-> s read-string
(.get java.util.Calendar/MILLISECOND)))))))
(let [s "#inst \"2010-11-12T13:14:15.123456789\""
s2 "#inst \"2010-11-12T13:14:15.123\""
s3 "#inst \"2010-11-12T13:14:15.123456789123\""]
(binding [*data-readers* {'inst read-instant-timestamp}]
(testing "read-instant-timestamp produces java.sql.Timestamp"
(is (= java.sql.Timestamp (class (read-string s)))))
(testing "java.sql.Timestamp preserves nanoseconds"
(is (= 123456789 (-> s read-string .getNanos)))
(is (= 123456789 (-> s read-string pr-str read-string .getNanos)))
;; truncate at nanos for s3
(is (= 123456789 (-> s3 read-string pr-str read-string .getNanos))))
(testing "java.sql.Timestamp should compare nanos"
(is (= (read-string s) (read-string s3)))
(is (not= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date should truncate at milliseconds"
(is (= (read-string s) (read-string s2) (read-string s3))))))
(let [s "#inst \"2010-11-12T03:14:15.123+05:00\""
s2 "#inst \"2010-11-11T22:14:15.123Z\""]
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date should convert to UTC"
(is (= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-timestamp}]
(testing "read-instant-timestamp should convert to UTC"
(is (= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-calendar}]
(testing "read-instant-calendar should preserve timezone"
(is (not= (read-string s) (read-string s2)))))))
(deftest UUID
(is (= java.util.UUID (class #uuid "550e8400-e29b-41d4-a716-446655440000")))
(is (.equals #uuid "550e8400-e29b-41d4-a716-446655440000"
#uuid "550e8400-e29b-41d4-a716-446655440000"))
(is (not (identical? #uuid "550e8400-e29b-41d4-a716-446655440000"
#uuid "550e8400-e29b-41d4-a716-446655440000")))
(is (= 4 (.version #uuid "550e8400-e29b-41d4-a716-446655440000")))
(is (= (print-str #uuid "550e8400-e29b-41d4-a716-446655440000")
"#uuid \"550e8400-e29b-41d4-a716-446655440000\"")))
(deftest unknown-tag
(let [my-unknown (fn [tag val] {:unknown-tag tag :value val})
throw-on-unknown (fn [tag val] (throw (RuntimeException. (str "No data reader function for tag " tag))))
my-uuid (partial my-unknown 'uuid)
u "#uuid \"550e8400-e29b-41d4-a716-446655440000\""
s "#never.heard.of/some-tag [1 2]" ]
(binding [*data-readers* {'uuid my-uuid}
*default-data-reader-fn* my-unknown]
(testing "Unknown tag"
(is (= (read-string s)
{:unknown-tag 'never.heard.of/some-tag
:value [1 2]})))
(testing "Override uuid tag"
(is (= (read-string u)
{:unknown-tag 'uuid
:value "550e8400-e29b-41d4-a716-446655440000"}))))
(binding [*default-data-reader-fn* throw-on-unknown]
(testing "Unknown tag with custom throw-on-unknown"
(are [err msg form] (thrown-with-msg? err msg (read-string form))
Exception #"No data reader function for tag foo" "#foo [1 2]"
Exception #"No data reader function for tag bar/foo" "#bar/foo [1 2]"
Exception #"No data reader function for tag bar.baz/foo" "#bar.baz/foo [1 2]")))
(testing "Unknown tag out-of-the-box behavior (like Clojure 1.4)"
(are [err msg form] (thrown-with-msg? err msg (read-string form))
Exception #"No reader function for tag foo" "#foo [1 2]"
Exception #"No reader function for tag bar/foo" "#bar/foo [1 2]"
Exception #"No reader function for tag bar.baz/foo" "#bar.baz/foo [1 2]"))))
(deftest preserve-read-cond-test
(let [x (read-string {:read-cond :preserve} "#?(:clj foo :cljs bar)" )]
(is (reader-conditional? x))
(is (not (:splicing? x)))
(is (= :foo (get x :no-such-key :foo)))
(is (= (:form x) '(:clj foo :cljs bar)))
(is (= x (reader-conditional '(:clj foo :cljs bar) false))))
(let [x (read-string {:read-cond :preserve} "#?@(:clj [foo])" )]
(is (reader-conditional? x))
(is (:splicing? x))
(is (= :foo (get x :no-such-key :foo)))
(is (= (:form x) '(:clj [foo])))
(is (= x (reader-conditional '(:clj [foo]) true))))
(is (thrown-with-msg? RuntimeException #"No reader function for tag"
(read-string {:read-cond :preserve} "#js {:x 1 :y 2}" )))
(let [x (read-string {:read-cond :preserve} "#?(:cljs #js {:x 1 :y 2})")
[platform tl] (:form x)]
(is (reader-conditional? x))
(is (tagged-literal? tl))
(is (= 'js (:tag tl)))
(is (= {:x 1 :y 2} (:form tl)))
(is (= :foo (get tl :no-such-key :foo)))
(is (= tl (tagged-literal 'js {:x 1 :y 2}))))
(testing "print form roundtrips"
(doseq [s ["#?(:clj foo :cljs bar)"
"#?(:cljs #js {:x 1, :y 2})"
"#?(:clj #clojure.test_clojure.reader.TestRecord [42 85])"]]
(is (= s (pr-str (read-string {:read-cond :preserve} s)))))))
(deftest reader-conditionals
(testing "basic read-cond"
(is (= '[foo-form]
(read-string {:read-cond :allow :features #{:foo}} "[#?(:foo foo-form :bar bar-form)]")))
(is (= '[bar-form]
(read-string {:read-cond :allow :features #{:bar}} "[#?(:foo foo-form :bar bar-form)]")))
(is (= '[foo-form]
(read-string {:read-cond :allow :features #{:foo :bar}} "[#?(:foo foo-form :bar bar-form)]")))
(is (= '[]
(read-string {:read-cond :allow :features #{:baz}} "[#?( :foo foo-form :bar bar-form)]"))))
(testing "environmental features"
(is (= "clojure" #?(:clj "clojure" :cljs "clojurescript" :default "default"))))
(testing "default features"
(is (= "default" #?(:clj-clr "clr" :cljs "cljs" :default "default"))))
(testing "splicing"
(is (= [] [#?@(:clj [])]))
(is (= [:a] [#?@(:clj [:a])]))
(is (= [:a :b] [#?@(:clj [:a :b])]))
(is (= [:a :b :c] [#?@(:clj [:a :b :c])]))
(is (= [:a :b :c] [#?@(:clj [:a :b :c])])))
(testing "nested splicing"
(is (= [:a :b :c :d :e]
[#?@(:clj [:a #?@(:clj [:b #?@(:clj [:c]) :d]):e])]))
(is (= '(+ 1 (+ 2 3))
'(+ #?@(:clj [1 (+ #?@(:clj [2 3]))]))))
(is (= '(+ (+ 2 3) 1)
'(+ #?@(:clj [(+ #?@(:clj [2 3])) 1]))))
(is (= [:a [:b [:c] :d] :e]
[#?@(:clj [:a [#?@(:clj [:b #?@(:clj [[:c]]) :d])] :e])])))
(testing "bypass unknown tagged literals"
(is (= [1 2 3] #?(:cljs #js [1 2 3] :clj [1 2 3])))
(is (= :clojure #?(:foo #some.nonexistent.Record {:x 1} :clj :clojure))))
(testing "error cases"
(is (thrown-with-msg? RuntimeException #"Feature should be a keyword" (read-string {:read-cond :allow} "#?((+ 1 2) :a)")))
(is (thrown-with-msg? RuntimeException #"even number of forms" (read-string {:read-cond :allow} "#?(:cljs :a :clj)")))
(is (thrown-with-msg? RuntimeException #"read-cond-splicing must implement" (read-string {:read-cond :allow} "#?@(:clj :a)")))
(is (thrown-with-msg? RuntimeException #"is reserved" (read-string {:read-cond :allow} "#?@(:foo :a :else :b)")))
(is (thrown-with-msg? RuntimeException #"must be a list" (read-string {:read-cond :allow} "#?[:foo :a :else :b]")))
(is (thrown-with-msg? RuntimeException #"Conditional read not allowed" (read-string {:read-cond :BOGUS} "#?[:clj :a :default nil]")))
(is (thrown-with-msg? RuntimeException #"Conditional read not allowed" (read-string "#?[:clj :a :default nil]")))
(is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj [1 2])")))
(is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj [1])")))
(is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj []) 1"))))
(testing "clj-1698-regression"
(let [opts {:features #{:clj} :read-cond :allow}]
(is (= 1 (read-string opts "#?(:cljs {'a 1 'b 2} :clj 1)")))
(is (= 1 (read-string opts "#?(:cljs (let [{{b :b} :a {d :d} :c} {}]) :clj 1)")))
(is (= '(def m {}) (read-string opts "(def m #?(:cljs ^{:a :b} {} :clj ^{:a :b} {}))")))
(is (= '(def m {}) (read-string opts "(def m #?(:cljs ^{:a :b} {} :clj ^{:a :b} {}))")))
(is (= 1 (read-string opts "#?(:cljs {:a #_:b :c} :clj 1)")))))
(testing "nil expressions"
(is (nil? #?(:default nil)))
(is (nil? #?(:foo :bar :clj nil)))
(is (nil? #?(:clj nil :foo :bar)))
(is (nil? #?(:foo :bar :default nil)))))
(deftest eof-option
(is (= 23 (read-string {:eof 23} "")))
(is (= 23 (read {:eof 23} (clojure.lang.LineNumberingPushbackReader.
(java.io.StringReader. ""))))))
(require '[clojure.string :as s])
(deftest namespaced-maps
(is (= #:a{1 nil, :b nil, :b/c nil, :_/d nil}
#:a {1 nil, :b nil, :b/c nil, :_/d nil}
{1 nil, :a/b nil, :b/c nil, :d nil}))
(is (= #::{1 nil, :a nil, :a/b nil, :_/d nil}
#:: {1 nil, :a nil, :a/b nil, :_/d nil}
{1 nil, :clojure.test-clojure.reader/a nil, :a/b nil, :d nil} ))
(is (= #::s{1 nil, :a nil, :a/b nil, :_/d nil}
#::s {1 nil, :a nil, :a/b nil, :_/d nil}
{1 nil, :clojure.string/a nil, :a/b nil, :d nil}))
(is (= (read-string "#:a{b 1 b/c 2}") {'a/b 1, 'b/c 2}))
(is (= (binding [*ns* (the-ns 'clojure.test-clojure.reader)] (read-string "#::{b 1, b/c 2, _/d 3}")) {'clojure.test-clojure.reader/b 1, 'b/c 2, 'd 3}))
(is (= (binding [*ns* (the-ns 'clojure.test-clojure.reader)] (read-string "#::s{b 1, b/c 2, _/d 3}")) {'clojure.string/b 1, 'b/c 2, 'd 3})))
(deftest namespaced-map-errors
(are [err msg form] (thrown-with-msg? err msg (read-string form))
Exception #"Invalid token" "#:::"
Exception #"Namespaced map literal must contain an even number of forms" "#:s{1}"
Exception #"Namespaced map must specify a valid namespace" "#:s/t{1 2}"
Exception #"Unknown auto-resolved namespace alias" "#::BOGUS{1 2}"
Exception #"Namespaced map must specify a namespace" "#: s{:a 1}"
Exception #"Duplicate key: :user/a" "#::{:a 1 :a 2}"
Exception #"Duplicate key: user/a" "#::{a 1 a 2}"))
(deftest namespaced-map-edn
(is (= {1 1, :a/b 2, :b/c 3, :d 4}
(edn/read-string "#:a{1 1, :b 2, :b/c 3, :_/d 4}")
(edn/read-string "#:a {1 1, :b 2, :b/c 3, :_/d 4}"))))
(deftest invalid-symbol-value
(is (thrown-with-msg? Exception #"Invalid token" (read-string "##5")))
(is (thrown-with-msg? Exception #"Invalid token" (edn/read-string "##5")))
(is (thrown-with-msg? Exception #"Unknown symbolic value" (read-string "##Foo")))
(is (thrown-with-msg? Exception #"Unknown symbolic value" (edn/read-string "##Foo"))))
(deftest test-read+string
(let [[r s] (read+string (str->lnpr "[:foo 100]"))]
(is (= [:foo 100] r))
(is (= "[:foo 100]" s)))
(deftest t-Explicit-line-column-numbers
(is (= {:line 42 :column 99}
(-> "^{:line 42 :column 99} (1 2)" read-string meta (select-keys [:line :column]))))
clojure/clojurescript
(ns cljs.ns-test
(:refer-clojure :exclude [+ for] :rename {mapv core-mapv})
(:require-macros [clojure.core :as lang :refer [when when-let] :rename {when always
when-let always-let}]
[cljs.test :refer [deftest is]])
(:require [goog :as goog-alias]
[cljs.test]
[cljs.ns-test.foo :refer [baz]]
[clojure.set :as s :refer [intersection] :rename {intersection itsc}]
[cljs.analyzer :as ana]
[fake.ns :as-alias fake])
(:use [cljs.ns-test.bar :only [quux]]))
(deftest test-ns
(is (= 4 (clojure.core/+ 2 1 1)))
(is (= 0 (cljs.ns-test/+ 2 1 1)))
(is (= 0 (+ 2 1 1)))
(is (= 123 (baz)))
(is (= 123 (quux)))
(deftest test-cljs-1508
(is (= (itsc #{1 2 3} #{2}) #{2}))
(is (= #'itsc #'clojure.set/intersection))
(is (= itsc clojure.set/intersection))
(is (= (always true 42) 42))
(is (= (core-mapv inc [1 2]) [2 3]))
(is (= (always-let [foo 42] foo) 42)))
(deftest test-cljs-1677
(let [array-like #js {:length 3}]
(is (.isArrayLike js/goog array-like))
(is (goog/isArrayLike array-like))
(is (goog-alias/isArrayLike array-like))))
(deftest test-cljs-3399
(is (= ::fake/foo :fake.ns/foo)
(= `fake/foo `fake.ns/foo)))
noprompt/meander
(ns multimethods
(:refer-clojure :exclude [defmethod defmulti])
(:require
#?(:clj [clojure.core :as clj] :cljs [cljs.core :as cljs])
[meander.epsilon :as m]))
(defmulti test-fn)
(defmethod test-fn
[:add ?x ?y]
(println ?x ?y)
(+ ?x ?y))
(test-fn :add 1 2)
;; => 3
(defmethod test-fn
[:sub ?x ?y]
(- ?x ?y))
(test-fn :sub 2 1)
;; => 1
(defmethod test-fn
[:sum . [!xs ...]]
(reduce + !xs))
(test-fn :sum [1 2 3 4 5 6 7 8 9 10])
;; => 55
cognitect-labs/aws-api
(ns s3-examples
(:require [clojure.core.async :as a]
[clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]
[clojure.java.io :as io]
[clojure.repl :as repl]
[cognitect.aws.client.api :as aws]))
;; * use this bucket-name to avoid collisions with other devs
;; * don't forget to delete the bucket when done exploring!
(def bucket-name (str "cognitect-aws-test-" (.getEpochSecond (java.time.Instant/now))))
hraberg/deuce
(ns deuce.emacs.chartab
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc]
[deuce.emacs.data :as data]
[deuce.emacs.fns :as fns])
(:import [deuce.emacs.data CharTable]
[java.util Arrays])
(:refer-clojure :exclude []))
(defun optimize-char-table (char-table &optional test)
"Optimize CHAR-TABLE.
TEST is the comparison function used to decide whether two entries are
equivalent and can be merged. It defaults to `equal'."
nil)
fluree/db
(ns json-ld
(:require [fluree.db.method.ipfs.core :as ipfs]
[fluree.db.db.json-ld :as jld-db]
[fluree.db.json-ld.transact :as jld-tx]
[clojure.core.async :as async]
[fluree.db.flake :as flake]
[fluree.db.json-ld.api :as fluree]
[fluree.db.util.async :refer [<?? go-try channel?]]
[fluree.db.query.range :as query-range]
[fluree.db.constants :as const]
[fluree.db.dbproto :as dbproto]
[fluree.db.did :as did]
[fluree.db.conn.proto :as conn-proto]
[fluree.db.util.json :as json]
[fluree.json-ld :as json-ld]
[fluree.db.util.log :as log]))
(-> (fluree/connect-ipfs
{:server nil ;; use default
:ipns {:key "self"} ;; publish to ipns by default using the provided key/profile
:context {"id" "@id"
"type" "@type"
"schema" "http://schema.org/"
"rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
"rdfs" "http://www.w3.org/2000/01/rdf-schema#"
"wiki" "https://www.wikidata.org/wiki/"
"skos" "http://www.w3.org/2008/05/skos#"
"f" "https://ns.flur.ee/ledger#"}
:did (did/private->did-map "8ce4eca704d653dec594703c81a84c403c39f262e54ed014ed857438933a2e1c")})
(.then (fn [conn]
(println "conn" (pr-str conn))
(-> (fluree/create conn "test/dan1")
(.then (fn [ledger]
(println "ledger" (pr-str ledger))
(-> (fluree/query (fluree/db ledger)
{:select {'?s [:* {:f/role [:*]}]}
:where [['?s :type :f/DID]]})
(.then (fn [q0] (println "q0" q0))))
(def ledger @(fluree/create ipfs-conn "test/db1"
{:id "fluree:ipns:k51qzi5uqu5dljuijgifuqz9lt1r45lmlnvmu3xzjew9v8oafoqb122jov0mr2"}))
(def latest-db (fluree/db ledger))
@(fluree/query latest-db {:select [:* {:f/function [:*]}]
:from :f/Rule})
@(fluree/query latest-db {:select {'?s [:* {:f/role [:*]}]}
:where [['?s :type :f/DID]]})
;(def l1 (fluree/create file-conn "test/db1"))
;; get latest db, should be = to db4*
(def latest-db (fluree/db ledger)))