-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: ygqygq2 <[email protected]>
- Loading branch information
Showing
5 changed files
with
164 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |