Skip to content
Newer
Older
100644 456 lines (414 sloc) 14.5 KB
810d9ae @daveray Pulled out general impl of hooking a range model to an atom.
authored May 31, 2011
1 (ns seesaw.test.bind
047facd @daveray Added seesaw.bind/filter.
authored Dec 18, 2011
2 (:refer-clojure :exclude [some filter])
840255b @daveray Bindable selection
authored Jul 11, 2011
3 (:require [seesaw.core :as ssc])
4 (:use seesaw.bind)
810d9ae @daveray Pulled out general impl of hooking a range model to an atom.
authored Jun 1, 2011
5 (:use [lazytest.describe :only (describe it testing)]
6 [lazytest.expect :only (expect)]))
7
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 8, 2011
8 (describe bind
c0a6f31 @daveray Reworking some binding stuff. Support for composites and tee.
authored Jul 9, 2011
9 (it "returns a composite bindable"
2c418b8 @daveray Discovered and fixed massive bug in bind. Subscribers of the returned…
authored Jan 10, 2012
10 (let [a (atom 0) b (atom 1) c (atom 2) d (atom 3)
11 cb (bind a b c d)
12 called (atom nil) ]
13 (expect (satisfies? Bindable cb))
14
15 ; make sure that subscribing to the composite subscribes
16 ; to the *end* of the chain!
17 (subscribe cb (fn [v] (reset! called v)))
18 (reset! d 10)
19 (expect (= 10 @called))))
c0a6f31 @daveray Reworking some binding stuff. Support for composites and tee.
authored Jul 10, 2011
20
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
21 (it "can chain bindables together"
22 (let [a (atom 1)
23 b (atom nil)]
24 (bind a (transform + 5) b)
25 (reset! a 5)
26 (expect (= 10 @b))))
1f6b01d @daveray bind now returns a callable object which will reverse the effects of …
authored Jan 10, 2012
27
28 (it "returns something function-like that can be called to undo the binding"
29 (let [a (atom 0) b (atom 1) c (atom 2)
30 cb (bind a b c)]
31 (reset! a 5)
32 (expect (= [5 5 5] [@a @b @c]))
33 (cb)
34 (reset! a 6)
35 (expect (= [6 5 5] [@a @b @c]))))
c0a6f31 @daveray Reworking some binding stuff. Support for composites and tee.
authored Jul 10, 2011
36
37 (it "can chain bindables, including composites, together"
38 (let [a (atom 1)
39 b (atom nil)]
40 (bind a (bind (transform + 5) (transform * 2) (atom nil)) b)
41 (reset! a 2)
42 (expect (= 14 @b))))
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
43
44 (it "should sync the enabled? property of a widget with an atom"
45 (let [v (atom true)
840255b @daveray Bindable selection
authored Jul 12, 2011
46 b (ssc/button)]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
47 (bind (property b :enabled?) v)
840255b @daveray Bindable selection
authored Jul 12, 2011
48 (ssc/config! b :enabled? false)
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
49 (expect (not @v))
50 (reset! v true)))
51
52 (it "should sync the enabled? property of a widget with an atom"
53 (let [v (atom true)
840255b @daveray Bindable selection
authored Jul 12, 2011
54 b (ssc/button)]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
55 (bind (property b :enabled?) v)
840255b @daveray Bindable selection
authored Jul 12, 2011
56 (ssc/config! b :enabled? false)
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
57 (expect (not @v))))
58
59 (it "should sync an atom to the enabled? property of a widget"
60 (let [v (atom true)
840255b @daveray Bindable selection
authored Jul 12, 2011
61 b (ssc/button)]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
62 (bind v (property b :enabled?))
63 (reset! v false)
64 (expect (not (.isEnabled b)))))
65
66 (testing "with a BoundedRangeModel"
67 (it "Updates an atom when the model changes"
68 (let [a (atom -1)
69 m (javax.swing.DefaultBoundedRangeModel. 50 0 2 100)]
70 (bind m a)
71 (.setValue m 51)
72 (expect (= 51 @a))))
73 (it "Updates the model when the atom changes"
74 (let [a (atom -1)
75 m (javax.swing.DefaultBoundedRangeModel. 50 0 2 100)]
76 (bind a m)
77 (reset! a 99)
78 (expect (= 99 (.getValue m))))))
79
80 (testing "given a text field"
81 (it "should update an atom when the underlying document changes"
82 (let [a (atom nil)
840255b @daveray Bindable selection
authored Jul 12, 2011
83 t (ssc/text "initial")]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
84 (bind (.getDocument t) a)
840255b @daveray Bindable selection
authored Jul 12, 2011
85 (ssc/text! t "foo")
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
86 (expect (= "foo" @a))))
87
88 (it "should update the underlying document when the atom changes"
89 (let [a (atom "initial")
840255b @daveray Bindable selection
authored Jul 12, 2011
90 t (ssc/text "")]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
91 (bind a (.getDocument t))
92 (reset! a "foo")
840255b @daveray Bindable selection
authored Jul 12, 2011
93 (expect (= "foo" (ssc/text t))))))
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
94
95 (testing "given a slider"
96 (it "should sync the value of the atom with the slider value, if slider value changed"
97 (let [v (atom 15)
840255b @daveray Bindable selection
authored Jul 12, 2011
98 sl (ssc/slider :value @v)]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
99 (bind (.getModel sl) v)
100 (.setValue sl 20)
101 (expect (= @v 20))))
102 (it "should sync the value of the slider with the atom value, if atom value changed"
103 (let [v (atom 15)
840255b @daveray Bindable selection
authored Jul 12, 2011
104 sl (ssc/slider :value @v)]
c425cbd @daveray Ripped out all the automatic atom syncing stuff for properties in fav…
authored Jul 9, 2011
105 (bind v (.getModel sl))
106 (reset! v 20)
433a43d @daveray Fix for issue #71. Bind support for sliders and slider models.
authored Dec 15, 2011
107 (expect (= (.getValue sl) 20)))))
6dad712 @daveray Extend to-bindable* to toggle button and friends. Thanks to Jeff Rose…
authored Dec 24, 2011
108
109 (testing "given a toggle button (or any button/menu)"
110 (it "should sync the selection state of the button"
111 (let [v (atom nil)
112 b (ssc/toggle :selected? false)]
113 (bind b v)
114 (.setSelected b true)
115 (expect @v)))
116
117 (it "should sync the selection state of the button"
118 (let [v (atom nil)
119 b (ssc/toggle :selected? false)]
120 (bind v b)
121 (reset! v true)
122 (expect (.isSelected b)))))
123
0fa0f0c @daveray Test for bindable combobox
authored Jan 17, 2012
124 (testing "given a combobox"
125 (it "should sync the selection state of the combobox"
126 (let [v (atom nil)
127 b (ssc/combobox :model [1 2 3 4])]
128 (bind b v)
129 (ssc/selection! b 2)
130 (expect (= 2 @v))))
131
132 (it "should sync the selection state of the combobox"
133 (let [v (atom nil)
134 b (ssc/combobox :model [1 2 3 4])]
135 (bind v b)
136 (reset! v 4)
137 (expect (= 4 (ssc/selection b))))))
138
433a43d @daveray Fix for issue #71. Bind support for sliders and slider models.
authored Dec 16, 2011
139 (testing "given a spinner"
140 (it "should sync the value of the atom with the spinner value, if spinner value changed"
141 (let [v (atom 15)
142 sl (ssc/spinner :model @v)]
143 (bind (.getModel sl) v)
144 (.setValue sl 20)
145 (expect (= @v 20))))
146 (it "should sync the value of the spinner with the atom value, if atom value changed"
147 (let [v (atom 15)
148 sl (ssc/spinner :model @v)]
149 (bind v (.getModel sl))
150 (reset! v 20)
135b147 @daveray Implemented binding for agents.
authored Oct 20, 2011
151 (expect (= (.getValue sl) 20)))))
152
153 (testing "given an agent"
154
155 (it "should pass along changes to the agent's value"
156 (let [start (agent nil)
157 end (atom nil)]
158 (bind start end)
159 (send start (constantly :called))
160 (await start)
161 (expect (= :called @start))
162 (expect (= :called @end))))
163
164 (it "should throw an exception if you try to notify an agent"
165 (let [start (atom nil)]
166 (bind start (agent nil))
167 (expect (try
168 (reset! start 99)
169 false
9a92d82 @daveray Got all tests passing on Clojure 1.3
authored Oct 25, 2011
170 ; In Clojure 1.3, the exception propagates correctly
171 (catch IllegalStateException e
172 true)
173 ; Unfortunately, in Clojure 1.2, IllegalStateException gets wrapped by reset!
135b147 @daveray Implemented binding for agents.
authored Oct 21, 2011
174 (catch RuntimeException e
175 (= IllegalStateException (class (.getCause e))))))))))
810d9ae @daveray Pulled out general impl of hooking a range model to an atom.
authored Jun 1, 2011
176
e3b2826 @daveray Implemented b-do* and b-do macro. seesaw.bind equivalenet of (do).
authored Nov 20, 2011
177 (describe b-do*
178 (it "executes a function with a single argument and ends a chain"
179 (let [start (atom 0)
180 called (atom nil) ]
181 (bind start (b-do* #(reset! called %)))
182 (reset! start 5)
183 (expect (= 5 @called)))))
184
185 (describe b-do
186 (it "executes body with a single argument and ends a chain"
187 (let [start (atom [1 2])
188 called (atom nil)]
189 (bind start (b-do [[a b]] (reset! called (+ a b))))
190 (reset! start [3 4])
191 (expect (= 7 @called)))))
192
c0a6f31 @daveray Reworking some binding stuff. Support for composites and tee.
authored Jul 10, 2011
193 (describe tee
194 (it "creates a tee junction in a bind"
195 (let [start (atom 0)
196 end1 (atom 0)
197 end2 (atom 0)]
198 (bind start (tee (bind (transform * 2) end1)
199 (bind (transform * 4) end2)))
200 (reset! start 5)
201 (expect (= 10 @end1))
202 (expect (= 20 @end2)))))
f6ec8bf @daveray Auto-convert widgets to bindable when it makes sense.
authored Jul 11, 2011
203
eab36a1 @daveray test for bind/funnel
authored Aug 25, 2011
204 (describe funnel
205 (it "create a funnel in a bind which listens to multiple source and produces a vector of values"
206 (let [a (atom 0)
207 b (atom 1)
208 f (funnel a b)
209 end (atom nil)]
210 (bind f end)
211 (reset! a 5)
212 (expect (= [5 nil] @end))
213 (reset! b 6)
214 (expect (= [5 6] @end)))))
215
047facd @daveray Added seesaw.bind/filter.
authored Dec 19, 2011
216 (describe filter
217 (it "doesn't pass along value when predicate returns falsey"
218 (let [start (atom :foo)
219 end (atom :bar)]
220 (bind start (filter (constantly false)) end)
221 (reset! start :something)
222 (expect (= :bar @end))))
223 (it "passes along value when predicate returns truthy"
224 (let [start (atom :foo)
225 end (atom :bar)]
226 (bind start (filter (constantly true)) end)
227 (reset! start :something)
228 (expect (= :something @end)))))
229
840255b @daveray Bindable selection
authored Jul 12, 2011
230 (describe some
231 (it "doesn't pass along falsey values returned by the predicate"
232 (let [start (atom :foo)
233 end (atom :bar)]
234 (bind start (some (constantly nil)) end)
235 (reset! start :something)
236 (expect (= :bar @end))))
047facd @daveray Added seesaw.bind/filter.
authored Dec 19, 2011
237 (it "passes along result of predicate when it returns truthy"
840255b @daveray Bindable selection
authored Jul 12, 2011
238 (let [start (atom :foo)
239 end (atom :bar)]
240 (bind start (some (constantly :yum)) end)
241 (reset! start :something)
242 (expect (= :yum @end)))))
243
244 (describe selection
245 (it "sends out selection changes on a widget"
246 (let [lb (ssc/listbox :model [:a :b :c])
247 output (atom nil)]
248 (bind (selection lb) output)
249 (ssc/selection! lb :b)
250 (expect (= :b @output))))
3281db7 @daveray Integrate Value into binding framework.
authored Dec 12, 2011
251 (it "maps its input to the selection of a widget"
840255b @daveray Bindable selection
authored Jul 12, 2011
252 (let [input (atom nil)
253 lb (ssc/listbox :model [:a :b :c])]
254 (bind input (selection lb))
255 (reset! input :b)
256 (expect (= :b (ssc/selection lb))))))
257
3281db7 @daveray Integrate Value into binding framework.
authored Dec 13, 2011
258 (describe value
259 (it "maps its input to the value of a widget"
260 (let [input (atom nil)
261 lb (ssc/listbox :id :lb :model [:a :b :c])
262 tb (ssc/text :id :text)
263 p (ssc/border-panel :north lb :center tb)]
264 (bind input (value p))
265 (reset! input {:lb :b :text "hi"})
266 (expect (= {:lb :b :text "hi"} (ssc/value p))))))
267
f6ec8bf @daveray Auto-convert widgets to bindable when it makes sense.
authored Jul 11, 2011
268 (describe to-bindable
269 (it "returns arg if it's already bindable"
270 (let [a (atom nil)]
271 (expect (= a (to-bindable a)))))
272 (it "converts a text component to its document"
840255b @daveray Bindable selection
authored Jul 12, 2011
273 (let [t (ssc/text)]
f6ec8bf @daveray Auto-convert widgets to bindable when it makes sense.
authored Jul 11, 2011
274 (expect (= (.getDocument t) (to-bindable t)))))
275 (it "converts a slider to its model"
840255b @daveray Bindable selection
authored Jul 12, 2011
276 (let [s (ssc/slider)]
f6ec8bf @daveray Auto-convert widgets to bindable when it makes sense.
authored Jul 11, 2011
277 (expect (= (.getModel s) (to-bindable s))))))
278
74e6340 @daveray Implemented b-swap! which gives more control when binding to an atom.
authored Aug 8, 2011
279 (describe b-swap!
280 (it "acts like swap! passing the old value, new value, and additional args to a function"
281 (let [start (atom nil)
282 target (atom [])
283 end (atom nil)]
284 (bind start
285 (b-swap! target conj)
286 end)
287 (reset! start 1)
288 (reset! start 2)
289 (reset! start 3)
290 (expect (= [1 2 3] @target))
291 (expect (= @end @target)))))
292
135b147 @daveray Implemented binding for agents.
authored Oct 21, 2011
293 (describe b-send
294 (it "acts like send passing the old value, new value, and additional args to a function"
295 (let [start (atom nil)
296 target (agent [])]
297 (bind start
298 (b-send target conj) )
299 (reset! start 1)
300 (reset! start 2)
301 (reset! start 3)
302 (await target)
303 (expect (= [1 2 3] @target)))))
304
305 (describe b-send-off
306 (it "acts like sendoff passing the old value, new value, and additional args to a function"
307 (let [start (atom nil)
308 target (agent [])]
309 (bind start
310 (b-send-off target conj) )
311 (reset! start 1)
312 (reset! start 2)
313 (reset! start 3)
314 (await target)
315 (expect (= [1 2 3] @target)))))
316
9730615 @daveray Binding functions for ensuring that values are correctly passed over …
authored Oct 20, 2011
317 (describe notify-later
318 (it "passes incoming values to the swing thread with invoke-later"
319 (let [start (atom nil)
320 end (atom nil)
321 p (promise)]
322 (bind start
323 (notify-later)
324 (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
325 end)
326 (subscribe end (fn [v] (deliver p :got-it)))
327 (reset! start 99)
328 (expect (= :got-it @p))
329 (expect (= {:value 99 :edt? true} @end)))))
330
331 (describe notify-soon
332 (it "passes incoming values to the swing thread with invoke-soon"
333 (let [start (atom nil)
334 end (atom nil)]
335 (bind start
336 (notify-soon)
337 (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
338 end)
339 (ssc/invoke-now (reset! start 99))
340 (expect (= {:value 99 :edt? true} @end)))))
341
342 (describe notify-now
343 (it "passes incoming values to the swing thread with invoke-now"
344 (let [start (atom nil)
345 end (atom nil)]
346 (bind start
347 (notify-now)
348 (transform (fn [v] {:value v :edt? (javax.swing.SwingUtilities/isEventDispatchThread)}))
349 end)
350 (reset! start 99)
351 (expect (= {:value 99 :edt? true} @end)))))
4e6644e @daveray Added the constraint to bind/subscribe that it must return an unsubsc…
authored Jan 10, 2012
352
353 (describe subscribe
354 (testing "on an atom"
355 (it "should return a function that unsubscribes"
356 (let [calls (atom 0)
357 target (atom "hi")
358 unsubscribe (subscribe target (fn [_] (swap! calls inc)))]
359 (reset! target "a")
360 (expect (= 1 @calls))
361 (unsubscribe)
362 (reset! target "b")
363 (expect (= 1 @calls)))))
364
365 (testing "on an agent"
366 (it "should return a function that unsubscribes"
367 (let [calls (atom 0)
368 target (agent "hi")
369 unsubscribe (subscribe target (fn [_] (swap! calls inc)))]
370 (await (send target (fn [_] "a")))
371 (expect (= 1 @calls))
372 (unsubscribe)
373 (await (send target (fn [_] "b")))
374 (expect (= 1 @calls)))))
375
376 (testing "on a javax.swing.text.Document"
377 (it "should return a function that unsubscribes"
378 (let [calls (atom 0)
379 target (.getDocument (ssc/text))
380 unsub (subscribe target (fn [_] (swap! calls inc)))]
381 (.insertString target 0 "hi" nil)
382 (expect (= 1 @calls))
383 (unsub)
384 (.insertString target 0 "bye" nil)
385 (expect (= 1 @calls))
386 )))
387 (testing "on a javax.swing.BoundedRangeModel"
388 (it "should return a function that unsubscribes"
389 (let [calls (atom 0)
390 target (javax.swing.DefaultBoundedRangeModel. 50 0 2 100)
391 unsub (subscribe target (fn [_] (swap! calls inc)))]
392 (.setValue target 1)
393 (expect (= 1 @calls))
394 (unsub)
395 (.setValue target 2)
396 (expect (= 1 @calls)))))
397
398 (testing "on a funnel"
399 (it "should return a function that unsubscribes"
400 (let [calls (atom 0)
401 a (atom "hi")
402 target (funnel a)
403 unsub (subscribe target (fn [_] (swap! calls inc)))]
404 (reset! a "bye")
405 (expect (= 1 @calls))
406 (unsub)
407 (reset! a "hi")
408 (expect (= 1 @calls))
409 )))
410
411 (testing "on a widget property"
412 (it "should return a function that unsubscribes"
413 (let [calls (atom 0)
414 w (ssc/text)
415 target (property w :enabled?)
416 unsub (subscribe target (fn [_] (swap! calls inc)))]
417 (.setEnabled w false)
418 (expect (= 1 @calls))
419 (unsub)
420 (.setEnabled w true)
421 (expect (= 1 @calls))
422 )))
423 (testing "on a transform"
424 (it "should return a function that unsubscribes"
425 (let [calls (atom 0)
426 target (transform identity)
427 unsub (subscribe target (fn [_] (swap! calls inc)))]
428 (notify target "hi")
429 (expect (= 1 @calls))
430 (unsub)
431 (notify target "bye")
432 (expect (= 1 @calls))
433 )))
434 (testing "on a filter"
435 (it "should return a function that unsubscribes"
436 (let [calls (atom 0)
437 target (filter (constantly true))
438 unsub (subscribe target (fn [_] (swap! calls inc)))]
439 (notify target "hi")
440 (expect (= 1 @calls))
441 (unsub)
442 (notify target "bye")
443 (expect (= 1 @calls)))))
444 (testing "on a some"
445 (it "should return a function that unsubscribes"
446 (let [calls (atom 0)
447 target (some (constantly true))
448 unsub (subscribe target (fn [_] (swap! calls inc)))]
449 (notify target "hi")
450 (expect (= 1 @calls))
451 (unsub)
452 (notify target "bye")
453 (expect (= 1 @calls))
454 ))))
455
Something went wrong with that request. Please try again.