11import { expect } from 'chai' ;
2- import { RegExpForeignCodeExtractor } from '../extractors/regexp' ;
32import { is_within_range , VirtualDocument } from './document' ;
43import * as CodeMirror from 'codemirror' ;
54import { ISourcePosition , IVirtualPosition } from '../positioning' ;
65import { CodeEditor } from '@jupyterlab/codeeditor' ;
6+ import { foreign_code_extractors } from '../extractors/defaults' ;
77
88let R_LINE_MAGICS = `%R df = data.frame()
99print("df created")
@@ -46,26 +46,18 @@ describe('is_within_range', () => {
4646} ) ;
4747
4848describe ( 'VirtualDocument' , ( ) => {
49- let r_line_extractor_removing = new RegExpForeignCodeExtractor ( {
50- language : 'R' ,
51- pattern : '(^|\n)%R (.*)\n?' ,
52- extract_to_foreign : '$2' ,
53- keep_in_host : false ,
54- is_standalone : false ,
55- file_extension : 'R'
56- } ) ;
5749 let document = new VirtualDocument (
5850 'python' ,
5951 'test.ipynb' ,
6052 { } ,
61- { python : [ r_line_extractor_removing ] } ,
53+ foreign_code_extractors ,
6254 false ,
6355 'py' ,
6456 false
6557 ) ;
6658
6759 describe ( '#extract_foreign_code' , ( ) => {
68- it ( 'joins non-standalone fragments together for both foreign and host code ' , ( ) => {
60+ it ( 'joins non-standalone fragments together' , ( ) => {
6961 let {
7062 cell_code_kept,
7163 foreign_document_map
@@ -74,15 +66,14 @@ describe('VirtualDocument', () => {
7466 column : 0
7567 } ) ;
7668
77- expect ( cell_code_kept ) . to . equal (
78- 'print("df created")\nprint("plotted")\n'
79- ) ;
69+ // note R cell lines are kept in code (keep_in_host=true)
70+ expect ( cell_code_kept ) . to . equal ( R_LINE_MAGICS ) ;
8071 expect ( foreign_document_map . size ) . to . equal ( 2 ) ;
8172
8273 let { virtual_document : r_document } = foreign_document_map . get (
8374 foreign_document_map . keys ( ) . next ( ) . value
8475 ) ;
85- expect ( r_document . language ) . to . equal ( 'R ' ) ;
76+ expect ( r_document . language ) . to . equal ( 'r ' ) ;
8677 expect ( r_document . value ) . to . equal ( 'df = data.frame()\n\n\nggplot(df)\n' ) ;
8778 } ) ;
8879 } ) ;
@@ -94,14 +85,24 @@ describe('VirtualDocument', () => {
9485 let init_document_with_Python_and_R = ( ) => {
9586 let cm_editor_for_cell_1 = { } as CodeMirror . Editor ;
9687 let cm_editor_for_cell_2 = { } as CodeMirror . Editor ;
88+ let cm_editor_for_cell_3 = { } as CodeMirror . Editor ;
89+ let cm_editor_for_cell_4 = { } as CodeMirror . Editor ;
9790 document . append_code_block (
98- 'test line in Python 1\n%R test line in R 1' ,
91+ 'test line in Python 1\n%R 1st test line in R line magic 1' ,
9992 cm_editor_for_cell_1
10093 ) ;
10194 document . append_code_block (
102- 'test line in Python 2\n%R test line in R 2' ,
95+ 'test line in Python 2\n%R 1st test line in R line magic 2' ,
10396 cm_editor_for_cell_2
10497 ) ;
98+ document . append_code_block (
99+ '%%R\n1st test line in R cell magic 1' ,
100+ cm_editor_for_cell_3
101+ ) ;
102+ document . append_code_block (
103+ '%%R -i imported_variable\n1st test line in R cell magic 2' ,
104+ cm_editor_for_cell_4
105+ ) ;
105106 } ;
106107
107108 describe ( 'transform_virtual_to_editor' , ( ) => {
@@ -131,14 +132,30 @@ describe('VirtualDocument', () => {
131132 ch : 3
132133 } as ISourcePosition ) ;
133134 expect ( foreign_document ) . to . not . equal ( document ) ;
135+ expect ( foreign_document . value ) . to . equal (
136+ '1st test line in R line magic 1\n\n\n' +
137+ '1st test line in R line magic 2\n\n\n' +
138+ '1st test line in R cell magic 1\n\n\n' +
139+ 'imported_variable <- data.frame(); 1st test line in R cell magic 2\n'
140+ ) ;
134141
135- // The second (R) line in the first block
136- let editor_position = foreign_document . transform_virtual_to_editor ( {
142+ // The second (R) line in the first block ("s" in "1st", "1st" in "1st test line in R line magic")
143+ let virtual_r_1_1 = {
137144 line : 0 ,
138- ch : 0
139- } as IVirtualPosition ) ;
145+ ch : 1
146+ } as IVirtualPosition ;
147+
148+ // For future reference, the code below would be wrong:
149+ // let source_position = foreign_document.transform_virtual_to_source(virtual_r_1_1);
150+ // expect(source_position.line).to.equal(1);
151+ // expect(source_position.ch).to.equal(4);
152+ // because it checks R source position, rather than checking root source positions.
153+
154+ let editor_position = foreign_document . transform_virtual_to_editor (
155+ virtual_r_1_1
156+ ) ;
140157 expect ( editor_position . line ) . to . equal ( 1 ) ;
141- expect ( editor_position . ch ) . to . equal ( 3 ) ;
158+ expect ( editor_position . ch ) . to . equal ( 4 ) ;
142159
143160 // The second (R) line in the second block
144161 editor_position = foreign_document . transform_virtual_to_editor ( {
0 commit comments