Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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()
Exemplo 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}'
        )
Exemplo 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}'
        )
Exemplo 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
Exemplo 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}'
        )
Exemplo 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}'
        )
Exemplo 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()
Exemplo 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}')
Exemplo 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}'
        )
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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()
Exemplo 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()
Exemplo n.º 20
0
    def test_should_format_empty_object(self):
        reader = JsonReader('{ }')
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal('{}')
Exemplo 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}')
Exemplo 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]')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo n.º 28
0
    def test_should_format_empty_object(self):
        reader = JsonReader("{ }")
        formatter = JsonFormatter(reader)

        output = formatter.format()
        expect(output).to_equal("{}")
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}')
Exemplo 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}")
Exemplo 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}')
Exemplo 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]")
Exemplo 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}')
Exemplo 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}")