Hi Tim,

Example below. This is from a demo porting Java code to Clojure, the  
original Java code is in the Apache Commons [1]. Note that test does  
not resolve symbols:

; don't do this
(test 'index-of-any)
-> :no-test

; do this
(test #'index-of-any)
-> :ok

Cheers,
Stuart

[1] 
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=markup
----------------------
(ns exploring.index-of-any
     (use clojure.contrib.seq-utils))

(defn
   #^{:test (fn []
             (assert (nil? (index-of-any nil #{\a})))
             (assert (nil? (index-of-any "" #{\a})))
             (assert (nil? (index-of-any "foo" nil)))
             (assert (nil? (index-of-any "foo" #{})))
             (assert (zero? (index-of-any "zzabyycdxx" #{\z \a})))
             (assert (= 3 (index-of-any "zzabyycdxx" #{\b \y})))
             (assert (nil? (index-of-any "aba" #{\z}))))}
   index-of-any
   [str chars]
   (some (fn [[idx char]] (and (get chars char) idx)) (indexed str)))

>
> I've noticed around the place a few hints at testing capabilities such
> as
> (test v)
> and contrib having a run tests, but I'm at a bit of a loss as to how
> it fits together. If there is a good example I can take a look at
> please point me toward it.
>
> Generally what I've been doing is writting a test case at the bottom
> of my function and commenting it out when I see the correct output.
> But if there is a more formal definition I'd prefer to use that.
>
> (defn poly-expand [points]
>  (loop [aa (first points) remaining (rest points) built (empty
> points)]
>    (if (empty? remaining)
>      (concat built [aa (first points)])
>      (recur (first remaining) (rest remaining) (concat built [aa
> (first remaining)])))))
> ;(pr (poly-expand '(a b c d)))
> ;  -> (a b b c c d d a) ie: lines of polygon connected
>
> I imagine what I should be doing is somehow attaching a :test metadata
> which checks that a given input equates to a given output, I just need
> an example to follow. Coming from an imperitive background, I have to
> say that unit testing seems to be one of the stand out advantages to
> the functional approach. I can't even begin to imagine how to
> represent test cases for the C/C++ projects I've worked on hahahahaha,
> but it just feels like a natural part of the development cycle in
> clojure which is really great.
>
> Regards,
> Tim.
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to