Unicode Clojure 单元测试输出

发布于 2024-10-15 03:45:16 字数 888 浏览 5 评论 0原文

当对一些将 ascii 序列转换为 unicode 字符的代码进行单元测试时,我发现 Clojure 测试的输出存在问题。

我已经测试过我的终端可以输出 unicode 字符(通过 cat-ing 测试文件)并且工作正常,所以问题似乎与 leiningen、Clojure 或 clojure.test 有关。

这是一个示例测试(使用 unicode 的希腊语部分 - 我也将使用希腊语扩展,但我假设也会出现相同的问题):

(deftest bc-string-w-comma
  (is (= "αβγ, ΑΒΓ" (parse "abg,*a*b*g"))))

由于输入中缺少空格,它会失败。 lein test 的输出如下:

Testing parse_perseus.test.betacode
FAIL in (bc-string-w-comma) (betacode.clj:15)
expected: (= "???, ???" (parse "abg,*a*b*g"))
  actual: (not (= "???, ???" "???,???"))
Testing parse_perseus.test.core
Testing parse_perseus.test.pluralise
Ran 10 tests containing 59 assertions.
1 failures, 0 errors.

我在这里做错了什么?这是终端仿真问题还是与 clojure 相关的问题?我在使用 Slime/swank/emacs 的 REPL 中运行代码时遇到同样的问题。 emacs 中的 REPL 只输出 unicode 输出的问号(尽管 emacs 非常有能力理解 unicode)。

我尝试在终端和 iTerm (OS X) 中运行它,得到相同的结果。

When unit testing some code that translates ascii sequences into unicode characters I have found a problem with the output of Clojure tests.

I have tested that my terminal can output unicode characters (by cat-ing the test files) and that works fine, so the problem seems related to leiningen, Clojure or clojure.test somehow.

Here's an example test (using the Greek section of unicode - I will also be using Greek extended but I assume the same problems will apply):

(deftest bc-string-w-comma
  (is (= "αβγ, ΑΒΓ" (parse "abg,*a*b*g"))))

It is meant to fail due to the missing space in the input. The output from lein test is the following:

Testing parse_perseus.test.betacode
FAIL in (bc-string-w-comma) (betacode.clj:15)
expected: (= "???, ???" (parse "abg,*a*b*g"))
  actual: (not (= "???, ???" "???,???"))
Testing parse_perseus.test.core
Testing parse_perseus.test.pluralise
Ran 10 tests containing 59 assertions.
1 failures, 0 errors.

What am I doing wrong here? Is this a terminal emulation problem or something clojure-related? I have the same problem running code in the REPL with Slime/swank/emacs. The REPL in emacs only outputs question marks for unicode output (although emacs is quite capable of understanding unicode).

I have tried running this in Terminal and iTerm (OS X) with the same results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-10-22 03:45:17

事实证明,您可以将选项传递给 java 以强制 *out* 的输出编码,以便 unicode 工作,如下所示:

java -Dfile.encoding=utf-8 -cp lib/clojure-1.2.0.jar:lib/clojure-contrib-1.2.0.jar clojure.main -i src/whatever.clj

当我使用 Leiningen 时,我将此属性添加到了我的 project.clj 中文件:

(defproject project_name "1.0.0-SNAPSHOT"
  :description "A Clojure Project"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]]
  :dev-dependencies [[swank-clojure "1.2.0"]]
  :jvm-opts ["-Dfile.encoding=utf-8"])

It turns out that you can pass options to java to force the output encoding of *out* so that unicode works, like this:

java -Dfile.encoding=utf-8 -cp lib/clojure-1.2.0.jar:lib/clojure-contrib-1.2.0.jar clojure.main -i src/whatever.clj

As I'm using Leiningen, I added this property to my project.clj file:

(defproject project_name "1.0.0-SNAPSHOT"
  :description "A Clojure Project"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]]
  :dev-dependencies [[swank-clojure "1.2.0"]]
  :jvm-opts ["-Dfile.encoding=utf-8"])
十年不长 2024-10-22 03:45:17

Clojure 本身似乎很清楚(这是 Ubuntu 10.10、gnome-terminal、OpenJDK):

john@woc-desktop$ java -cp /home/john/.m2/repository/org/clojure/clojure/1.2.0/clojure-1.2.0.jar:/home/john/.m2/repository/org/clojure/clojure-contrib/1.2.0/clojure-contrib-1.2.0.jar clojure.main
Clojure 1.2.0
user=> (use 'clojure.test)
nil
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "αβγ, ΑΒΓ" (parse ""))
  actual: (not (= "αβγ, ΑΒΓ" "αβγ,ΑΒΓ"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

破坏了 emacs/swank/clojure-maven-plugin/maven

但它确实在 emacs 中的 REPL 处

> (is "αβγ""αβγ")

slime-net-send: Coding system iso-latin-1-unix not suitable for "000052(:emacs-rex (swank:listener-eval \"(is \\\"αβγ\\\"\\\"αβγ\\\")

\") \"user\" :repl-thread 33)
"

:如果我使用 maven,下面的简单 pom 文件,和 mvn clojure:repl 那么就可以了:

[INFO] [clojure:repl {execution: default-cli}]
Clojure 1.2.0
user=> (use 'clojure.test) (is "αβγ""αβγ")
nil
"αβγ"
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "αβγ, ΑΒΓ" (parse ""))
  actual: (not (= "αβγ, ΑΒΓ" "αβγ,ΑΒΓ"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

但是如果我使用这个代码片段添加 jline 库:

<dependency>
  <groupId>jline</groupId>
  <artifactId>jline</artifactId>
  <version>0.9.94</version>
</dependency>

那么我得到:

[INFO] [clojure:repl {execution: default-cli}]
[INFO] Enabling JLine support
Clojure 1.2.0
user=> (use 'clojure.test) (is "αβγ""αβγ")
nil
"���"
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "���, ���" (parse ""))
  actual: (not (= "���, ���" "���,���"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

这看起来非常像你的错误。所以问题可能出在 jLine 中,或者 Leiningen 和 Maven 中与 jLine 相关的其他一些共同点。

或者当然,可能存在两个独立的与 unicode 相关的故障。

这是我的 Maven pom.xml 文件,以防有人尝试调试它。

<project>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aspden</groupId>
  <artifactId>maven-clojure-simple</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>maven-clojure-simple</name>
  <description>maven, clojure: simple project</description>

  <repositories>

    <repository>
      <id>clojure</id>
      <url>http://build.clojure.org/releases</url>
    </repository>
    <repository>
      <id>central</id>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
    <groupId>com.theoryinpractise</groupId>
    <artifactId>clojure-maven-plugin</artifactId>
    <version>1.3.5-SNAPSHOT</version>
      </plugin>
    </plugins>
  </build>

</project>

我很高兴这不是一个答案,但我认为这可能会有所帮助。

Clojure itself seems in the clear (this is Ubuntu 10.10, gnome-terminal, OpenJDK):

john@woc-desktop$ java -cp /home/john/.m2/repository/org/clojure/clojure/1.2.0/clojure-1.2.0.jar:/home/john/.m2/repository/org/clojure/clojure-contrib/1.2.0/clojure-contrib-1.2.0.jar clojure.main
Clojure 1.2.0
user=> (use 'clojure.test)
nil
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "αβγ, ΑΒΓ" (parse ""))
  actual: (not (= "αβγ, ΑΒΓ" "αβγ,ΑΒΓ"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

But it does break emacs/swank/clojure-maven-plugin/maven

at REPL in emacs:

> (is "αβγ""αβγ")

slime-net-send: Coding system iso-latin-1-unix not suitable for "000052(:emacs-rex (swank:listener-eval \"(is \\\"αβγ\\\"\\\"αβγ\\\")

\") \"user\" :repl-thread 33)
"

If I use maven, the simple pom file below, and mvn clojure:repl then it's ok:

[INFO] [clojure:repl {execution: default-cli}]
Clojure 1.2.0
user=> (use 'clojure.test) (is "αβγ""αβγ")
nil
"αβγ"
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "αβγ, ΑΒΓ" (parse ""))
  actual: (not (= "αβγ, ΑΒΓ" "αβγ,ΑΒΓ"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

but if I add the jline library using this snippet:

<dependency>
  <groupId>jline</groupId>
  <artifactId>jline</artifactId>
  <version>0.9.94</version>
</dependency>

then I get:

[INFO] [clojure:repl {execution: default-cli}]
[INFO] Enabling JLine support
Clojure 1.2.0
user=> (use 'clojure.test) (is "αβγ""αβγ")
nil
"���"
user=> (defn parse [s] "αβγ,ΑΒΓ")
#'user/parse
user=> (deftest greek (is (= "αβγ, ΑΒΓ" (parse ""))))
#'user/greek
user=> (run-tests)

Testing user

FAIL in (greek) (NO_SOURCE_FILE:3)
expected: (= "���, ���" (parse ""))
  actual: (not (= "���, ���" "���,���"))

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
{:type :summary, :test 1, :pass 0, :fail 1, :error 0}
user=> 

Which looks awfully like your error. So it may be that the problem is in jLine, or some other piece which Leiningen and maven have in common which is associated with jLine.

Or of course, there may be two independent unicode-related failures.

Here is my maven pom.xml file in case anyone is trying to debug this.

<project>

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aspden</groupId>
  <artifactId>maven-clojure-simple</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>maven-clojure-simple</name>
  <description>maven, clojure: simple project</description>

  <repositories>

    <repository>
      <id>clojure</id>
      <url>http://build.clojure.org/releases</url>
    </repository>
    <repository>
      <id>central</id>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>org.clojure</groupId>
      <artifactId>clojure</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
    <groupId>com.theoryinpractise</groupId>
    <artifactId>clojure-maven-plugin</artifactId>
    <version>1.3.5-SNAPSHOT</version>
      </plugin>
    </plugins>
  </build>

</project>

I appreciate this is not an answer, but i thought it might be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文