Esempio n. 1
0
    def test_should_format_single_quote_properties_with_forced_double_quotes(
            self):
        reader = JsonReader('{\'hello\':"world" ,\'value\':123}')
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Esempio n. 2
0
    def test_should_format_object_with_array(self):
        reader = JsonReader('{"hello":"world" ,"arr":["hello","world"]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "hello": "world",\n  "arr": ["hello", "world"]\n}')
Esempio n. 3
0
    def test_should_preserve_end_line_comment(self):
        reader = JsonReader('{\'"hello"\':"world", // comment\n\'value\':123}')
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world", // comment\n  "value": 123\n}')
Esempio n. 4
0
    def test_should_format_nested_object(self):
        reader = JsonReader('{"hello":"world" ,"obj":{"abc":123}}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "hello": "world",\n  "obj": {\n    "abc": 123\n  }\n}')
Esempio n. 5
0
def result_resolver(input):
    reader = JsonReader(input)
    formatter = JsonFormatter(
        reader, {"force_property_quotes": True, "indent_character": "\t", "normalize_strings": True}
    )

    return formatter.format()
Esempio n. 6
0
    def test_should_format_array_of_objects(self):
        reader = JsonReader('{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "arr": [{\n    "int": 123,\n    "str": "hello"\n  }, {\n    "int": 123,\n    "str": "hello"\n  }]\n}'
        )
Esempio n. 7
0
    def test_should_format_array_of_objects_with_tabs(self):
        reader = JsonReader('{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader, {"indent_character": "\t"})

        output = formatter.format()
        expect(output).to_equal(
            '{\n\t"arr": [{\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}, {\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}]\n}'
        )
Esempio n. 8
0
    def format(self, text, options):
        reader = JsonReader(text)
        formatter = JsonFormatter(reader, {'force_property_quotes': True, 'quote_char': '"', 'normalize_strings': True, 'indent_character': options['indent_character']})
        formattedText = formatter.format()

        self.view.set_syntax_file('Packages/JavaScript/JSON.tmLanguage')

        return formattedText
Esempio n. 9
0
    def test_should_format_array_of_objects(self):
        reader = JsonReader(
            '{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "arr": [{\n    "int": 123,\n    "str": "hello"\n  }, {\n    "int": 123,\n    "str": "hello"\n  }]\n}'
        )
Esempio n. 10
0
    def test_should_format_array_of_objects_with_tabs(self):
        reader = JsonReader(
            '{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}')
        formatter = JsonFormatter(reader, {'indent_character': '\t'})

        output = formatter.format()
        expect(output).to_equal(
            '{\n\t"arr": [{\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}, {\n\t\t"int": 123,\n\t\t"str": "hello"\n\t}]\n}'
        )
Esempio n. 11
0
def result_resolver(input):
    reader = JsonReader(input)
    formatter = JsonFormatter(
        reader, {
            'force_property_quotes': True,
            'indent_character': '\t',
            'normalize_strings': True
        })

    return formatter.format()
Esempio n. 12
0
    def test_should_format_property_with_empty_double_quote_string_and_normalized_strings(
            self):
        reader = JsonReader('{\'hello\':""}')
        formatter = JsonFormatter(reader, {
            'force_property_quotes': True,
            'normalize_strings': True
        })

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": ""\n}')
Esempio n. 13
0
    def test_should_preserve_end_line_comment_forcing_new_line_for_subsequent_tokens(
            self):
        reader = JsonReader(
            '{\'"hello"\':"world",\n// full line comment\n\'value\'://comment\n123}'
        )
        formatter = JsonFormatter(reader, {'force_property_quotes': True})

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world",\n  // full line comment\n  "value": //comment\n  123\n}'
        )
Esempio n. 14
0
    def test_should_strip_in_line_comment_block(self):
        reader = JsonReader(
            '{\'"hello"\':"world", /* comment block */ \'value\':123}')
        formatter = JsonFormatter(reader, {
            'force_property_quotes': True,
            'remove_comments': True
        })

        output = formatter.format()
        expect(output).to_equal(
            '{\n  "\\"hello\\"": "world",\n  "value": 123\n}')
Esempio n. 15
0
    def test_should_format_property_values_with_normalized_strings(self):
        reader = JsonReader('{\'hello\':"world" ,"value":123}')
        formatter = JsonFormatter(
            reader, {
                'force_property_quotes': True,
                'quote_char': '\'',
                'normalize_strings': True
            })

        output = formatter.format()
        expect(output).to_equal(
            '{\n  \'hello\': \'world\',\n  \'value\': 123\n}')
Esempio n. 16
0
    def test_should_format_json_on_one_line(self):
        reader = JsonReader(
            '{"hello":\n  "world", // comment\n \'value\':123}')
        formatter = JsonFormatter(
            reader, {
                'force_property_quotes': True,
                'remove_comments': True,
                'spacer': '',
                'newline': '',
                'indent_character': ''
            })

        output = formatter.format()
        expect(output).to_equal('{"hello":"world","value":123}')
Esempio n. 17
0
    def test_should_format_json_on_one_line(self):
        reader = JsonReader('{"hello":\n  "world", // comment\n \'value\':123}')
        formatter = JsonFormatter(
            reader,
            {
                "force_property_quotes": True,
                "remove_comments": True,
                "spacer": "",
                "newline": "",
                "indent_character": "",
            },
        )

        output = formatter.format()
        expect(output).to_equal('{"hello":"world","value":123}')
Esempio n. 18
0
    def format(self, text, indent_char='\t'):
        if text:
            match = first_char_exp.search(text)

            if match:
                first_char = match.group(1)

                if first_char == '<':
                    formatter = XmlIndentFormatter(indent_char)
                    return formatter.indent(text)
                else:
                    formatter = JsonFormatter(
                        JsonReader(text), {'indent_character': indent_char})
                    return formatter.format()
Esempio n. 19
0
    def unindent(self, text):
        if text:
            match = first_char_exp.search(text)

            if match:
                first_char = match.group(1)

                if first_char == '<':
                    formatter = XmlIndentFormatter('', True)
                    return formatter.indent(text)
                else:
                    formatter = JsonFormatter(
                        JsonReader(text), {
                            'remove_comments': True,
                            'spacer': '',
                            'newline': '',
                            'indent_character': ''
                        })
                    return formatter.format()
Esempio n. 20
0
    def test_should_format_empty_object(self):
        reader = JsonReader('{ }')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{}')
Esempio n. 21
0
    def test_should_format_object_with_array(self):
        reader = JsonReader('{"hello":"world" ,"arr":["hello","world"]}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "arr": ["hello", "world"]\n}')
Esempio n. 22
0
    def test_should_format_scalar_array(self):
        reader = JsonReader('[1,2,3]')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('[1, 2, 3]')
Esempio n. 23
0
    def test_should_format_multiple_properties(self):
        reader = JsonReader('{"hello":"world" ,"value":123}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Esempio n. 24
0
    def test_should_format_nested_object(self):
        reader = JsonReader('{"hello":"world" ,"obj":{"abc":123}}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "obj": {\n    "abc": 123\n  }\n}')
Esempio n. 25
0
    def test_should_format_property_with_empty_double_quote_string_and_normalized_strings(self):
        reader = JsonReader("{'hello':\"\"}")
        formatter = JsonFormatter(reader, {"force_property_quotes": True, "normalize_strings": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": ""\n}')
Esempio n. 26
0
    def test_should_format_single_property(self):
        reader = JsonReader('{"hello":"world"}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world"\n}')
Esempio n. 27
0
    def test_should_format_single_property(self):
        reader = JsonReader('{"hello":"world"}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world"\n}')
Esempio n. 28
0
    def test_should_format_empty_object(self):
        reader = JsonReader("{ }")
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal("{}")
Esempio n. 29
0
    def test_should_strip_in_line_comment_block(self):
        reader = JsonReader("{'\"hello\"':\"world\", /* comment block */ 'value':123}")
        formatter = JsonFormatter(reader, {"force_property_quotes": True, "remove_comments": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "\\"hello\\"": "world",\n  "value": 123\n}')
Esempio n. 30
0
    def test_should_format_multiple_properties(self):
        reader = JsonReader('{"hello":"world" ,"value":123}')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Esempio n. 31
0
    def test_should_preserve_end_line_comment_forcing_new_line_for_subsequent_tokens(self):
        reader = JsonReader("{'\"hello\"':\"world\",\n// full line comment\n'value'://comment\n123}")
        formatter = JsonFormatter(reader, {"force_property_quotes": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "\\"hello\\"": "world",\n  // full line comment\n  "value": //comment\n  123\n}')
Esempio n. 32
0
    def test_should_preserve_end_line_comment(self):
        reader = JsonReader("{'\"hello\"':\"world\", // comment\n'value':123}")
        formatter = JsonFormatter(reader, {"force_property_quotes": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "\\"hello\\"": "world", // comment\n  "value": 123\n}')
Esempio n. 33
0
    def test_should_format_property_values_with_normalized_strings(self):
        reader = JsonReader('{\'hello\':"world" ,"value":123}')
        formatter = JsonFormatter(reader, {"force_property_quotes": True, "quote_char": "'", "normalize_strings": True})

        output = formatter.format()
        expect(output).to_equal("{\n  'hello': 'world',\n  'value': 123\n}")
Esempio n. 34
0
    def test_should_format_multiple_properties_with_forced_double_quotes(self):
        reader = JsonReader('{hello:"world" ,value:123}')
        formatter = JsonFormatter(reader, {"force_property_quotes": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Esempio n. 35
0
    def test_should_format_scalar_array(self):
        reader = JsonReader("[1,2,3]")
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal("[1, 2, 3]")
Esempio n. 36
0
    def test_should_format_single_quote_properties_with_forced_double_quotes(self):
        reader = JsonReader("{'hello':\"world\" ,'value':123}")
        formatter = JsonFormatter(reader, {"force_property_quotes": True})

        output = formatter.format()
        expect(output).to_equal('{\n  "hello": "world",\n  "value": 123\n}')
Esempio n. 37
0
    def test_should_format_properties_with_forced_single_quotes(self):
        reader = JsonReader('{\'hello\':"world" ,"value":123}')
        formatter = JsonFormatter(reader, {"force_property_quotes": True, "quote_char": "'"})

        output = formatter.format()
        expect(output).to_equal("{\n  'hello': \"world\",\n  'value': 123\n}")