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_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()
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_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_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_in_line_comment_block(self): reader = JsonReader('before /* this is a\nblock comment */ after */') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value') expect(result.value).to_equal('before') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('in_line_comment_block') expect(result.value).to_equal('/* this is a\nblock comment */') result = reader.read() expect(result).to_be_true() expect(result.type).to_equal('value')
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_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_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_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_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_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()