@@ -68,6 +68,16 @@ impl FileObj {
6868 }
6969 ranges
7070 }
71+
72+ pub fn get_ranges ( & self , lines_changed_only : u8 ) -> Vec < RangeInclusive < u32 > > {
73+ if lines_changed_only == 2 {
74+ self . diff_chunks . to_vec ( )
75+ } else if lines_changed_only == 1 {
76+ self . added_ranges . to_vec ( )
77+ } else {
78+ Vec :: new ( )
79+ }
80+ }
7181}
7282
7383/// Describes if a specified `file_name` is contained within the given `set` of paths.
@@ -234,12 +244,16 @@ pub fn normalize_path(path: &Path) -> PathBuf {
234244
235245#[ cfg( test) ]
236246mod test {
237-
238- // *********************** tests for normalized paths
239- use super :: { list_source_files, normalize_path} ;
240247 use std:: env:: current_dir;
248+ use std:: env:: set_current_dir;
241249 use std:: path:: PathBuf ;
242250
251+ use super :: { get_line_cols_from_offset, list_source_files, normalize_path, FileObj } ;
252+ use crate :: cli:: { get_arg_parser, parse_ignore} ;
253+ use crate :: common_fs:: is_file_in_list;
254+
255+ // *********************** tests for normalized paths
256+
243257 #[ test]
244258 fn normalize_redirects ( ) {
245259 let mut src = current_dir ( ) . unwrap ( ) ;
@@ -274,9 +288,6 @@ mod test {
274288 }
275289
276290 // ************* tests for ignored paths
277- use crate :: cli:: { get_arg_parser, parse_ignore} ;
278- use crate :: common_fs:: is_file_in_list;
279- use std:: env:: set_current_dir;
280291
281292 fn setup_ignore ( input : & str ) -> ( Vec < String > , Vec < String > ) {
282293 let arg_parser = get_arg_parser ( ) ;
@@ -359,6 +370,8 @@ mod test {
359370 ) ) ;
360371 }
361372
373+ // *********************** tests for recursive path search
374+
362375 #[ test]
363376 fn walk_dir_recursively ( ) {
364377 let ( ignored, not_ignored) = setup_ignore ( "target" ) ;
@@ -378,12 +391,48 @@ mod test {
378391 }
379392 }
380393
381- use super :: get_line_cols_from_offset;
394+ // *********************** tests for translating byte offset into line/column
395+
382396 #[ test]
383397 fn translate_byte_offset ( ) {
384398 let ( lines, cols) = get_line_cols_from_offset ( & PathBuf :: from ( "tests/demo/demo.cpp" ) , 144 ) ;
385399 println ! ( "lines: {lines}, cols: {cols}" ) ;
386400 assert_eq ! ( lines, 13 ) ;
387401 assert_eq ! ( cols, 5 ) ;
388402 }
403+
404+ // *********************** tests for FileObj::get_ranges()
405+
406+ #[ test]
407+ fn get_ranges_0 ( ) {
408+ let file_obj = FileObj :: new ( PathBuf :: from ( "tests/demo/demo.cpp" ) ) ;
409+ let ranges = file_obj. get_ranges ( 0 ) ;
410+ assert ! ( ranges. is_empty( ) ) ;
411+ }
412+
413+ #[ test]
414+ fn get_ranges_2 ( ) {
415+ let diff_chunks = vec ! [ 1 ..=10 ] ;
416+ let added_lines = vec ! [ 4 , 5 , 9 ] ;
417+ let file_obj = FileObj :: from (
418+ PathBuf :: from ( "tests/demo/demo.cpp" ) ,
419+ added_lines,
420+ diff_chunks. clone ( ) ,
421+ ) ;
422+ let ranges = file_obj. get_ranges ( 2 ) ;
423+ assert_eq ! ( ranges, diff_chunks) ;
424+ }
425+
426+ #[ test]
427+ fn get_ranges_1 ( ) {
428+ let diff_chunks = vec ! [ 1 ..=10 ] ;
429+ let added_lines = vec ! [ 4 , 5 , 9 ] ;
430+ let file_obj = FileObj :: from (
431+ PathBuf :: from ( "tests/demo/demo.cpp" ) ,
432+ added_lines,
433+ diff_chunks,
434+ ) ;
435+ let ranges = file_obj. get_ranges ( 1 ) ;
436+ assert_eq ! ( ranges, vec![ 4 ..=5 , 9 ..=9 ] ) ;
437+ }
389438}
0 commit comments