Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make AssertionVisitor more overridable #19

Merged
merged 2 commits into from
Nov 3, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions lib/assertion-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function AssertionVisitor (matcher, assertionPath, options) {
}

AssertionVisitor.prototype.enter = function (currentNode, parentNode) {
this.canonicalCode = generateCanonicalCode(currentNode, this.options.visitorKeys);
this.powerAssertCalleeObject = guessPowerAssertCalleeObjectFor(currentNode.callee);
this.canonicalCode = this.generateCanonicalCode(currentNode);
this.powerAssertCalleeObject = this.guessPowerAssertCalleeObjectFor(currentNode.callee);

if (this.sourceMapConsumer) {
var pos = this.sourceMapConsumer.originalPositionFor({
Expand Down Expand Up @@ -203,30 +203,8 @@ AssertionVisitor.prototype.toBeCaptured = function (currentNode, parentNode, cur
return toBeCaptured(currentNode, parentNode, currentKey);
};

function fallbackOnBasename (filepath) {
if (filepath) {
if (filepath.split(_path.sep).indexOf('..') !== -1) {
return _path.basename(filepath);
} else if (isUrl(filepath)) {
return _path.basename(filepath);
} else if (isAbsolute(filepath)) {
return _path.basename(filepath);
}
}
return filepath;
}

function guessPowerAssertCalleeObjectFor (node) {
switch(node.type) {
case syntax.Identifier:
return node;
case syntax.MemberExpression:
return node.object; // Returns browser.assert when browser.assert.element(selector)
}
return null;
}

function generateCanonicalCode (node, visitorKeys) {
AssertionVisitor.prototype.generateCanonicalCode = function (node) {
var visitorKeys = this.options.visitorKeys;
var ast = espurifyWithRaw(node);
var visitor = {
leave: function (currentNode, parentNode) {
Expand All @@ -246,6 +224,29 @@ function generateCanonicalCode (node, visitorKeys) {
}
estraverse.replace(ast, visitor);
return escodegen.generate(ast, canonicalCodeOptions);
};

AssertionVisitor.prototype.guessPowerAssertCalleeObjectFor = function (node) {
switch(node.type) {
case syntax.Identifier:
return node;
case syntax.MemberExpression:
return node.object; // Returns browser.assert when browser.assert.element(selector)
}
return null;
};

function fallbackOnBasename (filepath) {
if (filepath) {
if (filepath.split(_path.sep).indexOf('..') !== -1) {
return _path.basename(filepath);
} else if (isUrl(filepath)) {
return _path.basename(filepath);
} else if (isAbsolute(filepath)) {
return _path.basename(filepath);
}
}
return filepath;
}

function addLiteralTo (props, createNode, name, data) {
Expand Down