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.

仔細閱讀錯誤訊息,因為它通常會直接指出問題所在。