Class: TestWwFunction

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/ww.rb

Instance Method Summary collapse

Instance Method Details

#setupObject



465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/ww.rb', line 465

def setup
  # Save original global state
  @original_debug = $debug
  @original_ww_log_file = $ww_log_file
  @original_ww_output = $ww_output
  @original_ww_category = $ww_category

  # Redirect output to capture it - ensure it's writable
  @output_buffer = StringIO.new
  @output_buffer.sync = true
  $ww_output = @output_buffer
  $ww_category = nil
end

#teardownObject



479
480
481
482
483
484
485
486
487
488
# File 'lib/ww.rb', line 479

def teardown
  # Restore original global state
  $debug = @original_debug
  $ww_log_file = @original_ww_log_file
  $ww_output = @original_ww_output
  $ww_category = @original_ww_category

  # Clean up any test log files
  Dir.glob('test*.log').each { |f| FileUtils.rm_f(f) }
end

#test_complex_data_structuresObject



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/ww.rb', line 754

def test_complex_data_structures
  $debug = false

  # Test with nested arrays
  nested = [1, [2, [3, 4]], 5]
  result = ww('start', nested)
  assert_equal nested, result, 'ww should handle nested arrays'

  # Test with complex hash
  complex_hash = {
    users: [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }],
    metadata: { created: Time.now, version: '1.0' }
  }
  result = ww('prefix', complex_hash)
  assert_equal complex_hash, result, 'ww should handle complex hashes'

  # Test with objects
  string_obj = String.new('test')
  result = ww('object', string_obj)
  assert_equal string_obj, result, 'ww should handle object instances'
end

#test_edge_casesObject

Edge case tests



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/ww.rb', line 728

def test_edge_cases
  $debug = false

  # Test with nil values
  result = ww(nil, 'not_nil')
  assert_equal 'not_nil', result, 'ww should handle nil values correctly'

  # Test with empty array as last item
  result = ww('first', [])
  assert_equal [], result,
               'ww should return empty array if it is the last item'

  # Test with false as last item
  result = ww('first', false)
  assert_equal false, result, 'ww should return false if it is the last item'

  # Test with zero as last item
  result = ww('first', 0)
  assert_equal 0, result, 'ww should return zero if it is the last item'

  # Test with empty string as last item
  result = ww('first', '')
  assert_equal '', result,
               'ww should return empty string if it is the last item'
end

#test_full_backtrace_optionObject



845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/ww.rb', line 845

def test_full_backtrace_option
  $debug = true
  $ww_log_file = nil

  # Create fresh buffer for this test
  fresh_buffer = StringIO.new
  $ww_output = fresh_buffer

  # Test full_backtrace option
  ww('test', full_backtrace: true)
  output = fresh_buffer.string

  # With full backtrace, we should see multiple stack levels
  # (This is hard to test precisely since it depends on call stack depth)
  refute_empty output, 'full_backtrace should produce output'
end

#test_location_informationObject



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/ww.rb', line 827

def test_location_information
  $debug = true
  $ww_log_file = nil

  # Create fresh buffer for this test
  fresh_buffer = StringIO.new
  $ww_output = fresh_buffer

  # Test that location information is included
  ww('location test')
  output = fresh_buffer.string

  # Should include file path and line number information
  assert_includes output, 'lib/ww.rb', 'output should include filename'
  assert_match(/\s:\s\d+\s:/, output,
               'output should include line number with format')
end

#test_multiple_consecutive_callsObject



862
863
864
865
866
867
868
869
870
871
872
873
# File 'lib/ww.rb', line 862

def test_multiple_consecutive_calls
  $debug = false

  # Test that multiple calls work correctly
  results = []
  results << ww('call1', 'result1')
  results << ww('call2a', 'call2b', 'result2')
  results << ww('call3a', 'call3b', 'call3c', 'result3')

  assert_equal %w[result1 result2 result3], results,
               'Multiple consecutive calls should work correctly'
end

#test_options_validationObject



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/ww.rb', line 776

def test_options_validation
  $debug = true
  $ww_log_file = nil

  # Test valid log levels
  %i[debug info warn error fatal].each do |level|
    result = ww('test', level: level)
    assert_equal 'test', result, "ww should work with log level #{level}"
  end

  # Test invalid log level raises error
  assert_raises(ArgumentError) do
    ww('test', level: :invalid)
  end
end

#test_output_formatting_optionsObject



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/ww.rb', line 792

def test_output_formatting_options
  $debug = true
  $ww_log_file = nil

  # Create fresh buffer for this test
  fresh_buffer = StringIO.new
  $ww_output = fresh_buffer

  # Test single_line option
  ww('test', 'data', single_line: true)
  output = fresh_buffer.string
  refute_includes output, "\n        [",
                  'single_line should format output on one line'

  # Reset buffer
  fresh_buffer = StringIO.new
  $ww_output = fresh_buffer

  # Test timestamp option
  ww('test', timestamp: true)
  output = fresh_buffer.string
  assert_match(/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]/, output,
               'timestamp should be included in output')

  # Reset buffer
  fresh_buffer = StringIO.new
  $ww_output = fresh_buffer

  # Test category option
  ww('test', category: 'MYCATEGORY')
  output = fresh_buffer.string
  assert_includes output, '[MYCATEGORY]',
                  'category should be included in output'
end

#test_return_value_consistency_across_debug_statesObject



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/ww.rb', line 875

def test_return_value_consistency_across_debug_states
  # Test the same call with debug enabled and disabled
  test_args = %w[first second third]

  # With debug disabled
  $debug = false
  $ww_log_file = nil
  result_no_debug = ww(*test_args)

  # With debug enabled, no log file
  $debug = true
  $ww_log_file = nil
  result_debug_no_log = ww(*test_args)

  # With debug enabled, with log file
  $debug = true
  $ww_log_file = 'consistency_test.log'
  begin
    result_debug_with_log = ww(*test_args)
  ensure
    FileUtils.rm_f('consistency_test.log')
  end

  # All should return the same value
  assert_equal 'third', result_no_debug,
               'Should return last item with debug disabled'
  assert_equal 'third', result_debug_no_log,
               'Should return last item with debug enabled, no log'
  assert_equal 'third', result_debug_with_log,
               'Should return last item with debug enabled, with log'
  assert_equal result_no_debug, result_debug_no_log,
               'Results should be consistent across debug states'
  assert_equal result_no_debug, result_debug_with_log,
               'Results should be consistent across log file states'
end

#test_ww_multiline_continuationObject



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/ww.rb', line 678

def test_ww_multiline_continuation
  $debug = true
  $ww_log_file = nil

  # Test ww with multiline continuation
  var1 = 'test_value'
  result = ww \
    var1
  assert_equal 'test_value', result,
               'ww should work with multiline continuation'

  # Test with multiple items in multiline
  result = ww 'prefix', \
              'value'
  assert_equal 'value', result,
               'ww should work with multiple items in multiline'

  # Test with debug disabled
  $debug = false
  result = ww \
    'no_debug_value'
  assert_equal 'no_debug_value', result,
               'ww should return correct value with debug disabled'
end

#test_ww_returns_last_item_with_debug_disabledObject

Core functionality tests



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/ww.rb', line 491

def test_ww_returns_last_item_with_debug_disabled
  $debug = false
  $ww_log_file = nil

  # Test with single item
  result = ww('single_item')
  assert_equal 'single_item', result,
               'ww should return the single item when debug is disabled'

  # Test with multiple items
  result = ww('first', 'second', 'third')
  assert_equal 'third', result,
               'ww should return the last item when debug is disabled'

  # Test with various data types
  result = ww(1, 'string', :symbol, [1, 2, 3])
  assert_equal [1, 2, 3], result,
               'ww should return the last item regardless of type'

  # Verify no output when debug is disabled
  assert_empty @output_buffer.string,
               'No output should be generated when debug is disabled'
end

#test_ww_returns_last_item_with_debug_enabled_no_log_fileObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/ww.rb', line 515

def test_ww_returns_last_item_with_debug_enabled_no_log_file
  $debug = true
  $ww_log_file = nil

  # Test with single item
  result = ww('single_item')
  assert_equal 'single_item', result,
               'ww should return the single item when debug is enabled and no log file'

  # Test with multiple items
  result = ww('first', 'second', 'third')
  assert_equal 'third', result,
               'ww should return the last item when debug is enabled and no log file'

  # Test with mixed types
  result = ww(42, 'hello', :world, { key: 'value' })
  assert_equal({ key: 'value' }, result,
               'ww should return the last item even with hash')

  # Verify output is generated when debug is enabled
  refute_empty @output_buffer.string,
               'Output should be generated when debug is enabled'
end

#test_ww_returns_last_item_with_debug_enabled_with_log_fileObject



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/ww.rb', line 539

def test_ww_returns_last_item_with_debug_enabled_with_log_file
  $debug = true
  $ww_log_file = 'test_ww.log'

  begin
    # Test with single item
    result = ww('single_item')
    assert_equal 'single_item', result,
                 'ww should return the single item when debug is enabled with log file'

    # Test with multiple items
    result = ww('first', 'second', 'third')
    assert_equal 'third', result,
                 'ww should return the last item when debug is enabled with log file'

    # Test with various data types
    result = ww(1, 'string', :symbol, [1, 2, 3])
    assert_equal [1, 2, 3], result,
                 'ww should return the last item regardless of type with log file'

    # Verify log file was created and contains content
    assert File.exist?('test_ww.log'), 'Log file should be created'
    refute_empty File.read('test_ww.log'), 'Log file should contain content'
  ensure
    # Clean up test log file
    FileUtils.rm_f('test_ww.log')
  end
end

#test_ww_with_named_optionsObject



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/ww.rb', line 568

def test_ww_with_named_options
  $debug = false

  # Test that named options don't affect the return value
  result = ww('first', 'second', category: 'test', level: :info)
  assert_equal 'second', result,
               'ww should return the last item even with named options'

  # Test with single item and options
  result = ww('only_item', timestamp: true, single_line: true)
  assert_equal 'only_item', result,
               'ww should return the single item even with named options'

  # Test with various option combinations
  result = ww('a', 'b', 'c', category: 'testing', level: :warn,
                             timestamp: true, single_line: false)
  assert_equal 'c', result,
               'ww should return the last item with complex options'
end

#test_wwa_function_behaviorObject



703
704
705
706
707
708
709
710
# File 'lib/ww.rb', line 703

def test_wwa_function_behavior
  $debug = true

  # Test that wwa exits (we can't easily test exit behavior in minitest)
  # So we'll just verify it would call ww0 properly by testing the structure
  # Note: wwa calls exit, so we can't test it directly without special handling
  skip 'wwa exits the program, cannot test directly in minitest'
end

#test_wwe_function_behaviorObject



712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/ww.rb', line 712

def test_wwe_function_behavior
  $debug = true

  # Test that wwe raises an error
  assert_raises(StandardError) do
    wwe('error message')
  end

  # Test that wwe raises with the first object as the error message
  error = assert_raises(StandardError) do
    wwe('custom error', 'additional', 'data')
  end
  assert_equal 'custom error', error.message
end

#test_wwp_function_returns_last_itemObject



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/ww.rb', line 608

def test_wwp_function_returns_last_item
  $debug = true
  $ww_log_file = nil

  # Test wwp with multiple items
  result = wwp('first', 'second', 'third')
  assert_equal 'third', result, 'wwp should return the last item'

  # Test wwp with single item
  result = wwp('only_item')
  assert_equal 'only_item', result, 'wwp should return the single item'

  # Test wwp with debug disabled
  $debug = false
  result = wwp('a', 'b', 'c')
  assert_equal 'c', result,
               'wwp should return the last item even when debug is disabled'
end

#test_wwr_function_returns_last_itemObject

Test all ww function variants



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/ww.rb', line 589

def test_wwr_function_returns_last_item
  $debug = true
  $ww_log_file = nil

  # Test wwr with multiple items
  result = wwr('first', 'second', 'third')
  assert_equal 'third', result, 'wwr should return the last item'

  # Test wwr with single item
  result = wwr('only_item')
  assert_equal 'only_item', result, 'wwr should return the single item'

  # Test wwr with debug disabled
  $debug = false
  result = wwr('a', 'b', 'c')
  assert_equal 'c', result,
               'wwr should return the last item even when debug is disabled'
end

#test_wwt_function_returns_last_itemObject



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/ww.rb', line 627

def test_wwt_function_returns_last_item
  $debug = true
  $ww_log_file = nil

  # Test wwt with multiple items (first item is tag)
  result = wwt(:test_tag, 'first', 'second', 'third')
  assert_equal 'third', result, 'wwt should return the last item'

  # Test wwt with single data item after tag
  result = wwt(:tag, 'only_data_item')
  assert_equal 'only_data_item', result,
               'wwt should return the single data item'

  # Test wwt with skipped tags
  result = wwt(:blocks, 'data')
  assert_equal 'data', result, 'wwt should return data even for skipped tags'

  # Test wwt with debug disabled
  $debug = false
  result = wwt(:any_tag, 'a', 'b', 'c')
  assert_equal 'c', result,
               'wwt should return the last item even when debug is disabled'
end

#test_wwt_multiline_continuationObject



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/ww.rb', line 651

def test_wwt_multiline_continuation
  $debug = true
  $ww_log_file = nil

  # Test multiline continuation pattern
  var1 = 'test_value'
  result = wwt :tag1, \
               var1
  assert_equal 'test_value', result,
               'wwt should work with multiline continuation'

  # Test with complex expression
  complex_var = [1, 2, 3]
  result = wwt(:processing, \
               complex_var.map { |x| x * 2 })
  assert_equal [2, 4, 6], result,
               'wwt should work with complex expressions in multiline'

  # Test with debug disabled
  $debug = false
  simple_var = 'no_debug'
  result = wwt :disabled, \
               simple_var
  assert_equal 'no_debug', result,
               'wwt should return correct value even with debug disabled'
end