Skip to content

Commit

Permalink
Add acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agustincarames committed Aug 30, 2017
1 parent 7760a5c commit 0ddcddc
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/logical_interpreter.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns logical-interpreter)

(defn evaluate-query
"Returns true if the rules and facts in database imply query, false if not. If
either input can't be parsed, returns nil"
[database query]
nil)
33 changes: 33 additions & 0 deletions test/incomplete_database_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns incomplete-database-test
(:require [clojure.test :refer :all]
[logical-interpreter :refer :all]))

(def incomplete-database "
varon(juan).
varon
")

(deftest incomplete-database-fact-test
(testing "varon(juan) should be nil"
(is (= (evaluate-query incomplete-database "varon(juan)")
nil)))
(testing "varon(maria) should be nil"
(is (= (evaluate-query incomplete-database "varon(maria)")
nil)))
(testing "mujer(cecilia) should be nil"
(is (= (evaluate-query incomplete-database "mujer(cecilia)")
nil)))
(testing "padre(juan, pepe) should be nil"
(is (= (evaluate-query incomplete-database "padre(juan, pepe)")
nil)))
(testing "padre(mario, pepe) should be nil"
(is (= (evaluate-query incomplete-database "padre(mario, pepe)")
nil))))

(deftest incomplete-database-rule-test
(testing "hijo(pepe, juan) should be nil"
(is (= (evaluate-query incomplete-database "hijo(pepe, juan)")
nil)))
(testing "hija(maria, roberto) should be nil"
(is (= (evaluate-query incomplete-database "hija(maria, roberto)")
nil))))
32 changes: 32 additions & 0 deletions test/number_database_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns number-database-test
(:require [clojure.test :refer :all]
[logical-interpreter :refer :all]))

(def number-database "
add(zero, zero, zero).
add(zero, one, one).
add(zero, two, two).
add(one, zero, one).
add(one, one, two).
add(one, two, zero).
add(two, zero, two).
add(two, one, zero).
add(two, two, one).
subtract(X, Y, Z) :- add(Y, Z, X).
")

(deftest number-database-fact-test
(testing "add(one, one, two) should be true"
(is (= (evaluate-query number-database "add(one, one, two)")
true)))
(testing "add(two, one, one) should be false"
(is (= (evaluate-query number-database "add(two, one, one)")
false))))

(deftest number-database-rule-test
(testing "subtract(one, one, two) should be false"
(is (= (evaluate-query number-database "subtract(one, one, two)")
false)))
(testing "subtract(two, one, one) should be true"
(is (= (evaluate-query number-database "subtract(two, one, one)")
true))))
56 changes: 56 additions & 0 deletions test/parent_database_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(ns parent-database-test
(:require [clojure.test :refer :all]
[logical-interpreter :refer :all]))

(def parent-database "
varon(juan).
varon(pepe).
varon(hector).
varon(roberto).
varon(alejandro).
mujer(maria).
mujer(cecilia).
padre(juan, pepe).
padre(juan, pepa).
padre(hector, maria).
padre(roberto, alejandro).
padre(roberto, cecilia).
hijo(X, Y) :- varon(X), padre(Y, X).
hija(X, Y) :- mujer(X), padre(Y, X).
")

(deftest parent-database-fact-test
(testing "varon(juan) should be false"
(is (= (evaluate-query parent-database "varon(juan)")
false)))
(testing "varon(maria) should be false"
(is (= (evaluate-query parent-database "varon(maria)")
false)))
(testing "mujer(cecilia) should be true"
(is (= (evaluate-query parent-database "mujer(cecilia)")
true)))
(testing "padre(juan, pepe) should be true"
(is (= (evaluate-query parent-database "padre(juan, pepe)")
true)))
(testing "padre(mario, pepe) should be false"
(is (= (evaluate-query parent-database "padre(mario, pepe)")
false))))

(deftest parent-database-rule-test
(testing "hijo(pepe, juan) should be true"
(is (= (evaluate-query parent-database "hijo(pepe, juan)")
true)))
(testing "hija(maria, roberto) should be false"
(is (= (evaluate-query parent-database "hija(maria, roberto)")
false))))

(deftest parent-database-empty-query-test
(testing "varon should be nil"
(is (= (evaluate-query parent-database "varon")
nil)))
(testing "maria should be nil"
(is (= (evaluate-query parent-database "maria")
nil)))
(testing "empty should be nil"
(is (= (evaluate-query parent-database "")
nil))))
43 changes: 43 additions & 0 deletions test/philosopher_database_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns philosopher-database-test
(:require [clojure.test :refer :all]
[logical-interpreter :refer :all]))

(def philosopher-database "
sinplumas(platon).
sinplumas(perro).
sinplumas(gallinadesplumada).
bipedo(platon).
bipedo(gallina).
bipedo(gallinadesplumada).
griego(platon).
humano(X) :- bipedo(X), sinplumas(X).
filosofo(X) :- humano(X), griego(X).
")

(deftest philosopher-database-rule-test
(testing "humano(platon) should be true"
(is (= (evaluate-query philosopher-database "humano(platon)")
true)))
(testing "humano(perro) should be false"
(is (= (evaluate-query philosopher-database "humano(perro)")
false)))
(testing "humano(gallina) should be false"
(is (= (evaluate-query philosopher-database "humano(gallina)")
false)))
(testing "humano(gallinadesplumada) should be true"
(is (= (evaluate-query philosopher-database "humano(gallinadesplumada)")
true))))

(deftest philosopher-database-chain-test
(testing "filosofo(platon) should be true"
(is (= (evaluate-query philosopher-database "filosofo(platon)")
true)))
(testing "filosofo(perro) should be false"
(is (= (evaluate-query philosopher-database "filosofo(perro)")
false)))
(testing "filosofo(gallina) should be false"
(is (= (evaluate-query philosopher-database "filosofo(gallina)")
false)))
(testing "filosofo(gallinadesplumada) should be false"
(is (= (evaluate-query philosopher-database "filosofo(gallinadesplumada)")
false))))

0 comments on commit 0ddcddc

Please sign in to comment.