<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import collections
import io

import pytest

from dumbconf._roundtrip import dump
from dumbconf._roundtrip import dump_roundtrip
from dumbconf._roundtrip import dumps
from dumbconf._roundtrip import dumps_roundtrip
from dumbconf._roundtrip import load
from dumbconf._roundtrip import load_roundtrip
from dumbconf._roundtrip import loads
from dumbconf._roundtrip import loads_roundtrip


def test_replace_value_same_type():
    val = loads_roundtrip('True  # comment')
    val.replace_value(False)
    ret = dumps_roundtrip(val)
    assert ret == 'False  # comment'


def test_replace_value_new_type():
    val = loads_roundtrip('True  # comment')
    val.replace_value(None)
    ret = dumps_roundtrip(val)
    assert ret == 'None  # comment'


def test_replace_string():
    val = loads_roundtrip('True  # comment')
    val.replace_value('ohai')
    ret = dumps_roundtrip(val)
    assert ret == "'ohai'  # comment"


def test_replace_int():
    val = loads_roundtrip('True  # comment')
    val.replace_value(5)
    ret = dumps_roundtrip(val)
    assert ret == '5  # comment'


def test_replace_float():
    val = loads_roundtrip('True  # comment')
    val.replace_value(5.)
    ret = dumps_roundtrip(val)
    assert ret == '5.0  # comment'


def test_replace_key():
    val = loads_roundtrip("{k: 'v'}")
    val['k'].replace_key('new key!')
    ret = dumps_roundtrip(val)
    assert ret == "{'new key!': 'v'}"


def test_replace_key_at_root():
    val = loads_roundtrip('{True: False}')
    with pytest.raises(TypeError) as excinfo:
        val.replace_key('new key')
    assert excinfo.value.args == ('Index into a map to replace a key.',)


def test_replace_key_not_a_map():
    val = loads_roundtrip('[1, 2, 3]')
    with pytest.raises(TypeError) as excinfo:
        val[0].replace_key('new key')
    assert excinfo.value.args == ('Can only replace Map keys, not List',)


def test_replace_key_illegal_type():
    val = loads_roundtrip('{True: False}')
    with pytest.raises(TypeError) as excinfo:
        val[True].replace_key([1, 2, 3])
    assert excinfo.value.args == (
        'Keys must be of type (BareWordKey, Bool, Float, Int, Null, String) '
        'but got List',
    )


def test_replace_map_value_top_level():
    val = loads_roundtrip(
        '{\n'
        '    a: True,  # comment\n'
        '    b: False,  # comment\n'
        '}\n'
    )
    val['b'] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '{\n'
        '    a: True,  # comment\n'
        '    b: None,  # comment\n'
        '}\n'
    )


def test_replace_list_value_top_level():
    val = loads_roundtrip(
        '[\n'
        '    True,  # comment\n'
        '    False,  # comment\n'
        ']\n'
    )
    val[0] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '[\n'
        '    None,  # comment\n'
        '    False,  # comment\n'
        ']\n'
    )


def test_replace_nested_map_value():
    val = loads_roundtrip(
        '{\n'
        '    a: {\n'
        '        b: True,  # comment\n'
        '    },\n'
        '}\n'
    )
    val['a']['b'] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '{\n'
        '    a: {\n'
        '        b: None,  # comment\n'
        '    },\n'
        '}\n'
    )


def test_deplace_nested_map_value_deeper():
    val = loads_roundtrip('{a: {b: {c: True}}}')
    val['a']['b']['c'] = False
    ret = dumps_roundtrip(val)
    assert ret == '{a: {b: {c: False}}}'


def test_delete_dictionary_key():
    val = loads_roundtrip(
        '{\n'
        '    # comment documenting a\n'<commit_msg>Fix a typo in a test name<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import collections
import io

import pytest

from dumbconf._roundtrip import dump
from dumbconf._roundtrip import dump_roundtrip
from dumbconf._roundtrip import dumps
from dumbconf._roundtrip import dumps_roundtrip
from dumbconf._roundtrip import load
from dumbconf._roundtrip import load_roundtrip
from dumbconf._roundtrip import loads
from dumbconf._roundtrip import loads_roundtrip


def test_replace_value_same_type():
    val = loads_roundtrip('True  # comment')
    val.replace_value(False)
    ret = dumps_roundtrip(val)
    assert ret == 'False  # comment'


def test_replace_value_new_type():
    val = loads_roundtrip('True  # comment')
    val.replace_value(None)
    ret = dumps_roundtrip(val)
    assert ret == 'None  # comment'


def test_replace_string():
    val = loads_roundtrip('True  # comment')
    val.replace_value('ohai')
    ret = dumps_roundtrip(val)
    assert ret == "'ohai'  # comment"


def test_replace_int():
    val = loads_roundtrip('True  # comment')
    val.replace_value(5)
    ret = dumps_roundtrip(val)
    assert ret == '5  # comment'


def test_replace_float():
    val = loads_roundtrip('True  # comment')
    val.replace_value(5.)
    ret = dumps_roundtrip(val)
    assert ret == '5.0  # comment'


def test_replace_key():
    val = loads_roundtrip("{k: 'v'}")
    val['k'].replace_key('new key!')
    ret = dumps_roundtrip(val)
    assert ret == "{'new key!': 'v'}"


def test_replace_key_at_root():
    val = loads_roundtrip('{True: False}')
    with pytest.raises(TypeError) as excinfo:
        val.replace_key('new key')
    assert excinfo.value.args == ('Index into a map to replace a key.',)


def test_replace_key_not_a_map():
    val = loads_roundtrip('[1, 2, 3]')
    with pytest.raises(TypeError) as excinfo:
        val[0].replace_key('new key')
    assert excinfo.value.args == ('Can only replace Map keys, not List',)


def test_replace_key_illegal_type():
    val = loads_roundtrip('{True: False}')
    with pytest.raises(TypeError) as excinfo:
        val[True].replace_key([1, 2, 3])
    assert excinfo.value.args == (
        'Keys must be of type (BareWordKey, Bool, Float, Int, Null, String) '
        'but got List',
    )


def test_replace_map_value_top_level():
    val = loads_roundtrip(
        '{\n'
        '    a: True,  # comment\n'
        '    b: False,  # comment\n'
        '}\n'
    )
    val['b'] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '{\n'
        '    a: True,  # comment\n'
        '    b: None,  # comment\n'
        '}\n'
    )


def test_replace_list_value_top_level():
    val = loads_roundtrip(
        '[\n'
        '    True,  # comment\n'
        '    False,  # comment\n'
        ']\n'
    )
    val[0] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '[\n'
        '    None,  # comment\n'
        '    False,  # comment\n'
        ']\n'
    )


def test_replace_nested_map_value():
    val = loads_roundtrip(
        '{\n'
        '    a: {\n'
        '        b: True,  # comment\n'
        '    },\n'
        '}\n'
    )
    val['a']['b'] = None
    ret = dumps_roundtrip(val)
    assert ret == (
        '{\n'
        '    a: {\n'
        '        b: None,  # comment\n'
        '    },\n'
        '}\n'
    )


def test_replace_nested_map_value_deeper():
    val = loads_roundtrip('{a: {b: {c: True}}}')
    val['a']['b']['c'] = False
    ret = dumps_roundtrip(val)
    assert ret == '{a: {b: {c: False}}}'


def test_delete_dictionary_key():
    val = loads_roundtrip(
        '{\n'
        '    # comment documenting a\n'
{'fraction_non_alphanumeric': 0.1387220098306936, 'fraction_numerical': 0.0038230475150191155, 'mean_word_length': 2.5233285233285234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3710308', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2544, 'n_words': 581}