Skip to content

Commit da0f013

Browse files
committed
Add unit tests
1 parent 958cf1a commit da0f013

File tree

8 files changed

+4718
-7
lines changed

8 files changed

+4718
-7
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node: [12, 13]
16+
17+
steps:
18+
- name: Checkout Repo
19+
uses: actions/checkout@v2.0.0
20+
21+
- name: Install Node
22+
uses: actions/setup-node@v1.3.0
23+
with:
24+
node-version: ${{ matrix.node }}
25+
26+
- run: npm ci
27+
28+
- run: npm run lint
29+
30+
- run: npm test
31+
32+
- run: npm run build

__data__/dotnetFormatMatcher.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { problemMatcher } from "../.github/dotnet-format-problem-matcher.json";
2+
3+
export interface ProblemMatcher {
4+
owner: string;
5+
pattern: ProblemMatcherPattern[];
6+
};
7+
8+
export interface ProblemMatcherPattern {
9+
regexp: string;
10+
file?: number;
11+
line?: number;
12+
column?: number;
13+
severity?: number;
14+
message?: number;
15+
code?: number;
16+
loop?: boolean;
17+
}
18+
19+
export const dotnetFormatMatcher: ProblemMatcher = problemMatcher[0];

__helpers__/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function matchResults(report: string[], regexp: RegExp): RegExpExecArray[] {
2+
return report.map(line => regexp.exec(line))
3+
.filter(match => match);
4+
}

__tests__/problemMatcher.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { matchResults } from "../__helpers__/utils";
2+
import { dotnetFormatMatcher, ProblemMatcherPattern } from "../__data__/dotnetFormatMatcher";
3+
4+
describe("problemMatcher", () => {
5+
it("has one pattern", () => {
6+
expect(dotnetFormatMatcher.pattern.length).toEqual(1);
7+
});
8+
9+
describe("pattern", () => {
10+
const reportOutput = [
11+
" Formatting code files in workspace 'C:\\dev\\application\\application.sln'.",
12+
" src\\ConsoleApp\\Program.cs(5,18): Fix whitespace formatting 1.",
13+
" src\\ConsoleApp\\Program.cs(8,30): Fix whitespace formatting 2.",
14+
" Formatted code file 'Program.cs'.",
15+
" Format complete in 4451ms.",
16+
];
17+
18+
let pattern: ProblemMatcherPattern;
19+
let regexp: RegExp;
20+
21+
beforeEach(() => {
22+
pattern = dotnetFormatMatcher.pattern[0];
23+
regexp = new RegExp(pattern.regexp);
24+
});
25+
26+
it("matches violations", () => {
27+
const results = matchResults(reportOutput, regexp);
28+
29+
expect(results.length).toEqual(2);
30+
});
31+
32+
it("matches violation details", () => {
33+
const results = matchResults(reportOutput, regexp);
34+
35+
expect(results.length).toEqual(2);
36+
37+
expect(results[0][pattern.file]).toEqual("src\\ConsoleApp\\Program.cs");
38+
expect(results[0][pattern.line]).toEqual("5");
39+
expect(results[0][pattern.column]).toEqual("18");
40+
expect(results[0][pattern.message]).toEqual("Fix whitespace formatting 1.");
41+
42+
expect(results[1][pattern.file]).toEqual("src\\ConsoleApp\\Program.cs");
43+
expect(results[1][pattern.line]).toEqual("8");
44+
expect(results[1][pattern.column]).toEqual("30");
45+
expect(results[1][pattern.message]).toEqual("Fix whitespace formatting 2.");
46+
});
47+
});
48+
});

jest.config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
clearMocks: true,
3+
collectCoverage: true,
4+
collectCoverageFrom: [
5+
"**/*.ts",
6+
"!dist/**",
7+
"!lib/**",
8+
"!**/node_modules/**",
9+
],
10+
coverageDirectory: "./coverage/",
11+
globals: {
12+
"ts-jest": {
13+
diagnostics: false,
14+
}
15+
},
16+
moduleFileExtensions: ["js", "ts"],
17+
testEnvironment: "node",
18+
testMatch: ["**/*.test.ts"],
19+
testRunner: "jest-circus/runner",
20+
transform: {
21+
"^.+\\.ts$": "ts-jest",
22+
},
23+
verbose: true,
24+
};
25+
26+
const processStdoutWrite = process.stdout.write.bind(process.stdout);
27+
28+
process.stdout.write = (str, encoding, cb) => {
29+
if (!str.match(/^::/)) {
30+
return processStdoutWrite(str, encoding, cb);
31+
}
32+
};

0 commit comments

Comments
 (0)