Skip to content

Commit

Permalink
Fix toml-test scripts (#433)
Browse files Browse the repository at this point in the history
Update tests/decoding_test.py to use the format used by the current
version of toml-test. I also removed Python 2 support (my distro doesn't
ship it, so I can't test it).

Also add tests/encoding_test.py for encoder tests

Still quite a few failures, mostly because it looks like it hasn't been
updated to use 1.0 yet:

    % toml-test ./decoding_test.py | tail -n2
      valid tests: 139 passed, 18 failed
    invalid tests: 230 passed, 53 failed

    % toml-test ./encoding_test.py -encoder | tail -n1
    encoder tests: 146 passed, 11 failed
  • Loading branch information
arp242 authored Oct 10, 2023
1 parent ce4f7a4 commit f6e1e4c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ jobs:
with:
go-version: 1.21

- name: get toml-test
- name: install toml-test
run: |
git clone https://github.com/BurntSushi/toml-test
# cd toml-test/cmd/toml-test
# go build
go install github.com/toml-lang/toml-test/cmd/toml-test@v1.4.0
- name: Run python tests
run: |
pip -q install "tox>=4"
tox run-parallel --parallel-no-spinner
# TODO(warchant): this cmd is old, need to update it
# TODO(warchant): many tests fail
# - name: Run go tests
# run: |
# pip -q install setuptools
# python setup.py install
# ./toml-test/cmd/toml-test/toml-test -i ./tests -g ./tests/gotests.sh
# export PYTHONPATH=.
# e=0
# toml-test ./tests/decoding_test.py || e=1
# toml-test -encoder ./tests/encoding_test.py || e=1
# exit $e
2 changes: 1 addition & 1 deletion CONTRIBUTING
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Directions
----------

1. Install `Go <https://golang.org/>`_ (AKA golang)
2. Get the toml-test suite from `here <https://github.com/BurntSushi/toml-test>`_
2. Get the toml-test suite from `here <https://github.com/toml-lang/toml-test>`_
and follow the instructions under the **Try it out** section of the README.
3. Test your changes for both versions of Python:

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TOML

A Python library for parsing and creating `TOML <https://en.wikipedia.org/wiki/TOML>`_.

The module passes `the TOML test suite <https://github.com/BurntSushi/toml-test>`_.
The module passes `the TOML test suite <https://github.com/toml-lang/toml-test>`_.

See also:

Expand Down
45 changes: 12 additions & 33 deletions tests/decoding_test.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,35 @@
"""Decodes toml and outputs it as tagged JSON"""
#!/usr/bin/env python

"""Decodes TOML and outputs it as tagged JSON"""

import datetime
import json
import sys
import toml

if sys.version_info < (3,):
_range = xrange # noqa: F821
iteritems = dict.iteritems
else:
unicode = str
_range = range
basestring = str
unichr = chr
iteritems = dict.items
long = int


def tag(value):
if isinstance(value, dict):
d = {}
for k, v in iteritems(value):
d[k] = tag(v)
return d
return {k: tag(v) for (k, v) in value.items()}
elif isinstance(value, list):
a = []
for v in value:
a.append(tag(v))
try:
a[0]["value"]
except KeyError:
return a
except IndexError:
pass
return {'type': 'array', 'value': a}
elif isinstance(value, basestring):
return [tag(v) for v in value]
elif isinstance(value, str):
return {'type': 'string', 'value': value}
elif isinstance(value, bool):
return {'type': 'bool', 'value': str(value).lower()}
elif isinstance(value, int):
return {'type': 'integer', 'value': str(value)}
elif isinstance(value, long):
return {'type': 'integer', 'value': str(value)}
elif isinstance(value, float):
return {'type': 'float', 'value': repr(value)}
elif isinstance(value, datetime.datetime):
return {'type': 'datetime', 'value': value.isoformat()
.replace('+00:00', 'Z')}
return {
'type': 'datetime-local' if value.tzinfo is None else 'datetime',
'value': value.isoformat().replace('+00:00', 'Z'),
}
elif isinstance(value, datetime.date):
return {'type': 'date', 'value': value.isoformat()}
return {'type': 'date-local', 'value': value.isoformat()}
elif isinstance(value, datetime.time):
return {'type': 'time', 'value': value.strftime('%H:%M:%S.%f')}
return {'type': 'time-local', 'value': value.strftime('%H:%M:%S.%f')}
assert False, 'Unknown type: %s' % type(value)


Expand Down
28 changes: 28 additions & 0 deletions tests/encoding_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python

"""Reads tagged JSON and encodes it as TOML"""

import json
import sys
import toml

def convert(v):
if type(v) == list:
return [convert(vv) for vv in v]
elif v.get('type', None) is None or v.get('value', None) is None:
return {k: convert(vv) for (k, vv) in v.items()}
elif v['type'] == 'string':
return v['value']
elif v['type'] == 'integer':
return int(v['value'])
elif v['type'] == 'float':
return float(v['value'])
elif v['type'] == 'bool':
return True if v['value'] == 'true' else False
elif v['type'] in ['datetime', 'datetime-local', 'date-local', 'time-local']:
return toml.loads('a=' + v['value'])['a']
else:
raise Exception(f'unknown type: {v}')

j = json.loads(sys.stdin.read())
print(toml.dumps({k: convert(v) for (k, v) in j.items()}))

0 comments on commit f6e1e4c

Please sign in to comment.