Skip to content

Commit

Permalink
Init test
Browse files Browse the repository at this point in the history
  • Loading branch information
xcatliu committed Nov 22, 2018
1 parent a0f36e8 commit cd2d695
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 5 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"tslint": "tslint --project . ./**/*.ts",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha -r ts-node/register test/index.ts"
},
"repository": {
"type": "git",
Expand All @@ -31,8 +31,11 @@
},
"homepage": "https://github.com/AlloyTeam/tslint-config-alloy#readme",
"devDependencies": {
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.9",
"mocha": "^5.2.0",
"prettier": "^1.15.2",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.16.0",
"typescript": "^3.1.6"
Expand Down
8 changes: 4 additions & 4 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// prettier.config.js or .prettierrc.js
module.exports = {
// 一行最多 160 字符
printWidth: 160,
// 一行最多 80 字符
printWidth: 80,
// 使用 4 个空格缩进
tabWidth: 4,
// 不使用缩进符,而使用空格
Expand All @@ -12,8 +12,8 @@ module.exports = {
singleQuote: true,
// jsx 不使用单引号,而使用双引号
jsxSingleQuote: false,
// 末尾需要逗号
trailingComma: 'all',
// 末尾不需要逗号
trailingComma: 'none',
// 大括号内的首尾需要空格
bracketSpacing: true,
// jsx 标签的反尖括号需要换行
Expand Down
20 changes: 20 additions & 0 deletions test/adjacent-overload-signatures/bad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare namespace Foo1 {
export function foo(s: string): void;
export function foo(n: number): void;
export function bar(): void;
export function foo(sn: string | number): void;
}

type Foo2 = {
foo(s: string): void;
foo(n: number): void;
bar(): void;
foo(sn: string | number): void;
};

interface Foo3 {
foo(s: string): void;
foo(n: number): void;
bar(): void;
foo(sn: string | number): void;
}
20 changes: 20 additions & 0 deletions test/adjacent-overload-signatures/good.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare namespace Foo1 {
export function foo(s: string): void;
export function foo(n: number): void;
export function foo(sn: string | number): void;
export function bar(): void;
}

type Foo4 = {
foo(s: string): void;
foo(n: number): void;
foo(sn: string | number): void;
bar(): void;
};

interface Foo3 {
foo(s: string): void;
foo(n: number): void;
foo(sn: string | number): void;
bar(): void;
}
1 change: 1 addition & 0 deletions test/adjacent-overload-signatures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/adjacent-overload-signatures/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"rules": {
"adjacent-overload-signatures": true
},
"meta": {
"description": "定义函数时如果用到了覆写,则必须将覆写的函数写到一起",
"ts-only": true
}
}
64 changes: 64 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';

import { Linter } from 'tslint';
import { parseConfigFile } from 'tslint/lib/configuration';

const testNameList = fs
.readdirSync(__dirname)
.filter((filename) =>
fs.lstatSync(path.resolve(__dirname, filename)).isDirectory(),
);

describe('TSLint rules', () => {
testNameList.forEach((testName) => {
describe(testName, () => {
it('should have lint error for bad.ts', () => {
const linter = new Linter({
fix: false,
});
linter.lint(
`./${testName}/bad.ts`,
fs.readFileSync(
path.resolve(__dirname, `./${testName}/bad.ts`),
'utf-8',
),
parseConfigFile(require(`./${testName}/tslint.json`)),
);
const lintResult = linter.getResult();
assert.ok(
lintResult.errorCount > 0,
'Does not have lint error',
);
lintResult.failures.forEach((failure) => {
const failedRuleName = failure.getRuleName();
assert.equal(
failedRuleName,
testName,
`Failed rule '${failedRuleName}' does not match '${testName}'`,
);
});
});
it('should pass the lint for good.ts', () => {
const linter = new Linter({
fix: false,
});
linter.lint(
`./${testName}/good.ts`,
fs.readFileSync(
path.resolve(__dirname, `./${testName}/good.ts`),
'utf-8',
),
parseConfigFile(require(`./${testName}/tslint.json`)),
);
const lintResult = linter.getResult();
assert.equal(
lintResult.errorCount,
0,
'Have at least one error',
);
});
});
});
});

0 comments on commit cd2d695

Please sign in to comment.