Refactor legacy indent: int -> indent: str

The type of `indent` parameter is preferably `str`. See
http://simplejson.readthedocs.io/en/latest/index.html?highlight=dump#simplejson.dump
. This change allows to specify TABs as indentation delimiter to
`pretty_format_json`. Add input validator/converter for backward compat.
This commit is contained in:
Sander Maijers 2016-06-10 20:16:00 +02:00
parent 6f2b0a27e5
commit a5628863e8
No known key found for this signature in database
GPG key ID: 2E974A73F1B0F2F7
3 changed files with 53 additions and 3 deletions

View file

@ -2,10 +2,21 @@ import io
import pytest
from pre_commit_hooks.pretty_format_json import parse_indent
from pre_commit_hooks.pretty_format_json import pretty_format_json
from testing.util import get_resource_path
def test_parse_indent():
assert parse_indent('0') == ''
assert parse_indent('2') == ' '
assert parse_indent('\t') == '\t'
with pytest.raises(ValueError):
parse_indent('a')
with pytest.raises(ValueError):
parse_indent('-2')
@pytest.mark.parametrize(('filename', 'expected_retval'), (
('not_pretty_formatted_json.json', 1),
('unsorted_pretty_formatted_json.json', 1),
@ -26,6 +37,17 @@ def test_unsorted_pretty_format_json(filename, expected_retval):
assert ret == expected_retval
@pytest.mark.parametrize(('filename', 'expected_retval'), (
('not_pretty_formatted_json.json', 1),
('unsorted_pretty_formatted_json.json', 1),
('pretty_formatted_json.json', 1),
('tab_pretty_formatted_json.json', 0),
))
def test_tab_pretty_format_json(filename, expected_retval):
ret = pretty_format_json(['--indent', '\t', get_resource_path(filename)])
assert ret == expected_retval
def test_autofix_pretty_format_json(tmpdir):
srcfile = tmpdir.join('to_be_json_formatted.json')
with io.open(get_resource_path('not_pretty_formatted_json.json')) as f: