@@ -47,26 +47,26 @@ void testParseNotebook() throws IOException {
4747 assertThat (result .contents ()).hasLineCount (27 );
4848 assertThat (StringUtils .countMatches (result .contents (), IpynbNotebookParser .SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER ))
4949 .isEqualTo (7 );
50- assertThat (result .locationMap ()).extracting ( map -> map . get ( 1 ) ).isEqualTo (new IPythonLocation (17 , 5 ));
50+ assertThat (result .locationMap ()).extractingByKey ( 1 ).isEqualTo (new IPythonLocation (17 , 5 ));
5151 //" print \"not none\"\n"
52- assertThat (result .locationMap ()).extracting ( map -> map . get ( 3 ) ).isEqualTo (new IPythonLocation (19 , 5 ,
52+ assertThat (result .locationMap ()).extractingByKey ( 3 ).isEqualTo (new IPythonLocation (19 , 5 ,
5353 mapToColumnMappingList (Map .of (10 , 1 , 19 , 1 ))));
5454 //"source": "#Some code\nprint(\"hello world\\n\")",
55- assertThat (result .locationMap ()).extracting ( map -> map . get ( 16 ) ).isEqualTo (new IPythonLocation (64 , 14 , List .of (), true ));
56- assertThat (result .locationMap ()).extracting ( map -> map . get ( 17 ) ).isEqualTo (new IPythonLocation (64 , 26 , mapToColumnMappingList (Map .of (6
55+ assertThat (result .locationMap ()).extractingByKey ( 16 ).isEqualTo (new IPythonLocation (64 , 14 , List .of (), true ));
56+ assertThat (result .locationMap ()).extractingByKey ( 17 ).isEqualTo (new IPythonLocation (64 , 26 , mapToColumnMappingList (Map .of (6
5757 , 1 , 18 , 1 , 20 , 1 )), true ));
5858 //"source": "print(\"My\\ntext\")\nprint(\"Something else\\n\")"
59- assertThat (result .locationMap ()).extracting ( map -> map . get ( 22 ) ).isEqualTo (new IPythonLocation (83 , 14 , mapToColumnMappingList (Map .of (6
59+ assertThat (result .locationMap ()).extractingByKey ( 22 ).isEqualTo (new IPythonLocation (83 , 14 , mapToColumnMappingList (Map .of (6
6060 , 1 , 9 , 1 , 15 , 1 )), true ));
61- assertThat (result .locationMap ()).extracting ( map -> map . get ( 23 ) ).isEqualTo (new IPythonLocation (83 , 36 , mapToColumnMappingList (Map .of (6
61+ assertThat (result .locationMap ()).extractingByKey ( 23 ).isEqualTo (new IPythonLocation (83 , 36 , mapToColumnMappingList (Map .of (6
6262 , 1 , 21 , 1 , 23 , 1 )), true ));
6363
6464 //"source": "a = \"A bunch of characters \\n \\f \\r \\ \"\nb = None"
65- assertThat (result .locationMap ()).extracting ( map -> map . get ( 25 ) )
65+ assertThat (result .locationMap ()).extractingByKey ( 25 )
6666 .isEqualTo (new IPythonLocation (90 , 14 , mapToColumnMappingList (Map .of (4 , 1 , 27 , 1 , 30 , 1 , 33 , 1 , 36 , 1 , 39 , 1 )), true ));
67- assertThat (result .locationMap ()).extracting ( map -> map . get ( 26 ) ).isEqualTo (new IPythonLocation (90 , 62 , List .of (), true ));
68- // last line with the cell delimiter which contains the EOF token
69- assertThat (result .locationMap ()).extracting ( map -> map . get ( 27 )) .isEqualTo (new IPythonLocation (90 , 14 , List .of ()));
67+ assertThat (result .locationMap ()).extractingByKey ( 26 ).isEqualTo (new IPythonLocation (90 , 62 , List .of (), true ));
68+ // last line with the cell delimiter which contains the EOF token the column of this token should be at the end of the previous line
69+ assertThat (result .locationMap ()).extractingByKey ( 27 ).isEqualTo (new IPythonLocation (90 , 72 , List .of (), false ));
7070 }
7171
7272 @ Test
@@ -108,14 +108,15 @@ void testParseNotebookWithEmptyLines() throws IOException {
108108
109109 var result = resultOptional .get ();
110110
111- assertThat (result .locationMap ().keySet ()).hasSize (4 );
112- assertThat (result .contents ()).hasLineCount (4 );
111+ assertThat (result .locationMap ().keySet ()).hasSize (5 );
112+ assertThat (result .contents ()).hasLineCount (5 );
113113 assertThat (StringUtils .countMatches (result .contents (), IpynbNotebookParser .SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER ))
114114 .isEqualTo (1 );
115- assertThat (result .locationMap ()).extracting (map -> map .get (3 )).isEqualTo (new IPythonLocation (11 , 5 ));
115+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::line ).isEqualTo (11 );
116+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::column ).isEqualTo (5 );
116117
117118 // last line with the cell delimiter which contains the EOF token
118- assertThat (result .locationMap ()).extracting ( map -> map . get ( 4 ) ).isEqualTo (new IPythonLocation (11 , 5 ));
119+ assertThat (result .locationMap ()).extractingByKey ( 5 ).isEqualTo (new IPythonLocation (11 , 5 ));
119120 }
120121
121122 @ Test
@@ -146,16 +147,55 @@ void testParseNotebookWithNoLanguage() {
146147 }
147148
148149 @ Test
149- void testParseNotebookWithExtraLineEndInArray () throws IOException {
150+ void testDifferentJsonRepresentationOfEmptyLine () throws IOException {
151+ var inputFile = createInputFile (baseDir , "notebook_extra_line.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
152+ var inputFileExtraLineExplicit = createInputFile (baseDir , "notebook_extra_line_explicit.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
153+ var inputFileExtraLineExplicitSingleLine = createInputFile (baseDir , "notebook_extra_line_compressed.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
154+
155+ var notebook = IpynbNotebookParser .parseNotebook (inputFile );
156+ var notebookExtraLineExplicit = IpynbNotebookParser .parseNotebook (inputFileExtraLineExplicit );
157+ var notebookExtraLineExplicitSingleLine = IpynbNotebookParser .parseNotebook (inputFileExtraLineExplicitSingleLine );
158+
159+ assertThat (notebook ).isPresent ();
160+ assertThat (notebookExtraLineExplicit ).isPresent ();
161+ assertThat (notebookExtraLineExplicitSingleLine ).isPresent ();
162+
163+ assertThat (notebook .get ().contents ()).isEqualTo (notebookExtraLineExplicit .get ().contents ());
164+ assertThat (notebook .get ().contents ()).isEqualTo (notebookExtraLineExplicitSingleLine .get ().contents ());
165+ }
166+
167+ @ Test
168+ void testParseNotebookEndingWithEmptyLine () throws IOException {
150169 var inputFile = createInputFile (baseDir , "notebook_extra_line.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
151170
152171 var resultOptional = IpynbNotebookParser .parseNotebook (inputFile );
153172
154173 assertThat (resultOptional ).isPresent ();
155174
156175 var result = resultOptional .get ();
157- assertThat (result .locationMap ()).hasSize (3 );
158- assertThat (result .contents ()).hasLineCount (3 );
176+ assertThat (result .locationMap ()).hasSize (4 );
177+ assertThat (result .contents ()).hasLineCount (4 );
178+ // The empty line
179+ assertThat (result .locationMap ()).extractingByKey (3 ).extracting (IPythonLocation ::line ).isEqualTo (19 );
180+ assertThat (result .locationMap ()).extractingByKey (3 ).extracting (IPythonLocation ::column ).isEqualTo (5 );
181+ // The delimiter
182+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::line ).isEqualTo (19 );
183+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::column ).isEqualTo (5 );
184+
185+ inputFile = createInputFile (baseDir , "notebook_extra_line_compressed.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
186+ resultOptional = IpynbNotebookParser .parseNotebook (inputFile );
187+
188+ assertThat (resultOptional ).isPresent ();
189+
190+ result = resultOptional .get ();
191+ assertThat (result .locationMap ()).hasSize (4 );
192+ assertThat (result .contents ()).hasLineCount (4 );
193+ // The empty line
194+ assertThat (result .locationMap ()).extractingByKey (3 ).extracting (IPythonLocation ::line ).isEqualTo (1 );
195+ assertThat (result .locationMap ()).extractingByKey (3 ).extracting (IPythonLocation ::column ).isEqualTo (317 );
196+ // The delimiter is added after the empty line
197+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::line ).isEqualTo (1 );
198+ assertThat (result .locationMap ()).extractingByKey (4 ).extracting (IPythonLocation ::column ).isEqualTo (319 );
159199 }
160200
161201 @ Test
0 commit comments