|
1 | 1 | (ns tests.basilisp.test-defrecord |
2 | 2 | (:import abc) |
3 | 3 | (:require |
4 | | - [basilisp.test :refer [deftest is testing]])) |
| 4 | + [basilisp.test :refer [deftest is testing are]])) |
5 | 5 |
|
6 | 6 | (deftype Square [dim] |
7 | 7 | (__eq__ [this other] |
8 | 8 | (and (instance? Square other) |
9 | 9 | (= dim (.-dim other))))) |
10 | 10 |
|
| 11 | +(deftype InvokeConstructorsType [] |
| 12 | + (__call__ [this ctor] |
| 13 | + (case ctor |
| 14 | + :positional (->InvokeConstructorsType) |
| 15 | + :dot (InvokeConstructorsType.)))) |
| 16 | + |
11 | 17 | (deftest deftype-constructors |
12 | 18 | (let [elems [(tests.basilisp.test-defrecord.Square. 1) |
13 | 19 | (Square. 1) |
14 | 20 | (new tests.basilisp.test-defrecord.Square 1) |
15 | 21 | (new Square 1) |
16 | 22 | (tests.basilisp.test-defrecord/Square 1)]] |
17 | | - (is (apply = elems)))) |
| 23 | + (is (apply = elems))) |
| 24 | + (testing "can invoke own constructors" |
| 25 | + (let [invoke (->InvokeConstructorsType)] |
| 26 | + (are [k] (instance? InvokeConstructorsType (invoke k)) |
| 27 | + :positional |
| 28 | + :dot)))) |
18 | 29 |
|
19 | 30 | (deftest deftype-reader-form |
20 | 31 | (testing "type" |
|
184 | 195 | (area [self] |
185 | 196 | (* 3.14 radius radius))) |
186 | 197 |
|
| 198 | +(defrecord InvokeConstructorsRecord [] |
| 199 | + (__call__ [self ctor] |
| 200 | + (case ctor |
| 201 | + :map (map->InvokeConstructorsRecord self) |
| 202 | + :positional (->InvokeConstructorsRecord) |
| 203 | + :dot (InvokeConstructorsRecord.)))) |
| 204 | + |
187 | 205 | (deftest defrecord-constructors |
188 | 206 | (let [elems [(tests.basilisp.test-defrecord.Circle. 1) |
189 | 207 | (Circle. 1) |
190 | 208 | (new tests.basilisp.test-defrecord.Circle 1) |
191 | 209 | (new Circle 1) |
192 | 210 | (tests.basilisp.test-defrecord/Circle 1)]] |
193 | | - (is (apply = elems)))) |
| 211 | + (is (apply = elems))) |
| 212 | + (testing "can invoke own constructors" |
| 213 | + (let [invoke (->InvokeConstructorsRecord)] |
| 214 | + (are [k] (instance? InvokeConstructorsRecord (invoke k)) |
| 215 | + :map |
| 216 | + :positional |
| 217 | + :dot)))) |
194 | 218 |
|
195 | 219 | (deftest defrecord-with-methods |
196 | 220 | (let [c (->Circle 1) |
|
0 commit comments