def test_should_unescape_double_quotes_within_string(self): output = ensure_quotes('"I\'m \\"great\\""', '\'') expect(output).to_equal('\'I\\\'m "great"\'')
def test_should_not_escape_already_escaped_quotes(self): output = ensure_quotes('\'how\\\'s it \\"going\\"?\'', '"') expect(output).to_equal('"how\'s it \\"going\\"?"')
def test_should_escape_double_quotes_within_string(self): output = ensure_quotes('\'hi "all"\'', '"') expect(output).to_equal('"hi \\"all\\""')
def test_should_unescape_single_quotes_within_string(self): output = ensure_quotes('\'how\\\'s it going?\'', '"') expect(output).to_equal('"how\'s it going?"')
def test_should_flip_double_quotes_to_single_quotes(self): output = ensure_quotes('"hello"', '\'') expect(output).to_equal('\'hello\'')
def test_should_flip_empty_single_quotes_to_double_quotes(self): output = ensure_quotes('\'\'', '"') expect(output).to_equal('""')
def test_should_not_add_quotes_to_unterminated_value(self): output = ensure_quotes('"hello', '"') expect(output).to_equal('"hello"')
def format(self): output = '' indent = 0 reader = self._reader has_properties = False is_last_token_comment = False spacer = self._options['spacer'] newline = self._options['newline'] remove_comments = self._options['remove_comments'] while True: should_linebreak = False result = reader.read() is_comment = False if not result: break if is_last_token_comment: should_linebreak = True is_last_token_comment = False if result.type == 'begin_object': has_properties = False indent += 1 elif result.type == 'end_object': indent -= 1 if has_properties: should_linebreak = True elif result.type == 'property': should_linebreak = True has_properties = True elif result.type == 'new_line_comment': is_comment = True is_last_token_comment = True should_linebreak = True elif result.type == 'end_line_comment': is_comment = True is_last_token_comment = True elif result.type == 'new_line_comment_block': is_comment = True should_linebreak = True elif result.type == 'in_line_comment_block': is_comment = True if is_comment and remove_comments: continue # new line if should_linebreak: output = join(newline, strip_trailing(output, spacer), indent * self._options['indent_character']) # actual character if result.type == 'property' and 'force_property_quotes' in self._options and self._options['force_property_quotes']: output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'value' and 'normalize_strings' in self._options and self._options['normalize_strings'] and is_string_value(result.value): output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'end_line_comment': output = strip_trailing(output, spacer) output += spacer + result.value elif result.type == 'new_line_comment_block': output += format_block_comment(result.value, indent * self._options['indent_character']) + newline elif result.type == 'in_line_comment_block': output = strip_trailing(output, spacer) output += spacer + format_block_comment(result.value, indent * self._options['indent_character']) else: output += result.value # suffix if result.type == 'property_separator': output += spacer elif result.type == 'value_separator': output += spacer elif result.type == 'in_line_comment_block': output += spacer return output
def format(self): output = '' indent = 0 reader = self._reader has_properties = False is_last_token_comment = False spacer = self._options['spacer'] newline = self._options['newline'] remove_comments = self._options['remove_comments'] while True: should_linebreak = False result = reader.read() is_comment = False if not result: break if is_last_token_comment: should_linebreak = True is_last_token_comment = False if result.type == 'begin_object': has_properties = False indent += 1 elif result.type == 'end_object': indent -= 1 if has_properties: should_linebreak = True elif result.type == 'property': should_linebreak = True has_properties = True elif result.type == 'new_line_comment': is_comment = True is_last_token_comment = True should_linebreak = True elif result.type == 'end_line_comment': is_comment = True is_last_token_comment = True elif result.type == 'new_line_comment_block': is_comment = True should_linebreak = True elif result.type == 'in_line_comment_block': is_comment = True if is_comment and remove_comments: continue # new line if should_linebreak: output = join(newline, strip_trailing(output, spacer), indent * self._options['indent_character']) # actual character if result.type == 'property' and 'force_property_quotes' in self._options and self._options[ 'force_property_quotes']: output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'value' and 'normalize_strings' in self._options and self._options[ 'normalize_strings'] and is_string_value(result.value): output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'end_line_comment': output = strip_trailing(output, spacer) output += spacer + result.value elif result.type == 'new_line_comment_block': output += format_block_comment( result.value, indent * self._options['indent_character']) + newline elif result.type == 'in_line_comment_block': output = strip_trailing(output, spacer) output += spacer + format_block_comment( result.value, indent * self._options['indent_character']) else: output += result.value # suffix if result.type == 'property_separator': output += spacer elif result.type == 'value_separator': output += spacer elif result.type == 'in_line_comment_block': output += spacer return output