我正在尝试使用 org-babel 在识字程序中进行简单的单元测试。
这个想法是我会定义一个函数(的主体),就像这样
#+NAME: square
#+BEGIN_SRC js :var x=0
return x*x;
#+END_SRC
当然,我还需要实际创建一个用于 tangling 的函数,我可以通过
#+BEGIN_SRC js :noweb tangle :tangle the-script.js
function square(x) {
<<square>>
}
#+END_SRC
然后我会设置一个测试值表,就像这样
#+TBLNAME: squares-test-data
| the square of | is |
|---------------+---------|
| 0 | 0 |
| 1 | 1 |
| 10 | 100 |
| 12 | 144 |
| 4 | 16 |
| 9 | 81 |
| 100 | 10000 |
| 234 | 8273424 |
问题是,我如何使用这些数据从单独的块中对函数体运行测试,请记住
- 我不希望在纠结的输出中包含测试代码
- 对于使用字符串的函数,我不希望表中引用的字符串
- 我宁愿避免表中重复的“预期”和“得到”值——简单的检查或 X 表示通过/失败会很棒
基本上,我想做的是这样的:
#+BEGIN_SRC js :var table=squares-test-data
// Import square block here somehow?
return table.map(function(row) {
var input = row[0],
expected = row[1],
got = square(input), // this won't be defined
passed = got == expected;
return [input, expected, passed]
});
#+END_SRC
但是这个块并不“知道”另一个块。
有任何想法吗?