From 141ea98b89028c0ea2cbc0120d121c5ad47e295c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 12 Oct 2014 10:19:51 +1100 Subject: [PATCH] more elaborate traverse test --- test/traverse.js | 59 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/test/traverse.js b/test/traverse.js index beb774717a..432ef6b0c3 100644 --- a/test/traverse.js +++ b/test/traverse.js @@ -1,9 +1,11 @@ var traverse = require("../lib/6to5/traverse"); var assert = require("assert"); +var _ = require("lodash"); suite("traverse", function () { - test("hasType", function () { - assert.ok(traverse.hasType([ + var ast = { + type: "Program", + body: [ { "type": "VariableDeclaration", "declarations": [ @@ -45,6 +47,57 @@ suite("traverse", function () { } } } - ], "ThisExpression")); + ] + }; + + var body = ast.body; + + test("hasType", function () { + assert.ok(traverse.hasType(ast, "ThisExpression")); + assert.ok(!traverse.hasType(ast, "ThisExpression", ["AssignmentExpression"])); + }); + + test("traverse", function () { + var expect = [ + body[0], body[0].declarations[0], body[0].declarations[0].id, body[0].declarations[0].init, + body[1], body[1].expression, body[1].expression.left, body[1].expression.left.object, body[1].expression.left.property, body[1].expression.right + ]; + + var actual = []; + + traverse(ast, function (node) { + actual.push(node); + }); + + assert.deepEqual(actual, expect); + }); + + test("traverse blacklistTypes", function () { + var expect = [ + body[0], body[0].declarations[0], body[0].declarations[0].id, body[0].declarations[0].init, + body[1], body[1].expression, body[1].expression.right + ]; + + var actual = []; + + traverse(ast, function (node) { + actual.push(node); + }, ["MemberExpression"]); + + assert.deepEqual(actual, expect); + }); + + test("traverse replace", function () { + var replacement = { + type: "Literal", + value: "foo" + }; + var ast2 = _.cloneDeep(ast); + + traverse(ast2, function (node) { + if (node.type === "ThisExpression") return replacement; + }); + + assert.equal(ast2.body[1].expression.left.object, replacement); }); });