def test_should_find_expression_like_value_terminated_by_comma(self): reader = JsonReader('abc + 123 - xyz, abc') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('abc + 123 - xyz')
def test_should_find_new_line_comment_block(self): reader = JsonReader('/* this is a\nblock comment */') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('new_line_comment_block') expect(result.value).to_equal('/* this is a\nblock comment */')
def test_should_find_comment_on_line_by_itself(self): reader = JsonReader('\t// this is a comment') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('new_line_comment') expect(result.value).to_equal('// this is a comment')
def test_should_find_expression_like_value_terminated_by_new_line(self): reader = JsonReader('abc("hello")') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('abc("hello")')
def test_should_find_comment_on_end_of_line(self): reader = JsonReader('something // this is a comment') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('something') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_line_comment') expect(result.value).to_equal('// this is a comment')
def test_should_read_property_with_asterisk(self): reader = JsonReader('{"*":"asterisk"}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"*"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"asterisk"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()
def test_should_read_json_with_unterminated_strings(self): reader = JsonReader('{"hello\n:"world\n}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"hello') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"world') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()
def test_should_read_formatted_json(self): reader = JsonReader('{\n "hello": "world"\n}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"hello"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"world"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()
def test_should_read_json_with_literals(self): reader = JsonReader('{hello:123.4}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('hello') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('123.4') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()
def test_should_read_empty_json_object(self): reader = JsonReader('{}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()
def test_should_read_empty_array(self): reader = JsonReader('[]') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_array') expect(result.value).to_equal('[') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_array') expect(result.value).to_equal(']') result = reader.read() expect(result).to_be_false()
def test_should_format_object_with_array(self): reader = JsonReader('{"hello":"world" ,"arr":["hello","world"]}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('hello: world\narr:\n - hello\n - world')
def test_should_format_nested_object(self): reader = JsonReader('{"hello":"world" ,"obj":{"abc":123}}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('hello: world\nobj:\n abc: 123')
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}')
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}')
def test_should_format_single_property(self): reader = JsonReader('{"hello":"world"}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('hello: world')
def test_should_create_document_with_property_value_as_array_of_objects( self): reader = JsonReader( '{"arr":[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]}') doc = document_builder.build(reader) expect(doc).not_to_be_null() expect(doc).to_be_instance_of(Document) expect(len(doc.children)).to_equal(1) prop = doc.children[0] expect(prop).not_to_be_null() expect(prop.name.value).to_equal('arr') expect(prop.value.value_type).to_equal('array') arr = prop.value.value expect(arr).not_to_be_null() expect(arr).to_be_instance_of(Collection) obj = arr.children[0].value expect(obj).not_to_be_null() expect(obj).to_be_instance_of(Document) obj = arr.children[1].value expect(obj).not_to_be_null() expect(obj).to_be_instance_of(Document)
def test_should_create_document_with_comments_after_object_property(self): reader = JsonReader('{obj:{hello:"world"}/*comment*/}') doc = document_builder.build(reader) expect(doc).not_to_be_null() expect(doc).to_be_instance_of(Document) expect(len(doc.children)).to_equal(1) prop = doc.children[0] expect(prop).not_to_be_null() expect(prop.name.value).to_equal('obj') expect(prop.value.value_type).to_equal('object') obj = prop.value.value val = prop.value expect(obj).not_to_be_null() expect(len(obj.children)).to_equal(1) prop = obj.children[0] expect(prop).not_to_be_null() expect(prop.name.value).to_equal('hello') expect(prop.value.value).to_equal('world') expect(prop.value.value_type).to_equal('string') comm = val.comments[0] expect(comm).not_to_be_null() expect(comm).to_be_instance_of(Comment) expect(comm.value).to_equal('comment')
def test_should_create_document_new_line_comment_block(self): reader = JsonReader( '{\'"hello"\':"world",\n/* comment\nblock */ \'value\':123}') doc = document_builder.build(reader) expect(doc).not_to_be_null() expect(doc).to_be_instance_of(Document) expect(len(doc.children)).to_equal(3) prop = doc.children[0] expect(prop).not_to_be_null() expect(prop.name.value).to_equal('"hello"') expect(prop.value.value).to_equal('world') expect(prop.value.value_type).to_equal('string') comm = doc.children[1] expect(comm).not_to_be_null() expect(comm).to_be_instance_of(Comment) expect(comm.value).to_equal('comment\nblock') prop = doc.children[2] expect(prop).not_to_be_null() expect(prop.name.value).to_equal('value') expect(prop.value.value).to_equal('123') expect(prop.value.value_type).to_equal('number')
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}')
def test_should_create_new_collection_with_comment(self): reader = JsonReader('[1,2 /*comment*/,-3]') doc = document_builder.build(reader) expect(doc).not_to_be_null() expect(doc).to_be_instance_of(Collection) expect(len(doc.children)).to_equal(3) val = doc.children[0] expect(val).not_to_be_null() expect(val.value).to_equal('1') expect(val.value_type).to_equal('number') val = doc.children[1] expect(val).not_to_be_null() expect(val.value).to_equal('2') expect(val.value_type).to_equal('number') comm = val.comments[0] expect(comm).not_to_be_null() expect(comm.value).to_equal('comment') val = doc.children[2] expect(val).not_to_be_null() expect(val.value).to_equal('-3') expect(val.value_type).to_equal('number')
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}')
def test_should_format_scalar_array_with_value_comment(self): reader = JsonReader('[1,2 /*comment*/,3]') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('- 1\n- 2 # comment\n- 3')
def test_should_format_property_with_empty_string(self): reader = JsonReader('{\'hello\':\'\'}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('hello:')
def test_should_format_multiple_properties(self): reader = JsonReader('{"hello":"world" ,"value":123}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('hello: world\nvalue: 123')
def test_should_format_empty_array(self): reader = JsonReader('[ ]') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('')
def test_should_format_scalar_array(self): reader = JsonReader('[1,2,3]') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document) output = renderer.render() expect(output).to_equal('- 1\n- 2\n- 3')
def test_should_format_array_of_arrays(self): reader = JsonReader('[[1,2,3],[4,5,6]]') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('[[1, 2, 3], [4, 5, 6]]')
def test_should_format_empty_object(self): reader = JsonReader('{ }') document = document_builder.build(reader) renderer = JsonDocumentRenderer(document) output = renderer.render() expect(output).to_equal('{}')
def test_should_preserve_end_line_comment(self): reader = JsonReader('{\'"hello"\':"world", // comment\n\'value\':123}') document = document_builder.build(reader) renderer = YamlDocumentRenderer(document, {'force_property_quotes': True}) output = renderer.render() expect(output).to_equal('"\\"hello\\"": world # comment\nvalue: 123')
def test_should_read_basic_array(self): reader = JsonReader('[1, 2, 3]') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_array') expect(result.value).to_equal('[') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('1') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('2') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('3') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_array') expect(result.value).to_equal(']') result = reader.read() expect(result).to_be_false()
def test_should_read_complex_array(self): reader = JsonReader('[{"int":123,"str":"hello"},{"int":123,"str":"hello"}]') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_array') expect(result.value).to_equal('[') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"int"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('123') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"str"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"hello"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"int"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('123') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"str"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"hello"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_array') expect(result.value).to_equal(']') result = reader.read() expect(result).to_be_false()
def test_should_read_json_with_multiple_properties(self): reader = JsonReader('{"hello": "world", value: 123.4}') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('begin_object') expect(result.value).to_equal('{') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('"hello"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('"world"') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value_separator') expect(result.value).to_equal(',') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property') expect(result.value).to_equal('value') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('property_separator') expect(result.value).to_equal(':') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('123.4') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('end_object') expect(result.value).to_equal('}') result = reader.read() expect(result).to_be_false()