Skip to content

Update: no-identical-tests despite of properties order. #21

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

Merged
merged 7 commits into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
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
46 changes: 42 additions & 4 deletions lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,53 @@ module.exports = {
const message = 'This test case is identical to another case.';
const sourceCode = context.getSourceCode();

// ----------------------------------------------------------------------
// Helpers
// ----------------------------------------------------------------------
/**
*compare two test cases despite of properties order.
*@returns {boolean} if eq, return true, else return false.
*/
function eq (testA, testB) {
if (testA.type !== testB.type) {
return false;
}

if (testA.type !== 'ObjectExpression') {
return sourceCode.getText(testA) === sourceCode.getText(testB);
}

const propertiesA = testA.properties || [];
const propertiesB = testB.properties || [];

// if properties length not eq; return false;
if (propertiesA.length !== propertiesB.length) {
return false;
}

const propertiesSetA = new Set();
propertiesA.forEach(item => {
const code = sourceCode.getText(item);
propertiesSetA.add(code);
});

for (let i = 0; i < propertiesB.length; i++) {
const code = sourceCode.getText(propertiesB[i]);
if (!propertiesSetA.has(code)) {
return false;
}
}
return true;
}

return {
Program (ast) {
utils.getTestInfo(context, ast).forEach(testRun => {
[testRun.valid, testRun.invalid].forEach(tests => {
const cache = Object.create(null);
const cache = [];
// to avoid tests being null
(tests || []).forEach(test => {
const testCode = sourceCode.getText(test);
if (cache[testCode]) {
if (cache.some(item => eq(item, test))) {
context.report({
node: test,
message,
Expand All @@ -50,7 +88,7 @@ module.exports = {
},
});
} else {
cache[testCode] = true;
cache.push(test);
}
});
});
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ ruleTester.run('no-identical-tests', rule, {
invalid: []
});
`,
`
new RuleTester().run('foo', bar, {
valid: [
'foo',
],
invalid: []
});
`,
],

invalid: [
Expand Down Expand Up @@ -106,5 +114,45 @@ ruleTester.run('no-identical-tests', rule, {
`,
errors: [ERROR, ERROR],
},
{
code: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo', options: ['bar'] },
{ options: ['bar'], code: 'foo' },
],
invalid: []
});
`,
output: `
new RuleTester().run('foo', bar, {
valid: [
{ code: 'foo', options: ['bar'] },
],
invalid: []
});
`,
errors: [ERROR],
},
{
code: `
new RuleTester().run('foo', bar, {
valid: [
'foo',
'foo',
],
invalid: []
});
`,
output: `
new RuleTester().run('foo', bar, {
valid: [
'foo',
],
invalid: []
});
`,
errors: [ERROR],
},
],
});