@@ -394,7 +394,7 @@ impl<'a> Searcher<'a> {
394
394
true ,
395
395
) ;
396
396
}
397
-
397
+
398
398
let compute_time = std:: time:: Instant :: now ( ) ;
399
399
400
400
// ======== Compute results =========
@@ -412,9 +412,9 @@ impl<'a> Searcher<'a> {
412
412
. collect ( ) ;
413
413
let buffer_partitions = self . partitioned_output_buffer . clone ( ) ;
414
414
let buffer_partitions = buffer_partitions. iter ( ) . collect :: < Vec < _ > > ( ) ;
415
-
415
+
416
416
let mut results = vec ! [ ] ;
417
-
417
+
418
418
buffer_partitions. iter ( ) . for_each ( |f| {
419
419
let mut items: Vec < ( String , String ) > = Vec :: new ( ) ;
420
420
@@ -546,13 +546,13 @@ impl<'a> Searcher<'a> {
546
546
self . results_writer . write_footer ( & mut std:: io:: stdout ( ) ) ?;
547
547
548
548
let completion_time = std:: time:: Instant :: now ( ) ;
549
-
549
+
550
550
if self . config . debug {
551
551
eprintln ! ( "Search: {}ms\n Compute: {}ms" ,
552
552
compute_time. duration_since( start_time) . as_millis( ) ,
553
553
completion_time. duration_since( compute_time) . as_millis( ) ) ;
554
554
}
555
-
555
+
556
556
Ok ( ( ) )
557
557
}
558
558
@@ -834,7 +834,7 @@ impl<'a> Searcher<'a> {
834
834
if file_map. contains_key ( & column_expr_str) {
835
835
return Variant :: from_string ( & file_map[ & column_expr_str] ) ;
836
836
}
837
-
837
+
838
838
if let Some ( ref _function) = column_expr. function {
839
839
let result =
840
840
self . get_function_value ( entry, file_info, file_map, buffer_data, column_expr) ;
@@ -2406,3 +2406,299 @@ impl<'a> Searcher<'a> {
2406
2406
)
2407
2407
}
2408
2408
}
2409
+
2410
+ #[ cfg( test) ]
2411
+ mod tests {
2412
+ use super :: * ;
2413
+ use crate :: expr:: Expr ;
2414
+ use crate :: field:: Field ;
2415
+ use crate :: function:: Function ;
2416
+ use crate :: query:: { OutputFormat , Query } ;
2417
+ use std:: rc:: Rc ;
2418
+
2419
+ // Tests for FileMetadataState
2420
+ #[ test]
2421
+ fn test_file_metadata_state_new ( ) {
2422
+ let state = FileMetadataState :: new ( ) ;
2423
+
2424
+ assert ! ( !state. file_metadata_set) ;
2425
+ assert ! ( state. file_metadata. is_none( ) ) ;
2426
+
2427
+ assert ! ( !state. line_count_set) ;
2428
+ assert ! ( state. line_count. is_none( ) ) ;
2429
+
2430
+ assert ! ( !state. dimensions_set) ;
2431
+ assert ! ( state. dimensions. is_none( ) ) ;
2432
+
2433
+ assert ! ( !state. duration_set) ;
2434
+ assert ! ( state. duration. is_none( ) ) ;
2435
+
2436
+ assert ! ( !state. mp3_metadata_set) ;
2437
+ assert ! ( state. mp3_metadata. is_none( ) ) ;
2438
+
2439
+ assert ! ( !state. exif_metadata_set) ;
2440
+ assert ! ( state. exif_metadata. is_none( ) ) ;
2441
+ }
2442
+
2443
+ #[ test]
2444
+ fn test_file_metadata_state_clear ( ) {
2445
+ let mut state = FileMetadataState :: new ( ) ;
2446
+
2447
+ // Set some values
2448
+ state. file_metadata_set = true ;
2449
+ state. line_count_set = true ;
2450
+ state. dimensions_set = true ;
2451
+ state. duration_set = true ;
2452
+ state. mp3_metadata_set = true ;
2453
+ state. exif_metadata_set = true ;
2454
+
2455
+ // Clear the state
2456
+ state. clear ( ) ;
2457
+
2458
+ // Verify all values are reset
2459
+ test_file_metadata_state_new ( ) ;
2460
+ }
2461
+
2462
+ fn create_test_searcher ( ) -> Searcher < ' static > {
2463
+ // Create a minimal Query instance for testing
2464
+ let query = Box :: leak ( Box :: new ( Query {
2465
+ fields : Vec :: new ( ) ,
2466
+ roots : Vec :: new ( ) ,
2467
+ expr : None ,
2468
+ grouping_fields : Rc :: new ( Vec :: new ( ) ) ,
2469
+ ordering_fields : Rc :: new ( Vec :: new ( ) ) ,
2470
+ ordering_asc : Rc :: new ( Vec :: new ( ) ) ,
2471
+ limit : 0 ,
2472
+ output_format : OutputFormat :: Tabs ,
2473
+ } ) ) ;
2474
+
2475
+ // Use default configurations
2476
+ let config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2477
+ let default_config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2478
+
2479
+ Searcher :: new ( query, config, default_config, false )
2480
+ }
2481
+
2482
+ fn create_test_searcher_with_ordering ( ) -> Searcher < ' static > {
2483
+ // Create a Query instance with ordering fields
2484
+ let query = Box :: leak ( Box :: new ( Query {
2485
+ fields : Vec :: new ( ) ,
2486
+ roots : Vec :: new ( ) ,
2487
+ expr : None ,
2488
+ grouping_fields : Rc :: new ( Vec :: new ( ) ) ,
2489
+ ordering_fields : Rc :: new ( vec ! [ Expr :: field( Field :: Name ) ] ) ,
2490
+ ordering_asc : Rc :: new ( vec ! [ true ] ) ,
2491
+ limit : 0 ,
2492
+ output_format : OutputFormat :: Tabs ,
2493
+ } ) ) ;
2494
+
2495
+ // Use default configurations
2496
+ let config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2497
+ let default_config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2498
+
2499
+ Searcher :: new ( query, config, default_config, false )
2500
+ }
2501
+
2502
+ fn create_test_searcher_with_aggregate ( ) -> Searcher < ' static > {
2503
+ // Create a Query instance with an aggregate function in fields
2504
+ let mut expr = Expr :: field ( Field :: Name ) ;
2505
+ expr. function = Some ( Function :: Count ) ;
2506
+
2507
+ let query = Box :: leak ( Box :: new ( Query {
2508
+ fields : vec ! [ expr] ,
2509
+ roots : Vec :: new ( ) ,
2510
+ expr : None ,
2511
+ grouping_fields : Rc :: new ( Vec :: new ( ) ) ,
2512
+ ordering_fields : Rc :: new ( Vec :: new ( ) ) ,
2513
+ ordering_asc : Rc :: new ( Vec :: new ( ) ) ,
2514
+ limit : 0 ,
2515
+ output_format : OutputFormat :: Tabs ,
2516
+ } ) ) ;
2517
+
2518
+ // Use default configurations
2519
+ let config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2520
+ let default_config = Box :: leak ( Box :: new ( Config :: default ( ) ) ) ;
2521
+
2522
+ Searcher :: new ( query, config, default_config, false )
2523
+ }
2524
+
2525
+ #[ test]
2526
+ fn test_is_buffered_with_ordering ( ) {
2527
+ let searcher = create_test_searcher_with_ordering ( ) ;
2528
+ assert ! ( searcher. is_buffered( ) ) ;
2529
+ }
2530
+
2531
+ #[ test]
2532
+ fn test_is_buffered_with_aggregate ( ) {
2533
+ let searcher = create_test_searcher_with_aggregate ( ) ;
2534
+ assert ! ( searcher. is_buffered( ) ) ;
2535
+ }
2536
+
2537
+ #[ test]
2538
+ fn test_is_buffered_without_ordering_or_aggregate ( ) {
2539
+ let searcher = create_test_searcher ( ) ;
2540
+ assert ! ( !searcher. is_buffered( ) ) ;
2541
+ }
2542
+
2543
+ #[ test]
2544
+ fn test_has_ordering ( ) {
2545
+ let searcher_with_ordering = create_test_searcher_with_ordering ( ) ;
2546
+ assert ! ( searcher_with_ordering. has_ordering( ) ) ;
2547
+
2548
+ let searcher_without_ordering = create_test_searcher ( ) ;
2549
+ assert ! ( !searcher_without_ordering. has_ordering( ) ) ;
2550
+ }
2551
+
2552
+ #[ test]
2553
+ fn test_has_aggregate_column ( ) {
2554
+ let searcher_with_aggregate = create_test_searcher_with_aggregate ( ) ;
2555
+ assert ! ( searcher_with_aggregate. has_aggregate_column( ) ) ;
2556
+
2557
+ let searcher_without_aggregate = create_test_searcher ( ) ;
2558
+ assert ! ( !searcher_without_aggregate. has_aggregate_column( ) ) ;
2559
+ }
2560
+
2561
+ #[ test]
2562
+ fn test_is_zip_archive ( ) {
2563
+ let searcher = create_test_searcher ( ) ;
2564
+
2565
+ // Test with zip extensions
2566
+ assert ! ( searcher. is_zip_archive( "test.zip" ) ) ;
2567
+ assert ! ( searcher. is_zip_archive( "test.jar" ) ) ;
2568
+ assert ! ( searcher. is_zip_archive( "test.war" ) ) ;
2569
+ assert ! ( searcher. is_zip_archive( "test.ear" ) ) ;
2570
+
2571
+ // Test with non-zip extensions
2572
+ assert ! ( !searcher. is_zip_archive( "test.txt" ) ) ;
2573
+ assert ! ( !searcher. is_zip_archive( "test.rar" ) ) ;
2574
+ assert ! ( !searcher. is_zip_archive( "test" ) ) ;
2575
+ }
2576
+
2577
+ #[ test]
2578
+ fn test_is_archive ( ) {
2579
+ let searcher = create_test_searcher ( ) ;
2580
+
2581
+ // Test with archive extensions
2582
+ assert ! ( searcher. is_archive( "test.zip" ) ) ;
2583
+ assert ! ( searcher. is_archive( "test.tar" ) ) ;
2584
+ assert ! ( searcher. is_archive( "test.gz" ) ) ;
2585
+ assert ! ( searcher. is_archive( "test.rar" ) ) ;
2586
+
2587
+ // Test with non-archive extensions
2588
+ assert ! ( !searcher. is_archive( "test.txt" ) ) ;
2589
+ assert ! ( !searcher. is_archive( "test.jpg" ) ) ;
2590
+ assert ! ( !searcher. is_archive( "test" ) ) ;
2591
+ }
2592
+
2593
+ #[ test]
2594
+ fn test_is_audio ( ) {
2595
+ let searcher = create_test_searcher ( ) ;
2596
+
2597
+ // Test with audio extensions
2598
+ assert ! ( searcher. is_audio( "test.mp3" ) ) ;
2599
+ assert ! ( searcher. is_audio( "test.wav" ) ) ;
2600
+ assert ! ( searcher. is_audio( "test.flac" ) ) ;
2601
+ assert ! ( searcher. is_audio( "test.ogg" ) ) ;
2602
+
2603
+ // Test with non-audio extensions
2604
+ assert ! ( !searcher. is_audio( "test.txt" ) ) ;
2605
+ assert ! ( !searcher. is_audio( "test.jpg" ) ) ;
2606
+ assert ! ( !searcher. is_audio( "test" ) ) ;
2607
+ }
2608
+
2609
+ #[ test]
2610
+ fn test_is_book ( ) {
2611
+ let searcher = create_test_searcher ( ) ;
2612
+
2613
+ // Test with book extensions
2614
+ assert ! ( searcher. is_book( "test.pdf" ) ) ;
2615
+ assert ! ( searcher. is_book( "test.epub" ) ) ;
2616
+ assert ! ( searcher. is_book( "test.mobi" ) ) ;
2617
+ assert ! ( searcher. is_book( "test.djvu" ) ) ;
2618
+
2619
+ // Test with non-book extensions
2620
+ assert ! ( !searcher. is_book( "test.txt" ) ) ;
2621
+ assert ! ( !searcher. is_book( "test.jpg" ) ) ;
2622
+ assert ! ( !searcher. is_book( "test" ) ) ;
2623
+ }
2624
+
2625
+ #[ test]
2626
+ fn test_is_doc ( ) {
2627
+ let searcher = create_test_searcher ( ) ;
2628
+
2629
+ // Test with document extensions
2630
+ assert ! ( searcher. is_doc( "test.doc" ) ) ;
2631
+ assert ! ( searcher. is_doc( "test.docx" ) ) ;
2632
+ assert ! ( searcher. is_doc( "test.pdf" ) ) ;
2633
+ assert ! ( searcher. is_doc( "test.xls" ) ) ;
2634
+
2635
+ // Test with non-document extensions
2636
+ assert ! ( !searcher. is_doc( "test.txt" ) ) ;
2637
+ assert ! ( !searcher. is_doc( "test.jpg" ) ) ;
2638
+ assert ! ( !searcher. is_doc( "test" ) ) ;
2639
+ }
2640
+
2641
+ #[ test]
2642
+ fn test_is_font ( ) {
2643
+ let searcher = create_test_searcher ( ) ;
2644
+
2645
+ // Test with font extensions
2646
+ assert ! ( searcher. is_font( "test.ttf" ) ) ;
2647
+ assert ! ( searcher. is_font( "test.otf" ) ) ;
2648
+ assert ! ( searcher. is_font( "test.woff" ) ) ;
2649
+ assert ! ( searcher. is_font( "test.woff2" ) ) ;
2650
+
2651
+ // Test with non-font extensions
2652
+ assert ! ( !searcher. is_font( "test.txt" ) ) ;
2653
+ assert ! ( !searcher. is_font( "test.jpg" ) ) ;
2654
+ assert ! ( !searcher. is_font( "test" ) ) ;
2655
+ }
2656
+
2657
+ #[ test]
2658
+ fn test_is_image ( ) {
2659
+ let searcher = create_test_searcher ( ) ;
2660
+
2661
+ // Test with image extensions
2662
+ assert ! ( searcher. is_image( "test.jpg" ) ) ;
2663
+ assert ! ( searcher. is_image( "test.png" ) ) ;
2664
+ assert ! ( searcher. is_image( "test.gif" ) ) ;
2665
+ assert ! ( searcher. is_image( "test.svg" ) ) ;
2666
+
2667
+ // Test with non-image extensions
2668
+ assert ! ( !searcher. is_image( "test.txt" ) ) ;
2669
+ assert ! ( !searcher. is_image( "test.mp3" ) ) ;
2670
+ assert ! ( !searcher. is_image( "test" ) ) ;
2671
+ }
2672
+
2673
+ #[ test]
2674
+ fn test_is_source ( ) {
2675
+ let searcher = create_test_searcher ( ) ;
2676
+
2677
+ // Test with source code extensions
2678
+ assert ! ( searcher. is_source( "test.rs" ) ) ;
2679
+ assert ! ( searcher. is_source( "test.c" ) ) ;
2680
+ assert ! ( searcher. is_source( "test.cpp" ) ) ;
2681
+ assert ! ( searcher. is_source( "test.java" ) ) ;
2682
+
2683
+ // Test with non-source extensions
2684
+ assert ! ( !searcher. is_source( "test.txt" ) ) ;
2685
+ assert ! ( !searcher. is_source( "test.jpg" ) ) ;
2686
+ assert ! ( !searcher. is_source( "test" ) ) ;
2687
+ }
2688
+
2689
+ #[ test]
2690
+ fn test_is_video ( ) {
2691
+ let searcher = create_test_searcher ( ) ;
2692
+
2693
+ // Test with video extensions
2694
+ assert ! ( searcher. is_video( "test.mp4" ) ) ;
2695
+ assert ! ( searcher. is_video( "test.avi" ) ) ;
2696
+ assert ! ( searcher. is_video( "test.mkv" ) ) ;
2697
+ assert ! ( searcher. is_video( "test.mov" ) ) ;
2698
+
2699
+ // Test with non-video extensions
2700
+ assert ! ( !searcher. is_video( "test.txt" ) ) ;
2701
+ assert ! ( !searcher. is_video( "test.jpg" ) ) ;
2702
+ assert ! ( !searcher. is_video( "test" ) ) ;
2703
+ }
2704
+ }
0 commit comments