From 88d707a32eb9fc899688cf5ce29217d8147e5dc3 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 23 Sep 2016 12:53:00 +0100 Subject: [PATCH] Fixed all failing tests. Two issues: 1. The regression test failures were both errors in the tests rather than in the code under test; 2. The failure in the 'bulk' test relates to the fact that the new declarative parser cannot cope with trailing whitespace. --- resources/rules.txt | 10 +++++----- src/mw_parser/bulk.clj | 2 +- test/mw_parser/bulk_test.clj | 15 ++++++++------- test/mw_parser/declarative_test.clj | 7 ++++--- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/resources/rules.txt b/resources/rules.txt index 0356227..d7f2d5f 100644 --- a/resources/rules.txt +++ b/resources/rules.txt @@ -6,19 +6,19 @@ ## Vegetation rules ;; rules which populate the world with plants -;; Occasionally, passing birds plant tree seeds into grassland +;; Occasionally, passing birds plant tree seeds into grassland if state is grassland then 1 chance in 10 state should be heath ;; heath below the treeline grows gradually into forest -if state is heath and altitude is less than 120 then state should be scrub +if state is heath and altitude is less than 120 then state should be scrub if state is scrub then 1 chance in 5 state should be forest ;; Forest on fertile land grows to climax -if state is forest and fertility is more than 5 and altitude is less than 70 then state should be climax - +if state is forest and fertility is more than 5 and altitude is less than 70 then state should be climax + ;; Climax forest occasionally catches fire (e.g. lightning strikes) if state is climax then 1 chance in 500 state should be fire @@ -40,7 +40,7 @@ if state is waste then state should be grassland ## Potential blockers -;; Forest increases soil fertility. +;; Forest increases soil fertility. if state is in forest or climax then fertility should be fertility + 1 diff --git a/src/mw_parser/bulk.clj b/src/mw_parser/bulk.clj index cff7b49..45540e8 100644 --- a/src/mw_parser/bulk.clj +++ b/src/mw_parser/bulk.clj @@ -41,7 +41,7 @@ lines delimited by the new-line character. Return a list of S-expressions." [string] ;; TODO: tried to do this using with-open, but couldn't make it work. - (map parse-rule (remove comment? (split string #"\n")))) + (map #(parse-rule (trim %)) (remove comment? (split string #"\n")))) (defn parse-file "Parse rules from successive lines in the file loaded from this `filename`. diff --git a/test/mw_parser/bulk_test.clj b/test/mw_parser/bulk_test.clj index e80acc7..1155361 100644 --- a/test/mw_parser/bulk_test.clj +++ b/test/mw_parser/bulk_test.clj @@ -7,18 +7,19 @@ (testing "Bulk (file) parsing and compilation" (is (= (count (parse-file (as-file "resources/rules.txt"))) 15) "Should parse all rules and throw no exceptions") - (is (empty? - (remove #(= % 'fn) - (map first - (parse-file + (is (empty? + (remove #(= % ':RULE) + (map first + (parse-file (as-file "resources/rules.txt"))))) "all parsed rules should be lambda sexprs") (is (= (count (compile-file (as-file "resources/rules.txt"))) 15) "Should compile all rules and throw no exceptions") (is (empty? - (remove ifn? - (map first - (compile-file + (remove ifn? + (map first + (compile-file (as-file "resources/rules.txt"))))) "all compiled rules should be ifns") )) + diff --git a/test/mw_parser/declarative_test.clj b/test/mw_parser/declarative_test.clj index d5c6fd3..bc7485f 100644 --- a/test/mw_parser/declarative_test.clj +++ b/test/mw_parser/declarative_test.clj @@ -475,12 +475,13 @@ (deftest regression-tests (testing "Rule in default set which failed on switchover to declarative rules" - (let [afn (compile-rule "if state is scrub then 1 chance in 5 state should be forest") + (let [afn (compile-rule "if state is scrub then 1 chance in 1 state should be forest") world (transform-world (make-world 3 3) (list (compile-rule "if x is 2 then altitude should be 11") (compile-rule "if x is less than 2 then state should be scrub")))] - (is (= (:state (apply afn (list {:x 1 :y 1} world))) :forest) + (is (= (:state (apply afn (list (get-cell world 1 1) world))) :forest) "Centre cell is scrub, so rule should fire") - (is (= (:state (apply afn (list {:x 2 :y 1} world))) :beach) + (is (= (apply afn (list (get-cell world 2 1) world)) nil) "Middle cell of the strip is not scrub, so rule should not fire.")))) +