Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Moved existing unit tests inline (and fixed a few)
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jul 12, 2012
1 parent 02573cf commit d3a6df3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -3,4 +3,5 @@
:url "https://github.com/jspahrsummers/objective-clojure" :url "https://github.com/jspahrsummers/objective-clojure"
:license "Public Domain" :license "Public Domain"
:main objclj.repl :main objclj.repl
:dependencies [[org.clojure/clojure "1.4.0"] [org.clojure/core.match "0.2.0-alpha10"] [org.van-clj/zetta-parser "0.0.3"]]) :dependencies [[org.clojure/clojure "1.4.0"] [org.clojure/core.match "0.2.0-alpha10"] [org.van-clj/zetta-parser "0.0.3"]]
:test-paths ["src"])
77 changes: 54 additions & 23 deletions src/objclj/reader.clj
Expand Up @@ -3,10 +3,18 @@
(:refer-clojure :exclude [char take-while replicate take]) (:refer-clojure :exclude [char take-while replicate take])
(:require [clojure.string :as s]) (:require [clojure.string :as s])
(:use clojure.algo.monads) (:use clojure.algo.monads)
(:use clojure.test)
(:use objclj.test)
(:use [zetta.core :exclude [parse]]) (:use [zetta.core :exclude [parse]])
(:use [zetta.parser.seq :exclude [get ensure whitespace skip-whitespaces]]) (:use [zetta.parser.seq :exclude [get ensure whitespace skip-whitespaces]])
(:use zetta.combinators)) (:use zetta.combinators))


(defn parse-str
"Runs parser p on string s and returns a two-item vector containing the result and any unparsed part of the string."
[p s]
(let [result (parse-once p s)]
[(-> result :result) (s/join (-> result :remainder))]))

;;; ;;;
;;; AST structure ;;; AST structure
;;; ;;;
Expand All @@ -22,13 +30,21 @@
vals (map #(nth % 1) pairs)] vals (map #(nth % 1) pairs)]
(zipmap keys vals))) (zipmap keys vals)))


(defn strip-empty-forms (with-test
"Collapses all instances of empty-form from the given sequence of forms (and all their sub-forms)." ; TODO: this doesn't work on maps right now
[forms] (defn strip-empty-forms
"Collapses all instances of empty-form from the given sequence of forms (and all their sub-forms)."
[forms]

; TODO: this could get nasty with too much recursion
(let [mapped-forms (map #(if (seq? %) (strip-empty-forms %) %) forms)]
(filter #(not (= empty-form %)) mapped-forms)))


; TODO: this could get nasty with too much recursion (is= (list) (strip-empty-forms (list empty-form)))
(let [mapped-forms (map #(if (seq? %) (strip-empty-forms %) %) forms)] (is= (list) (strip-empty-forms (list empty-form empty-form)))
(filter #(not (= empty-form %)) mapped-forms))) (is= (list '()) (strip-empty-forms (list empty-form (list empty-form))))
(is= (list []) (strip-empty-forms (list empty-form [empty-form])))
(is= (list #{}) (strip-empty-forms (list empty-form #{ empty-form }))))


;;; ;;;
;;; Character classes ;;; Character classes
Expand Down Expand Up @@ -105,22 +121,37 @@
\\ "\\" }) \\ "\\" })
(<$> str (not-char \")))) (<$> str (not-char \"))))


; TODO: this should be a reader macro (with-test
(def line-comment ; TODO: this should be a reader macro
"Parser that matches a line comment. Returns empty-form." (def line-comment
(<* (always empty-form) "Parser that matches a line comment. Returns empty-form."
(char \;) (<* (always empty-form)
(many-till any-token (char \;)
(<|> end-of-input eol)))) (many-till any-token

(<|> end-of-input eol))))
(def whitespace
"Parser that matches whitespace and comments. Returns empty-form." (is= [empty-form ""] (parse-str line-comment "; foobar"))
(<* (always empty-form) (is= [empty-form "foobar"] (parse-str line-comment "; foobar\r\nfoobar")))
(<|> (satisfy? whitespace?) line-comment)))

(with-test
(def skip-whitespaces (def whitespace
"Skips whitespace and comments." "Parser that matches whitespace and comments. Returns empty-form."
(skip-many whitespace)) (<* (always empty-form)
(<|> (satisfy? whitespace?) line-comment)))

(is= [empty-form ""] (parse-str whitespace " "))
(is= [empty-form ""] (parse-str whitespace "\t"))
(is= [empty-form ""] (parse-str whitespace "\n"))
(is= [empty-form ""] (parse-str whitespace "\r")))

(with-test
(def skip-whitespaces
"Skips whitespace and comments. Returns empty-form."
(<* (always empty-form)
(skip-many whitespace)))

(is= [empty-form ""] (parse-str skip-whitespaces " \n\t\r ; foo\n "))
(is= [empty-form "foo\n"] (parse-str skip-whitespaces " \n\t\r foo\n")))


(def sym-special-char (def sym-special-char
"Parser that matches any non-alphanumeric character that is allowed in a symbol. Returns the matched character." "Parser that matches any non-alphanumeric character that is allowed in a symbol. Returns the matched character."
Expand Down Expand Up @@ -216,4 +247,4 @@
(defn parse (defn parse
"Parses a string of Clojure code into an AST. Returns a sequence of forms." "Parses a string of Clojure code into an AST. Returns a sequence of forms."
[str] [str]
(strip-empty-forms (-> (parse-once (many form) str) :result))) (strip-empty-forms (parse-str (many form) str)))
2 changes: 1 addition & 1 deletion test/objclj/test/assertions.clj → src/objclj/test.clj
@@ -1,4 +1,4 @@
(ns objclj.test.assertions (ns objclj.test
(:use clojure.test)) (:use clojure.test))


(defmacro is= (defmacro is=
Expand Down
31 changes: 0 additions & 31 deletions test/objclj/test/reader.clj

This file was deleted.

0 comments on commit d3a6df3

Please sign in to comment.