ReScriptトラックでのテスト

ExercismでReScriptの演習をテストする方法を学びます


演習をダウンロードする

取り組みたい演習をダウンロードするには、Exercism CLIを使います。

exercism download --track=rescript --exercise=hello-world

次に、演習のディレクトリへ移動します。

cd /path/to/exercism/rescript/hello-world

依存関係をインストールする

テストを実行する前に、演習の依存関係をインストールします。

npm install

テストを実行する

用意されたテストスイートをコンパイルして実行するには、exercism testかnpm run testのいずれかを使います。

テスト結果の見方

テストランナーは、実行した各テストを成功・失敗のステータスで表示します。

1/3: no name given
  PASS - no name given
2/3: a name given
  FAIL - a name given
    ---
    operator: equal
    left:  One for you, one for me.
    right: One for Alice, one for me.
    ...
3/3: another name given
  FAIL - another name given
    ---
    operator: equal
    left:  One for you, one for me.
    right: One for Bob, one for me.
    ...

# Ran 3 tests (3 assertions)
# 1 passed
# 2 failed

leftはコードが返した値、rightはテストが期待した値です。

演習の構成

two-fer/
├── src/
│   ├── TwoFer.res   // your solution goes here
│   └── TwoFer.resi  // the optional interface file containing signatures
├── tests/
│   └── TwoFer_test.res // your test suite
├── package.json // used to install project dependencies
└── ...

ここにあるTwoFer.resiは、省略可能なインターフェースファイルです。 このファイルがあると、解答が満たさなければならない関数シグネチャが定義され、何を実装すべきかのヒントにもなります。

たとえば、インターフェースで次のように宣言されているとします。

let hello: unit => string

実装では、次のように定義します。

let hello = () => "Hello, World!"

コンパイラのエラー

コードがコンパイルできない場合、ReScriptのコンパイラが、正確な位置と期待される型を示す詳しいエラーメッセージを出します。

  We've found a bug for you!

  1 │ let x: string = 42

  This has type: int
  But it's expected to have type: string

  You can convert int to string with Int.toString.

エラーメッセージは、たいてい問題の箇所を直接指し示してくれます。よく読んでみましょう。