test.js 796 B

123456789101112131415161718192021222324252627282930313233
  1. var tape = require('tape')
  2. var genfun = require('./')
  3. tape('generate add function', function(t) {
  4. var fn = genfun()
  5. ('function add(n) {')
  6. ('return n + %d', 42)
  7. ('}')
  8. t.same(fn.toString(), 'function add(n) {\n return n + 42\n}', 'code is indented')
  9. t.same(fn.toFunction()(10), 52, 'function works')
  10. t.end()
  11. })
  12. tape('generate function + closed variables', function(t) {
  13. var fn = genfun()
  14. ('function add(n) {')
  15. ('return n + %d + number', 42)
  16. ('}')
  17. var notGood = fn.toFunction()
  18. var good = fn.toFunction({number:10})
  19. try {
  20. notGood(10)
  21. t.ok(false, 'function should not work')
  22. } catch (err) {
  23. t.same(err.message, 'number is not defined', 'throws reference error')
  24. }
  25. t.same(good(11), 63, 'function with closed var works')
  26. t.end()
  27. })