-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from jvalue/feature/parsing-util-test
parsing-util tests
- Loading branch information
Showing
8 changed files
with
293 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import * as path from 'path'; | ||
|
||
import { TestLogger } from '@jvalue/jayvee-execution/test'; | ||
import { | ||
JayveeServices, | ||
createJayveeServices, | ||
useExtension, | ||
} from '@jvalue/jayvee-language-server'; | ||
import { | ||
ParseHelperOptions, | ||
TestLangExtension, | ||
expectNoParserAndLexerErrors, | ||
parseHelper, | ||
readJvTestAssetHelper, | ||
} from '@jvalue/jayvee-language-server/test'; | ||
import { AstNode, LangiumDocument } from 'langium'; | ||
import { NodeFileSystem } from 'langium/node'; | ||
|
||
import { extractDocumentFromFile, validateDocument } from './parsing-util'; | ||
|
||
describe('Validation of parsing-util', () => { | ||
let parse: ( | ||
input: string, | ||
options?: ParseHelperOptions, | ||
) => Promise<LangiumDocument<AstNode>>; | ||
|
||
let exitSpy: jest.SpyInstance; | ||
|
||
let services: JayveeServices; | ||
|
||
const logger = new TestLogger(true, undefined, false); | ||
|
||
const readJvTestAsset = readJvTestAssetHelper( | ||
__dirname, | ||
'../test/assets/parsing-util/', | ||
); | ||
|
||
beforeAll(() => { | ||
// Mock Process.exit | ||
exitSpy = jest | ||
.spyOn(process, 'exit') | ||
.mockImplementation((code?: number) => { | ||
if (code === undefined || code === 0) { | ||
return undefined as never; | ||
} | ||
throw new Error(`process.exit: ${code}`); | ||
}); | ||
|
||
// Register test extension | ||
useExtension(new TestLangExtension()); | ||
// Create language services | ||
services = createJayveeServices(NodeFileSystem).Jayvee; | ||
// Parse function for Jayvee (without validation) | ||
parse = parseHelper(services); | ||
}); | ||
|
||
afterEach(() => { | ||
exitSpy.mockClear(); | ||
logger.clearLogs(); | ||
}); | ||
|
||
describe('Function of validateDocument', () => { | ||
async function parseAndValidateDocument(input: string) { | ||
const document = await parse(input); | ||
expectNoParserAndLexerErrors(document); | ||
|
||
return validateDocument(document, services, logger); | ||
} | ||
|
||
it('should diagnose no error on valid document', async () => { | ||
const text = readJvTestAsset('validateDocument/valid-document.jv'); | ||
|
||
await parseAndValidateDocument(text); | ||
|
||
expect(exitSpy).toHaveBeenCalledTimes(0); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(0); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(0); | ||
}); | ||
|
||
it('should diagnose error on wrong loader type', async () => { | ||
const text = readJvTestAsset( | ||
'validateDocument/invalid-wrong-loader-type.jv', | ||
); | ||
|
||
try { | ||
await parseAndValidateDocument(text); | ||
} catch (e) { | ||
expect(exitSpy).toHaveBeenCalledTimes(1); | ||
expect(exitSpy).toHaveBeenCalledWith(1); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(0); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(2 * 5); // 2 calls that get formated to 5 lines each | ||
} | ||
}); | ||
|
||
it('should diagnose no error on nonErr diagnostics', async () => { | ||
const text = readJvTestAsset( | ||
'validateDocument/valid-simplify-warning.jv', | ||
); | ||
|
||
try { | ||
await parseAndValidateDocument(text); | ||
} catch (e) { | ||
expect(exitSpy).toHaveBeenCalledTimes(1); | ||
expect(exitSpy).toHaveBeenCalledWith(1); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(0); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(1); | ||
} | ||
}); | ||
}); | ||
|
||
describe('Function of extractDocumentFromFile', () => { | ||
it('should diagnose no error on valid model file', async () => { | ||
await extractDocumentFromFile( | ||
path.resolve( | ||
__dirname, | ||
'../test/assets/parsing-util/', | ||
'extractDocumentFromFile/valid-model.jv', | ||
), | ||
services, | ||
logger, | ||
); | ||
|
||
expect(exitSpy).toHaveBeenCalledTimes(0); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(0); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(0); | ||
}); | ||
|
||
it('should diagnose error on invalid extension', async () => { | ||
try { | ||
await extractDocumentFromFile( | ||
path.resolve( | ||
__dirname, | ||
'../test/assets/parsing-util/', | ||
'extractDocumentFromFile/invalid-extension.lv', | ||
), | ||
services, | ||
logger, | ||
); | ||
} catch (e) { | ||
expect(exitSpy).toHaveBeenCalledTimes(1); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(1); | ||
expect(logger.getLogs().errorLogs[0]).toEqual( | ||
expect.stringContaining( | ||
'Please choose a file with this extension: ".jv"', | ||
), | ||
); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(0); | ||
} | ||
}); | ||
|
||
it('should diagnose error on missing file', async () => { | ||
try { | ||
await extractDocumentFromFile( | ||
path.resolve( | ||
__dirname, | ||
'../test/assets/parsing-util/', | ||
'extractDocumentFromFile/invalid-missing-file.jv', | ||
), | ||
services, | ||
logger, | ||
); | ||
} catch (e) { | ||
expect(exitSpy).toHaveBeenCalledTimes(1); | ||
expect(logger.getLogs().infoLogs).toHaveLength(0); | ||
expect(logger.getLogs().errorLogs).toHaveLength(1); | ||
expect(logger.getLogs().errorLogs[0]).toMatch( | ||
// eslint-disable-next-line no-useless-escape | ||
/File [\w\-\/]*\/libs\/interpreter-lib\/test\/assets\/parsing-util\/extractDocumentFromFile\/invalid-missing-file\.jv does not exist\./, | ||
); | ||
expect(logger.getLogs().debugLogs).toHaveLength(0); | ||
expect(logger.getLogs().diagnosticLogs).toHaveLength(0); | ||
} | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
libs/interpreter-lib/test/assets/parsing-util/extractDocumentFromFile/invalid-extension.lv
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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestFileLoader { | ||
} | ||
|
||
TestExtractor -> TestLoader; | ||
} |
14 changes: 14 additions & 0 deletions
14
libs/interpreter-lib/test/assets/parsing-util/extractDocumentFromFile/valid-model.jv
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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestFileLoader { | ||
} | ||
|
||
TestExtractor -> TestLoader; | ||
} |
15 changes: 15 additions & 0 deletions
15
libs/interpreter-lib/test/assets/parsing-util/extractDocumentFromString/invalid-model.jv
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,15 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestFileLoader { | ||
} | ||
sta | ||
|
||
TestExtractor -> TestLoader; | ||
} |
14 changes: 14 additions & 0 deletions
14
libs/interpreter-lib/test/assets/parsing-util/extractDocumentFromString/valid-model.jv
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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestFileLoader { | ||
} | ||
|
||
TestExtractor -> TestLoader; | ||
} |
14 changes: 14 additions & 0 deletions
14
libs/interpreter-lib/test/assets/parsing-util/validateDocument/invalid-wrong-loader-type.jv
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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestLoader; | ||
} |
14 changes: 14 additions & 0 deletions
14
libs/interpreter-lib/test/assets/parsing-util/validateDocument/valid-document.jv
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,14 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestLoader oftype TestFileLoader { | ||
} | ||
|
||
TestExtractor -> TestLoader; | ||
} |
19 changes: 19 additions & 0 deletions
19
libs/interpreter-lib/test/assets/parsing-util/validateDocument/valid-simplify-warning.jv
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,19 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestPropertyBlock oftype TestProperty { | ||
textProperty: "Test"; | ||
integerProperty: 20 + 20; | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestPropertyBlock -> TestLoader; | ||
} |