Skip to content

Commit

Permalink
Add constraint! macro and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lynaghk committed Jun 11, 2012
1 parent f337805 commit b8baf93
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
.lein*
lib
public/*.js
out/
target/
14 changes: 9 additions & 5 deletions project.clj
@@ -1,16 +1,20 @@
(defproject com.keminglabs/reflex "0.1.0-SNAPSHOT"
:description "ClojureScript state propagation."
:license {:name "BSD" :url "http://www.opensource.org/licenses/BSD-3-Clause"}

:dependencies [[org.clojure/clojure "1.4.0"]]

:min-lein-version "2.0.0"

:plugins [[lein-cljsbuild "0.1.10"]]
:plugins [[lein-cljsbuild "0.2.1"]]

:source-paths ["src/clj" "src/cljs"]

:cljsbuild {:builds {:test {:source-path "test/cljs"
:cljsbuild {:builds {:test {:source-path "test/integration"
:compiler {:output-to "out/test/integration.js"
:optimizations :simple
:pretty-print true}}}})
:optimizations :whitespace
:pretty-print true}}}

:test-commands {"integration" ["phantomjs"
"test/integration/runner.coffee"]}
})
7 changes: 7 additions & 0 deletions src/clj/reflex/macros.clj
Expand Up @@ -10,3 +10,10 @@
`(let [co# (reflex.core/ComputedObservable. nil true (fn [] ~@body) (gensym "computed-observable") {} {})]
@co# ;;initial deref to setup watchers
co#))

(defmacro constrain!
"Reruns body whenever atoms deferenced in the body change. Body should side effect."
[& body]
`(let [co# (computed-observable ~@body)]
(add-watch co# :reflex-constraint (fn [] ~@body))
co#))
1 change: 0 additions & 1 deletion test/cljs/integration.cljs

This file was deleted.

42 changes: 42 additions & 0 deletions test/integration/integration.cljs
@@ -0,0 +1,42 @@
(ns test.integration.integration
(:use-macros [reflex.macros :only [computed-observable constrain!]])
(:require [reflex.core :as [reflex]]) ;;need to explicitly require since macros reference this ns
)


(let [!counter (atom 0)
!signal (atom "All I do is change")]

(constrain!
;;when I change...
@!signal
;;update the counter
(swap! !counter inc))

(assert (= 1 @!counter)
"Constraint run on init")

(reset! !signal "foo")

(assert (= 2 @!counter)
"Counter auto updated"))



(let [!x (atom 0)
!co (computed-observable (inc @!x))]

(assert (= 1 @!co)
"CO has correct value on first deref")

(swap! !x inc)

(assert (= 2 @!co)
"CO auto-updates"))





(.log js/console "Hurray, all tests passed!")

9 changes: 9 additions & 0 deletions test/integration/runner.coffee
@@ -0,0 +1,9 @@
#Test runner for use with PhantomJS
page = new WebPage()
page.onConsoleMessage = (msg) -> console.log msg

page.onLoadFinished = ->
page.injectJs "out/test/integration.js"
phantom.exit()

page.open "about:blank"
10 changes: 10 additions & 0 deletions test/integration/runner.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Test</title>
</head>
<body>
<script src='../../out/test/integration.js'></script>
</body>
</html>

0 comments on commit b8baf93

Please sign in to comment.