Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
feat: support shoud.be.true/false() (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored and popomore committed May 2, 2017
1 parent 0f953c9 commit 3152eb0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,34 @@ module.exports = ({ path, source }, { jscodeshift: j }) => {

// @example
// a.b.should.be.ok() => assert(a.b);
// a.b.should.not.be.ok() => assert(!a.b);
// a.b.should.be.empty() => assert(a.b.length === 0);
root.find(j.CallExpression, {
callee: {
object: {
object: { property: { name: 'should' } },
property: { name: 'be' },
},
property: { name: 'ok' },
property(node) {
return /^ok|true|false|empty$/.test(node.name);
},
},
}).replaceWith(path => {
if (path.value.callee.property.name === 'false') {
return j.callExpression(
j.identifier('assert'), [
j.unaryExpression('!', path.value.callee.object.object.object),
]);
}
if (path.value.callee.property.name === 'empty') {
return j.callExpression(
j.identifier('assert'), [
j.binaryExpression('===',
j.memberExpression(path.value.callee.object.object.object, j.identifier('length')),
j.literal(0)
),
]
);
}
return j.callExpression(
j.identifier('assert'), [ path.value.callee.object.object.object ]
);
Expand Down Expand Up @@ -329,6 +347,17 @@ module.exports = ({ path, source }, { jscodeshift: j }) => {
]
));
}
if (path.value.expression.property.name === 'empty') {
return j.expressionStatement(
j.callExpression(
j.identifier('assert'), [
j.binaryExpression('===',
j.memberExpression(path.value.expression.object.object.object, j.identifier('length')),
j.literal(0)
),
]
));
}
return path;
});
root.find(j.ExpressionStatement, {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/input/test/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ describe('test/xxx.test.js', () => {
obj.should.have.properties({ a: '123', c: false });
obj.should.have.property('a');
a.should.length(3);
a.should.be.empty;
a.should.be.empty();
});
});
2 changes: 2 additions & 0 deletions test/fixtures/input/test/b.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ describe('test/b.test.js', () => {
obj.should.not.be.a.Date();
b.should.be.true;
c.should.be.false;
b.should.be.true();
c.should.be.false();
});
});
2 changes: 2 additions & 0 deletions test/fixtures/output/test/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ describe('test/xxx.test.js', () => {
Object.keys({ a: '123', c: false }).forEach(p => assert.deepEqual((obj)[p], ({ a: '123', c: false })[p]));
assert(Object.prototype.hasOwnProperty.call(obj, 'a'));
assert(a.length === 3);
assert(a.length === 0);
assert(a.length === 0);
});
});
2 changes: 2 additions & 0 deletions test/fixtures/output/test/b.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ describe('test/b.test.js', () => {
assert(Object.prototype.toString.call(obj) !== '[object Date]');
assert(b);
assert(!c);
assert(b);
assert(!c);
});
});

0 comments on commit 3152eb0

Please sign in to comment.