Class: AtCoderFriends::TestRunner::Base
- Inherits:
-
Object
- Object
- AtCoderFriends::TestRunner::Base
show all
- Includes:
- PathUtil
- Defined in:
- lib/at_coder_friends/test_runner/base.rb
Overview
run tests for the specified program.
Constant Summary
collapse
- STATUS_STR =
{
OK: '<< OK >>'.green,
WA: '!!!!! WA !!!!!'.red,
RE: '!!!!! RE !!!!!'.red,
NO_EXP: ''
}.freeze
Constants included
from PathUtil
PathUtil::CASES_DIR, PathUtil::SMP_DIR, PathUtil::TMP_DIR
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#call_remote_test(infile) ⇒ Object
-
#check_file(infile, outfile) ⇒ Object
-
#check_status(is_success, result, expected) ⇒ Object
-
#detail_str(input, result, expected) ⇒ Object
-
#initialize(ctx) ⇒ Base
constructor
-
#local_test(infile, outfile) ⇒ Object
-
#remote_test(infile, outfile) ⇒ Object
-
#run_test(id, infile, outfile, expfile) ⇒ Object
-
#test_cmd ⇒ Object
-
#test_loc ⇒ Object
-
#test_mtd ⇒ Object
-
#which_os ⇒ Object
Methods included from PathUtil
cases_dir, contest_name, makedirs_unless, smp_dir, split_prg_path, tmp_dir
Constructor Details
#initialize(ctx) ⇒ Base
20
21
22
23
24
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 20
def initialize(ctx)
@ctx = ctx
@path, @dir, @prg, @base, @ext, @q = split_prg_path(ctx.path)
@detail = true
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def base
@base
end
|
#ctx ⇒ Object
Returns the value of attribute ctx.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def ctx
@ctx
end
|
#dir ⇒ Object
Returns the value of attribute dir.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def dir
@dir
end
|
#ext ⇒ Object
Returns the value of attribute ext.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def ext
@ext
end
|
#path ⇒ Object
Returns the value of attribute path.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def path
@path
end
|
#prg ⇒ Object
Returns the value of attribute prg.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def prg
@prg
end
|
#q ⇒ Object
Returns the value of attribute q.
18
19
20
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 18
def q
@q
end
|
Instance Method Details
#call_remote_test(infile) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 75
def call_remote_test(infile)
res = ctx.scraping_agent.code_test(infile)
(res && res['Result']) || (return [false, 'Remote test failed.'])
puts "Exit code: #{res.dig('Result', 'ExitCode')}"
puts "Time: #{res.dig('Result', 'TimeConsumption')}ms"
puts "Memory: #{res.dig('Result', 'MemoryConsumption')}KB"
res.dig('Result', 'ExitCode').zero? || (return [false, res['Stderr']])
[true, res['Stdout']]
end
|
#check_file(infile, outfile) ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 56
def check_file(infile, outfile)
unless File.exist?(infile)
puts "#{File.basename(infile)} not found."
return false
end
makedirs_unless(File.dirname(outfile))
true
end
|
#check_status(is_success, result, expected) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 87
def check_status(is_success, result, expected)
if !is_success
:RE
elsif !expected
:NO_EXP
elsif result != expected
:WA
else
:OK
end
end
|
#detail_str(input, result, expected) ⇒ Object
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 99
def detail_str(input, result, expected)
ret = ''
ret += "-- input --\n"
ret += input
ret += "-- expected --\n"
ret += expected || "(no expected value)\n"
ret += "-- result --\n"
ret += result
ret
end
|
#local_test(infile, outfile) ⇒ Object
65
66
67
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 65
def local_test(infile, outfile)
system("#{test_cmd} < #{infile} > #{outfile}")
end
|
#remote_test(infile, outfile) ⇒ Object
69
70
71
72
73
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 69
def remote_test(infile, outfile)
is_success, result = call_remote_test(infile)
File.write(outfile, result)
is_success
end
|
#run_test(id, infile, outfile, expfile) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 42
def run_test(id, infile, outfile, expfile)
puts "==== #{id} ===="
return false unless check_file(infile, outfile)
is_success = send(test_mtd, infile, outfile)
input = File.read(infile)
result = File.read(outfile)
expected = File.exist?(expfile) && File.read(expfile)
status = check_status(is_success, result, expected)
print detail_str(input, result, expected) if @detail
puts STATUS_STR[status]
status == :OK
end
|
#test_cmd ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 26
def test_cmd
@test_cmd ||= begin
cmds = ctx.config.dig('ext_settings', ext, 'test_cmd')
cmd = cmds && (cmds[which_os.to_s] || cmds['default'])
cmd&.gsub('{dir}', dir)&.gsub('{base}', base)
end
end
|
#test_loc ⇒ Object
34
35
36
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 34
def test_loc
test_cmd ? 'local' : 'remote'
end
|
#test_mtd ⇒ Object
38
39
40
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 38
def test_mtd
test_cmd ? :local_test : :remote_test
end
|
#which_os ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/at_coder_friends/test_runner/base.rb', line 110
def which_os
@which_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
|