test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. })
  28. tape('generate property', function(t) {
  29. var gen = genfun()
  30. t.same(gen.property('a'), 'a')
  31. t.same(gen.property('42'), '"42"')
  32. t.same(gen.property('b', 'a'), 'b.a')
  33. t.same(gen.property('b', '42'), 'b["42"]')
  34. t.same(gen.sym(42), 'tmp')
  35. t.same(gen.sym('a'), 'a')
  36. t.same(gen.sym('a'), 'a1')
  37. t.same(gen.sym(42), 'tmp1')
  38. t.same(gen.sym('const'), 'tmp2')
  39. t.end()
  40. })