首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试不符合Instaparse语法的文本(Clojure)?

如何测试不符合Instaparse语法的文本(Clojure)?
EN

Stack Overflow用户
提问于 2014-10-13 11:28:31
回答 1查看 606关注 0票数 6

我编写了一个项目,在Instaparse (Clojure)中使用上下文无关语法来解析字符串。现在,我想测试几个输入-字符串的解析结果。有些输入字符串可能不适合语法。到目前为止,我只测试了“解析的字符串不符合预期”。但我认为使用(is (thrown? ...))测试异常会更准确。有异常抛出吗?在我看来,生成了一些输出(包含Parse error...),但没有抛出任何异常。

我的project.clj是:

代码语言:javascript
复制
(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"]])

我的核心来源是:

代码语言:javascript
复制
(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)))

我的测试来源是:

代码语言:javascript
复制
(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)

我应该如何测试错误(这里:当句子不是以.结尾,而是以!结尾时)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-13 15:49:55

Instaparse不对解析错误抛出异常;相反,它返回一个"failure对象“(ref:解析错误)。可以使用(insta/failure? result)测试失败对象。

如果希望解析器/格式化程序对意外输入抛出异常,请将其添加到核心中:

代码语言:javascript
复制
(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)))

...and现在您可以在测试中使用(is (thrown? ...))

代码语言:javascript
复制
(deftest parser-tests
  (is (= [[:word "Hello"] [:word "World"]] (parser "Hello World.")))
  (is (thrown? Exception (= [[:word "Hello"] [:word "World"]] (parser "Hello World?"))))

此方法使用instaparse漂亮地打印故障并将其封装在异常中。另一种方法是使用ex-info,如本回答所述。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26338945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档