1
0
mirror of https://github.com/bspeice/dtparse synced 2024-11-14 09:58:09 -05:00
dtparse/build_tests.py

77 lines
2.1 KiB
Python
Raw Normal View History

2018-05-17 00:31:57 -04:00
#import dateutil.parser._timelex.split as time_split
from dateutil.parser import _timelex
2018-05-23 23:01:00 -04:00
from dateutil.parser import parse as duparse
import pytz
2018-05-17 00:31:57 -04:00
# The TEST_STRINGS list should be the only thing that actually needs changing
TEST_STRINGS = [
'2018.5.15',
'May 5, 2018',
'Mar. 5, 2018',
]
2018-05-23 23:11:20 -04:00
AUTOGEN_HEADER = '''
// WARNING
// This file was auto-generated using the `build_tests.py` script.
// Please do not edit it manually.
'''
2018-05-17 00:31:57 -04:00
S4 = ' ' * 4
S8 = ' ' * 8
S12 = ' ' * 12
2018-05-23 23:01:00 -04:00
def rust_tokenize(time_string):
2018-05-17 00:31:57 -04:00
split_array = _timelex.split(time_string)
return ['"{}".to_owned()'.format(token) for token in split_array]
2018-05-17 00:31:57 -04:00
2018-05-23 23:01:00 -04:00
def build_split_string_tests():
2018-05-24 22:29:58 -04:00
header = '''use tokenize;
2018-05-17 00:31:57 -04:00
#[test]
fn test_python_compat() {\n'''
tests = []
for test_string in TEST_STRINGS:
token_string = '\n'.join(['{}{},'.format(S12, s)
2018-05-23 23:01:00 -04:00
for s in rust_tokenize(test_string)])
2018-05-17 00:31:57 -04:00
tests.append(' assert_eq!(\n{}tokenize("{}"),\n{}vec![\n{}\n{}]\n{});'
.format(S8, test_string, S8, token_string, S8, S4))
body = '\n'.join(tests)
footer = '\n}\n'
2018-05-23 23:01:00 -04:00
return header + body + footer
def test_parse(time_string):
dt = duparse(time_string)
# TODO: Don't make this dependent on New_York
iso8601 = pytz.timezone('America/New_York').localize(dt).astimezone(pytz.utc)
return 'assert_eq!(\n{}parse("{}".to_owned())\n{}.unwrap()\n{}.to_rfc3339_opts(SecondsFormat::Micros, false),\n{}"{}"\n{});'.format(
S8, time_string, S12, S12, S8, iso8601, S4)
def build_parse_tests():
header = '''use chrono::SecondsFormat;
use parse;
#[test]
fn test_python_compat() {\n'''
asserts = [' {}'.format(test_parse(a)) for a in TEST_STRINGS]
body = '\n'.join(asserts)
footer = '\n}\n'
return header + body + footer
2018-05-17 00:31:57 -04:00
if __name__ == '__main__':
2018-05-23 23:01:00 -04:00
split_string_test = build_split_string_tests()
with open('src/tests/compat_split_string.rs', 'w+') as handle:
2018-05-23 23:11:20 -04:00
handle.write(AUTOGEN_HEADER + split_string_test)
2018-05-23 23:01:00 -04:00
parse_test = build_parse_tests()
with open('src/tests/compat_parse.rs', 'w+') as handle:
2018-05-23 23:11:20 -04:00
handle.write(AUTOGEN_HEADER + parse_test)