Skip to content

Commit

Permalink
Add tests for glossary completion from bib files
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelong committed Dec 26, 2024
1 parent 5c4a45b commit 3b722d6
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/completion/completer/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const logger = lw.log('Intelli', 'Glossary')
export const provider: CompletionProvider = { from }
export const glossary = {
parse,
getItem
getItem,
parseBibFile
}

const data = {
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/armory/intellisense/glossary.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@symbol{fs,
name = {\ensuremath{f_s}},
description = {sample rate}
}

@symbol{theta,
name = {\ensuremath{\theta}},
description = {horizontal angle}
}

@entry{caesar,
name={\sortname{Gaius Julius}{Caesar}},
first={\sortname{Julius}{Caesar}},
text={Caesar},
description={Roman politician and general},
born={13~July 100 BC},
died={15~March 44 BC},
identifier={person}
}

@entry{wellesley,
name={\sortname{Arthur}{Wellesley}},
text={Wellington},
description={Anglo-Irish soldier and statesman},
born={1~May 1769 AD},
died={14~September 1852 AD},
othername={1st Duke of Wellington},
identifier={person}
}

@index{wellington,
name={Wellington},
alias={wellesley},
identifier={person}
}
9 changes: 9 additions & 0 deletions test/fixtures/armory/intellisense/glossary_bib.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass{article}
\usepackage{glossaries-extra}
\GlsXtrLoadResources[
src={glos.bib},
]

\begin{document}
abc\gls{}
\end{document}
35 changes: 35 additions & 0 deletions test/fixtures/unittest/22_completion_glossary/glossary.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@symbol{fs,
name = {\ensuremath{f_s}},
description = {sample rate}
}

@symbol{theta,
name = {\ensuremath{\theta}},
description = {horizontal angle}
}

@entry{caesar,
name={\sortname{Gaius Julius}{Caesar}},
first={\sortname{Julius}{Caesar}},
text={Caesar},
description={Roman politician and general},
born={13~July 100 BC},
died={15~March 44 BC},
identifier={person}
}

@entry{wellesley,
name={\sortname{Arthur}{Wellesley}},
text={Wellington},
description={Anglo-Irish soldier and statesman},
born={1~May 1769 AD},
died={14~September 1852 AD},
othername={1st Duke of Wellington},
identifier={person}
}

@index{wellington,
name={Wellington},
alias={wellesley},
identifier={person}
}
16 changes: 15 additions & 1 deletion test/suites/04_intellisense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as test from './utils'
import { EnvSnippetType } from '../../src/types'
import { isTriggerSuggestNeeded } from '../../src/completion/completer/macro'

suite.skip('Intellisense test suite', () => {
suite('Intellisense test suite', () => {
test.suite.name = path.basename(__filename).replace('.test.js', '')
test.suite.fixture = 'testground'

Expand Down Expand Up @@ -420,6 +420,20 @@ suite.skip('Intellisense test suite', () => {
assert.ok(suggestions.items.find(item => item.label === 'abbr_x' && item.detail === 'A first abbreviation'))
})

test.run('glossary intellisense from .bib files', async (fixture: string) => {
await test.load(fixture, [
{src: 'intellisense/glossary_bib.tex', dst: 'main.tex'},
{src: 'intellisense/glossary.bib', dst: 'glos.bib'}
])
const suggestions = test.suggest(7, 8)
assert.strictEqual(suggestions.items.length, 5)
assert.ok(suggestions.items.find(item => item.label === 'fs' && item.detail?.includes('\\ensuremath{f_s}')))
assert.ok(suggestions.items.find(item => item.label === 'theta' && item.detail?.includes('\\ensuremath{\theta}')))
assert.ok(suggestions.items.find(item => item.label === 'caesar' && item.detail?.includes('\\sortname{Gaius Julius}{Caesar}')))
assert.ok(suggestions.items.find(item => item.label === 'wellesley' && item.detail?.includes('\\sortname{Arthur}{Wellesley}')))
assert.ok(suggestions.items.find(item => item.label === 'wellington' && item.detail?.includes('Wellington')))
})

test.run('@-snippet intellisense and configs intellisense.atSuggestion*', async (fixture: string) => {
const replaces = {'@+': '\\sum', '@8': '', '@M': '\\sum'}
await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.atSuggestion.user', replaces)
Expand Down
40 changes: 30 additions & 10 deletions test/units/22_completion_glossary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import * as sinon from 'sinon'
import { lw } from '../../src/lw'
import { assert, get, mock, set } from './utils'
import { provider } from '../../src/completion/completer/glossary'
import { glossary, provider } from '../../src/completion/completer/glossary'

describe(path.basename(__filename).split('.')[0] + ':', () => {
const fixture = path.basename(__filename).split('.')[0]
Expand All @@ -24,16 +24,16 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
sinon.restore()
})

describe('lw.completion->glossary', () => {
function getSuggestions() {
return provider.from(['', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
line: '',
position: new vscode.Position(0, 0),
})
}
function getSuggestions() {
return provider.from(['', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
line: '',
position: new vscode.Position(0, 0),
})
}

describe('lw.completion->glossary', () => {
it('should parse and provide \\newacronym definition', async () => {
readStub.resolves('\\newacronym{rf}{RF}{radio-frequency}')
await lw.cache.refreshCache(texPath)
Expand Down Expand Up @@ -107,4 +107,24 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
assert.strictEqual(suggestions.find(s => s.label === 'rf')?.detail, 'radio-frequency')
})
})

describe('lw.completion->glossary.parseBibFile', () => {
const bibFile = 'glossary.bib'
const bibPath = get.path(fixture, bibFile)

it('should parse the bib file', async () => {
readStub.withArgs(texPath).resolves(`\\GlsXtrLoadResources[src={${bibFile}}]`)
await lw.cache.refreshCache(texPath)
sinon.restore()

await glossary.parseBibFile(bibPath)

const suggestions = getSuggestions()
assert.ok(suggestions.find(item => item.label === 'fs' && item.detail?.includes('\\ensuremath{f_s}')))
assert.ok(suggestions.find(item => item.label === 'theta' && item.detail?.includes('\\ensuremath{\\theta}')))
assert.ok(suggestions.find(item => item.label === 'caesar' && item.detail?.includes('\\sortname{Gaius Julius}{Caesar}')))
assert.ok(suggestions.find(item => item.label === 'wellesley' && item.detail?.includes('\\sortname{Arthur}{Wellesley}')))
assert.ok(suggestions.find(item => item.label === 'wellington' && item.detail?.includes('Wellington')))
})
})
})

0 comments on commit 3b722d6

Please sign in to comment.