Search code examples
javascriptorg-babel

Call a function defined in another source block


I'm trying to use org-babel for simple unit testing within a literate program.

The idea is that I'd define (the body of) a function, like so

#+NAME: square
#+BEGIN_SRC js :var x=0
return x*x;
#+END_SRC

Of course, I also need to actually create a function for tangling, which I can do by

#+BEGIN_SRC js :noweb tangle :tangle the-script.js
function square(x) {
    <<square>>
}
#+END_SRC

Then I'd set up a table of test values, like so

#+TBLNAME: squares-test-data
| the square of |      is |
|---------------+---------|
|             0 |       0 |
|             1 |       1 |
|            10 |     100 |
|            12 |     144 |
|             4 |      16 |
|             9 |      81 |
|           100 |   10000 |
|           234 | 8273424 | 

The question is, how can I use this data to run tests against the function body from a separate block, keeping in mind that

  • I don't want the test code included in the tangled output
  • for functions using strings, I don't want the strings quoted in the table
  • I'd rather avoid duplicate "expected" and "got" values in the table -- a simple check or X for pass/fail would be great

Basically, what I want to do is something like this:

#+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

But this block doesn't "know about" the other block.

Any ideas?


Solution

  • This was mostly just a simple oversight.

    You can "import" blocks into other blocks before evaluation in this manner:

    #+BEGIN_SRC js :noweb eval
    <<code-under-test>>
    // run some tests
    #+END_SRC
    

    I was so habituated to using :noweb tangle that I failed to realize there are other options. :noweb eval ensures that blocks are tangled before evaluation, which is what I want here.

    That said, I did find that org tables were unworkable as input sources in most cases, but I have a satisfactory alternative which I've written about and will link to here when it is posted.