@@ -47,26 +47,26 @@ void testParseNotebook() throws IOException {
47
47
assertThat (result .contents ()).hasLineCount (27 );
48
48
assertThat (StringUtils .countMatches (result .contents (), IpynbNotebookParser .SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER ))
49
49
.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 ));
51
51
//" 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 ,
53
53
mapToColumnMappingList (Map .of (10 , 1 , 19 , 1 ))));
54
54
//"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
57
57
, 1 , 18 , 1 , 20 , 1 )), true ));
58
58
//"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
60
60
, 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
62
62
, 1 , 21 , 1 , 23 , 1 )), true ));
63
63
64
64
//"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 )
66
66
.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 ));
70
70
}
71
71
72
72
@ Test
@@ -108,14 +108,15 @@ void testParseNotebookWithEmptyLines() throws IOException {
108
108
109
109
var result = resultOptional .get ();
110
110
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 );
113
113
assertThat (StringUtils .countMatches (result .contents (), IpynbNotebookParser .SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER ))
114
114
.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 );
116
117
117
118
// 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 ));
119
120
}
120
121
121
122
@ Test
@@ -146,16 +147,55 @@ void testParseNotebookWithNoLanguage() {
146
147
}
147
148
148
149
@ 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 {
150
169
var inputFile = createInputFile (baseDir , "notebook_extra_line.ipynb" , InputFile .Status .CHANGED , InputFile .Type .MAIN );
151
170
152
171
var resultOptional = IpynbNotebookParser .parseNotebook (inputFile );
153
172
154
173
assertThat (resultOptional ).isPresent ();
155
174
156
175
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 );
159
199
}
160
200
161
201
@ Test
0 commit comments