Skip to content

Instantly share code, notes, and snippets.

@JamesParrott
Created April 26, 2023 20:14
Show Gist options
  • Save JamesParrott/88f5cf4e9ff4bfc40f7b6b86d89a8183 to your computer and use it in GitHub Desktop.
Save JamesParrott/88f5cf4e9ff4bfc40f7b6b86d89a8183 to your computer and use it in GitHub Desktop.
Test code to apply toml test_bug_148 and its fix to multiple toml libraries, for https://github.com/uiri/toml/issues/422
r""" Commands to reproduce:
python -m venv test_bug_148
Posix: source test_bug_148/bin.activate.sh
Windows: .\test_bug_148\Scripts\activate.bat
pip install pytest toml tomli tomli-w tomlkit toml_tools
pytest --tb=no -rA -q test_bug_148_fixed.py
Expected on Windows 10 Python 3.12:
.F....... [100%]
======================================================================== short test summary info ========================================================================
PASSED test_bug_148_correctly.py::test_inverse_bug_148_toml
PASSED test_bug_148_correctly.py::test_inverse_bug_148_tomllib
PASSED test_bug_148_correctly.py::test_inverse_bug_148_tomli
PASSED test_bug_148_correctly.py::test_inverse_bug_148_tomlkit
PASSED test_bug_148_correctly.py::test_inverse_bug_148_toml_tools
PASSED test_bug_148_correctly.py::test_bug_148_tomli_w
PASSED test_bug_148_correctly.py::test_bug_148_tomlkit
PASSED test_bug_148_correctly.py::test_bug_148_toml_tools
FAILED test_bug_148_correctly.py::test_bug_148_toml - assert 'a = "\\\\x64"\n' == 'a = "\\u0064"\n'
1 failed, 8 passed in 0.13s
"""
import sys
if sys.version_info >= (3,11):
import tomllib
import tomli
import tomli_w
import tomlkit
import toml
import toml_tools
def make_correct_bug_148_test_function(toml_module):
def test_bug_148():
assert r'a = "\\x64"' + '\n' == toml_module.dumps({'a': r'\x64'})
assert r'a = "\\\\x64"' + '\n' == toml_module.dumps({'a': r'\\x64'})
assert r'a = "\\\\\\x64"' + '\n' == toml_module.dumps({'a': r'\\\x64'})
# original from
# assert 'a = "\\u0064"\n' == toml_module.dumps({'a': '\\x64'})
# assert 'a = "\\\\x64"\n' == toml_module.dumps({'a': '\\\\x64'})
# assert 'a = "\\\\\\u0064"\n' == toml_module.dumps({'a': '\\\\\\x64'})
return test_bug_148
def make_correct_inverse_bug_148_test_function(toml_module):
def test_bug_148_inverse():
assert toml_module.loads(r'a = "\\x64"' + '\n') == {'a': r'\x64'}
assert toml_module.loads(r'a = "\\\\x64"' + '\n') == {'a': r'\\x64'}
assert toml_module.loads(r'a = "\\\\\\x64"' + '\n') == {'a': r'\\\x64'}
return test_bug_148_inverse
test_inverse_bug_148_toml = make_correct_inverse_bug_148_test_function(toml)
test_bug_148_toml = make_correct_bug_148_test_function(toml)
# native in Python 3.11
try:
test_inverse_bug_148_tomllib = make_correct_inverse_bug_148_test_function(tomllib)
except NameError:
pass
test_inverse_bug_148_tomli = make_correct_inverse_bug_148_test_function(tomli)
test_inverse_bug_148_tomlkit = make_correct_inverse_bug_148_test_function(tomlkit)
test_inverse_bug_148_toml_tools = make_correct_inverse_bug_148_test_function(toml_tools)
test_bug_148_tomli_w = make_correct_bug_148_test_function(tomli_w)
test_bug_148_tomlkit = make_correct_bug_148_test_function(tomlkit)
test_bug_148_toml_tools = make_correct_bug_148_test_function(toml_tools)
r""" Commands to reproduce:
python -m venv test_bug_148
Posix: source test_bug_148/bin.activate.sh
Windows: .\test_bug_148\Scripts\activate.bat
pip install pytest toml tomli tomli-w tomlkit toml_tools
pytest --tb=no -rA -q test_bug_148_original.py
Expected on Windows 10 Python 3.12:
F.FFFFFFF [100%]
======================================================================== short test summary info ========================================================================
PASSED test_bug_148_original.py::test_bug_148_toml
FAILED test_bug_148_original.py::test_inverse_bug_148_toml - AssertionError: assert {'a': 'd'} == {'a': '\\x64'}
FAILED test_bug_148_original.py::test_inverse_bug_148_tomllib - AssertionError: assert {'a': 'd'} == {'a': '\\x64'}
FAILED test_bug_148_original.py::test_inverse_bug_148_tomli - AssertionError: assert {'a': 'd'} == {'a': '\\x64'}
FAILED test_bug_148_original.py::test_inverse_bug_148_tomlkit - AssertionError: assert {'a': 'd'} == {'a': '\\x64'}
FAILED test_bug_148_original.py::test_inverse_bug_148_toml_tools - AssertionError: assert {'a': 'd'} == {'a': '\\x64'}
FAILED test_bug_148_original.py::test_bug_148_tomli_w - assert 'a = "\\u0064"\n' == 'a = "\\\\x64"\n'
FAILED test_bug_148_original.py::test_bug_148_tomlkit - assert 'a = "\\u0064"\n' == 'a = "\\\\x64"\n'
FAILED test_bug_148_original.py::test_bug_148_toml_tools - assert 'a = "\\u0064"\n' == 'a = "\\\\x64"\n'
8 failed, 1 passed in 0.11s
"""
import sys
if sys.version_info >= (3,11):
import tomllib
import tomli
import tomli_w
import tomlkit
import toml
import toml_tools
def make_bug_148_test_function(toml_module):
def test_bug_148():
assert 'a = "\\u0064"\n' == toml_module.dumps({'a': '\\x64'})
assert 'a = "\\\\x64"\n' == toml_module.dumps({'a': '\\\\x64'})
assert 'a = "\\\\\\u0064"\n' == toml_module.dumps({'a': '\\\\\\x64'})
return test_bug_148
def make_inverse_bug_148_test_function(toml_module):
def test_bug_148_inverse():
assert toml_module.loads('a = "\\u0064"\n') == {'a': '\\x64'}
assert toml_module.loads('a = "\\\\x64"\n') == {'a': '\\\\x64'}
assert toml_module.loads('a = "\\\\\\u0064"\n') == {'a': '\\\\\\x64'}
return test_bug_148_inverse
test_inverse_bug_148_toml = make_inverse_bug_148_test_function(toml)
test_bug_148_toml = make_bug_148_test_function(toml)
# native in Python 3.11
try:
test_inverse_bug_148_tomllib = make_inverse_bug_148_test_function(tomllib)
except NameError:
pass
test_inverse_bug_148_tomli = make_inverse_bug_148_test_function(tomli)
test_inverse_bug_148_tomlkit = make_inverse_bug_148_test_function(tomlkit)
test_inverse_bug_148_toml_tools = make_inverse_bug_148_test_function(toml_tools)
test_bug_148_tomli_w = make_bug_148_test_function(tomli_w)
test_bug_148_tomlkit = make_bug_148_test_function(tomlkit)
test_bug_148_toml_tools = make_bug_148_test_function(toml_tools)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment