LoginSignup
5
5

More than 5 years have passed since last update.

R17.0のeunitでタイトルに日本語を使う方法

Posted at

Erlang/OTPのR17.0からソースコードのデフォルトエンコーディングがISO-8859-1(latin-1)からUTF-8へと変更されたらしい。
ただ、eunitがまだユニコードに完全に対応できていないようで、ユニットテストのタイトル部分に日本語を使うと、以下のようなエラーが表示されてしまう。

%% サンプルユニットテスト
-module(sample_tests).

-include_lib("eunit/include/eunit.hrl").

hoge_test_() ->
  [
   {"テストのタイトルを日本語で記述",
    fun () ->
      ?assert(true)
    end}
  ].
$ erl
> c(sample_tests).
{ok,sample_tests}

> eunit:test([sample_tests]).
undefined
*unexpected termination of test process*
::{badarg,[{erlang,list_to_binary,
                   [[12486,12473,12488,12398,12479,12452,12488|...]],
                   []},
           {eunit_data,parse,1,[{file,"eunit_data.erl"},{line,394}]},
           {eunit_data,next,1,[{file,"eunit_data.erl"},{line,170}]},
           {eunit_data,lookahead,1,[{file,"eunit_data.erl"},{line,530}]},
           {eunit_data,group,1,[{file,[...]},{line,...}]},
           {eunit_data,next,1,[{file,...},{...}]},
           {eunit_data,iter_next,1,[{...}|...]},
           {eunit_proc,get_next_item,1,[...]}]}

=======================================================
  Failed: 0.  Skipped: 0.  Passed: 0.
One or more tests were cancelled.

=ERROR REPORT==== 30-Jun-2014::04:38:26 ===
Error in process <0.80.0> with exit value: {badarg,[{erlang,list_to_binary,[[12486,12473,12488,12398,12479,12452,12488,12523,12434,26085,26412,35486,12391,35352,36848]],[]},{eunit_data,parse,1,[{file,"eunit_data.erl"},{line,394}]},{eunit_data,next,1,[{file,"eunit_data.erl"},{line,170}]},{eunit_data,lookahead,1,[{file,"eunit_data.erl"},{line,530}]},{eunit_data...

error

タイトル部分の文字列はユニコード文字としてパースされているのに、それを(1byte文字に対してのみ適用可能な) erlang:list_to_binary/1を使ってバイナリに変換しようとしているためにエラーが発生している模様 (unicode:characters_to_binary/1を使うのが正解)。

とりあえずの暫定対処としては、下記のように、ユニットテストモジュールの先頭でソースコードのエンコーディングを明示的にlatin-1と指定するようにすれば、(R16以前と同じ挙動となり)エラーはでなくなる。

%% coding: latin-1
%% エンコーディングの指定方法は http://www.erlang.org/doc/man/epp.html を参照
%%
%% サンプルユニットテスト
-module(sample_tests).

-include_lib("eunit/include/eunit.hrl").

hoge_test_() ->
  [
   {"テストのタイトルを日本語で記述",
    fun () ->
      ?assert(true)
    end}
  ].
$ erl
> c(sample_tests).
{ok, sample_tests}.

> eunit:test([sample_tests]).
  Test passed.
ok
5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5