1 var test = require('tap').test;
2 var burrito = require('../');
3 var vm = require('vm');
5 test('preserve ternary parentheses', function (t) {
6 var originalSource = '"anything" + (x ? y : z) + "anything"';
7 var burritoSource = burrito(originalSource, function (node) {
8 // do nothing. we just want to check that ternary parens are persisted
13 y : 'y_'+~~(Math.random()*10),
14 z : 'z_'+~~(Math.random()*10)
17 var expectedOutput = vm.runInNewContext(originalSource, ctxt);
18 var burritoOutput = vm.runInNewContext(burritoSource, ctxt);
20 t.equal(burritoOutput, expectedOutput);
24 expectedOutput = vm.runInNewContext(originalSource, ctxt);
25 burritoOutput = vm.runInNewContext(burritoSource, ctxt);
27 t.equal(burritoOutput, expectedOutput);
31 test('wrap calls', function (t) {
33 var src = burrito('f() && g(h())\nfoo()', function (node) {
34 if (node.name === 'call') node.wrap('qqq(%s)');
35 if (node.name === 'binary') node.wrap('bbb(%s)');
37 t.equal(this, node.state);
40 var times = { bbb : 0, qqq : 0 };
43 vm.runInNewContext(src, {
54 f : function () { return true },
59 h : function () { return 7 },
60 foo : function () { return 'foo!' },
67 false, // f() && g(h())
70 t.equal(times.bbb, 1);
71 t.equal(times.qqq, 4);
75 test('wrap fn', function (t) {
76 var src = burrito('f(g(h(5)))', function (node) {
77 if (node.name === 'call') {
78 node.wrap(function (s) {
79 return 'z(' + s + ')';
86 vm.runInNewContext(src, {
87 f : function (x) { return x + 1 },
88 g : function (x) { return x + 2 },
89 h : function (x) { return x + 3 },
95 (((((5 + 3) * 10) + 2) * 10) + 1) * 10
101 test('binary string', function (t) {
102 var src = 'z(x + y)';
106 z : function (n) { return n * 10 },
109 var res = burrito.microwave(src, context, function (node) {
110 if (node.name === 'binary') {
111 node.wrap('%a*2 - %b*2');
115 t.equal(res, 10 * (3*2 - 4*2));
119 test('binary fn', function (t) {
120 var src = 'z(x + y)';
124 z : function (n) { return n * 10 },
127 var res = burrito.microwave(src, context, function (node) {
128 if (node.name === 'binary') {
129 node.wrap(function (expr, a, b) {
130 return '(' + a + ')*2 - ' + '(' + b + ')*2';
135 t.equal(res, 10 * (3*2 - 4*2));
139 test('intersperse', function (t) {
140 var src = '(' + (function () {
143 }).toString() + ')()';
145 var times = { f : 0, g : 0, zzz : 0 };
148 f : function () { times.f ++ },
149 g : function () { times.g ++ },
150 zzz : function () { times.zzz ++ },
153 burrito.microwave(src, context, function (node) {
154 if (node.name === 'stat') node.wrap('{ zzz(); %s }');
157 t.same(times, { f : 1, g : 1, zzz : 3 });