Skip to content

Commit

Permalink
test: 增加几个单元测试
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Apr 29, 2024
1 parent 9a880ac commit 8093892
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 47 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@
"@vitest/coverage-v8": "^1.5.2",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.3.9",
"chai": "^5.1.0",
"cross-env": "^7.0.3",
"esbuild": "^0.20.2",
"eslint": "^9.1.1",
Expand Down
46 changes: 0 additions & 46 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions src/test/unit/utils/checkRangeOverlapping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it, vi } from 'vitest';
import { Position, Range, TextEditor } from 'vscode';

import { Message } from '@/typings/extension/types';
import { checkRangeOverlapping } from '@/utils/checkRangeOverlapping';

const mockPosition = (line: number, character: number) =>
({
line,
character,
isBefore: () => true,
isBeforeOrEqual: () => true,
isAfter: () => true,
isAfterOrEqual: () => true,
isEqual: () => true,
translate: () => this,
with: () => this,
compareTo: () => 1,
}) as unknown as Position;

vi.mock('vscode');

describe('checkRangeOverlapping', () => {
it('should return true if there are overlapping messages', () => {
const logMessages: Message[] = [
{ spaces: '', lines: [{ start: mockPosition(1, 0), end: mockPosition(3, 0) }] as unknown as Range[] },
{ spaces: '', lines: [{ start: mockPosition(2, 0), end: mockPosition(4, 0) }] as unknown as Range[] },
];
const editor = {
selections: [{ start: mockPosition(2, 0), end: mockPosition(3, 0) }],
} as unknown as TextEditor;
const result = checkRangeOverlapping(logMessages, editor);
expect(result).to.be.true;
});

it('should return false if there are no overlapping messages', () => {
const logMessages: Message[] = [
{ spaces: '', lines: [{ start: mockPosition(1, 0), end: mockPosition(3, 0) }] as unknown as Range[] },
{ spaces: '', lines: [{ start: mockPosition(4, 0), end: mockPosition(6, 0) }] as unknown as Range[] },
];
const editor = {
selections: [{ start: mockPosition(5, 0), end: mockPosition(7, 0) }],
} as unknown as TextEditor;
const result = checkRangeOverlapping(logMessages, editor);
expect(result).to.be.false;
});

it('should handle empty logMessages array', () => {
const logMessages: Message[] = [];
const editor = {
selections: [{ start: mockPosition(2, 0), end: mockPosition(3, 0) }],
} as unknown as TextEditor;
const result = checkRangeOverlapping(logMessages, editor);
expect(result).to.be.false;
});

it('should handle empty editor selections array', () => {
const logMessages: Message[] = [
{ spaces: '', lines: [{ start: mockPosition(1, 0), end: mockPosition(3, 0) }] as unknown as Range[] },
{ spaces: '', lines: [{ start: mockPosition(4, 0), end: mockPosition(6, 0) }] as unknown as Range[] },
];
const editor = {
selections: [],
} as unknown as TextEditor;
const result = checkRangeOverlapping(logMessages, editor);
expect(result).to.be.false;
});
});
56 changes: 56 additions & 0 deletions src/test/unit/utils/closingContextLine.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it, vi } from 'vitest';
import { TextDocument } from 'vscode';

import { BracketType } from '@/typings/extension/enums';
import { closingContextLine } from '@/utils/closingContextLine';

vi.mock('vscode');
describe('closingContextLine', () => {
it('should return the declaration line when the number of opening and closing brackets is equal', () => {
const document = {
lineCount: 5,
lineAt: (line: number) => {
return {
text: line === 2 ? 'const foo = { bar: { baz: 1 } };' : '',
};
},
} as TextDocument;
const declarationLine = 2;
const bracketType = BracketType.CURLY_BRACES;
const expected = 2;
const result = closingContextLine(document, declarationLine, bracketType);
expect(result).to.equal(expected);
});

it('should return -1 when the number of opening and closing brackets is not equal', () => {
const document = {
lineCount: 5,
lineAt: (line: number) => {
return {
text: line === 2 ? 'const foo = { bar: { baz: 1 };' : '',
};
},
} as TextDocument;
const declarationLine = 2;
const bracketType = BracketType.CURLY_BRACES;
const expected = -1;
const result = closingContextLine(document, declarationLine, bracketType);
expect(result).to.equal(expected);
});

it('should return -1 when the declaration line is greater than or equal to the document line count', () => {
const document = {
lineCount: 5,
lineAt: (_line: number) => {
return {
text: '',
};
},
} as TextDocument;
const declarationLine = 5;
const bracketType = BracketType.CURLY_BRACES;
const expected = -1;
const result = closingContextLine(document, declarationLine, bracketType);
expect(result).to.equal(expected);
});
});
40 changes: 40 additions & 0 deletions src/test/unit/utils/locBrackets.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest';

import { BracketType } from '@/typings/extension/enums';
import { LogBracket } from '@/typings/extension/types';
import { locBrackets } from '@/utils/locBrackets';

describe('locBrackets', () => {
it('should count opening and closing brackets correctly for parentheses', () => {
const loc = '(a + b) * (c - d)';
const bracketType = BracketType.PARENTHESIS;
const expected: LogBracket = {
openingBrackets: 2,
closingBrackets: 2,
};
const result = locBrackets(loc, bracketType);
expect(result).to.deep.equal(expected);
});

it('should count opening and closing brackets correctly for curly braces', () => {
const loc = '{ a: 1, b: 2 }';
const bracketType = BracketType.CURLY_BRACES;
const expected: LogBracket = {
openingBrackets: 1,
closingBrackets: 1,
};
const result = locBrackets(loc, bracketType);
expect(result).to.deep.equal(expected);
});

it('should count opening and closing brackets correctly for empty string', () => {
const loc = '';
const bracketType = BracketType.PARENTHESIS;
const expected: LogBracket = {
openingBrackets: 0,
closingBrackets: 0,
};
const result = locBrackets(loc, bracketType);
expect(result).to.deep.equal(expected);
});
});

0 comments on commit 8093892

Please sign in to comment.