forked from aws/aws-toolkit-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving the extractCodeBlockLanguage function to shared/markdown.ts an…
…d added test cases for this function
- Loading branch information
1 parent
b375876
commit 483591b
Showing
3 changed files
with
62 additions
and
19 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
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,22 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export const extractCodeBlockLanguage = (message: string) => { | ||
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages. | ||
const codeBlockStart = message.indexOf('```') | ||
if (codeBlockStart === -1) { | ||
return 'plaintext' | ||
} | ||
|
||
const languageStart = codeBlockStart + 3 | ||
const languageEnd = message.indexOf('\n', languageStart) | ||
|
||
if (languageEnd === -1) { | ||
return 'plaintext' | ||
} | ||
|
||
const language = message.substring(languageStart, languageEnd).trim() | ||
return language !== '' ? language : 'plaintext' | ||
} |
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,38 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import assert from 'assert' | ||
import { extractCodeBlockLanguage } from '../../shared/markdown' | ||
|
||
describe('extractCodeBlockLanguage', () => { | ||
it('should return "plaintext" when no code block is present', () => { | ||
const message = 'This is a message without a code block' | ||
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext') | ||
}) | ||
|
||
it('should return the language when a code block with language is present', () => { | ||
const message = 'Here is some code:\n```javascript\nconsole.log("Hello");\n```' | ||
assert.strictEqual(extractCodeBlockLanguage(message), 'javascript') | ||
}) | ||
|
||
it('should return "plaintext" when a code block is present but no language is specified', () => { | ||
const message = 'Here is some code:\n```\nconsole.log("Hello");\n```' | ||
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext') | ||
}) | ||
|
||
it('should handle whitespace before the language specification', () => { | ||
const message = 'Code:\n``` typescript\nconst x: number = 5;\n```' | ||
assert.strictEqual(extractCodeBlockLanguage(message), 'typescript') | ||
}) | ||
|
||
it('should return "plaintext" when the code block is not closed', () => { | ||
const message = 'Incomplete code block:\n```javascript\nconsole.log("Hello");' | ||
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext') | ||
}) | ||
|
||
it('should handle empty messages', () => { | ||
assert.strictEqual(extractCodeBlockLanguage(''), 'plaintext') | ||
}) | ||
}) |