Edw*_*ard 6 parsing exception clojure context-free-grammar instaparse
我在Instaparse(Clojure)中编写了一个使用无上下文语法解析字符串的项目.现在我想测试几个输入字符串的解析结果.某些输入字符串可能不适合语法.到目前为止,我只测试了"解析后的字符串不符合预期".但我认为使用测试异常会更准确(is (thrown? ...)).是否抛出异常?在我看来,Parse error...生成了一些输出(Containing ),但没有抛出任何异常.
我的project.clj是:
(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"]])
Run Code Online (Sandbox Code Playgroud)
我的核心资源是:
(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)))
Run Code Online (Sandbox Code Playgroud)
我的测试来源是:
(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)
Run Code Online (Sandbox Code Playgroud)
我该如何测试错误(这里:当句子没有以a结尾.但是有a时!)?
Instaparse不会在解析错误上抛出异常; 相反,它返回一个"失败对象"(ref:解析错误).您可以使用测试失败对象(insta/failure? result).
如果您希望解析器/格式化程序在意外输入上引发异常,请将其添加到您的核心:
(ns com.stackoverflow.clojure.testInstaparseWrongGrammar
(:require [instaparse.core :as insta])
(:require [instaparse.failure :as fail]))
(def raw-parser (insta/parser "
<sentence> = words <DOT>
DOT = '.'
<words> = word (<SPACE> word)*
SPACE = ' '
word = #'(?U)\\w+'
"))
; pretty-print a failure as a string
(defn- failure->string [result]
(with-out-str (fail/pprint-failure result)))
; create an Exception with the pretty-printed failure message
(defn- failure->exn [result]
(Exception. (failure->string result)))
(defn parser [expr]
(let [result (raw-parser expr)]
(if (insta/failure? result)
(throw (failure->exn result))
result)))
(defn formatter [expr]
(->> (parser expr)
(insta/transform {:word identity})
(apply str)))
Run Code Online (Sandbox Code Playgroud)
...现在你可以(is (thrown? ...))在测试中使用:
(deftest parser-tests
(is (= [[:word "Hello"] [:word "World"]] (parser "Hello World.")))
(is (thrown? Exception (= [[:word "Hello"] [:word "World"]] (parser "Hello World?"))))
Run Code Online (Sandbox Code Playgroud)
这种方法使用instaparse来完美地打印故障并将其包装在Exception中.另一种方法是使用ex-info本答案中概述的内容.
| 归档时间: |
|
| 查看次数: |
510 次 |
| 最近记录: |