Allow python to take dictionary or tuple

This commit is contained in:
Liam Newman 2018-08-27 16:46:33 -07:00
parent 4383295024
commit bfa695d6e2
3 changed files with 93 additions and 43 deletions

View File

@ -24,6 +24,7 @@
import copy
import re
from collections import namedtuple
class Options:
def __init__(self, options=None, merge_child_field=None):
@ -132,7 +133,17 @@ class Options:
def _mergeOpts(options, childFieldName):
finalOpts = copy.copy(options) or object()
if options is None:
options = {}
if isinstance(options, tuple):
options = dict(options)
if isinstance(options, dict):
options = _normalizeOpts(options)
options = namedtuple("CustomOptions", options.keys())(*options.values())
finalOpts = copy.copy(options)
local = getattr(finalOpts, childFieldName, None)
if local:
@ -143,11 +154,18 @@ def _mergeOpts(options, childFieldName):
return finalOpts
def _normalizeOpts(options):
convertedOpts = copy.copy(options) or object()
option_keys = copy.copy(getattr(convertedOpts, '__dict__', {}))
for key in option_keys:
if '-' in key:
delattr(convertedOpts, key)
setattr(convertedOpts, key.replace('-', '_'), getattr(options, key, None))
convertedOpts = copy.copy(options)
if isinstance(convertedOpts, dict):
option_keys = list(convertedOpts.keys())
for key in option_keys:
if '-' in key:
del convertedOpts[key]
convertedOpts[key.replace('-', '_')] = options[key]
else:
option_keys = list(getattr(convertedOpts, '__dict__', {}))
for key in option_keys:
if '-' in key:
delattr(convertedOpts, key)
setattr(convertedOpts, key.replace('-', '_'), getattr(options, key, None))
return convertedOpts

View File

@ -5370,6 +5370,21 @@ class TestJSBeautifier(unittest.TestCase):
setattr(self.options, 'end-with-newline', True)
test_fragment(None, '\n')
self.reset_options()
#============================================================
# Test passing dictionary or tuple
self.options = {'end_with_newline': True, 'eol': '\r\n' }
test_fragment(None, '\r\n')
self.options = {'end-with-newline': True}
test_fragment(None, '\n')
self.options = {'end-with-newline': False}
test_fragment(None, '')
self.options = ( ('end-with-newline', True), ('eol', '\r') )
test_fragment(None, '\r')
self.reset_options()
#============================================================
self.options.indent_size = 1
@ -6498,26 +6513,27 @@ class TestJSBeautifier(unittest.TestCase):
self.assertMultiLineEqual(
jsbeautifier.beautify(expectation, self.options), expectation)
# Everywhere we do newlines, they should be replaced with opts.eol
self.options.eol = '\r\\n'
expectation = expectation.replace('\n', '\r\n')
self.options.disabled = True
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), input or '')
self.assertMultiLineEqual(
jsbeautifier.beautify('\n\n' + expectation, self.options), '\n\n' + expectation)
self.options.disabled = False;
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
if self.options is None or not isinstance(self.options, (dict, tuple)):
# Everywhere we do newlines, they should be replaced with opts.eol
self.options.eol = '\r\\n'
expectation = expectation.replace('\n', '\r\n')
self.options.disabled = True
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), input or '')
self.assertMultiLineEqual(
jsbeautifier.beautify('\n\n' + expectation, self.options), '\n\n' + expectation)
self.options.disabled = False;
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
# Ensure support for auto eol detection
self.options.eol = 'auto'
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
self.options.eol = '\n'
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
# Ensure support for auto eol detection
self.options.eol = 'auto'
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
self.options.eol = '\n'
def wrap(self, text):
return self.wrapregex.sub(' \\1', text)

View File

@ -150,6 +150,21 @@ class TestJSBeautifier(unittest.TestCase):
setattr(self.options, 'end-with-newline', True)
test_fragment(None, '\n')
self.reset_options()
#============================================================
# Test passing dictionary or tuple
self.options = {'end_with_newline': True, 'eol': '\r\n' }
test_fragment(None, '\r\n')
self.options = {'end-with-newline': True}
test_fragment(None, '\n')
self.options = {'end-with-newline': False}
test_fragment(None, '')
self.options = ( ('end-with-newline', True), ('eol', '\r') )
test_fragment(None, '\r')
self.reset_options()
#============================================================
self.options.indent_size = 1
@ -1278,26 +1293,27 @@ class TestJSBeautifier(unittest.TestCase):
self.assertMultiLineEqual(
jsbeautifier.beautify(expectation, self.options), expectation)
# Everywhere we do newlines, they should be replaced with opts.eol
self.options.eol = '\r\\n'
expectation = expectation.replace('\n', '\r\n')
self.options.disabled = True
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), input or '')
self.assertMultiLineEqual(
jsbeautifier.beautify('\n\n' + expectation, self.options), '\n\n' + expectation)
self.options.disabled = False;
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
if self.options is None or not isinstance(self.options, (dict, tuple)):
# Everywhere we do newlines, they should be replaced with opts.eol
self.options.eol = '\r\\n'
expectation = expectation.replace('\n', '\r\n')
self.options.disabled = True
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), input or '')
self.assertMultiLineEqual(
jsbeautifier.beautify('\n\n' + expectation, self.options), '\n\n' + expectation)
self.options.disabled = False;
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
# Ensure support for auto eol detection
self.options.eol = 'auto'
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
self.options.eol = '\n'
if input and input.find('\n') != -1:
input = input.replace('\n', '\r\n')
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
# Ensure support for auto eol detection
self.options.eol = 'auto'
self.assertMultiLineEqual(
jsbeautifier.beautify(input, self.options), expectation)
self.options.eol = '\n'
def wrap(self, text):
return self.wrapregex.sub(' \\1', text)