170 assertions, no failures, generating docs.

This commit is contained in:
Simon Brooke 2019-08-20 16:42:44 +01:00
parent fe6fba87e0
commit 7a5596dc55
27 changed files with 3928 additions and 45 deletions

View file

@ -176,16 +176,24 @@
(deftest member-tests
(testing "member"
(let [expected 'T
actual (MEMBER (gsp "ALBERT") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
actual (MEMBER
(gsp "ALBERT")
(gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'T
actual (MEMBER (gsp "BELINDA") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
actual (MEMBER
(gsp "BELINDA")
(gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'T
actual (MEMBER (gsp "ELFREDA") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
actual (MEMBER
(gsp "ELFREDA")
(gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))
(let [expected 'F
actual (MEMBER (gsp "BERTRAM") (gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
actual (MEMBER
(gsp "BERTRAM")
(gsp "(ALBERT BELINDA CHARLIE DORIS ELFREDA FRED)"))]
(= actual expected))))
(deftest pairlis-tests

View file

@ -0,0 +1,57 @@
(ns beowulf.core-test
(:require [clojure.test :refer :all]
[beowulf.cons-cell :refer :all]))
(deftest cons-cell-tests
(testing "make-cons-cell"
(let [expected "(A . B)"
actual (print-str (beowulf.cons_cell.ConsCell. 'A 'B))]
(is (= actual expected) "Cons cells should print as cons cells, natch."))
(let [expected "(A . B)"
actual (print-str (make-cons-cell 'A 'B))]
(is (= actual expected) "Even if build with the macro."))
(let [expected beowulf.cons_cell.ConsCell
actual (print-str (make-cons-cell 'A 'B))]
(is (= actual expected) "And they should be cons cells."))
)
(testing "make-beowulf-list"
(let [expected "(A (B C) (D E (F) G) H)"
actual (print-str (make-beowulf-list '(A (B C) (D E (F) G) H)))]
(is (= actual expected) "Should work for clojure lists, recursively."))
(let [expected "(A (B C) (D E (F) G) H)"
actual (print-str (make-beowulf-list [A [B C] [D E [F] G] H]))]
(is (= actual expected) "Should work for vectors, too."))
(let [expected "NIL"
actual (print-str (make-beowulf-list []))]
(is (= actual expected) "An empty sequence is NIL."))
(let [expected beowulf.cons_cell.ConsCell
actual (make-beowulf-list '(A (B C) (D E (F) G) H))]
(is (= actual expected) "A beowulf list is made of cons cells.")))
(testing "pretty-print"
(let [expected "(A\n (B C)\n (D E (F) G) H)"
actual (pretty-print (make-beowulf-list '(A (B C) (D E (F) G) H)) 20 0)]
(is (= actual expected)))
(let [expected "(A (B C) (D E (F) G) H)"
actual (pretty-print (make-beowulf-list '(A (B C) (D E (F) G) H)))]
(is (= actual expected))))
(testing "count"
(let [expected 4
actual (count (make-beowulf-list '(A (B C) (D E (F) G) H)) 20 0)]
(is (= actual expected)))
(let [expected 1
actual (count (make-beowulf-list '(A)))]
(is (= actual expected)))
(let [expected 1
actual (count (make-cons-cell 'A 'B))]
(is (= actual expected))))
(testing "sequence functions"
(let [expected "A"
actual (print-str (first (make-beowulf-list '(A (B C) (D E (F) G) H))))]
(is (= actual expected)))
(let [expected "((B C) (D E (F) G) H)"
actual (print-str (more (make-beowulf-list '(A (B C) (D E (F) G) H))))]
(is (= actual expected)))
(let [expected "((B C) (D E (F) G) H)"
actual (print-str (next (make-beowulf-list '(A (B C) (D E (F) G) H))))]
(is (= actual expected)))
))

View file

@ -1,7 +1,175 @@
(ns beowulf.core-test
(:require [clojure.test :refer :all]
(:require [clojure.java.io :refer [reader]]
[clojure.string :refer [split]]
[clojure.test :refer :all]
[beowulf.core :refer :all]))
;; (deftest a-test
;; (testing "FIXME, I fail."
;; (is (= 0 1))))
(defn string->stream
"Copied shamelessly from
https://stackoverflow.com/questions/38283891/how-to-wrap-a-string-in-an-input-stream"
([s] (string->stream s "UTF-8"))
([s encoding]
(-> s
(.getBytes encoding)
(java.io.ByteArrayInputStream.))))
(deftest repl-tests
(testing "quit functionality"
(with-open [r (reader (string->stream "quit"))]
(binding [*in* r]
(is (thrown-with-msg? Exception #"\nFærwell!" (repl "")))))
(let [expected nil
actual (with-open [r (reader (string->stream "quit"))]
(binding [*in* r]
(-main)))]
(is (= actual expected)))))
(deftest flag-tests
(testing "No flags"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error ""
expected-result #".*\(A \. B\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main)) #"\n")))]
(is (= greeting expected-greeting))
(is (= error expected-error))
(is (re-matches expected-result result))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "unknown flag"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error #"Unknown option:.*"
expected-result #".*\(A \. B\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--unknown")) #"\n")))]
(is (= greeting expected-greeting))
(is (re-matches expected-error error))
(is (re-matches expected-result result))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "help"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-h1 " -h, --help"
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-result #".*\(A \. B\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version h1 h2 h3 h4 h5 quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--help")) #"\n")))]
(is (= greeting expected-greeting))
(is (= h1 expected-h1))
(is (re-matches expected-result result))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "prompt"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error ""
expected-result #".*\(A \. B\).*"
expected-prompt "? "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--prompt" "?")) #"\n")))]
(is (= greeting expected-greeting))
(is (= error expected-error))
(is (re-matches expected-result result ))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "read - file not found"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error #"Failed to validate.*"
expected-result #".*\(A \. B\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--read" "froboz")) #"\n")))]
(is (= greeting expected-greeting))
(is (re-matches expected-error error))
(is (re-matches expected-result result))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "read - file found"
;; TODO: there's no feedback from this because the initfile
;; is not yet read. This will change
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error ""
expected-result #".*\(A \. B\)"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--read" "README.md")) #"\n")))]
(is (= greeting expected-greeting))
(is (= error expected-error))
(is (re-matches expected-result result))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "strict"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error ""
expected-result #".*Cannot parse meta expressions in strict mode.*"
expected-prompt "Sprecan:: "
expected-signoff "Færwell!"
[_ greeting version error quit-message _ result prompt signoff]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--strict")) #"\n")))]
(is (= greeting expected-greeting))
(is (= error expected-error))
(is (re-matches expected-result result ))
(is (= quit-message expected-quit-message))
(is (= prompt expected-prompt))
(is (= signoff expected-signoff))
))
(testing "trace"
(let [expected-greeting "Hider wilcuman. Béowulf is mín nama."
expected-quit-message "Sprecan 'quit' tó laéfan"
expected-error ""
expected-trace #".*traced-eval.*"
[_ greeting version error quit-message _ trace & _]
(with-open [r (reader (string->stream "cons[A; B]\nquit"))]
(binding [*in* r]
(split (with-out-str (-main "--trace")) #"\n")))]
(is (= greeting expected-greeting))
(is (= error expected-error))
(is (re-matches expected-trace trace))
))
)

View file

@ -1,4 +1,6 @@
(ns beowulf.mexpr-test
"These tests are taken generally from the examples on page 10 of
Lisp 1.5 Programmers Manual"
(:require [clojure.test :refer :all]
[beowulf.bootstrap :refer [*options*]]
[beowulf.read :refer [parse simplify generate gsp]]))