2018-02-27 20:53:23 +01:00
|
|
|
;; Copyright 2018 the authors.
|
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
|
|
|
|
|
|
|
(import [hy._compat [PY3]])
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-hyphen []
|
|
|
|
(setv a-b 1)
|
|
|
|
(assert (= a-b 1))
|
|
|
|
(assert (= a_b 1))
|
|
|
|
(setv -a-_b- 2)
|
|
|
|
(assert (= -a-_b- 2))
|
|
|
|
(assert (= -a--b- 2))
|
|
|
|
(assert (= -a__b- 2))
|
|
|
|
(setv -_- 3)
|
|
|
|
(assert (= -_- 3))
|
|
|
|
(assert (= --- 3))
|
|
|
|
(assert (= ___ 3)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-underscore-number []
|
|
|
|
(setv _42 3)
|
|
|
|
(assert (= _42 3))
|
|
|
|
(assert (!= _42 -42))
|
|
|
|
(assert (not (in "_hyx_42" (locals)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-question-mark []
|
|
|
|
(setv foo? "nachos")
|
|
|
|
(assert (= foo? "nachos"))
|
|
|
|
(assert (= is_foo "nachos"))
|
|
|
|
(setv ___ab_cd? "tacos")
|
|
|
|
(assert (= ___ab_cd? "tacos"))
|
|
|
|
(assert (= ___is_ab_cd "tacos")))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-py-forbidden-ascii []
|
|
|
|
|
|
|
|
(setv # "no comment")
|
|
|
|
(assert (= # "no comment"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_Xnumber_signX "no comment"))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
(setv $ "dosh")
|
|
|
|
(assert (= $ "dosh"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_Xdollar_signX "dosh")))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-basic-multilingual-plane []
|
|
|
|
(setv ♥ "love"
|
|
|
|
⚘ab "flower")
|
|
|
|
(assert (= (+ ⚘ab ♥) "flowerlove"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= (+ hyx_XflowerXab hyx_Xblack_heart_suitX) "flowerlove"))
|
2018-02-27 20:53:23 +01:00
|
|
|
(setv ⚘-⚘ "doubleflower")
|
|
|
|
(assert (= ⚘-⚘ "doubleflower"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_XflowerX_XflowerX "doubleflower"))
|
2018-02-27 20:53:23 +01:00
|
|
|
(setv ⚘? "mystery")
|
|
|
|
(assert (= ⚘? "mystery"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_is_XflowerX "mystery")))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-higher-unicode []
|
|
|
|
(setv 😂 "emoji")
|
|
|
|
(assert (= 😂 "emoji"))
|
|
|
|
(if PY3
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_Xface_with_tears_of_joyX "emoji"))
|
2018-02-27 20:53:23 +01:00
|
|
|
(assert (= hyx_XU1f602X "emoji"))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-nameless-unicode []
|
|
|
|
(setv "private use")
|
|
|
|
(assert (= "private use"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_XUe000X "private use")))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-charname-with-hyphen []
|
|
|
|
(setv a<b "little")
|
|
|
|
(assert (= a<b "little"))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_aXlessHthan_signXb "little")))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-delimiters []
|
|
|
|
(setv X☠ "treasure")
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_Xlatin_capital_letter_xXXskull_and_crossbonesX "treasure")))
|
2018-02-27 20:53:23 +01:00
|
|
|
|
|
|
|
|
2017-11-16 01:17:28 +01:00
|
|
|
(defmacro m---x [form]
|
|
|
|
[form form])
|
|
|
|
(defn test-macro []
|
|
|
|
(setv x "")
|
|
|
|
(assert (= (m---x (do (+= x "a") 1)) [1 1]))
|
|
|
|
(assert (= (m___x (do (+= x "b") 2)) [2 2]))
|
|
|
|
(assert (= x "aabb")))
|
|
|
|
|
|
|
|
|
2018-02-27 20:53:23 +01:00
|
|
|
(deftag tm---x [form]
|
|
|
|
[form form])
|
|
|
|
(defn test-tag-macro []
|
|
|
|
(setv x "")
|
|
|
|
(assert (= #tm---x (do (+= x "a") 1) [1 1]))
|
|
|
|
(assert (= #tm___x (do (+= x "b") 2) [2 2]))
|
|
|
|
(assert (= x "aabb")))
|
|
|
|
|
|
|
|
|
2017-11-16 02:21:01 +01:00
|
|
|
(defn test-special-form []
|
|
|
|
(setv not-in 1)
|
|
|
|
; We set the variable to make sure that if this test works, it's
|
|
|
|
; because we're calling the special form instead of the shadow
|
|
|
|
; function.
|
|
|
|
(assert (is (not-in 2 [1 2 3]) False))
|
|
|
|
(assert (is (not_in 2 [1 2 3]) False)))
|
|
|
|
|
|
|
|
|
2017-11-16 00:43:46 +01:00
|
|
|
(defn test-python-keyword []
|
|
|
|
(setv if 3)
|
|
|
|
(assert (= if 3))
|
|
|
|
(assert (= hyx_if 3)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-operator []
|
|
|
|
(setv + 3)
|
|
|
|
(assert (= + 3))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= hyx_Xplus_signX 3)))
|
2017-11-16 00:43:46 +01:00
|
|
|
|
|
|
|
|
2017-11-19 17:35:20 +01:00
|
|
|
(defn test-keyword-args []
|
|
|
|
|
|
|
|
(defn f [a a-b foo? ☘]
|
|
|
|
[a a-b foo? ☘])
|
|
|
|
(assert (= (f :foo? 3 :☘ 4 :a 1 :a-b 2) [1 2 3 4]))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= (f :is_foo 3 :hyx_XshamrockX 4 :a 1 :a_b 2) [1 2 3 4]))
|
2017-11-19 17:35:20 +01:00
|
|
|
|
|
|
|
(defn g [&kwargs x]
|
|
|
|
x)
|
|
|
|
(assert (= (g :foo? 3 :☘ 4 :a 1 :a-b 2)
|
2018-04-04 20:04:34 +02:00
|
|
|
{"a" 1 "a_b" 2 "is_foo" 3 "hyx_XshamrockX" 4}))
|
2018-04-04 19:50:33 +02:00
|
|
|
(assert (= (g :is_foo 3 :hyx_XshamrockX 4 :a 1 :a_b 2)
|
2018-04-04 20:04:34 +02:00
|
|
|
{"a" 1 "a_b" 2 "is_foo" 3 "hyx_XshamrockX" 4})))
|
2017-11-19 17:35:20 +01:00
|
|
|
|
|
|
|
|
2018-02-27 20:53:23 +01:00
|
|
|
(defn test-late-mangling []
|
|
|
|
; Mangling should only happen during compilation.
|
|
|
|
(assert (!= 'foo? 'is_foo))
|
|
|
|
(setv sym 'foo?)
|
|
|
|
(assert (= sym "foo?"))
|
|
|
|
(assert (!= sym "is_foo"))
|
|
|
|
(setv out (eval `(do
|
2018-04-04 20:04:34 +02:00
|
|
|
(setv ~sym 10)
|
|
|
|
[foo? is_foo])))
|
2018-02-27 20:53:23 +01:00
|
|
|
(assert (= out [10 10])))
|
2018-03-04 23:20:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
(defn test-functions []
|
2018-04-04 20:04:34 +02:00
|
|
|
(for [[a b] [["---ab-cd?" "___is_ab_cd"]
|
|
|
|
["if" "hyx_if"]
|
|
|
|
["⚘-⚘" "hyx_XflowerX_XflowerX"]]]
|
2018-03-04 23:20:46 +01:00
|
|
|
(assert (= (mangle a) b))
|
|
|
|
(assert (= (unmangle b) a))))
|
Catch IndentationError in isidentifier()
Fixes:
>>> from hy._compat import isidentifier
>>> isidentifier(u" 0\n 0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hy/_compat.py", line 47, in isidentifier
tokens = list(T.generate_tokens(StringIO(x).readline))
File "/usr/lib/python2.7/tokenize.py", line 374, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 2
0
^
IndentationError: unindent does not match any outer indentation level
2018-11-07 20:15:43 +01:00
|
|
|
|
2018-12-14 21:54:23 +01:00
|
|
|
|
|
|
|
(defn test-nongraphic []
|
|
|
|
; https://github.com/hylang/hy/issues/1694
|
|
|
|
|
|
|
|
(assert (= (mangle " ") "hyx_XspaceX"))
|
|
|
|
(assert (= (mangle "\a") "hyx_XU7X"))
|
|
|
|
(assert (= (mangle "\t") "hyx_XU9X"))
|
|
|
|
(assert (= (mangle "\n") "hyx_XUaX"))
|
|
|
|
(assert (= (mangle "\r") "hyx_XUdX"))
|
|
|
|
(assert (= (mangle "\r") "hyx_XUdX"))
|
|
|
|
|
|
|
|
(setv c (try unichr (except [NameError] chr)))
|
|
|
|
(assert (= (mangle (c 127)) "hyx_XU7fX"))
|
|
|
|
(assert (= (mangle (c 128)) "hyx_XU80X"))
|
|
|
|
(assert (= (mangle (c 0xa0)) "hyx_XnoHbreak_spaceX"))
|
|
|
|
(assert (= (mangle (c 0x378)) "hyx_XU378X"))
|
|
|
|
(assert (= (mangle (c 0x200a) "hyx_Xhair_spaceX")))
|
|
|
|
(assert (= (mangle (c 0x2065)) "hyx_XU2065X"))
|
|
|
|
(assert (= (mangle (c 0x1000c)) "hyx_XU1000cX")))
|
|
|
|
|
|
|
|
|
Catch IndentationError in isidentifier()
Fixes:
>>> from hy._compat import isidentifier
>>> isidentifier(u" 0\n 0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "hy/_compat.py", line 47, in isidentifier
tokens = list(T.generate_tokens(StringIO(x).readline))
File "/usr/lib/python2.7/tokenize.py", line 374, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 2
0
^
IndentationError: unindent does not match any outer indentation level
2018-11-07 20:15:43 +01:00
|
|
|
(defn test-mangle-bad-indent []
|
|
|
|
; Shouldn't crash with IndentationError
|
|
|
|
(mangle " 0\n 0"))
|