Skip to content

Commit

Permalink
#234: add test for test command
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Jan 3, 2019
1 parent da77e35 commit 9a0a19c
Showing 1 changed file with 227 additions and 0 deletions.
227 changes: 227 additions & 0 deletions tests/command_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import unittest

import contextlib
import json
import os
import subprocess
import sys
import tempfile


@contextlib.contextmanager
def chdir(path):
cwd = os.getcwd()
try:
os.chdir(path)
yield
finally:
os.chdir(cwd)

def run_in_sandbox(args, files):
ojtools = os.path.abspath('oj')
with tempfile.TemporaryDirectory() as tempdir:
with chdir(tempdir):
for f in files:
if os.path.dirname(f['path']):
os.makedirs(os.path.dirname(f['path']), exist_ok=True)
with open(f['path'], 'w') as fh:
fh.write(f['data'])
if f.get('executable', False):
os.chmod(f['path'], 0o755)
proc = subprocess.run([ ojtools ] + args, stdout=subprocess.PIPE, stderr=sys.stderr)
return {
'proc': proc,
'tempdir': tempdir,
}


class TestTest(unittest.TestCase):

def snippet_call_test(self, args, files, expected):
result = run_in_sandbox(args=[ '-v', 'test', '--json' ] + args, files=files)
self.assertTrue(result['proc'].stdout)
data = json.loads(result['proc'].stdout.decode())
self.assertEqual(len(data), len(expected))
for a, b in zip(data, expected):
self.assertEqual(a['testcase']['name'], b['testcase']['name'])
self.assertEqual(a['testcase']['input'], b['testcase']['input'] % result['tempdir'])
self.assertEqual('output' in a['testcase'], 'output' in b['testcase'])
if 'output' in b['testcase']:
self.assertEqual(a['testcase']['output'], b['testcase']['output'] % result['tempdir'])
self.assertEqual(a['exitcode'], b['exitcode'])
self.assertEqual(a['result'], b['result'])
self.assertEqual(a['output'], b['output'])

def test_call_test_simple(self):
self.snippet_call_test(
args=[ '-c', 'cat' ],
files=[
{ 'path': 'test/sample-1.in', 'data': 'foo\n' },
{ 'path': 'test/sample-1.out', 'data': 'foo\n' },
{ 'path': 'test/sample-2.in', 'data': 'bar\n' },
{ 'path': 'test/sample-2.out', 'data': 'foo\n' },
],
expected=[ {
'result': 'AC',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'foo\n',
'exitcode': 0,
}, {
'result': 'WA',
'testcase': {
'name': 'sample-2',
'input': '%s/test/sample-2.in',
'output': '%s/test/sample-2.out',
},
'output': 'bar\n',
'exitcode': 0,
} ],
)

def test_call_test_select(self):
self.snippet_call_test(
args=[ '-c', 'cat', 'test/sample-2.in', 'test/sample-3.in', 'test/sample-3.out' ],
files=[
{ 'path': 'test/sample-1.in', 'data': 'foo\n' },
{ 'path': 'test/sample-1.out', 'data': 'Yes\n' },
{ 'path': 'test/sample-2.in', 'data': 'bar\n' },
{ 'path': 'test/sample-2.out', 'data': 'No\n' },
{ 'path': 'test/sample-3.in', 'data': 'baz\n' },
{ 'path': 'test/sample-3.out', 'data': 'No\n' },
],
expected=[ {
'result': 'AC',
'testcase': {
'name': 'sample-2',
'input': '%s/test/sample-2.in',
},
'output': 'bar\n',
'exitcode': 0,
}, {
'result': 'WA',
'testcase': {
'name': 'sample-3',
'input': '%s/test/sample-3.in',
'output': '%s/test/sample-3.out',
},
'output': 'baz\n',
'exitcode': 0,
} ],
)

def test_call_test_shell(self):
self.snippet_call_test(
args=[ '-c', './build/foo.sh hoge' ],
files=[
{ 'path': 'build/foo.sh', 'data': '#!/bin/sh\necho $1\n', 'executable': True },
{ 'path': 'test/sample-1.in', 'data': 'foo\n' },
{ 'path': 'test/sample-1.out', 'data': 'foo\n' },
],
expected=[ {
'result': 'WA',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'hoge\n',
'exitcode': 0,
} ],
)

def test_call_test_fail(self):
self.snippet_call_test(
args=[ '-c', './foo.sh' ],
files=[
{ 'path': 'foo.sh', 'data': '#!/bin/sh\necho bar\nexit 3\n', 'executable': True },
{ 'path': 'test/sample-1.in', 'data': 'foo\n' },
{ 'path': 'test/sample-1.out', 'data': 'foo\n' },
],
expected=[ {
'result': 'WA',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'bar\n',
'exitcode': 3,
} ],
)

def test_call_test_dir(self):
self.snippet_call_test(
args=[ '-c', 'cat', '-d', 'path/to/../../p/o/y/o' ],
files=[
{ 'path': 'p/o/y/o/sample-1.in', 'data': 'foo\n' },
{ 'path': 'p/o/y/o/sample-1.out', 'data': 'foo\n' },
{ 'path': 'test/sample-2.in', 'data': 'bar\n' },
{ 'path': 'test/sample-2.out', 'data': 'bar\n' },
],
expected=[ {
'result': 'AC',
'testcase': {
'name': 'sample-1',
'input': '%s/p/o/y/o/sample-1.in',
'output': '%s/p/o/y/o/sample-1.out',
},
'output': 'foo\n',
'exitcode': 0,
} ],
)

def test_call_test_format(self):
self.snippet_call_test(
args=[ '-c', 'cat', '-d', 'yuki/coder', '-f', 'test_%e/%s' ],
files=[
{ 'path': 'yuki/coder/test_in/sample-1.txt', 'data': 'foo\n' },
{ 'path': 'yuki/coder/test_out/sample-1.txt', 'data': 'foo\n' },
{ 'path': 'test_in/sample-2.in', 'data': 'bar\n' },
{ 'path': 'test_out/sample-2.out', 'data': 'bar\n' },
],
expected=[ {
'result': 'AC',
'testcase': {
'name': 'sample-1.txt',
'input': '%s/yuki/coder/test_in/sample-1.txt',
'output': '%s/yuki/coder/test_out/sample-1.txt',
},
'output': 'foo\n',
'exitcode': 0,
} ],
)

def test_call_test_format_select(self):
self.snippet_call_test(
args=[ '-c', 'cat', '-d', 'yuki/coder', '-f', 'test_%e/%s', 'yuki/coder/test_in/sample-2.txt', 'yuki/coder/test_in/sample-3.txt', 'yuki/coder/test_out/sample-3.txt' ],
files=[
{ 'path': 'yuki/coder/test_in/sample-1.txt', 'data': 'foo\n' },
{ 'path': 'yuki/coder/test_out/sample-1.txt', 'data': 'foo\n' },
{ 'path': 'yuki/coder/test_in/sample-2.txt', 'data': 'bar\n' },
{ 'path': 'yuki/coder/test_out/sample-2.txt', 'data': 'bar\n' },
{ 'path': 'yuki/coder/test_in/sample-3.txt', 'data': 'baz\n' },
{ 'path': 'yuki/coder/test_out/sample-3.txt', 'data': 'baz\n' },
],
expected=[ {
'result': 'AC',
'testcase': {
'name': 'sample-2.txt',
'input': '%s/yuki/coder/test_in/sample-2.txt',
},
'output': 'bar\n',
'exitcode': 0,
}, {
'result': 'AC',
'testcase': {
'name': 'sample-3.txt',
'input': '%s/yuki/coder/test_in/sample-3.txt',
'output': '%s/yuki/coder/test_out/sample-3.txt',
},
'output': 'baz\n',
'exitcode': 0,
} ],
)

0 comments on commit 9a0a19c

Please sign in to comment.