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



2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
# File 'lib/oktest.rb', line 2098

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



2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
# File 'lib/oktest.rb', line 2121

def run(*args)
  color_enabled = nil
  opts = Options.new
  parser = option_parser(opts)
  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] '-C' or '--create' option prints test code skeleton.
  if opts.create
    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?
    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.
  #; [!ef5v7] '-s simple' or '-ss' option prints test results in simple 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