Export 0.2.1
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / nodeunit / node_modules / tap / node_modules / runforcover / node_modules / bunker / node_modules / burrito / test / wrap.js
1 var test = require('tap').test;
2 var burrito = require('../');
3 var vm = require('vm');
4
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
9     });
10     
11     var ctxt = {
12         x : false,
13         y : 'y_'+~~(Math.random()*10),
14         z : 'z_'+~~(Math.random()*10)
15     };
16     
17     var expectedOutput = vm.runInNewContext(originalSource, ctxt);
18     var burritoOutput = vm.runInNewContext(burritoSource, ctxt);
19     
20     t.equal(burritoOutput, expectedOutput);
21     
22     ctxt.x = true;
23     
24     expectedOutput = vm.runInNewContext(originalSource, ctxt);
25     burritoOutput = vm.runInNewContext(burritoSource, ctxt);
26     
27     t.equal(burritoOutput, expectedOutput);
28     t.end();
29 });
30
31 test('wrap calls', function (t) {
32     t.plan(20);
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)');
36         t.ok(node.state);
37         t.equal(this, node.state);
38     });
39     
40     var times = { bbb : 0, qqq : 0 };
41     
42     var res = [];
43     vm.runInNewContext(src, {
44         bbb : function (x) {
45             times.bbb ++;
46             res.push(x);
47             return x;
48         },
49         qqq : function (x) {
50             times.qqq ++;
51             res.push(x);
52             return x;
53         },
54         f : function () { return true },
55         g : function (h) {
56             t.equal(h, 7);
57             return h !== 7
58         },
59         h : function () { return 7 },
60         foo : function () { return 'foo!' },
61     });
62     
63     t.same(res, [
64         true, // f()
65         7, // h()
66         false, // g(h())
67         false, // f() && g(h())
68         'foo!', // foo()
69     ]);
70     t.equal(times.bbb, 1);
71     t.equal(times.qqq, 4);
72     t.end();
73 });
74
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 + ')';
80             });
81         }
82     });
83     
84     var times = 0;
85     t.equal(
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 },
90             z : function (x) {
91                 times ++;
92                 return x * 10;
93             },
94         }),
95         (((((5 + 3) * 10) + 2) * 10) + 1) * 10
96     );
97     t.equal(times, 3);
98     t.end();
99 });
100
101 test('binary string', function (t) {
102     var src = 'z(x + y)';
103     var context = {
104         x : 3,
105         y : 4,
106         z : function (n) { return n * 10 },
107     };
108     
109     var res = burrito.microwave(src, context, function (node) {
110         if (node.name === 'binary') {
111             node.wrap('%a*2 - %b*2');
112         }
113     });
114     
115     t.equal(res, 10 * (3*2 - 4*2));
116     t.end();
117 });
118
119 test('binary fn', function (t) {
120     var src = 'z(x + y)';
121     var context = {
122         x : 3,
123         y : 4,
124         z : function (n) { return n * 10 },
125     };
126     
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';
131             });
132         }
133     });
134     
135     t.equal(res, 10 * (3*2 - 4*2));
136     t.end();
137 });
138
139 test('intersperse', function (t) {
140     var src = '(' + (function () {
141         f();
142         g();
143     }).toString() + ')()';
144     
145     var times = { f : 0, g : 0, zzz : 0 };
146     
147     var context = {
148         f : function () { times.f ++ },
149         g : function () { times.g ++ },
150         zzz : function () { times.zzz ++ },
151     };
152     
153     burrito.microwave(src, context, function (node) {
154         if (node.name === 'stat') node.wrap('{ zzz(); %s }');
155     });
156     
157     t.same(times, { f : 1, g : 1, zzz : 3 });
158     t.end();
159 });