This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Fix unit tests #38
Merged
Merged
Fix unit tests #38
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,103 +1,76 @@ | ||
| import * as assert from 'assert' | ||
| import { Handler } from './handler' | ||
| import { Result } from './api' | ||
| import { Position } from 'sourcegraph' | ||
|
|
||
| interface SearchTest { | ||
| crossRepo?: boolean | ||
| doc: { | ||
| uri: string | ||
| text: string | ||
| } | ||
| expSearchQueries: string[] | ||
| } | ||
| import { referencesQueries, definitionQueries } from './handler' | ||
| import { TextDocument } from 'sourcegraph' | ||
|
|
||
| describe('search requests', () => { | ||
| it('makes correct search requests for goto definition', async () => { | ||
| const tests: SearchTest[] = [ | ||
| { | ||
| crossRepo: undefined, | ||
| doc: { | ||
| uri: 'git://github.com/foo/bar?rev#file.c', | ||
| text: 'token', | ||
| }, | ||
| expSearchQueries: [ | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:file', | ||
| ], | ||
| }, | ||
| { | ||
| crossRepo: true, | ||
| doc: { | ||
| uri: 'git://github.com/foo/bar?rev#file.c', | ||
| text: 'token', | ||
| }, | ||
| expSearchQueries: [ | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:symbol', | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:file', | ||
| ], | ||
| }, | ||
| interface DefinitionTest { | ||
| doc: TextDocument | ||
| definitionPatterns?: string[] | ||
| expectedSearchQueries: string[] | ||
| } | ||
| const tests: DefinitionTest[] = [ | ||
| { | ||
| crossRepo: false, | ||
| doc: { | ||
| uri: 'git://github.com/foo/bar?rev#file.c', | ||
| uri: 'git://github.com/foo/bar?rev#file.cpp', | ||
| languageId: 'cpp', | ||
| text: 'token', | ||
| }, | ||
| expSearchQueries: [ | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:symbol repo:^github.com/foo/bar$@rev', | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:file', | ||
| definitionPatterns: ['const\\s%s\\s='], | ||
| expectedSearchQueries: [ | ||
| // current file | ||
| 'const\\stoken\\s= case:yes file:.(cpp)$ type:file repo:^github.com/foo/bar$@rev file:^file.cpp$', | ||
| // current repo symbols | ||
| '^token$ case:yes file:.(cpp)$ type:symbol repo:^github.com/foo/bar$@rev', | ||
| // current repo definition patterns | ||
| 'const\\stoken\\s= case:yes file:.(cpp)$ type:file repo:^github.com/foo/bar$@rev', | ||
| // all repos definition patterns | ||
| 'const\\stoken\\s= case:yes file:.(cpp)$ type:file', | ||
| ], | ||
| }, | ||
| ] | ||
|
|
||
| for (const test of tests) { | ||
| const h = new Handler({ languageID: 'l' }) | ||
| const searchQueries: string[] = [] | ||
| h.api.search = (searchQuery: string): Promise<Result[]> => { | ||
| searchQueries.push(searchQuery) | ||
| return Promise.resolve([]) | ||
| } | ||
| await h.definition( | ||
| { | ||
| uri: test.doc.uri, | ||
| languageId: 'l', | ||
| text: test.doc.text, | ||
| }, | ||
| { line: 0, character: 0 } as Position | ||
| assert.deepStrictEqual( | ||
| definitionQueries({ | ||
| searchToken: 'token', | ||
| doc: test.doc, | ||
| fileExts: ['cpp'], | ||
| definitionPatterns: test.definitionPatterns || [], | ||
| }), | ||
| test.expectedSearchQueries | ||
| ) | ||
| assert.deepStrictEqual(test.expSearchQueries, searchQueries) | ||
| } | ||
| }) | ||
|
|
||
| it('makes correct search requests for references', async () => { | ||
| const tests: SearchTest[] = [ | ||
| interface ReferencesTest { | ||
| doc: TextDocument | ||
| expectedSearchQueries: string[] | ||
| } | ||
| const tests: ReferencesTest[] = [ | ||
| { | ||
| doc: { | ||
| uri: 'git://github.com/foo/bar?rev#file.c', | ||
| uri: 'git://github.com/foo/bar?rev#file.cpp', | ||
| languageId: 'cpp', | ||
| text: 'token', | ||
| }, | ||
| expSearchQueries: [ | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:file repo:^github.com/foo/bar$@rev', | ||
| '\\btoken\\b case:yes file:.(h|c|hpp|cpp|m|cc)$ type:file -repo:^github.com/foo/bar$', | ||
| expectedSearchQueries: [ | ||
| '\\btoken\\b case:yes file:.(cpp)$ type:file repo:^github.com/foo/bar$@rev', | ||
| '\\btoken\\b case:yes file:.(cpp)$ type:file -repo:^github.com/foo/bar$', | ||
| ], | ||
| }, | ||
| ] | ||
|
|
||
| for (const test of tests) { | ||
| const h = new Handler({ languageID: 'l' }) | ||
| const searchQueries: string[] = [] | ||
| h.api.search = (searchQuery: string): Promise<Result[]> => { | ||
| searchQueries.push(searchQuery) | ||
| return Promise.resolve([]) | ||
| } | ||
| await h.references( | ||
| { | ||
| uri: test.doc.uri, | ||
| languageId: 'l', | ||
| text: test.doc.text, | ||
| }, | ||
| { line: 0, character: 0 } as Position | ||
| assert.deepStrictEqual( | ||
| referencesQueries({ | ||
| searchToken: 'token', | ||
| doc: test.doc, | ||
| fileExts: ['cpp'], | ||
| }), | ||
| test.expectedSearchQueries | ||
| ) | ||
| assert.deepStrictEqual(test.expSearchQueries, searchQueries) | ||
| } | ||
| }) | ||
| }) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the dots in the
file:query should be escaped with a backslashThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yup, and in general the string needs to be run through something like
escapeRegexChars()(I made that up).