implement: travis → github actions

implement: #42
update: unit tests
This commit is contained in:
TheSecEng 2020-04-16 08:49:10 -04:00
parent f0c9c8b6a7
commit ced7e12107
No known key found for this signature in database
GPG Key ID: A7C3BA459E8C5C4E
6 changed files with 56 additions and 12 deletions

2
.gitattributes vendored
View File

@ -1 +1,3 @@
/tests export-ignore
/.github export-ignore
pyproject.toml export-ignore

20
.github/workflows/unit_tests.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: Pretty Json Tests
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Run Tests
run: |
cd tests
python tests.py

View File

@ -1,6 +1,7 @@
{
"use_entire_file_if_no_selection": true,
"indent": 4,
"sort_keys": false,
"ensure_ascii": false,
"line_separator": ",",
@ -9,7 +10,11 @@
"max_arrays_line_length": 120,
"pretty_on_save": false,
"validate_on_save": true,
"reindent_block": false,
"brace_newline": true,
"bracket_newline": true,
// Default: False
// Valid Options: False, start, minimal
"reindent_block": "minimal",
// Name or Path to jq binary
// Example: /usr/bin/local/jq
// Example: jq

View File

@ -43,8 +43,10 @@ def check_jq():
class PrettyJsonBaseCommand:
phantom_set = sublime.PhantomSet
phantoms = list()
json_char_matcher = re.compile(r'char (\d+)')
force_sorting = False
json_char_matcher = re.compile(r'char (\d+)')
brace_newline = re.compile(r'^((\s*)".*?":)\s*([{])', re.MULTILINE)
bracket_newline = re.compile(r'^((\s*)".*?":)\s*([\[])', re.MULTILINE)
@staticmethod
def json_loads(selection: str) -> dict:
@ -87,19 +89,31 @@ class PrettyJsonBaseCommand:
replacement = '[' + join_separator.join(items) + ']'
if len(replacement) <= s.get('max_arrays_line_length', 120):
output_json = output_json.replace(m, replacement, 1)
if s.get("brace_newline", True):
output_json = PrettyJsonBaseCommand.brace_newline.sub(r'\1\n\2\3', output_json)
if (
s.get("bracket_newline", True)
and s.get('keep_arrays_single_line', False) is False
):
output_json = PrettyJsonBaseCommand.bracket_newline.sub(r'\1\n\2\3', output_json)
return output_json
def brace_bracket_newline(json_data: str) -> str:
better_json = PrettyJsonBaseCommand.brace_bracket_newline.sub(r'\1\n\2\3', json_data)
return better_json
def reindent(self, text: str, selection: str):
current_line = self.view.line(selection.begin())
text_before_sel = sublime.Region(current_line.begin(), selection.begin())
reindent_mode = s.get('reindent_block', 'minimal')
reindent_mode = s.get('reindent_block', False)
if reindent_mode == 'start':
space_number = text_before_sel.size()
indent_space = ' ' * space_number
else:
indent_space = re.search('^\s*', self.view.substr(text_before_sel)).group(0)
elif reindent_mode == 'minimal':
indent_space = re.search(r'^\s*', self.view.substr(text_before_sel)).group(0)
lines = text.split('\n')

View File

@ -1,3 +1,6 @@
/* Description: Taken from LSP
- https://github.com/sublimelsp/LSP/blob/master/plugin/diagnostics.py
*/
div.error-arrow {
border-top: 0.4rem solid transparent;
border-left: 0.5rem solid color(var(--redish) blend(var(--background) 30%));

View File

@ -29,9 +29,9 @@ class TestIssues(unittest.TestCase):
# issue 15
def test_float_issue_15(self):
tmp_str = '{"real":0.99}'
expected_output = """{
expected_output = '''{
"real": 0.99
}"""
}'''
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
separators=(',', ': '),
@ -52,7 +52,7 @@ class TestIssues(unittest.TestCase):
def test_float_issue_16_2(self):
tmp_str = '{"test1":0.99, "test2":"1.99", "test3":1.00000000001, "test4":1.99, "test5":1,' \
' "test6":4.589999999999999999, "test7":1.0}'
expected_output = """{
expected_output = '''{
"test1": 0.99,
"test2": "1.99",
"test3": 1.00000000001,
@ -60,7 +60,7 @@ class TestIssues(unittest.TestCase):
"test5": 1,
"test6": 4.589999999999999999,
"test7": 1.0
}"""
}'''
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
separators=(',', ': '),
@ -68,9 +68,9 @@ class TestIssues(unittest.TestCase):
self.assertEqual(tmp_str, expected_output)
def test_compress_feature(self):
tmp_str = """{
tmp_str = '''{
"real": 0.99
}"""
}'''
expected_output = '{"real":0.99}'
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
tmp_str = json.dumps(obj, ensure_ascii=False, sort_keys=False,