From 34bb22dbfe921e34383c917f73a4d68e771afd9c Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 13 Aug 2014 18:06:33 +0100 Subject: [PATCH] Worked on test coverage. --- src/mw_parser/core.clj | 2 +- test/mw_parser/core_test.clj | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/mw_parser/core.clj b/src/mw_parser/core.clj index c9e32d3..7728818 100644 --- a/src/mw_parser/core.clj +++ b/src/mw_parser/core.clj @@ -65,7 +65,7 @@ (defn parse-numeric-value "Parse a number." [[value & remainder]] - (if (re-matches re-number value) [(read-string value) remainder])) + (if (and value (re-matches re-number value)) [(read-string value) remainder])) (defn parse-property-int "Parse a token assumed to be the name of a property of the current cell, diff --git a/test/mw_parser/core_test.clj b/test/mw_parser/core_test.clj index 18d2618..a2c64e7 100644 --- a/test/mw_parser/core_test.clj +++ b/test/mw_parser/core_test.clj @@ -6,6 +6,20 @@ (:require [clojure.test :refer :all] [mw-parser.core :refer :all])) +(deftest primitives-tests + (testing "Simple functions supporting the parser" + (is (= (parse-value '()) nil) + "if there's nothing to parse, return nil") + (is (= (first (parse-value '("1234" "and" "that"))) 1234) + "a simple value is expected to be just a number.") + (is (= (first (parse-value '("this" "and" "that"))) :this) + "or else just a keyword") + (is (= (first (parse-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") + )) + + (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")) @@ -134,6 +148,14 @@ (testing "Number neighbours have property equal to value" (let [afn (compile-rule "if 3 neighbours have state equal to new then state should be water") world (make-world 3 3)] + (is (= (apply afn (list {:x 0 :y 0} world)) + {:state :water :x 0 :y 0}) + "Rule fires when condition is met (in a new world all cells are new, corner cell has three neighbours)") + (is (nil? (apply afn (list {:x 1 :y 1} world))) + "Middle cell has eight neighbours, so rule does not fire.")) + (let [afn (compile-rule "if 3 neighbours are new then state should be water") + world (make-world 3 3)] + ;; 'are new' should be the same as 'have state equal to new' (is (= (apply afn (list {:x 0 :y 0} world)) {:state :water :x 0 :y 0}) "Rule fires when condition is met (in a new world all cells are new, corner cell has three neighbours)") @@ -182,7 +204,18 @@ (is (= (:state (apply afn (list {:x 1 :y 1} world))) :beach) "Rule fires when condition is met (strip of altitude 11 down right hand side)") (is (nil? (apply afn (list {:x 2 :y 1} world))) - "Middle cell of the strip has only two high neighbours, so rule should not fire."))) + "Middle cell of the strip has only two high neighbours, so rule should not fire.")) + (let [afn (compile-rule "if more than 2 neighbours are grassland then state should be beach") + ;; 'are grassland' should mean the same as 'have state equal to grassland'. + world (transform-world + (make-world 3 3) + (list (compile-rule "if x is 2 then altitude should be 11 and state should be grassland") + (compile-rule "if x is less than 2 then altitude should be 0 and state should be water")))] + (is (= (:state (apply afn (list {:x 1 :y 1} world))) :beach) + "Rule fires when condition is met (strip of altitude 11 down right hand side)") + (is (nil? (apply afn (list {:x 2 :y 1} world))) + "Middle cell of the strip has only two high neighbours, so rule should not fire.")) + ) (testing "Fewer than number neighbours have property equal to numeric-value" (let [afn (compile-rule "if fewer than 3 neighbours have altitude equal to 11 then state should be beach")