-
Notifications
You must be signed in to change notification settings - Fork 148
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 #148 from krassowski/fix/r-magics
Enhanced argument parsing in rpy2 magics
- Loading branch information
Showing
8 changed files
with
180 additions
and
13 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
106 changes: 106 additions & 0 deletions
106
packages/jupyterlab-lsp/src/extractors/defaults.spec.ts
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,106 @@ | ||
import { IVirtualDocumentBlock, VirtualDocument } from '../virtual/document'; | ||
import { expect } from 'chai'; | ||
import { foreign_code_extractors } from './defaults'; | ||
import { CodeEditor } from '@jupyterlab/codeeditor'; | ||
|
||
function wrap_in_python_lines(line: string) { | ||
return 'print("some code before")\n' + line + '\n' + 'print("and after")\n'; | ||
} | ||
|
||
describe('Default extractors', () => { | ||
let document: VirtualDocument; | ||
|
||
function extract(code: string) { | ||
return document.extract_foreign_code(code, null, { | ||
line: 0, | ||
column: 0 | ||
}); | ||
} | ||
|
||
function get_the_only_virtual( | ||
foreign_document_map: Map<CodeEditor.IRange, IVirtualDocumentBlock> | ||
) { | ||
expect(foreign_document_map.size).to.equal(1); | ||
|
||
let { virtual_document } = foreign_document_map.get( | ||
foreign_document_map.keys().next().value | ||
); | ||
|
||
return virtual_document; | ||
} | ||
|
||
beforeEach(() => { | ||
document = new VirtualDocument( | ||
'python', | ||
'test.ipynb', | ||
{}, | ||
foreign_code_extractors, | ||
false, | ||
'py', | ||
false | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
document.clear(); | ||
}); | ||
|
||
describe('%R line magic', () => { | ||
it('extracts simple commands', () => { | ||
let code = wrap_in_python_lines('%R ggplot()'); | ||
let { cell_code_kept, foreign_document_map } = extract(code); | ||
|
||
// should not be removed, but left for the static analysis (using magic overrides) | ||
expect(cell_code_kept).to.equal(code); | ||
let r_document = get_the_only_virtual(foreign_document_map); | ||
expect(r_document.language).to.equal('r'); | ||
expect(r_document.value).to.equal('ggplot()\n'); | ||
}); | ||
|
||
it('parses input (into a dummy data frame)', () => { | ||
let code = wrap_in_python_lines('%R -i df ggplot(df)'); | ||
let { foreign_document_map } = extract(code); | ||
|
||
let r_document = get_the_only_virtual(foreign_document_map); | ||
expect(r_document.language).to.equal('r'); | ||
expect(r_document.value).to.equal('df <- data.frame()\nggplot(df)\n'); | ||
}); | ||
|
||
it('parses multiple inputs (into dummy data frames)', () => { | ||
let code = wrap_in_python_lines('%R -i df -i x ggplot(df)'); | ||
let r_document = get_the_only_virtual(extract(code).foreign_document_map); | ||
expect(r_document.value).to.equal( | ||
'df <- data.frame()\n' + 'x <- data.frame()\n' + 'ggplot(df)\n' | ||
); | ||
}); | ||
|
||
it('parses inputs ignoring other arguments', () => { | ||
let code = wrap_in_python_lines('%R -i df --width 300 -o x ggplot(df)'); | ||
let r_document = get_the_only_virtual(extract(code).foreign_document_map); | ||
expect(r_document.value).to.equal( | ||
'df <- data.frame()\n' + 'ggplot(df)\n' | ||
); | ||
}); | ||
}); | ||
|
||
describe('%%R cell magic', () => { | ||
it('extracts simple commands', () => { | ||
let code = '%%R\nggplot()'; | ||
let { cell_code_kept, foreign_document_map } = extract(code); | ||
|
||
expect(cell_code_kept).to.equal(code); | ||
let r_document = get_the_only_virtual(foreign_document_map); | ||
expect(r_document.language).to.equal('r'); | ||
expect(r_document.value).to.equal('ggplot()\n'); | ||
}); | ||
}); | ||
|
||
it('parses input (into a dummy data frame)', () => { | ||
let code = '%%R -i df\nggplot(df)'; | ||
let { foreign_document_map } = extract(code); | ||
|
||
let r_document = get_the_only_virtual(foreign_document_map); | ||
expect(r_document.language).to.equal('r'); | ||
expect(r_document.value).to.equal('df <- data.frame()\nggplot(df)\n'); | ||
}); | ||
}); |
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
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
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