Class: Oktest::MainApp

Inherits:
Object
  • Object
show all
Defined in:
lib/oktest.rb

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.main(argv = nil) ⇒ Object



2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
# File 'lib/oktest.rb', line 2610

def self.main(argv=nil)
  #; [!tb6sx] returns 0 when no errors raised.
  #; [!d5mql] returns 1 when a certain error raised.
  argv ||= ARGV
  begin
    status = self.new.run(*argv)  or raise "** internal error"
    return status
  #; [!jr49p] reports error when unknown option specified.
  #; [!uqomj] reports error when required argument is missing.
  #; [!8i755] reports error when argument is invalid.
  rescue OptionParser::ParseError => exc
    case exc
    when OptionParser::InvalidOption   ; s = "unknown option."
    when OptionParser::InvalidArgument ; s = "invalid argument."
    when OptionParser::MissingArgument ; s = "argument required."
    else                               ; s = nil
    end
    msg = s ? "#{exc.args.join(' ')}: #{s}" : exc.message
    $stderr.puts("#{File.basename($0)}: #{msg}")
    return 1
  end
end

Instance Method Details

#run(*args) ⇒ Object



2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
# File 'lib/oktest.rb', line 2633

def run(*args)
  color_enabled = nil
  opts = Options.new
  parser = option_parser(opts)
  #; [!v5xie] parses $OKTEST_RB environment variable.
  if ENV.key?('OKTEST_RB')
    parser.parse(ENV['OKTEST_RB'].split())
  end
  #
  filenames = parser.parse(args)
  #; [!9973n] '-h' or '--help' option prints help message.
  if opts.help
    puts help_message()
    return 0
  end
  #; [!qqizl] '--version' option prints version number.
  if opts.version
    puts VERSION
    return 0
  end
  #; [!dk8eg] '-S' or '--skeleton' option prints test code skeleton.
  if opts.skeleton
    print SKELETON
    return 0
  end
  #; [!uxh5e] '-G' or '--generate' option prints test code.
  #; [!wmxu5] '--generate=unaryop' option prints test code with unary op.
  if opts.generate
    print generate(filenames, opts.generate)
    return 0
  end
  #; [!65vdx] prints help message if no arguments specified.
  if filenames.empty? && !THE_GLOBAL_SCOPE.has_child?
    puts help_message()
    return 0
  end
  #; [!6ro7j] '--color=on' option enables output coloring forcedly.
  #; [!vmw0q] '--color=off' option disables output coloring forcedly.
  if opts.color
    color_enabled = Config.color_enabled
    Config.color_enabled = (opts.color == 'on')
  end
  #; [!qs8ab] '--faster' chanages 'Config.ok_location' to false.
  if opts.faster
    Config.ok_location = false    # will make 'ok{}' faster
  end
  #
  $LOADED_FEATURES << __FILE__ unless $LOADED_FEATURES.include?(__FILE__) # avoid loading twice
  #; [!hiu5b] finds test scripts in directory and runs them.
  load_files(filenames)
  #; [!yz7g5] '-F topic=...' option filters topics.
  #; [!ww2mp] '-F spec=...' option filters specs.
  #; [!8uvib] '-F tag=...' option filters by tag name.
  #; [!m0iwm] '-F sid=...' option filters by spec id.
  #; [!noi8i] '-F' option supports negative filter.
  if opts.filter
    filter_obj = FILTER_CLASS.create_from(opts.filter)
    Oktest.filter(filter_obj)
  end
  #; [!bim36] changes auto-running to off.
  Config.auto_run = false
  #; [!18qpe] runs test scripts.
  #; [!0qd92] '-s verbose' or '-sv' option prints test results in verbose mode.
  #; [!zfdr5] '-s simple' or '-ss' option prints test results in simple mode.
  #; [!ef5v7] '-s compact' or '-sc' option prints test results in compact mode.
  #; [!244te] '-s plain' or '-sp' option prints test results in plain mode.
  #; [!ai61w] '-s quiet' or '-sq' option prints test results in quiet mode.
  n_errors = Oktest.run(:style=>opts.style)
  #; [!dsrae] reports if 'ok()' called but assertion not performed.
  AssertionObject.report_not_yet()
  #; [!bzgiw] returns total number of failures and errors.
  return n_errors
ensure
  #; [!937kw] recovers 'Config.color_enabled' value.
  Config.color_enabled = color_enabled if color_enabled != nil
end