Much more work on test coverage, exposed probable bug.

This commit is contained in:
Simon Brooke 2014-08-13 19:20:38 +01:00
parent 34bb22dbfe
commit 21c1044a6d
2 changed files with 28 additions and 5 deletions

View file

@ -195,7 +195,7 @@
comparator (cond (= MORE "more") '> comparator (cond (= MORE "more") '>
(member? MORE '("fewer" "less")) '<)] (member? MORE '("fewer" "less")) '<)]
(cond (cond
(not (= WITHIN "within")) (not= WITHIN "within")
(parse-comparator-neighbours-condition (parse-comparator-neighbours-condition
(flatten (flatten
;; two tokens were mis-parsed as 'within distance' that weren't ;; two tokens were mis-parsed as 'within distance' that weren't
@ -238,7 +238,7 @@
(cond (cond
(and quantity (= NEIGHBOURS "neighbours")) (and quantity (= NEIGHBOURS "neighbours"))
(cond (cond
(not (= WITHIN "within")) (not= WITHIN "within")
(parse-simple-neighbours-condition (parse-simple-neighbours-condition
(flatten (flatten
;; two tokens were mis-parsed as 'within distance' that weren't ;; two tokens were mis-parsed as 'within distance' that weren't
@ -323,7 +323,7 @@
e.g. 'fertility should be fertility + 1', or 'deer should be deer - wolves'." e.g. 'fertility should be fertility + 1', or 'deer should be deer - wolves'."
[previous [prop1 SHOULD BE prop2 operator value & rest]] [previous [prop1 SHOULD BE prop2 operator value & rest]]
(cond (cond
(member? prop2 '("x" "y")) (member? prop1 '("x" "y"))
(throw (throw
(Exception. reserved-properties-error)) (Exception. reserved-properties-error))
(and (= SHOULD "should") (and (= SHOULD "should")

View file

@ -1,13 +1,21 @@
(ns mw-parser.core-test (ns mw-parser.core-test
(:use clojure.pprint (:use clojure.pprint
mw-engine.core mw-engine.core
mw-engine.utils
mw-engine.world) mw-engine.world)
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[mw-parser.core :refer :all])) [mw-parser.core :refer :all]))
(deftest primitives-tests (deftest primitives-tests
(testing "Simple functions supporting the parser" (testing "Simple functions supporting the parser"
(is (= (parse-simple-value '()) nil)
"if there's nothing to parse, return nil")
(is (= (first (parse-simple-value '("1234" "and" "that"))) 1234)
"a simple value is expected to be just a number.")
(is (= (first (parse-simple-value '("this" "and" "that"))) :this)
"or else just a keyword")
(is (= (first (parse-simple-value '("this" "and" "that") true))
'(get-int cell :this))
"...unless an integer is explicitly sought, in which case it should be something which gets an integer from the current cell")
(is (= (parse-value '()) nil) (is (= (parse-value '()) nil)
"if there's nothing to parse, return nil") "if there's nothing to parse, return nil")
(is (= (first (parse-value '("1234" "and" "that"))) 1234) (is (= (first (parse-value '("1234" "and" "that"))) 1234)
@ -17,6 +25,10 @@
(is (= (first (parse-value '("this" "and" "that") true)) (is (= (first (parse-value '("this" "and" "that") true))
'(get-int cell :this)) '(get-int cell :this))
"...unless an integer is explicitly sought, in which case it should be something which gets an integer from the current cell") "...unless an integer is explicitly sought, in which case it should be something which gets an integer from the current cell")
(is (= (parse-property-value '()) nil)
"if there's nothing to parse, return nil")
(is (= (first (parse-property-value '("this" "and" "that"))) '(:this cell))
"Parsing a property value returns a code function to pull its value off the current cell")
)) ))
@ -47,7 +59,16 @@
(is (thrown-with-msg? (is (thrown-with-msg?
Exception #"The properties 'x' and 'y' of a cell are reserved and should not be set in rule actions" Exception #"The properties 'x' and 'y' of a cell are reserved and should not be set in rule actions"
(parse-rule "if state is new then y should be 0")) (parse-rule "if state is new then y should be 0"))
"Exception thrown on attempt to set 'y'"))) "Exception thrown on attempt to set 'y'")
(is (thrown? Exception (compile-rule "if state is new then x should be 0"))
"Can't set x property to number, as this would break the world")
(is (thrown? Exception (compile-rule "if state is new then y should be 0"))
"Can't set y property to number, as this would break the world")
(is (thrown? Exception (compile-rule "if state is new then x should be heath"))
"Can't set x property to symbol, as this would break the world")
(is (thrown? Exception (compile-rule "if state is new then y should be heath"))
"Can't set y property to symbol, as this would break the world")
))
(deftest correctness-tests (deftest correctness-tests
(testing "Simplest possible rule" (testing "Simplest possible rule"
@ -87,6 +108,8 @@
{:state :grassland}) {:state :grassland})
"Rule fires when condition is met"))) "Rule fires when condition is met")))
(testing "Can't set x or y properties")
(testing "Simple list membership rule" (testing "Simple list membership rule"
(let [afn (compile-rule "if state is in heath or scrub or forest then state should be climax")] (let [afn (compile-rule "if state is in heath or scrub or forest then state should be climax")]
(is (= (apply afn (list {:state :heath} nil)) (is (= (apply afn (list {:state :heath} nil))