From 8093892a05dbb6084e26f751203d3a7c143abec0 Mon Sep 17 00:00:00 2001 From: ygqygq2 Date: Mon, 29 Apr 2024 15:23:47 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=87=A0=E4=B8=AA?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ygqygq2 --- package.json | 1 - pnpm-lock.yaml | 46 ------------- .../unit/utils/checkRangeOverlapping.test.ts | 68 +++++++++++++++++++ .../unit/utils/closingContextLine.spec.ts | 56 +++++++++++++++ src/test/unit/utils/locBrackets.spec.ts | 40 +++++++++++ 5 files changed, 164 insertions(+), 47 deletions(-) create mode 100644 src/test/unit/utils/checkRangeOverlapping.test.ts create mode 100644 src/test/unit/utils/closingContextLine.spec.ts create mode 100644 src/test/unit/utils/locBrackets.spec.ts diff --git a/package.json b/package.json index 4a859ad..0873236 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 119ab28..209b52c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,9 +57,6 @@ importers: '@vscode/test-electron': specifier: ^2.3.9 version: 2.3.9 - chai: - specifier: ^5.1.0 - version: 5.1.0 cross-env: specifier: ^7.0.3 version: 7.0.3 @@ -705,10 +702,6 @@ packages: assertion-error@1.1.0: resolution: {integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==} - assertion-error@2.0.1: - resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==} - engines: {node: '>=12'} - balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} @@ -750,10 +743,6 @@ packages: resolution: {integrity: sha512-13sOfMv2+DWduEU+/xbun3LScLoqN17nBeTLUsmDfKdoiC1fr0n9PU4guu4AhRcOVFk/sW8LyZWHuhWtQZiF+g==} engines: {node: '>=4'} - chai@5.1.0: - resolution: {integrity: sha512-kDZ7MZyM6Q1DhR9jy7dalKohXQ2yrlXkk59CR52aRKxJrobmlBNqnFQxX9xOX8w+4mz8SYlKJa/7D7ddltFXCw==} - engines: {node: '>=12'} - chalk@4.1.2: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} @@ -765,10 +754,6 @@ packages: check-error@1.0.3: resolution: {integrity: sha512-iKEoDYaRmd1mxM90a2OEfWhjsjPpYPuQ+lMYsoxB126+t8fw7ySEO48nmDg5COTjxDI65/Y2OWpeEHk3ZOe8zg==} - check-error@2.0.0: - resolution: {integrity: sha512-tjLAOBHKVxtPoHe/SA7kNOMvhCRdCJ3vETdeY0RuAc9popf+hyaSV6ZEg9hr4cpWF7jmo/JSWEnLDrnijS9Tog==} - engines: {node: '>= 16'} - chokidar@3.5.3: resolution: {integrity: sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==} engines: {node: '>= 8.10.0'} @@ -847,10 +832,6 @@ packages: resolution: {integrity: sha512-WaEtAOpRA1MQ0eohqZjpGD8zdI0Ovsm8mmFhaDN8dvDZzyoUMcYDnf5Y6iu7HTXxf8JDS23qWa4a+hKCDyOPzw==} engines: {node: '>=6'} - deep-eql@5.0.1: - resolution: {integrity: sha512-nwQCf6ne2gez3o1MxWifqkciwt0zhl0LO1/UwVu4uMBuPmflWM4oQ70XMqHqnBJA+nhzncaqL9HVL6KkHJ28lw==} - engines: {node: '>=6'} - deep-is@0.1.4: resolution: {integrity: sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==} @@ -1285,9 +1266,6 @@ packages: loupe@2.3.7: resolution: {integrity: sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==} - loupe@3.1.0: - resolution: {integrity: sha512-qKl+FrLXUhFuHUoDJG7f8P8gEMHq9NFS0c6ghXG1J0rldmZFQZoNVv/vyirE9qwCIhWZDsvEFd1sbFu3GvRQFg==} - lru-cache@10.2.2: resolution: {integrity: sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==} engines: {node: 14 || >=16.14} @@ -1446,10 +1424,6 @@ packages: pathval@1.1.1: resolution: {integrity: sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==} - pathval@2.0.0: - resolution: {integrity: sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==} - engines: {node: '>= 14.16'} - picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} @@ -2424,8 +2398,6 @@ snapshots: assertion-error@1.1.0: {} - assertion-error@2.0.1: {} - balanced-match@1.0.2: {} binary-extensions@2.3.0: {} @@ -2475,14 +2447,6 @@ snapshots: pathval: 1.1.1 type-detect: 4.0.8 - chai@5.1.0: - dependencies: - assertion-error: 2.0.1 - check-error: 2.0.0 - deep-eql: 5.0.1 - loupe: 3.1.0 - pathval: 2.0.0 - chalk@4.1.2: dependencies: ansi-styles: 4.3.0 @@ -2494,8 +2458,6 @@ snapshots: dependencies: get-func-name: 2.0.2 - check-error@2.0.0: {} - chokidar@3.5.3: dependencies: anymatch: 3.1.3 @@ -2583,8 +2545,6 @@ snapshots: dependencies: type-detect: 4.0.8 - deep-eql@5.0.1: {} - deep-is@0.1.4: {} diff-sequences@29.6.3: {} @@ -3063,10 +3023,6 @@ snapshots: dependencies: get-func-name: 2.0.2 - loupe@3.1.0: - dependencies: - get-func-name: 2.0.2 - lru-cache@10.2.2: {} lru-cache@6.0.0: @@ -3230,8 +3186,6 @@ snapshots: pathval@1.1.1: {} - pathval@2.0.0: {} - picocolors@1.0.0: {} picomatch@2.3.1: {} diff --git a/src/test/unit/utils/checkRangeOverlapping.test.ts b/src/test/unit/utils/checkRangeOverlapping.test.ts new file mode 100644 index 0000000..98d8665 --- /dev/null +++ b/src/test/unit/utils/checkRangeOverlapping.test.ts @@ -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; + }); +}); diff --git a/src/test/unit/utils/closingContextLine.spec.ts b/src/test/unit/utils/closingContextLine.spec.ts new file mode 100644 index 0000000..15ae2dc --- /dev/null +++ b/src/test/unit/utils/closingContextLine.spec.ts @@ -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); + }); +}); diff --git a/src/test/unit/utils/locBrackets.spec.ts b/src/test/unit/utils/locBrackets.spec.ts new file mode 100644 index 0000000..bd530fc --- /dev/null +++ b/src/test/unit/utils/locBrackets.spec.ts @@ -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); + }); +});