Example #1
0
def test_top_level_map():
    ret = parse('true: false\n' 'false: true')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(),
            items=(
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=True, src='true'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.NL('\n'), ),
                ),
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=False, src='false'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=True, src='true'),
                    tail=(),
                ),
            ),
            tail=(),
        ),
        tail=(),
    )
    assert ret == expected
    assert ret.val.is_top_level_style
Example #2
0
def test_map_multiple_elements_inline():
    ret = parse('{true: false, false: true}')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(ast.MapStart('{'), ),
            items=(
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=True, src='true'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=False, src='false'),
                    tail=(ast.Comma(','), ast.Space(' ')),
                ),
                ast.MapItem(
                    head=(),
                    key=ast.Bool(val=False, src='false'),
                    inner=(ast.Colon(':'), ast.Space(' ')),
                    val=ast.Bool(val=True, src='true'),
                    tail=(),
                ),
            ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
Example #3
0
def _map_item_tokens(kv, settings):
    k, v = kv
    ret = []
    ret.extend(_to_tokens(k, settings, key=True))
    ret.extend((ast.Colon(':'), ast.Space(' ')))
    ret.extend(_to_tokens(v, settings))
    return ret
Example #4
0
def test_bare_word_key_starts_with_other_token():
    tokens = tokenize('{true_values: []}')
    assert tokens == (
        ast.MapStart('{'),
        ast.BareWordKey('true_values', 'true_values'),
        ast.Colon(':'),
        ast.Space(' '),
        ast.ListStart('['),
        ast.ListEnd(']'),
        ast.MapEnd('}'),
        ast.EOF(''),
    )
Example #5
0
def test_map_bare_word_key():
    ret = parse("{_Key-str1ng: 'value'}")
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(ast.MapStart('{'), ),
            items=(ast.MapItem(
                head=(),
                key=ast.BareWordKey('_Key-str1ng', '_Key-str1ng'),
                inner=(ast.Colon(':'), ast.Space(' ')),
                val=ast.String(val='value', src="'value'"),
                tail=(),
            ), ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected
Example #6
0
def test_comment_at_start_of_multiline_json():
    ret = parse('{  # bar\n' '    true: false,\n' '}')
    expected = ast.Doc(
        head=(),
        val=ast.Map(
            head=(
                ast.MapStart('{'),
                ast.Space(' '),
                ast.Space(' '),
                ast.Comment('# bar\n'),
            ),
            items=(ast.MapItem(
                head=(ast.Indent('    '), ),
                key=ast.Bool(val=True, src='true'),
                inner=(ast.Colon(':'), ast.Space(' ')),
                val=ast.Bool(val=False, src='false'),
                tail=(ast.Comma(','), ast.NL('\n')),
            ), ),
            tail=(ast.MapEnd('}'), ),
        ),
        tail=(),
    )
    assert ret == expected