@@ -127,7 +127,7 @@ mod tests {
127127 }
128128
129129 #[ test]
130- fn location_test ( ) {
130+ fn location_slice_test ( ) {
131131 let source = "111 + 222 + 333" ;
132132 let result = parse ( source. as_ref ( ) ) ;
133133
@@ -181,6 +181,48 @@ mod tests {
181181 assert_eq ! ( slice, "222" ) ;
182182 }
183183
184+ #[ test]
185+ fn location_line_column_test ( ) {
186+ let source = "first\n second\n third" ;
187+ let result = parse ( source. as_ref ( ) ) ;
188+
189+ let node = result. node ( ) ;
190+ let program = node. as_program_node ( ) . unwrap ( ) ;
191+ let statements = program. statements ( ) . body ( ) ;
192+ let mut iter = statements. iter ( ) ;
193+
194+ let _first = iter. next ( ) . unwrap ( ) ;
195+ let second = iter. next ( ) . unwrap ( ) ;
196+ let third = iter. next ( ) . unwrap ( ) ;
197+
198+ let second_loc = second. location ( ) ;
199+ assert_eq ! ( second_loc. start_line( ) , 2 ) ;
200+ assert_eq ! ( second_loc. end_line( ) , 2 ) ;
201+ assert_eq ! ( second_loc. start_column( ) , 0 ) ;
202+ assert_eq ! ( second_loc. end_column( ) , 6 ) ;
203+
204+ let third_loc = third. location ( ) ;
205+ assert_eq ! ( third_loc. start_line( ) , 3 ) ;
206+ assert_eq ! ( third_loc. end_line( ) , 3 ) ;
207+ assert_eq ! ( third_loc. start_column( ) , 0 ) ;
208+ assert_eq ! ( third_loc. end_column( ) , 5 ) ;
209+ }
210+
211+ #[ test]
212+ fn location_chop_test ( ) {
213+ let result = parse ( b"foo" ) ;
214+ let mut location = result. node ( ) . as_program_node ( ) . unwrap ( ) . location ( ) ;
215+
216+ assert_eq ! ( location. chop( ) . as_slice( ) , b"fo" ) ;
217+ assert_eq ! ( location. chop( ) . chop( ) . chop( ) . as_slice( ) , b"" ) ;
218+
219+ // Check that we don't go negative.
220+ for _ in 0 ..10 {
221+ location = location. chop ( ) ;
222+ }
223+ assert_eq ! ( location. as_slice( ) , b"" ) ;
224+ }
225+
184226 #[ test]
185227 fn visitor_test ( ) {
186228 use super :: { visit_interpolated_regular_expression_node, visit_regular_expression_node, InterpolatedRegularExpressionNode , RegularExpressionNode , Visit } ;
0 commit comments