I wrote a project to parse strings using context-free grammar in Instaparse (Clojure). Now I would like to test several input lines for their parsing results. Some input lines may not fit into the grammar. So far I have tested only "parsed strings that do not meet expectations." But I think it would be more accurate to check for exceptions using (is (thrown? ...)) . Are there any exceptions? It seems to me that some output is generated (containing Parse error... ), but an exception is not generated.
My .clj project:
(defproject com.stackoverflow.clojure/tests "0.1.0-SNAPSHOT" :description "Tests of Clojure test-framework." :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.6.0"] [instaparse "1.3.4"]])
My main source:
(ns com.stackoverflow.clojure.testInstaparseWrongGrammar (:require [instaparse.core :as insta])) (def parser (insta/parser " <sentence> = words <DOT> DOT = '.' <words> = word (<SPACE> word)* SPACE = ' ' word = #'(?U)\\w+' ")) (defn formatter [expr] (->> (parser expr) (insta/transform {:word identity}) (apply str)))
My test source:
(ns com.stackoverflow.clojure.testInstaparseWrongGrammar-test (:require [clojure.test :refer :all] [com.stackoverflow.clojure.testInstaparseWrongGrammar :refer :all])) (deftest parser-tests (is (= [[:word "Hello"] [:word "World"]] (parser "Hello World."))) (is (not (= [[:word "Hello"] [:word "World"]] (parser "Hello World?")))) ;(parser "Hello World?") gives: ; ;Parse error at line 1, column 12: ;Hello World? ; ^ ;Expected one of: ;"." (followed by end-of-string) ;" " ) (deftest formatter-tests (is (= "HelloWorld" (formatter "Hello World."))) (is (not (= "HelloWorld" (formatter "Hello World?")))) ;(formatter "Hello World?") gives: ;"[:index 11][:reason [{:tag :string, :expecting \".\", :full true} {:tag :string, :expecting \" \"}]][:text \"Hello World?\"][:column 12][:line 1]" ) ; run the tests (run-tests)
How should I check for errors (here: when the sentence does not end with . , But with ! )?
source share