Skip to content

Commit

Permalink
Update macros test
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dixon committed Jul 28, 2017
1 parent de1bf6a commit f16caf1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 89 deletions.
6 changes: 5 additions & 1 deletion src/clj/precept/macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
`(def ~name (precept.repl/reload-session-cljs! '~name))
`(precept.macros/session* ~name ~@sources-and-options)))))

(defn precept->clara-options
[precept-options-map precept-options-keys]
(mapcat identity (apply dissoc precept-options-map precept-options-keys)))

(defmacro session*
([m]
(let [name (:name m)
Expand All @@ -79,7 +83,7 @@
hierarchy `(schema/init! (select-keys ~options-in [:db-schema :client-schema]))
ancestors-fn `(util/make-ancestors-fn ~hierarchy)
precept-options-map (merge-default-options options-in ancestors-fn)
cr-options-list (mapcat identity (apply dissoc precept-options-map precept-options-keys))
cr-options-list (precept->clara-options precept-options-map precept-options-keys)
rule-nses (conj sources `'precept.impl.rules)
cr-body (into cr-options-list rule-nses)
interned-ns-name (com/cljs-ns)
Expand Down
135 changes: 47 additions & 88 deletions test/cljc/precept/macros_test.cljc
Original file line number Diff line number Diff line change
@@ -1,88 +1,47 @@
;(ns precept.macros-test
; (:require [precept.rules :refer [session]]
; [precept.util :as util]
; [precept.core :as core]
; [clara.rules :refer [defsession defrule]]
; [clara.rules.accumulators :as acc]
; [clojure.test :refer [deftest run-tests testing is]]
; [precept.schema :as schema]
; [precept.schema-fixture :refer [test-schema]]
; [precept.dsl :refer [<- entity]]
; [precept.state :as state]
; [clara.rules.compiler :as com])
; (:import [precept.util Tuple]))
;
;(deftest session-test
; (testing "Macroexpansion should be the same as equivalent
; arguments to defsession"
; (is (= (macroexpand `(defsession ~'foo
; 'precept.impl.rules
; 'precept.macros-test
; :fact-type-fn :a
; :ancestors-fn (util/make-ancestors-fn
; (schema/init! (select-keys {}
; [:db-schema :client-schema])))
; :activation-group-fn (util/make-activation-group-fn ~core/default-group)
; :activation-group-sort-fn (util/make-activation-group-sort-fn
; ~core/groups
; ~core/default-group)))
; (macroexpand '(session foo 'precept.macros-test)))))
;
; (testing "Allow overwrite defaults"
; (let [clara-session `(defsession ~'foo
; 'precept.impl.rules
; 'precept.macros-test
; :fact-type-fn ~'(fn [x] (or (:a x)
; (:b x)))
; :ancestors-fn ~'(fn [x] [:all :foo])
; :activation-group-fn (util/make-activation-group-fn ~core/default-group)
; :activation-group-sort-fn (util/make-activation-group-sort-fn
; ~core/groups
; ~core/default-group))
; wrapper '(session foo
; 'precept.macros-test
; :fact-type-fn (fn [x] (or (:a x)
; (:b x)))
; :ancestors-fn (fn [x] [:all :foo]))]))
; ;(is (= (macroexpand clara-session) (macroexpand wrapper)))))
;
; (testing "Expand :db-schema opt to ancestors fn")
; ;(is (= (macroexpand `(defsession ~'foo
; ; 'precept.impl.rules
; ; 'precept.macros-test
; ; :fact-type-fn :a
; ; :ancestors-fn (util/make-ancestors-fn
; ; (schema/init!
; ; (select-keys
; ; {:db-schema ~precept.schema-fixture/test-schema}
; ; [:db-schema :client-schema]
; ; :activation-group-fn (util/make-activation-group-fn ~core/default-group)
; ; :activation-group-sort-fn (util/make-activation-group-sort-fn
; ; ~core/groups
; ; ~core/default-group
; ; (macroexpand `(session ~'foo
; ; 'precept.macros-test
; ; :db-schema ~precept.schema-fixture/test-schema)
;
;;; TODO. Probably better off testing functionality over expansion at this point
; (testing "Expand :client-schema and :db-schema opts to ancestors fn"))
; ;(is (= (macroexpand `(defsession ~'foo
; ; 'precept.impl.rules
; ; 'precept.macros-test
; ; :fact-type-fn :a
; ; :ancestors-fn (util/make-ancestors-fn
; ; (schema/init!
; ; (select-keys
; ; {:db-schema ~precept.schema-fixture/test-schema
; ; :client-schema ~precept.schema-fixture/test-schema}
; ; [:db-schema :client-schema])))
; ; :activation-group-fn (util/make-activation-group-fn ~core/default-group)
; ; :activation-group-sort-fn (util/make-activation-group-sort-fn
; ; ~core/groups
; ; ~core/default-group)))
; ; (macroexpand `(session ~'foo
; ; 'precept.macros-test
; ; :db-schema ~precept.schema-fixture/test-schema
; ; :client-schema ~precept.schema-fixture/test-schema))))))
;
;(run-tests)
(ns precept.macros-test
(:require [precept.macros :as macros]
[clojure.test :refer [deftest run-tests testing is]]))

(def all-options (list 'precept.macros-test
:db-schema 'db-schema-ns
:client-schema 'client-schema-ns
:reload true))

(def no-options (list 'precept.macros-test))

(deftest options-map-test
(is (= (macros/options-map no-options)
{}))
(is (= (macros/options-map all-options)
{:db-schema 'db-schema-ns
:client-schema 'client-schema-ns
:reload true})))

(deftest sources-list-test
(is (= (macros/sources-list all-options)
(macros/sources-list no-options)
(list 'precept.macros-test))))

(deftest precept->clara-options-test
(let [cr-options '(:fact-type-fn :a
:ancestors-fn identity
:activation-group-fn
(precept.util/make-activation-group-fn :calc)
:activation-group-sort-fn
(precept.util/make-activation-group-sort-fn
[:action :calc :report :cleanup] :calc))]
(is (= (macros/precept->clara-options
(macros/merge-default-options
(macros/options-map no-options)
'identity)
macros/precept-options-keys)
cr-options))

(is (= (macros/precept->clara-options
(macros/merge-default-options
(macros/options-map all-options)
'identity)
macros/precept-options-keys)
cr-options))))

(run-tests)
4 changes: 4 additions & 0 deletions test/cljc/precept/rule_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@
(testing "Fact assignment: Attribute-only with brackets"
(is (= '[[?toggle <- :ui/toggle-complete]]
(rewrite-lhs '[[?toggle <- [:ui/toggle-complete]]])))
;; Returns persistent list instead of vector. Appears OK; test where we expand this
;; inside of a rule and pass to clara passes.
;(is (vector? (first (rewrite-lhs '[[?toggle <- [:ui/toggle-complete]]]))))
;(is (vector? (first (rewrite-lhs '[[?toggle <- [_ :ui/toggle-complete]]]))))
(is (= '[[:not [:ns/foo (= 3 (:v this))]]]
(rewrite-lhs '[[:not [_ :ns/foo 3]]]))))
(testing "S-expr in value slot"
Expand Down

0 comments on commit f16caf1

Please sign in to comment.