forked from jank-lang/clojure-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyword.cljc
More file actions
100 lines (92 loc) · 3 KB
/
keyword.cljc
File metadata and controls
100 lines (92 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(ns clojure.core-test.keyword
(:require [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/keyword
(deftest test-keyword
;; "Symbols begin with a non-numeric character and can contain
;; alphanumeric characters and *, +, !, -, _, ', ?, <, > and =
;; (other characters may be allowed eventually)."
;;
;; "Keywords are like symbols, except: * They can and must begin with a colon, e.g. :fred."
;;
;; (see http://clojure.org/reader for details)
;;
;; From https://clojuredocs.org/clojure.core/keyword
;; keyword does not validate input strings for ns and name, and may
;; return improper keywords with undefined behavior for non-conformant
;; ns and name.
(are [expected name] (= expected (keyword name))
:abc "abc"
:abc 'abc
:abc :abc
:* "*"
:* '*
:* :*
:+ "+"
:+ '+
:+ :+
:! "!"
:! '!
:! :!
:- "-"
:- '-
:- :-
:_ "_"
:_ '_
:_ :_
:? "?"
:? '?
:? :?
:< "<"
:< '<
:< :<
:> ">"
:> '>
:> :>
:= "="
:= '=
:= :=
:abc*+!-_'?<>= "abc*+!-_'?<>=")
(are [expected ns name] (= expected (keyword ns name))
:abc/abc "abc" "abc"
:abc.def/abc "abc.def" "abc"
:*/abc "*" "abc"
:+/abc "+" "abc"
:!/abc "!" "abc"
:-/abc "-" "abc"
:_/abc "_" "abc"
:?/abc "?" "abc"
:</abc "<" "abc"
:>/abc ">" "abc"
:=/abc "=" "abc"
:abc.def/* "abc.def" "*"
:abc.def/+ "abc.def" "+"
:abc.def/! "abc.def" "!"
:abc.def/- "abc.def" "-"
:abc.def/_ "abc.def" "_"
:abc.def/? "abc.def" "?"
:abc.def/< "abc.def" "<"
:abc.def/> "abc.def" ">"
:abc.def/= "abc.def" "="
:abc*+!-_'?<>=/abc*+!-_'?<>= "abc*+!-_'?<>=" "abc*+!-_'?<>=")
(is (nil? (keyword nil))) ; (keyword nil) => nil, surprisingly
(is (= :abc (keyword nil "abc"))) ; If ns is nil, we just ignore it.
;; But if name is nil, then maybe we throw or maybe we don't
#?(:cljs nil ; CLJS creates a keyword that isn't
; readable (symbol part is null string: ":abc/")
:default
(is (thrown? Exception (keyword "abc" nil))))
#?@(:cljs
;; IMO, CLJS gets this right
[(is (= :abc/abc (keyword 'abc "abc")))
(is (= :abc/abc (keyword "abc" 'abc)))
(is (= :abc/abc (keyword :abc "abc")))
(is (= :abc/abc (keyword "abc" :abc)))]
:default
;; In Clojure JVM, two arg version requires namespace and
;; symbol to be a string, not a symbol or keyword like the one
;; arg version.
[(is (thrown? Exception (keyword 'abc "abc")))
(is (thrown? Exception (keyword "abc" 'abc)))
(is (thrown? Exception (keyword :abc "abc")))
(is (thrown? Exception (keyword "abc" :abc)))])))