Remove native tests of Python 2

This commit is contained in:
Kodi Arfer 2019-05-20 14:46:52 -04:00
parent 5c7441b011
commit ea872c3983
8 changed files with 43 additions and 93 deletions

View File

@ -1,7 +1,6 @@
(import (import
types types
pytest pytest)
[hy._compat [PY3]])
(defn test-comprehension-types [] (defn test-comprehension-types []
@ -134,8 +133,7 @@
; An `lfor` that gets compiled to a real comprehension ; An `lfor` that gets compiled to a real comprehension
(setv x 0) (setv x 0)
(assert (= (lfor x [1 2 3] (inc x)) [2 3 4])) (assert (= (lfor x [1 2 3] (inc x)) [2 3 4]))
(assert (= x (if PY3 0 3))) (assert (= x 0))
; Python 2 list comprehensions leak their variables.
; An `lfor` that gets compiled to a loop ; An `lfor` that gets compiled to a loop
(setv x 0 l []) (setv x 0 l [])

View File

@ -3,7 +3,7 @@
;; license. See the LICENSE. ;; license. See the LICENSE.
(import (import
[hy._compat [PY3 PY36 PY37]] [hy._compat [PY36 PY37]]
[math [isnan]] [math [isnan]]
[hy.contrib.hy-repr [hy-repr hy-repr-register]]) [hy.contrib.hy-repr [hy-repr hy-repr-register]])
@ -79,10 +79,10 @@
(assert (is (type (get orig 1)) float)) (assert (is (type (get orig 1)) float))
(assert (is (type (get result 1)) HyFloat))) (assert (is (type (get result 1)) HyFloat)))
(when PY3 (defn test-dict-views [] (defn test-dict-views []
(assert (= (hy-repr (.keys {1 2})) "(dict-keys [1])")) (assert (= (hy-repr (.keys {1 2})) "(dict-keys [1])"))
(assert (= (hy-repr (.values {1 2})) "(dict-values [2])")) (assert (= (hy-repr (.values {1 2})) "(dict-values [2])"))
(assert (= (hy-repr (.items {1 2})) "(dict-items [(, 1 2)])")))) (assert (= (hy-repr (.items {1 2})) "(dict-items [(, 1 2)])")))
(defn test-datetime [] (defn test-datetime []
(import [datetime :as D]) (import [datetime :as D])
@ -91,9 +91,8 @@
"(datetime.datetime 2009 1 15 15 27 5)")) "(datetime.datetime 2009 1 15 15 27 5)"))
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123)) (assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123))
"(datetime.datetime 2009 1 15 15 27 5 123)")) "(datetime.datetime 2009 1 15 15 27 5 123)"))
(when PY3 (assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123 :tzinfo D.timezone.utc))
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 123 :tzinfo D.timezone.utc)) "(datetime.datetime 2009 1 15 15 27 5 123 :tzinfo datetime.timezone.utc)"))
"(datetime.datetime 2009 1 15 15 27 5 123 :tzinfo datetime.timezone.utc)")))
(when PY36 (when PY36
(assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 :fold 1)) (assert (= (hy-repr (D.datetime 2009 1 15 15 27 5 :fold 1))
"(datetime.datetime 2009 1 15 15 27 5 :fold 1)")) "(datetime.datetime 2009 1 15 15 27 5 :fold 1)"))
@ -114,17 +113,11 @@
(defn test-collections [] (defn test-collections []
(import collections) (import collections)
(assert (= (hy-repr (collections.defaultdict :a 8)) (assert (= (hy-repr (collections.defaultdict :a 8))
(if PY3 "(defaultdict None {\"a\" 8})"))
"(defaultdict None {\"a\" 8})"
"(defaultdict None {b\"a\" 8})")))
(assert (= (hy-repr (collections.defaultdict int :a 8)) (assert (= (hy-repr (collections.defaultdict int :a 8))
(if PY3 "(defaultdict <class 'int'> {\"a\" 8})"))
"(defaultdict <class 'int'> {\"a\" 8})"
"(defaultdict <type 'int'> {b\"a\" 8})")))
(assert (= (hy-repr (collections.Counter [15 15 15 15])) (assert (= (hy-repr (collections.Counter [15 15 15 15]))
(if PY3 "(Counter {15 4})"))
"(Counter {15 4})"
"(Counter {15 (int 4)})")))
(setv C (collections.namedtuple "Fooey" ["cd" "a_b"])) (setv C (collections.namedtuple "Fooey" ["cd" "a_b"]))
(assert (= (hy-repr (C 11 12)) (assert (= (hy-repr (C 11 12))
"(Fooey :cd 11 :a_b 12)"))) "(Fooey :cd 11 :a_b 12)")))
@ -155,9 +148,8 @@
(setv mo (re.search "b+" "aaaabbbccc")) (setv mo (re.search "b+" "aaaabbbccc"))
(assert (= (hy-repr mo) (assert (= (hy-repr mo)
(.format (.format
#[[<{} object; :span {} :match "bbb">]] #[[<{} object; :span (, 4 7) :match "bbb">]]
(if PY37 "re.Match" (+ (. (type mo) __module__) ".SRE_Match")) (if PY37 "re.Match" (+ (. (type mo) __module__) ".SRE_Match"))))))
(if PY3 "(, 4 7)" "(, (int 4) (int 7))")))))
(defn test-hy-repr-custom [] (defn test-hy-repr-custom []

View File

@ -2,8 +2,6 @@
;; This file is part of Hy, which is free software licensed under the Expat ;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE. ;; license. See the LICENSE.
(import [hy._compat [PY3]])
;;;; some simple helpers ;;;; some simple helpers
(defn assert-true [x] (defn assert-true [x]
@ -429,8 +427,7 @@ result['y in globals'] = 'y' in globals()")
(assert-true (neg? -2)) (assert-true (neg? -2))
(assert-false (neg? 1)) (assert-false (neg? 1))
(assert-false (neg? 0)) (assert-false (neg? 0))
(when PY3 (assert-requires-num neg?))
(assert-requires-num neg?)))
(defn test-zero [] (defn test-zero []
"NATIVE: testing the zero? function" "NATIVE: testing the zero? function"
@ -519,8 +516,7 @@ result['y in globals'] = 'y' in globals()")
(assert-true (pos? 2)) (assert-true (pos? 2))
(assert-false (pos? -1)) (assert-false (pos? -1))
(assert-false (pos? 0)) (assert-false (pos? 0))
(when PY3 (assert-requires-num pos?))
(assert-requires-num pos?)))
(defn test-remove [] (defn test-remove []
"NATIVE: testing the remove function" "NATIVE: testing the remove function"

View File

@ -2,13 +2,12 @@
;; This file is part of Hy, which is free software licensed under the Expat ;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE. ;; license. See the LICENSE.
(import [hy.extra.reserved [names]] [hy._compat [PY3]]) (import [hy.extra.reserved [names]])
(defn test-reserved [] (defn test-reserved []
(assert (is (type (names)) frozenset)) (assert (is (type (names)) frozenset))
(assert (in "and" (names))) (assert (in "and" (names)))
(when PY3 (assert (in "False" (names)))
(assert (in "False" (names))))
(assert (in "pass" (names))) (assert (in "pass" (names)))
(assert (in "class" (names))) (assert (in "class" (names)))
(assert (in "defclass" (names))) (assert (in "defclass" (names)))

View File

@ -11,7 +11,7 @@
pytest) pytest)
(import sys) (import sys)
(import [hy._compat [PY3 PY37 PY38]]) (import [hy._compat [PY38]])
(defn test-sys-argv [] (defn test-sys-argv []
"NATIVE: test sys.argv" "NATIVE: test sys.argv"
@ -71,13 +71,12 @@
(except [e [SyntaxError]] (assert (in "Can't assign to" (str e))))) (except [e [SyntaxError]] (assert (in "Can't assign to" (str e)))))
(try (eval '(defn None [] (print "hello"))) (try (eval '(defn None [] (print "hello")))
(except [e [SyntaxError]] (assert (in "Can't assign to" (str e))))) (except [e [SyntaxError]] (assert (in "Can't assign to" (str e)))))
(when PY3 (try (eval '(setv False 1))
(try (eval '(setv False 1)) (except [e [SyntaxError]] (assert (in "Can't assign to" (str e)))))
(except [e [SyntaxError]] (assert (in "Can't assign to" (str e))))) (try (eval '(setv True 0))
(try (eval '(setv True 0)) (except [e [SyntaxError]] (assert (in "Can't assign to" (str e)))))
(except [e [SyntaxError]] (assert (in "Can't assign to" (str e))))) (try (eval '(defn True [] (print "hello")))
(try (eval '(defn True [] (print "hello"))) (except [e [SyntaxError]] (assert (in "Can't assign to" (str e))))))
(except [e [SyntaxError]] (assert (in "Can't assign to" (str e)))))))
(defn test-setv-pairs [] (defn test-setv-pairs []
@ -513,9 +512,7 @@
(setv passed False) (setv passed False)
(try (try
(raise) (raise)
;; Python 2 raises IndexError here (due to the previous test) (except [RuntimeError]
;; Python 3 raises RuntimeError
(except [[IndexError RuntimeError]]
(setv passed True))) (setv passed True)))
(assert passed) (assert passed)
@ -747,16 +744,11 @@
(defn test-yield-with-return [] (defn test-yield-with-return []
"NATIVE: test yield with return" "NATIVE: test yield with return"
(defn gen [] (yield 3) "goodbye") (defn gen [] (yield 3) "goodbye")
(if PY3 (setv gg (gen))
(do (setv gg (gen)) (assert (= 3 (next gg)))
(assert (= 3 (next gg))) (try (next gg)
(try (next gg) (except [e StopIteration] (assert (hasattr e "value"))
(except [e StopIteration] (assert (hasattr e "value")) (assert (= (getattr e "value") "goodbye")))))
(assert (= (getattr e "value") "goodbye")))))
(do (setv gg (gen))
(assert (= 3 (next gg)))
(try (next gg)
(except [e StopIteration] (assert (not (hasattr e "value"))))))))
(defn test-yield-in-try [] (defn test-yield-in-try []
@ -1242,19 +1234,14 @@ cee\"} dee" "ey bee\ncee dee"))
; Conversion characters and format specifiers ; Conversion characters and format specifiers
(setv p:9 "other") (setv p:9 "other")
(setv !r "bar") (setv !r "bar")
(defn u [s] (assert (= f"a{p !r}" "a'xyzzy'"))
; Add a "u" prefix for Python 2.
(if PY3
s
(.replace (.replace s "'" "u'" 1) " " " " 1)))
(assert (= f"a{p !r}" (u "a'xyzzy'")))
(assert (= f"a{p :9}" "axyzzy ")) (assert (= f"a{p :9}" "axyzzy "))
(assert (= f"a{p:9}" "aother")) (assert (= f"a{p:9}" "aother"))
(assert (= f"a{p !r :9}" (u "a'xyzzy' "))) (assert (= f"a{p !r :9}" "a'xyzzy' "))
(assert (= f"a{p !r:9}" (u "a'xyzzy' "))) (assert (= f"a{p !r:9}" "a'xyzzy' "))
(assert (= f"a{p:9 :9}" "aother ")) (assert (= f"a{p:9 :9}" "aother "))
(assert (= f"a{!r}" "abar")) (assert (= f"a{!r}" "abar"))
(assert (= f"a{!r !r}" (u "a'bar'"))) (assert (= f"a{!r !r}" "a'bar'"))
; Fun with `r` ; Fun with `r`
(assert (= f"hello {r\"\\n\"}" r"hello \n")) (assert (= f"hello {r\"\\n\"}" r"hello \n"))
@ -1278,7 +1265,7 @@ cee\"} dee" "ey bee\ncee dee"))
(assert (= f"{(C) : {(str (+ 1 1)) !r :x<5}}" "C[ '2'xx]")) (assert (= f"{(C) : {(str (+ 1 1)) !r :x<5}}" "C[ '2'xx]"))
; Format bracket strings ; Format bracket strings
(assert (= #[f[a{p !r :9}]f] (u "a'xyzzy' "))) (assert (= #[f[a{p !r :9}]f] "a'xyzzy' "))
(assert (= #[f-string[result: {value :{width}.{precision}}]f-string] (assert (= #[f-string[result: {value :{width}.{precision}}]f-string]
"result: 12.34"))) "result: 12.34")))
@ -1549,17 +1536,12 @@ cee\"} dee" "ey bee\ncee dee"))
(defn test-disassemble [] (defn test-disassemble []
"NATIVE: Test the disassemble function" "NATIVE: Test the disassemble function"
(assert (= (disassemble '(do (leaky) (leaky) (macros))) (cond (assert (= (disassemble '(do (leaky) (leaky) (macros)))
[PY3 (.format "Module( (.format "Module(
body=[Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])), body=[Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])), Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),
Expr(value=Call(func=Name(id='macros'), args=[], keywords=[]))]{})" Expr(value=Call(func=Name(id='macros'), args=[], keywords=[]))]{})"
(if PY38 ",\n type_ignores=[]" ""))] (if PY38 ",\n type_ignores=[]" ""))))
[True "Module(
body=[
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),
Expr(value=Call(func=Name(id='macros'), args=[], keywords=[], starargs=None, kwargs=None))])"])))
(assert (= (disassemble '(do (leaky) (leaky) (macros)) True) (assert (= (disassemble '(do (leaky) (leaky) (macros)) True)
"leaky() "leaky()
leaky() leaky()

View File

@ -3,9 +3,6 @@
;; license. See the LICENSE. ;; license. See the LICENSE.
(import [hy._compat [PY3]])
(defn test-hyphen [] (defn test-hyphen []
(setv a-b 1) (setv a-b 1)
(assert (= a-b 1)) (assert (= a-b 1))
@ -63,9 +60,7 @@
(defn test-higher-unicode [] (defn test-higher-unicode []
(setv 😂 "emoji") (setv 😂 "emoji")
(assert (= 😂 "emoji")) (assert (= 😂 "emoji"))
(if PY3 (assert (= hyx_Xface_with_tears_of_joyX "emoji")))
(assert (= hyx_Xface_with_tears_of_joyX "emoji"))
(assert (= hyx_XU1f602X "emoji"))))
(defn test-nameless-unicode [] (defn test-nameless-unicode []

View File

@ -2,8 +2,6 @@
;; This file is part of Hy, which is free software licensed under the Expat ;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE. ;; license. See the LICENSE.
(import [hy._compat [PY3]])
(setv square (fn [x] (setv square (fn [x]
(* x x))) (* x x)))
@ -191,20 +189,12 @@
(defn test-matmul [] (defn test-matmul []
"NATIVE: test matrix multiplication" "NATIVE: test matrix multiplication"
(if PY3 (assert (= (@ first-test-matrix second-test-matrix)
(assert (= (@ first-test-matrix second-test-matrix) product-of-test-matrices)))
product-of-test-matrices))
;; Python <= 3.4
(do
(setv matmul-attempt (try (@ first-test-matrix second-test-matrix)
(except [e [Exception]] e)))
(assert (isinstance matmul-attempt NameError)))))
(defn test-augassign-matmul [] (defn test-augassign-matmul []
"NATIVE: test augmented-assignment matrix multiplication" "NATIVE: test augmented-assignment matrix multiplication"
(setv matrix first-test-matrix (setv matrix first-test-matrix
matmul-attempt (try (@= matrix second-test-matrix) matmul-attempt (try (@= matrix second-test-matrix)
(except [e [Exception]] e))) (except [e [Exception]] e)))
(if PY3 (assert (= product-of-test-matrices matrix)))
(assert (= product-of-test-matrices matrix))
(assert (isinstance matmul-attempt NameError))))

View File

@ -2,8 +2,6 @@
;; This file is part of Hy, which is free software licensed under the Expat ;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE. ;; license. See the LICENSE.
(import pytest [hy._compat [PY3]])
(defmacro op-and-shadow-test [op &rest body] (defmacro op-and-shadow-test [op &rest body]
; Creates two tests with the given `body`, one where all occurrences ; Creates two tests with the given `body`, one where all occurrences
; of the symbol `f` are syntactically replaced with `op` (a test of ; of the symbol `f` are syntactically replaced with `op` (a test of
@ -102,14 +100,14 @@
(forbid (f 1 2 3))) (forbid (f 1 2 3)))
(when PY3 (op-and-shadow-test @ (op-and-shadow-test @
(defclass C [object] [ (defclass C [object] [
__init__ (fn [self content] (setv self.content content)) __init__ (fn [self content] (setv self.content content))
__matmul__ (fn [self other] (C (+ self.content other.content)))]) __matmul__ (fn [self other] (C (+ self.content other.content)))])
(forbid (f)) (forbid (f))
(assert (do (setv c (C "a")) (is (f c) c))) (assert (do (setv c (C "a")) (is (f c) c)))
(assert (= (. (f (C "b") (C "c")) content) "bc")) (assert (= (. (f (C "b") (C "c")) content) "bc"))
(assert (= (. (f (C "d") (C "e") (C "f")) content) "def")))) (assert (= (. (f (C "d") (C "e") (C "f")) content) "def")))
(op-and-shadow-test << (op-and-shadow-test <<