Woohoo! Feature complete. All the language features I need to reimplement

the core rule set in the rule language now compile.
This commit is contained in:
Simon Brooke 2014-07-08 20:22:58 +01:00
parent 9fd29fab53
commit 0d3ca990c1
2 changed files with 70 additions and 18 deletions

View file

@ -1,12 +1,13 @@
(ns mw-parser.core-test
(:use [mw-engine.utils :refer :all])
(:use mw-engine.core
mw-engine.utils
mw-engine.world)
(:require [clojure.test :refer :all]
[mw-parser.core :refer :all]))
(deftest rules-tests
(testing "Rule parser - does not test whether generated functions actually work, just that something is generated!"
(is (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3"))
(is (parse-rule "if altitude is less than 100 and state is forest then state should be climax and deer should be 3"))
(is (parse-rule "if altitude is 100 or fertility is 25 then state should be heath and fertility should be 24.3"))
(is (parse-rule "if altitude is 100 or fertility is 25 then state should be heath"))
@ -16,6 +17,8 @@
(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"))
(is (parse-rule "if state is forest or state is climax and some neighbours have state is fire then 3 in 5 chance that state should be fire"))
(is (parse-rule "if state is pasture and more than 3 neighbours have state equal to scrub then state should be scrub"))
))
;; ideally should also test that the rule works, but I haven't worked out how
@ -30,6 +33,18 @@
(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))))))
(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")]
(= (apply afn (list {:state :forest :altitude 99} nil))
{:state :climax :altitude 99 :deer 3})))
(is (let [afn (compile-rule "if state is new and more than 3 neighbours have state equal to new then state should be scrub")]
(= (transform-world (make-world 3 3) (list afn))
'(({:x 0, :y 0, :state :new}
{:x 1, :y 0, :state :scrub}
{:x 2, :y 0, :state :new})
({:x 0, :y 1, :state :scrub}
{:x 1, :y 1, :state :scrub}
{:x 2, :y 1, :state :scrub})
({:x 0, :y 2, :state :new}
{:x 1, :y 2, :state :scrub}
{:x 2, :y 2, :state :new})))))
))