From 727e5ccb11df3b07e4052eccb85637a0df055da5 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Mon, 7 Jul 2014 22:21:12 +0100 Subject: [PATCH] Yay! Fixed the rule compiling bug! Rules now compile correctly. --- src/mw_parser/core.clj | 9 +++++++++ test/mw_parser/core_test.clj | 25 +++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/mw_parser/core.clj b/src/mw_parser/core.clj index 655fa98..7cd9817 100644 --- a/src/mw_parser/core.clj +++ b/src/mw_parser/core.clj @@ -285,3 +285,12 @@ (list 'fn ['cell 'world] (list 'if left right)) ))) +(defn compile-rule + "Parse this `rule`, a string conforming to the grammar of MicroWorld rules, + into Clojure source, and then compile it into an anonymous + function object, getting round the problem of binding mw-engine.utils in + the compiling environment." + [rule-text] + (do + (use 'mw-engine.utils) + (eval (parse-rule rule-text)))) diff --git a/test/mw_parser/core_test.clj b/test/mw_parser/core_test.clj index a3f061f..759781a 100644 --- a/test/mw_parser/core_test.clj +++ b/test/mw_parser/core_test.clj @@ -1,5 +1,5 @@ (ns mw-parser.core-test - (:use mw-engine.utils) + (:use [mw-engine.utils :refer :all]) (:require [clojure.test :refer :all] [mw-parser.core :refer :all])) @@ -16,11 +16,20 @@ (is (parse-rule "if state is forest and fertility is between 55 and 75 then state should be climax")) (is (parse-rule "if 6 neighbours have state equal to water then state should be village")) (is (parse-rule "if state is in grassland or pasture or heath and 4 neighbours are water then state should be village")) - - ;; ideally should also test that the rule works, but I haven't worked out how to make mw-engine.utils available - ;; during eval - ;; (is (let [cell (apply (eval (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3")) - ;; (list {:state :forest :altitude 99} nil))] - ;; (and (= (:state cell) :climax) (= (:deer cell) 3)))) )) - + +;; ideally should also test that the rule works, but I haven't worked out how +;; to make mw-engine.utils available during eval +(deftest generation-tests + (testing "Code generation" + (is + (do + (use 'mw-engine.utils) + (eval + (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3")))))) + +(deftest correctness-tests + (testing "Testing that generated code performs as expected." + (is (let [afn (compile-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3") + cell (apply afn (list {:state :forest :altitude 99} nil))] + (and (= (:state cell) :climax) (= (:deer cell) 3))))))