Skip to content

Commit

Permalink
JS-490 Fix Rule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez authored and kebetsi committed Jan 13, 2025
1 parent f071805 commit 0b7da41
Show file tree
Hide file tree
Showing 466 changed files with 27,886 additions and 25,984 deletions.
187 changes: 102 additions & 85 deletions packages/jsts/src/rules/S100/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,64 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import { NodeRuleTester } from '../../../tests/tools/testers/rule-tester.js';
import { rule } from './index.js';
import { fileURLToPath } from 'node:url';
import { RuleTester } from '../../../tests/tools/testers/rule-tester.js';
import { describe, it } from 'node:test';

const ruleTester = new NodeRuleTester({
parser: fileURLToPath(import.meta.resolve('@typescript-eslint/parser')),
parserOptions: { ecmaVersion: 2018, ecmaFeatures: { jsx: true } },
});
const ruleTester = new RuleTester();

const DEFAULT_FORMAT = '^[_a-z][a-zA-Z0-9]*$';
const ALLOW_UPPERCASE = '^[A-Z][a-zA-Z0-9]*$';

ruleTester.run('Function names should comply with a naming convention', rule, {
valid: [
{
code: `
describe('S100', () => {
it('S100', () => {
ruleTester.run('Function names should comply with a naming convention', rule, {
valid: [
{
code: `
function doSomething(){}
function _doSomething(){}
function* doSomething(){}
let doSomething = function Object () {}
`,
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
class C {
doSomething(){ }
* doSomething (){}
}
`,
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
function DoSomething() {}
`,
options: [{ format: ALLOW_UPPERCASE }],
},
{
code: `
options: [{ format: ALLOW_UPPERCASE }],
},
{
code: `
function Welcome() {
const greeting = 'Hello, world!';
return <h1>{greeting}</h1>
}`,
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
const Welcome = () => {
const greeting = 'Hello, world!';
return <h1>{greeting}</h1>
}`,
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
const Welcome = function() {
const greeting = 'Hello, world!';
Expand All @@ -82,78 +81,91 @@ ruleTester.run('Function names should comply with a naming convention', rule, {
</>
)
}`,
options: [{ format: DEFAULT_FORMAT }],
},
],
invalid: [
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
],
invalid: [
{
code: `
function DoSomething(){}
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
message: `Rename this 'DoSomething' function to match the regular expression '${DEFAULT_FORMAT}'.`,
line: 2,
endLine: 2,
column: 18,
endColumn: 29,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
message: `Rename this 'DoSomething' function to match the regular expression '${DEFAULT_FORMAT}'.`,
line: 2,
endLine: 2,
column: 18,
endColumn: 29,
},
],
},
],
},
{
code: `
{
code: `
function do_something(){}
function* DoSomething(){}
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
line: 2,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
messageId: `renameFunction`,
line: 2,
},
{
messageId: `renameFunction`,
line: 3,
},
],
},
{
line: 3,
},
],
},
{
code: `
code: `
class C {
DoSomething(){}
* DoSomething (){}
}
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [{ line: 3 }, { line: 4 }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
messageId: `renameFunction`,
line: 3,
},
{
messageId: `renameFunction`,
line: 4,
},
],
},
{
code: `
var MyObj1 = function Object () {
this.a = 1;
};
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
line: 2,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
messageId: `renameFunction`,
line: 2,
},
],
},
],
},
{
code: `
{
code: `
var MyObj1 = () => {
this.a = 1;
};
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
line: 2,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
messageId: `renameFunction`,
line: 2,
},
],
},
],
},
{
code: `
{
code: `
var myObj = {
Method1() {},
Method2: function() {},
Expand All @@ -169,10 +181,15 @@ ruleTester.run('Function names should comply with a naming convention', rule, {
}
};
`,
options: [{ format: DEFAULT_FORMAT }],
errors: [3, 4, 5, 6, 9, 12].map(line => {
return { line };
}),
},
],
options: [{ format: DEFAULT_FORMAT }],
errors: [3, 4, 5, 6, 9, 12].map(line => {
return {
messageId: `renameFunction`,
line,
};
}),
},
],
});
});
});
103 changes: 53 additions & 50 deletions packages/jsts/src/rules/S101/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,78 @@
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
import { NodeRuleTester } from '../../../tests/tools/testers/rule-tester.js';
import { rule } from './index.js';
import { fileURLToPath } from 'node:url';
import { RuleTester } from '../../../tests/tools/testers/rule-tester.js';
import { describe, it } from 'node:test';

const ruleTester = new NodeRuleTester({
parser: fileURLToPath(import.meta.resolve('@typescript-eslint/parser')),
parserOptions: { ecmaVersion: 2018, ecmaFeatures: { jsx: true } },
});
const ruleTester = new RuleTester();

const DEFAULT_FORMAT = '^[A-Z][a-zA-Z0-9]*$';
const CUSTOM_FORMAT = '^[_A-Z][a-zA-Z0-9]*$';

ruleTester.run('Class and interface names should comply with a naming convention', rule, {
valid: [
{
code: `
describe('S101', () => {
it('S101', () => {
ruleTester.run('Class and interface names should comply with a naming convention', rule, {
valid: [
{
code: `
class MyClass {}
var x = class y {} // Compliant, rule doesn't check class expressions
interface MyInterface {}
`,
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
options: [{ format: DEFAULT_FORMAT }],
},
{
code: `
class MyClass {}
class _MyClass {}
interface _MyInterface {}
`,
options: [{ format: CUSTOM_FORMAT }],
},
],
invalid: [
{
code: `class my_class {}`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
message: `Rename class "my_class" to match the regular expression ${DEFAULT_FORMAT}.`,
line: 1,
endLine: 1,
column: 7,
endColumn: 15,
options: [{ format: CUSTOM_FORMAT }],
},
],
},
{
code: `interface my_interface {}`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
invalid: [
{
message: `Rename interface "my_interface" to match the regular expression ${DEFAULT_FORMAT}.`,
code: `class my_class {}`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
message: `Rename class "my_class" to match the regular expression ${DEFAULT_FORMAT}.`,
line: 1,
endLine: 1,
column: 7,
endColumn: 15,
},
],
},
{
code: `interface my_interface {}`,
options: [{ format: DEFAULT_FORMAT }],
errors: [
{
message: `Rename interface "my_interface" to match the regular expression ${DEFAULT_FORMAT}.`,
},
],
},
{
code: `class __MyClass {}`,
options: [{ format: CUSTOM_FORMAT }],
errors: [
{
message: `Rename class "__MyClass" to match the regular expression ${CUSTOM_FORMAT}.`,
},
],
},
],
},
{
code: `class __MyClass {}`,
options: [{ format: CUSTOM_FORMAT }],
errors: [
{ message: `Rename class "__MyClass" to match the regular expression ${CUSTOM_FORMAT}.` },
],
},
{
code: `interface __MyInterface {}`,
options: [{ format: CUSTOM_FORMAT }],
errors: [
{
message: `Rename interface "__MyInterface" to match the regular expression ${CUSTOM_FORMAT}.`,
code: `interface __MyInterface {}`,
options: [{ format: CUSTOM_FORMAT }],
errors: [
{
message: `Rename interface "__MyInterface" to match the regular expression ${CUSTOM_FORMAT}.`,
},
],
},
],
},
],
});
});
});
Loading

0 comments on commit 0b7da41

Please sign in to comment.