2013-04-24 22:18:05 +02:00
|
|
|
(defn test-defclass []
|
|
|
|
"NATIVE: test defclass simple mechanism"
|
|
|
|
(defclass A)
|
|
|
|
(assert (isinstance (A) A)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-defclass-inheritance []
|
|
|
|
"NATIVE: test defclass inheritance"
|
|
|
|
(defclass A [])
|
|
|
|
(assert (isinstance (A) object))
|
|
|
|
(defclass A [object])
|
|
|
|
(assert (isinstance (A) object))
|
|
|
|
(defclass B [A])
|
|
|
|
(assert (isinstance (B) A))
|
|
|
|
(defclass C [object])
|
|
|
|
(defclass D [B C])
|
|
|
|
(assert (isinstance (D) A))
|
|
|
|
(assert (isinstance (D) B))
|
|
|
|
(assert (isinstance (D) C))
|
|
|
|
(assert (not (isinstance (A) D))))
|
|
|
|
|
|
|
|
|
2014-09-04 00:04:05 +02:00
|
|
|
(defn test-defclass-attrs []
|
|
|
|
"NATIVE: test defclass attributes"
|
2013-04-24 22:18:05 +02:00
|
|
|
(defclass A []
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 42])
|
2013-04-24 22:18:05 +02:00
|
|
|
(assert (= A.x 42))
|
|
|
|
(assert (= (getattr (A) "x") 42)))
|
|
|
|
|
|
|
|
|
2014-09-04 00:04:05 +02:00
|
|
|
(defn test-defclass-attrs-fn []
|
|
|
|
"NATIVE: test defclass attributes with fn"
|
2013-04-24 22:18:05 +02:00
|
|
|
(defclass B []
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 42
|
|
|
|
y (fn [self value]
|
|
|
|
(+ self.x value))])
|
2013-04-24 22:18:05 +02:00
|
|
|
(assert (= B.x 42))
|
|
|
|
(assert (= (.y (B) 5) 47))
|
2015-08-17 09:07:32 +02:00
|
|
|
(let [b (B)]
|
2013-04-24 22:18:05 +02:00
|
|
|
(setv B.x 0)
|
|
|
|
(assert (= (.y b 1) 1))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-defclass-dynamic-inheritance []
|
|
|
|
"NATIVE: test defclass with dynamic inheritance"
|
2016-11-24 03:35:17 +01:00
|
|
|
(defclass A [((fn [] (if True list dict)))]
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 42])
|
2013-04-24 22:18:05 +02:00
|
|
|
(assert (isinstance (A) list))
|
2016-11-24 03:35:17 +01:00
|
|
|
(defclass A [((fn [] (if False list dict)))]
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 42])
|
2013-04-24 22:18:05 +02:00
|
|
|
(assert (isinstance (A) dict)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn test-defclass-no-fn-leak []
|
2014-09-04 00:04:05 +02:00
|
|
|
"NATIVE: test defclass attributes with fn"
|
2013-04-24 22:18:05 +02:00
|
|
|
(defclass A []
|
2015-08-04 16:43:07 +02:00
|
|
|
[x (fn [] 1)])
|
2013-04-24 22:18:05 +02:00
|
|
|
(try
|
|
|
|
(do
|
|
|
|
(x)
|
2016-11-24 03:35:17 +01:00
|
|
|
(assert False))
|
2013-04-24 22:18:05 +02:00
|
|
|
(except [NameError])))
|
2013-07-15 01:25:57 +02:00
|
|
|
|
|
|
|
(defn test-defclass-docstring []
|
|
|
|
"NATIVE: test defclass docstring"
|
|
|
|
(defclass A []
|
2015-08-04 16:43:07 +02:00
|
|
|
[--doc-- "doc string"
|
|
|
|
x 1])
|
2013-07-15 01:25:57 +02:00
|
|
|
(setv a (A))
|
|
|
|
(assert (= a.__doc__ "doc string"))
|
|
|
|
(defclass B []
|
|
|
|
"doc string"
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 1])
|
2013-07-15 01:25:57 +02:00
|
|
|
(setv b (B))
|
|
|
|
(assert (= b.x 1))
|
|
|
|
(assert (= b.__doc__ "doc string"))
|
|
|
|
(defclass MultiLine []
|
|
|
|
"begin a very long multi-line string to make
|
|
|
|
sure that it comes out the way we hope
|
|
|
|
and can span 3 lines end."
|
2015-08-04 16:43:07 +02:00
|
|
|
[x 1])
|
2013-07-15 01:25:57 +02:00
|
|
|
(setv mL (MultiLine))
|
|
|
|
(assert (= mL.x 1))
|
|
|
|
(assert (in "begin" mL.__doc__))
|
|
|
|
(assert (in "end" mL.__doc__)))
|
2014-12-07 02:51:01 +01:00
|
|
|
|
|
|
|
(defn test-defclass-macroexpand []
|
|
|
|
"NATIVE: test defclass with macro expand"
|
2015-08-04 16:43:07 +02:00
|
|
|
(defmacro M [] `(defn x [self x] (setv self._x x)))
|
|
|
|
(defclass A [] (M))
|
2014-12-07 02:51:01 +01:00
|
|
|
(setv a (A))
|
|
|
|
(a.x 1)
|
|
|
|
(assert (= a._x 1)))
|
2015-08-04 16:43:07 +02:00
|
|
|
|
|
|
|
(defn test-defclass-syntax []
|
|
|
|
"NATIVE: test defclass syntax with properties and methods and side-effects"
|
|
|
|
(setv foo 1)
|
|
|
|
(defclass A []
|
|
|
|
[x 1
|
|
|
|
y 2]
|
|
|
|
(global foo)
|
|
|
|
(setv foo 2)
|
|
|
|
(defn greet [self]
|
|
|
|
"Greet the caller"
|
|
|
|
|
|
|
|
"hello!"))
|
|
|
|
(setv a (A))
|
|
|
|
(assert (= a.x 1))
|
|
|
|
(assert (= a.y 2))
|
|
|
|
(assert foo 2)
|
|
|
|
(assert (.greet a) "hello"))
|
2015-08-10 10:58:13 +02:00
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
(defn test-defclass-implicit-none-for-init []
|
|
|
|
"NATIVE: test that defclass adds an implicit None to --init--"
|
2015-08-10 10:58:13 +02:00
|
|
|
(defclass A []
|
|
|
|
[--init-- (fn [self] (setv self.x 1) 42)])
|
|
|
|
(defclass B []
|
|
|
|
(defn --init-- [self]
|
|
|
|
(setv self.x 2)
|
|
|
|
42))
|
|
|
|
|
|
|
|
(setv a (A))
|
|
|
|
(setv b (B))
|
|
|
|
(assert (= a.x 1))
|
|
|
|
(assert (= b.x 2)))
|