在 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.

仔细阅读错误信息,它通常能直接指出问题所在。