Skip to content

Commit

Permalink
test: commentAllLogMessage 单元测试
Browse files Browse the repository at this point in the history
Co-authored-by: ygqygq2 <[email protected]>
  • Loading branch information
ygqygq2 committed Feb 7, 2024
1 parent 77e64b5 commit 636a1c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
83 changes: 40 additions & 43 deletions src/test/unit/commands/commentAllLogMessages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ describe('commentAllLogMessagesCommand', () => {
.mockReturnValue(new Range(new Position(0, 0), new Position(0, 0)))
.mockReturnValueOnce(new Range(new Position(0, 0), new Position(0, 4))),
getText: vi.fn((range: Range): string => {
const { start, end } = range;
if (start.isEqual(end)) {
if (range.isEmpty) {
return ''; // 未选中字符串,返回空字符串
}
return 'myVar';
Expand All @@ -53,21 +52,21 @@ describe('commentAllLogMessagesCommand', () => {
text: 'console.info("🚀 ~ file: test.js:2 ~ a:", a)', // 模拟行的文本内容
firstNonWhitespaceCharacterIndex: 0, // 模拟行的第一个非空格字符的索引
range: {
start: { line: lineNumber, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber, character: 43 }, // 模拟行的结束位置
start: { line: lineNumber - 1, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber, character: 0 }, // 模拟行的结束位置
},
rangeIncludingLineBreak: {
start: { line: lineNumber, character: 0 },
end: { line: lineNumber, character: 43 },
start: { line: lineNumber - 1, character: 0 },
end: { line: lineNumber, character: 0 },
},
};
}
return {
text: '', // 模拟行的文本内容
firstNonWhitespaceCharacterIndex: 0, // 模拟行的第一个非空格字符的索引
range: {
start: { line: lineNumber, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber + 1, character: 0 }, // 模拟行的结束位置
start: { line: lineNumber - 1, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber, character: 0 }, // 模拟行的结束位置
},
};
}),
Expand Down Expand Up @@ -98,49 +97,47 @@ describe('commentAllLogMessagesCommand', () => {
vi.restoreAllMocks();
});

it.todo('应该直接返回,当没有调试日志时', async () => {
window.activeTextEditor = mockEditor ;
await commentAllLogMessagesCommand().handler(mockExtensionProperties);
expect(mockEditBuilder!.delete).not.toHaveBeenCalled();
expect(mockEditBuilder!.insert).not.toHaveBeenCalled();
});

it.todo('应该删除调试日志,再插入一行有注释的调试日志,当有调试日志时', async () => {
window.activeTextEditor = mockEditor;
await commentAllLogMessagesCommand().handler(mockExtensionProperties);
expect(mockEditBuilder!.delete).toHaveBeenCalledTimes(1);
expect(mockEditBuilder!.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String));
});

it.todo('应该插入调试日志,未选中,但光标放在变量名上时', async () => {
const mockSelection = new Selection(new Position(0, 0), new Position(0, 0));
mockSelections = [mockSelection];
it('应该直接返回,当没有调试日志时', async () => {
const mockLineAt = vi.fn().mockImplementation((lineNumber) => {
if (lineNumber === 2) {
return {
text: 'mock text', // 模拟行的文本内容
firstNonWhitespaceCharacterIndex: 0, // 模拟行的第一个非空格字符的索引
range: {
start: { line: lineNumber - 1, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber, character: 0 }, // 模拟行的结束位置
},
rangeIncludingLineBreak: {
start: { line: lineNumber - 1, character: 0 },
end: { line: lineNumber, character: 0 },
},
};
}
return {
text: '', // 模拟行的文本内容
firstNonWhitespaceCharacterIndex: 0, // 模拟行的第一个非空格字符的索引
range: {
start: { line: lineNumber - 1, character: 0 }, // 模拟行的起始位置
end: { line: lineNumber, character: 0 }, // 模拟行的结束位置
},
};
});
window.activeTextEditor = {
...mockEditor,
...{
document: {
...mockDocument,
selections: mockSelections,
lineAt: mockLineAt,
},
} as unknown as TextEditor;

await commentAllLogMessagesCommand().handler(mockExtensionProperties);
expect(mockEditor!.edit).toHaveBeenCalledTimes(1);
expect(mockEditor!.edit).toHaveBeenCalledWith(expect.any(Function));
expect(mockEditBuilder.insert).toHaveBeenCalledTimes(1);
expect(mockEditBuilder.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String));
expect(mockEditBuilder!.delete).not.toHaveBeenCalled();
expect(mockEditBuilder!.insert).not.toHaveBeenCalled();
});

it.todo('应该直接返回,当没有选中字符时和光标没有在字符串旁边时', async () => {
mockSelections = [];
window.activeTextEditor = {
...mockEditor,
...{
...mockDocument,
selections: mockSelections,
},
} as unknown as TextEditor;

it('应该删除调试日志,再插入一行有注释的调试日志,当有调试日志时', async () => {
window.activeTextEditor = mockEditor;
await commentAllLogMessagesCommand().handler(mockExtensionProperties);
expect(mockEditor!.edit).not.toHaveBeenCalled();
expect(mockEditBuilder!.delete).toHaveBeenCalledTimes(1);
expect(mockEditBuilder!.insert).toHaveBeenCalledWith(expect.any(Position), expect.any(String));
});
});
3 changes: 1 addition & 2 deletions src/test/unit/commands/displayLogMessage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ describe('displayLogMessageCommand', () => {
.mockReturnValue(new Range(new Position(0, 0), new Position(0, 0)))
.mockReturnValueOnce(new Range(new Position(0, 0), new Position(0, 4))),
getText: vi.fn((range: Range): string => {
const { start, end } = range;
if (start.isEqual(end)) {
if (range.isEmpty) {
return ''; // 未选中字符串,返回空字符串
}
return 'myVar';
Expand Down

0 comments on commit 636a1c6

Please sign in to comment.