Skip to content

Commit

Permalink
🔵 other: add test for matcher (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisam authored Sep 29, 2023
1 parent 4c0ed23 commit 376f535
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
59 changes: 59 additions & 0 deletions __tests__/problemMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as problemMatcherJson from '../src/problem-matcher.json';

const problemMatcher = problemMatcherJson.problemMatcher[0];

function matchResults(output: string[], regexp: RegExp): RegExpExecArray[] {
return output.map(line => regexp.exec(line)).filter(match => match) as RegExpExecArray[];
}

describe('problemMatcher', () => {
it('has the correct owner', () => {
expect(problemMatcher.owner).toEqual('dotnet-format-plus');
});

it('has one pattern', () => {
expect(problemMatcher.pattern.length).toEqual(1);
});

describe('pattern', () => {
const reportOutput = [
"/path/file.cs(15,2): error WHITESPACE: Fix whitespace formatting. Insert '\t'. [/path/project.csproj]",
"/path/file.cs(15,3): error WHITESPACE: Fix whitespace formatting. Replace 4 characters with '\n\t\t\t'. [/path/project.csproj]",
"/path/file.cs(16,84): error WHITESPACE: Fix whitespace formatting. Replace 4 characters with '\n\t\t\t'. [/path/project.csproj]"
];

let pattern: any;
let results: RegExpExecArray[];

beforeEach(() => {
pattern = problemMatcher.pattern[0];

const regexp = new RegExp(pattern.regexp);

results = matchResults(reportOutput, regexp);
});

it('matches violations', () => {
expect(results.length).toEqual(3);
});

it('matches violation details', () => {
expect(results[0][pattern.file]).toEqual('/path/file.cs');
expect(results[0][pattern.line]).toEqual('15');
expect(results[0][pattern.column]).toEqual('2');
expect(results[0][pattern.message]).toEqual("Fix whitespace formatting. Insert '\t'.");
expect(results[0][pattern.severity]).toEqual('error');
expect(results[0][pattern.code]).toEqual('WHITESPACE');

expect(results[1][pattern.file]).toEqual('/path/file.cs');
expect(results[1][pattern.line]).toEqual('15');
expect(results[1][pattern.column]).toEqual('3');
expect(results[1][pattern.message]).toEqual("Fix whitespace formatting. Replace 4 characters with '\n\t\t\t'.");

expect(results[2][pattern.file]).toEqual('/path/file.cs');
expect(results[2][pattern.line]).toEqual('16');
expect(results[2][pattern.column]).toEqual('84');
expect(results[2][pattern.message]).toEqual("Fix whitespace formatting. Replace 4 characters with '\n\t\t\t'.");
});
});
});
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
moduleFileExtensions: ['js', 'ts', 'json'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
Expand Down
36 changes: 17 additions & 19 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"compilerOptions": {
"target": "ES2022" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "ES2022" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": false,
"noUnusedParameters": true,
"noUnusedLocals": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
"**/*.test.ts"
]
}
"compilerOptions": {
"target": "ES2022" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "ES2022" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"forceConsistentCasingInFileNames": false,
"noUnusedParameters": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"exclude": ["node_modules", "**/*.test.ts"]
}

0 comments on commit 376f535

Please sign in to comment.