Skip to content

Commit

Permalink
Merge pull request #703 from kmyk/feature/allow-crlf
Browse files Browse the repository at this point in the history
Allow CRLF
  • Loading branch information
fukatani authored Mar 18, 2020
2 parents bd1472b + 2d4d971 commit d442859
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 13 deletions.
15 changes: 15 additions & 0 deletions onlinejudge/_implementation/command/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ def match(a, b):
if rstrip and a.rstrip(rstrip_targets) == b.rstrip(rstrip_targets):
log.warning('WA if no rstrip')
return True
if a == b.replace('\n', '\r\n'):
log.warning(r'WA if not replacing "\r\n" with "\n"')
return True
if rstrip and a.rstrip(rstrip_targets) == b.replace('\n', '\r\n').rstrip(rstrip_targets):
log.warning('WA if no rstrip')
log.warning(r'WA if not replacing "\r\n" with "\n"')
return True
if a.replace('\n', '\r\n') == b:
log.warning(r'WA if not replacing "\n" with "\r\n"')
return True
if rstrip and a.replace('\n', '\r\n').rstrip(rstrip_targets) == b.rstrip(rstrip_targets):
# TODO: use a smart way if you need more equality patterns
log.warning('WA if no rstrip')
log.warning(r'WA if not replacing "\n" with "\r\n"')
return True
return False

# prepare the function to print the input
Expand Down
105 changes: 94 additions & 11 deletions tests/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@


class TestTest(unittest.TestCase):
def snippet_call_test(self, args, files, expected, verbose=True):
def snippet_call_test(self, args, files, expected, verbose=True, replace_output_newline=True):
result = tests.utils.run_in_sandbox(args=(['-v'] if verbose else []) + ['test', '--json'] + args, files=files)
self.assertTrue(result['proc'].stdout)
data = json.loads(result['proc'].stdout.decode())
print(data)
if expected is None:
return data
else:
Expand All @@ -31,7 +32,11 @@ def snippet_call_test(self, args, files, expected, verbose=True):
self.assertEqual(a['testcase']['output'], b['testcase']['output'].replace('/', os.path.sep) % result['tempdir'])
self.assertEqual(a['exitcode'], b['exitcode'])
self.assertEqual(a['status'], b['status'])
self.assertEqual(a['output'].replace(os.linesep, '\n'), b['output'])
if replace_output_newline:
a_output = a['output'].replace(os.linesep, '\n')
else:
a_output = a['output']
self.assertEqual(a_output, b['output'])

def test_call_test_simple(self):
self.snippet_call_test(
Expand Down Expand Up @@ -155,11 +160,11 @@ def test_call_test_float(self):
},
{
'path': 'test/sample-5.in',
'data': '1.0\n2.0\n'.replace('\n', os.linesep)
'data': '1.0\n2.0\n'
},
{
'path': 'test/sample-5.out',
'data': '1.0\n'.replace('\n', os.linesep)
'data': '1.0\n'
},
],
expected=[{
Expand Down Expand Up @@ -205,7 +210,7 @@ def test_call_test_float(self):
'input': '%s/test/sample-5.in',
'output': '%s/test/sample-5.out',
},
'output': '1.0\n2.0\n'.replace('\n', os.linesep),
'output': '1.0\n2.0\n',
'exitcode': 0,
}],
)
Expand Down Expand Up @@ -284,19 +289,19 @@ def test_call_test_multiline_all(self):
files=[
{
'path': 'test/sample-1.in',
'data': 'foo\nfoobar\n'.replace('\n', os.linesep)
'data': 'foo\nfoobar\n'
},
{
'path': 'test/sample-1.out',
'data': 'foo\nfoobar\n'.replace('\n', os.linesep)
'data': 'foo\nfoobar\n'
},
{
'path': 'test/sample-2.in',
'data': 'bar\nfoobar\n'.replace('\n', os.linesep)
'data': 'bar\nfoobar\n'
},
{
'path': 'test/sample-2.out',
'data': 'bar\nbarbar\n'.replace('\n', os.linesep)
'data': 'bar\nbarbar\n'
},
],
expected=[{
Expand All @@ -306,7 +311,7 @@ def test_call_test_multiline_all(self):
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'foo\nfoobar\n'.replace('\n', os.linesep),
'output': 'foo\nfoobar\n',
'exitcode': 0,
}, {
'status': 'WA',
Expand All @@ -315,7 +320,7 @@ def test_call_test_multiline_all(self):
'input': '%s/test/sample-2.in',
'output': '%s/test/sample-2.out',
},
'output': 'bar\nfoobar\n'.replace('\n', os.linesep),
'output': 'bar\nfoobar\n',
'exitcode': 0,
}],
)
Expand Down Expand Up @@ -931,6 +936,84 @@ def test_call_non_unicode(self):
}],
)

def test_call_output_uses_crlf(self):
data = self.snippet_call_test(
args=['-c', tests.utils.python_c(r"import sys; sys.stdout.buffer.write(b'foo\r\nbar\r\nbaz\r\n')")],
files=[
{
'path': 'test/sample-1.in',
'data': '',
},
{
'path': 'test/sample-1.out',
'data': 'foo\nbar\nbaz\n'
},
],
expected=[{
'status': 'AC',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'foo\r\nbar\r\nbaz\r\n',
'exitcode': 0,
}],
replace_output_newline=False,
)

def test_call_expected_uses_crlf(self):
data = self.snippet_call_test(
args=['-c', tests.utils.python_c(r"import sys; sys.stdout.buffer.write(b'foo\nbar\nbaz\n')")],
files=[
{
'path': 'test/sample-1.in',
'data': '',
},
{
'path': 'test/sample-1.out',
'data': 'foo\r\nbar\r\nbaz\r\n',
},
],
expected=[{
'status': 'AC',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'foo\nbar\nbaz\n',
'exitcode': 0,
}],
replace_output_newline=False,
)

def test_call_output_uses_both_lf_and_crlf(self):
data = self.snippet_call_test(
args=['-c', tests.utils.python_c(r"import sys; sys.stdout.buffer.write(b'foo\r\nbar\nbaz\r\n')")],
files=[
{
'path': 'test/sample-1.in',
'data': '',
},
{
'path': 'test/sample-1.out',
'data': 'foo\nbar\nbaz\n'
},
],
expected=[{
'status': 'WA',
'testcase': {
'name': 'sample-1',
'input': '%s/test/sample-1.in',
'output': '%s/test/sample-1.out',
},
'output': 'foo\r\nbar\nbaz\r\n',
'exitcode': 0,
}],
replace_output_newline=False,
)

# TODO: fix
@unittest.expectedFailure
@unittest.skipIf(os.name == 'nt', "procfs is required")
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def prepare_files(files):
for f in files:
path = pathlib.Path(f['path'])
path.parent.mkdir(parents=True, exist_ok=True)
with open(str(path), 'w') as fh:
fh.write(f['data'])
with open(str(path), 'wb') as fh:
fh.write(f['data'].encode())
if f.get('executable', False):
path.chmod(0o755)

Expand Down

0 comments on commit d442859

Please sign in to comment.