diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-07 17:31:58 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-16 15:35:50 -0400 |
commit | 166b94c666e30c04b6df7cbf95376d1dbee0cbe5 (patch) | |
tree | 1e15cc5ca324f468cc4ca64cf2fb9fc15c991a2f | |
parent | 86eab09c6c16c66a6add465a1d8f964764c612ad (diff) |
more protocol tests
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | build.xml | 4 | ||||
-rw-r--r-- | test/clojure/test_clojure/protocols.clj | 34 | ||||
-rw-r--r-- | test/clojure/test_clojure/protocols/examples.clj | 2 | ||||
-rw-r--r-- | test/clojure/test_clojure/protocols/more_examples.clj | 7 |
4 files changed, 39 insertions, 8 deletions
@@ -108,6 +108,7 @@ <target name="compile-tests" description="Compile the subset of tests that require compilation."> + <delete dir="${test-classes}"/> <mkdir dir="${test-classes}"/> <java classname="clojure.lang.Compile" classpath="${test-classes}:${test}:${build}:${cljsrc}" @@ -133,7 +134,8 @@ </target> <target name="test" - description="Run clojure tests"> + description="Run clojure tests" + depends="compile-tests"> <!-- depends="clojure"> --> <java classname="clojure.main" failonerror="true"> <classpath> diff --git a/test/clojure/test_clojure/protocols.clj b/test/clojure/test_clojure/protocols.clj index 8b5adc71..78905af4 100644 --- a/test/clojure/test_clojure/protocols.clj +++ b/test/clojure/test_clojure/protocols.clj @@ -9,7 +9,8 @@ ; Author: Stuart Halloway (ns clojure.test-clojure.protocols - (:use clojure.test clojure.test-clojure.protocols.examples)) + (:use clojure.test clojure.test-clojure.protocols.examples) + (:require [clojure.test-clojure.protocols.more-examples :as other])) (defn method-names "return sorted list of method names on a class" @@ -22,19 +23,40 @@ (testing "protocol fns throw IllegalArgumentException if no impl matches" (is (thrown-with-msg? IllegalArgumentException - #"No implementation of method: :foo of protocol: #'clojure.test-clojure.protocols.examples/AnExampleProtocol found for class: nil" - (foo nil)))) + #"No implementation of method: :foo of protocol: #'clojure.test-clojure.protocols.examples/ExampleProtocol found for class: java.lang.Integer" + (foo 10)))) (testing "protocols generate a corresponding interface using _ instead of - for method names" - (is (= ["bar" "baz" "baz" "foo" "with_quux"] (method-names clojure.test_clojure.protocols.examples.AnExampleProtocol)))) + (is (= ["bar" "baz" "baz" "foo" "with_quux"] (method-names clojure.test_clojure.protocols.examples.ExampleProtocol)))) (testing "protocol will work with instances of its interface (use for interop, not in Clojure!)" - (let [obj (proxy [clojure.test_clojure.protocols.examples.AnExampleProtocol] [] + (let [obj (proxy [clojure.test_clojure.protocols.examples.ExampleProtocol] [] (foo [] "foo!"))] (is (= "foo!" (.foo obj)) "call through interface") (is (= "foo!" (foo obj)) "call through protocol"))) (testing "you can implement just part of a protocol if you want" - (let [obj (reify AnExampleProtocol + (let [obj (reify ExampleProtocol (baz [a b] "two-arg baz!"))] (is (= "two-arg baz!" (baz obj nil))) (is (thrown? AbstractMethodError (baz obj)))))) +(deftest extend-test + (testing "you can extend a protocol to a class" + (extend String ExampleProtocol + {:foo identity}) + (is (= "pow" (foo "pow")))) + (testing "you can have two methods with the same name. Just use namespaces!" + (extend String other/SimpleProtocol + {:foo (fn [s] (.toUpperCase s))}) + (is (= "POW" (other/foo "pow")))) + (testing "you can extend deftype types" + (deftype Widget [name]) + (extend + ::Widget + ExampleProtocol + {:foo (fn [this] (str "widget " (:name this)))}) + (is (= "widget z" (foo (Widget "z")))))) + + + +;; what happens if you extend after implementing directly? +;; todo: investigate how nil-handling changes error handling diff --git a/test/clojure/test_clojure/protocols/examples.clj b/test/clojure/test_clojure/protocols/examples.clj index 8104a26e..f0296955 100644 --- a/test/clojure/test_clojure/protocols/examples.clj +++ b/test/clojure/test_clojure/protocols/examples.clj @@ -1,6 +1,6 @@ (ns clojure.test-clojure.protocols.examples) -(defprotocol AnExampleProtocol +(defprotocol ExampleProtocol "example protocol used by clojure tests" (foo [a] "method with one arg") diff --git a/test/clojure/test_clojure/protocols/more_examples.clj b/test/clojure/test_clojure/protocols/more_examples.clj new file mode 100644 index 00000000..6bee018a --- /dev/null +++ b/test/clojure/test_clojure/protocols/more_examples.clj @@ -0,0 +1,7 @@ +(ns clojure.test-clojure.protocols.more-examples) + +(defprotocol SimpleProtocol + "example protocol used by clojure tests. Note that + foo collides with examples/ExampleProtocol." + + (foo [a] "")) |