Sha256: 17281b39331ff8bb32aa19737ee23c83748aef0f5c9eed15841eaf715b393fc8

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'rbconfig'

module AtCoderFriends
  # run tests for the specified program.
  class TestRunner
    include PathUtil

    def initialize(path)
      @path, @dir, @prg, @base, @ext, @q = split_prg_path(path)
    end

    # rubocop:disable Metrics/MethodLength
    def run_test(id, infile, outfile, expfile)
      return false unless File.exist?(infile) && File.exist?(expfile)

      puts "==== #{id} ===="
      ec = system("#{edit_cmd} < #{infile} > #{outfile}")

      input, result, expected =
        [infile, outfile, expfile].map { |file| File.read(file) }
      puts '-- input --'
      print input
      puts '-- expected --'
      print expected
      puts '-- result --'
      print result
      if !ec
        puts '!!!!! RE !!!!!'
      elsif result != expected
        puts '!!!!! WA !!!!!'
      else
        puts '<< OK >>'
      end
      true
    end
    # rubocop:enable Metrics/MethodLength

    # rubocop:disable Metrics/MethodLength
    def edit_cmd
      case @ext
      when 'java'
        "java -cp #{@dir} Main"
      when 'rb'
        "ruby #{@dir}/#{@base}.rb"
      when 'cs'
        case which_os
        when :windows
          "#{@dir}/#{@base}.exe"
        else
          "mono #{@dir}/#{@base}.exe"
        end
      else # c, cxx
        case which_os
        when :windows
          "#{@dir}/#{@base}.exe"
        else
          "#{@dir}/#{@base}"
        end
      end
    end
    # rubocop:enable Metrics/MethodLength

    def which_os
      @os ||= begin
        case RbConfig::CONFIG['host_os']
        when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
          :windows
        when /darwin|mac os/
          :macosx
        when /linux/
          :linux
        when /solaris|bsd/
          :unix
        else
          :unknown
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
at_coder_friends-0.3.3 lib/at_coder_friends/test_runner.rb
at_coder_friends-0.3.2 lib/at_coder_friends/test_runner.rb
at_coder_friends-0.3.1 lib/at_coder_friends/test_runner.rb