Skip to content

Commit

Permalink
Real tests; fix macro-expansion bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
llasram committed Apr 3, 2012
1 parent 54231d6 commit bc5ad57
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
10 changes: 3 additions & 7 deletions src/shady/defclass.clj
Expand Up @@ -3,11 +3,6 @@
producing `gen-class`-backed JVM interop classes."
(:use [shady.gen-class :only [generate-class]]))

(defmacro ^:private prog1
"Evaluates the result of expr, then evaluates the forms in body (presumably
for side-effects), then returns the result of expr."
[expr & body] `(let [value# ~expr] ~@body value#))

(defn- class-name
[sym] (.getName ^Class (resolve sym)))

Expand Down Expand Up @@ -112,6 +107,7 @@ provide an initialization function."
:methods methods
:prefix prefix)]
(generate-class opts)
`(prog1 (import ~(symbol pqname))
`(let [result# (import ~(symbol pqname))]
~@(map (partial defn-bodies prefix fields state init-name)
(apply concat (vals specs))))))
(apply concat (vals specs)))
result#)))
68 changes: 68 additions & 0 deletions test/shady/defclass_test.clj
@@ -0,0 +1,68 @@
(ns shady.defclass-test
(:use [clojure.test])
(:use [shady.defclass :only [defclass]]))

(defclass Definition []
(exampleMethod [this] :example))

(deftest test-definition
(testing "defclass can define classes"
(is (= :example (.exampleMethod (Definition.))))))


(defclass Redefinition []
(exampleMethod [this] :first))

(defclass Redefinition []
(exampleMethod [this] :second))

(deftest test-redefinition
(testing "defclass can re-define classes"
(is (= :second (.exampleMethod (Redefinition.))))))


(defclass ExtendBase []
(exampleMethod [this] :base))

(defclass ExtendSubclass []
:extends ExtendBase
ExtendBase
(exampleMethod [this] :subclass))

(deftest test-extend
(testing "defclass can extend a superclass"
(is (= :base (.exampleMethod (ExtendBase.))))
(is (= :subclass (.exampleMethod (ExtendSubclass.))))
(is (instance? ExtendBase (ExtendSubclass.)))))


(defclass InitDefault []
:state datum
:constructors {[Object] []}
(-init [datum] [[] datum]))

(defclass InitCustom []
:state datum
:constructors {[Object] []}
:init -init-datum
(-init-datum [datum] [[] datum]))

(deftest test-init
(testing "defclass can use an initializer function"
(is (= :default (.datum (InitDefault. :default))) "with the default name")
(is (= :custom (.datum (InitDefault. :custom))) "with a custom name")))


(defclass Fields [field-a field-b]
:state state
:constructors {[Object Object] []}
(-init [field-a field-b] [[] [field-a field-b]])
(fieldA [this] field-a)
(fieldB [this] field-b))

(deftest test-init
(testing "Field values are locally accessible in methods"
(let [obj (Fields. :left :right)]
(is (= :left (.fieldA obj)))
(is (= :right (.fieldB obj))))))

7 changes: 0 additions & 7 deletions test/shady/test/defclass.clj

This file was deleted.

0 comments on commit bc5ad57

Please sign in to comment.