1 /* globals describe:false, it:false */
6 var expect = require('expect.js');
7 var rocambole = require('../');
10 describe('moonwalk()', function () {
12 it('should generate AST if first arg is a string', function () {
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(){
17 expect( ast.body ).not.to.be(undefined);
18 expect( ast.tokens ).not.to.be(undefined);
19 expect( count ).to.be.greaterThan( 1 );
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}");
25 rocambole.moonwalk(ast, function(){
28 expect( count ).to.be.greaterThan( 1 );
31 it('should walk AST starting from leaf nodes until it reaches the root', function () {
32 var prevDepth = Infinity;
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){
37 expect( node.depth <= prevDepth ).to.be( true );
38 prevDepth = node.depth;
41 expect( count ).to.be.greaterThan( 1 );
42 expect( prevNode.type ).to.be( 'Program' ); // reached root
45 it('should skip null nodes', function(){
47 rocambole.moonwalk('[,3,[,4]]', function () {
50 expect(count).to.be(6);
56 describe('recursive()', function () {
58 it('should allow breaking the loop', function () {
59 var ast = rocambole.parse('function fn(x){ return x * 2 }');
61 rocambole.recursive(ast, function(){
65 rocambole.recursive(ast, function(node){
67 if (node.type === 'BlockStatement') {
68 return false; // break
71 expect( count_1 ).to.be( 9 );
72 expect( count_2 ).to.be( 5 );