rlm@10
|
1 ;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and
|
rlm@10
|
2 ;; distribution terms for this software are covered by the Eclipse Public
|
rlm@10
|
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
|
rlm@10
|
4 ;; be found in the file epl-v10.html at the root of this distribution. By
|
rlm@10
|
5 ;; using this software in any fashion, you are agreeing to be bound by the
|
rlm@10
|
6 ;; terms of this license. You must not remove this notice, or any other,
|
rlm@10
|
7 ;; from this software.
|
rlm@10
|
8 ;;
|
rlm@10
|
9 ;; test-dataflow
|
rlm@10
|
10 ;;
|
rlm@10
|
11 ;; A Library to Support a Dataflow Model of State - Tests
|
rlm@10
|
12 ;;
|
rlm@10
|
13 ;; straszheimjeffrey (gmail)
|
rlm@10
|
14 ;; Created 11 March 2009
|
rlm@10
|
15
|
rlm@10
|
16
|
rlm@10
|
17 (ns clojure.contrib.test-dataflow
|
rlm@10
|
18 (:use clojure.test)
|
rlm@10
|
19 (:use clojure.contrib.dataflow))
|
rlm@10
|
20
|
rlm@10
|
21 (def df-1
|
rlm@10
|
22 (build-dataflow
|
rlm@10
|
23 [(cell :source base 0)
|
rlm@10
|
24 (cell :source items ())
|
rlm@10
|
25 (cell product (* ?base (apply + ?items)))
|
rlm@10
|
26 (cell :validator (when (number? ?-product)
|
rlm@10
|
27 (assert (>= ?product ?-product))))]))
|
rlm@10
|
28
|
rlm@10
|
29 (deftest test-df-1
|
rlm@10
|
30 (is (= (get-value df-1 'product) 0))
|
rlm@10
|
31 (is (do (update-values df-1 {'items [4 5]})
|
rlm@10
|
32 (= (get-value df-1 'product) 0)))
|
rlm@10
|
33 (is (do (update-values df-1 {'base 2})
|
rlm@10
|
34 (= (get-value df-1 'product) 18)))
|
rlm@10
|
35 (is (thrown? AssertionError (update-values df-1 {'base 0})))
|
rlm@10
|
36 (is (= (get-value df-1 'product) 18)))
|
rlm@10
|
37
|
rlm@10
|
38 (def df-2
|
rlm@10
|
39 (build-dataflow
|
rlm@10
|
40 [(cell :source strength 10)
|
rlm@10
|
41 (cell :source agility 10)
|
rlm@10
|
42 (cell :source magic 10)
|
rlm@10
|
43
|
rlm@10
|
44 (cell total-cost (apply + ?*cost))
|
rlm@10
|
45
|
rlm@10
|
46 (cell cost (- ?strength 10))
|
rlm@10
|
47 (cell cost (- ?agility 10))
|
rlm@10
|
48 (cell cost (- ?magic 10))
|
rlm@10
|
49
|
rlm@10
|
50 (cell combat (+ ?strength ?agility ?combat-mod))
|
rlm@10
|
51 (cell speed (+ ?agility (/ ?strength 10.0) ?speed-mod))
|
rlm@10
|
52 (cell casting (+ ?agility ?magic ?magic-mod))
|
rlm@10
|
53
|
rlm@10
|
54 (cell combat-mod (apply + ?*combat-mods))
|
rlm@10
|
55 (cell speed-mod (apply + ?*speed-mods))
|
rlm@10
|
56 (cell magic-mod (apply + ?*magic-mods))]))
|
rlm@10
|
57
|
rlm@10
|
58 (def magic-skill
|
rlm@10
|
59 [(cell cost 5)
|
rlm@10
|
60 (cell speed-mods 1)
|
rlm@10
|
61 (cell magic-mods 2)])
|
rlm@10
|
62
|
rlm@10
|
63 (defn gv [n] (get-value df-2 n))
|
rlm@10
|
64
|
rlm@10
|
65 (deftest test-df-2
|
rlm@10
|
66 (is (and (= (gv 'total-cost) 0)
|
rlm@10
|
67 (= (gv 'strength) 10)
|
rlm@10
|
68 (= (gv 'casting) 20)))
|
rlm@10
|
69 (is (do (update-values df-2 {'magic 12})
|
rlm@10
|
70 (and (= (gv 'total-cost) 2)
|
rlm@10
|
71 (= (gv 'casting) 22))))
|
rlm@10
|
72 (is (do (add-cells df-2 magic-skill)
|
rlm@10
|
73 (and (= (gv 'total-cost) 7)
|
rlm@10
|
74 (= (gv 'casting) 24))))
|
rlm@10
|
75 (is (do (remove-cells df-2 magic-skill)
|
rlm@10
|
76 (and (= (gv 'total-cost) 2)
|
rlm@10
|
77 (= (gv 'casting) 22)))))
|
rlm@10
|
78
|
rlm@10
|
79
|
rlm@10
|
80 (comment
|
rlm@10
|
81 (run-tests)
|
rlm@10
|
82
|
rlm@10
|
83 (use :reload 'clojure.contrib.dataflow)
|
rlm@10
|
84 (use 'clojure.contrib.stacktrace) (e)
|
rlm@10
|
85 (use 'clojure.contrib.trace)
|
rlm@10
|
86
|
rlm@10
|
87 )
|
rlm@10
|
88
|
rlm@10
|
89
|
rlm@10
|
90 ;; End of file
|