1c8e15d1e3d82452a94fa6f16124b81145ef3142
[platform/framework/web/crosswalk-tizen.git] /
1 /* globals describe:false, it:false */
2 /* jshint node:true */
3 "use strict";
4
5
6 var expect = require('expect.js');
7 var rocambole = require('../');
8
9
10 describe('moonwalk()', function () {
11
12     it('should generate AST if first arg is a string', function () {
13         var count = 0;
14         var ast = rocambole.moonwalk("function fn(x){\n  var foo = 'bar';\n  if (x) {\n  foo += 'baz';\n  } else {\n  foo += 's';\n   }\n  return foo;\n}", function(){
15             count++;
16         });
17         expect( ast.body ).not.to.be(undefined);
18         expect( ast.tokens ).not.to.be(undefined);
19         expect( count ).to.be.greaterThan( 1 );
20     });
21
22     it('should work with existing AST', function () {
23         var ast = rocambole.parse("function fn(x){\n  var foo = 'bar';\n  if (x) {\n  foo += 'baz';\n  } else {\n  foo += 's';\n   }\n  return foo;\n}");
24         var count = 0;
25         rocambole.moonwalk(ast, function(){
26             count++;
27         });
28         expect( count ).to.be.greaterThan( 1 );
29     });
30
31     it('should walk AST starting from leaf nodes until it reaches the root', function () {
32         var prevDepth = Infinity;
33         var count = 0;
34         var prevNode;
35         rocambole.moonwalk("function fn(x){\n  var foo = 'bar';\n  if (x) {\n  foo += 'baz';\n  } else {\n  foo += 's';\n   }\n  return foo;\n}", function(node){
36             count++;
37             expect( node.depth <= prevDepth ).to.be( true );
38             prevDepth = node.depth;
39             prevNode = node;
40         });
41         expect( count ).to.be.greaterThan( 1 );
42         expect( prevNode.type ).to.be( 'Program' ); // reached root
43     });
44
45     it('should skip null nodes', function(){
46       var count = 0;
47       rocambole.moonwalk('[,3,[,4]]', function () {
48         count++;
49       });
50       expect(count).to.be(6);
51     });
52
53 });
54
55
56 describe('recursive()', function () {
57
58     it('should allow breaking the loop', function () {
59         var ast = rocambole.parse('function fn(x){ return x * 2 }');
60         var count_1 = 0;
61         rocambole.recursive(ast, function(){
62             count_1 += 1;
63         });
64         var count_2 = 0;
65         rocambole.recursive(ast, function(node){
66             count_2 += 1;
67             if (node.type === 'BlockStatement') {
68                 return false; // break
69             }
70         });
71         expect( count_1 ).to.be( 9 );
72         expect( count_2 ).to.be( 5 );
73     });
74
75 });
76
77