<commit_before>import os, types

from configpy import Config, FileConfig

def test_no_variables():
    config = Config("{}")

def test_simple_variable():
    config_str = """
    {
        "var_a": "AAAA",
        "var_b": 10,
        "var_c": "A longer string",
        "var_d": 10.04
    }
    """

    config = Config(config_str)
    assert "AAAA" == config.var_a
    assert type(config.var_a) == types.UnicodeType
    assert 10 == config.var_b
    assert type(config.var_b) == types.IntType
    assert 10 == config.var_b
    assert "A longer string" == config.var_c
    assert 10.04 == config.var_d
    assert type(config.var_d) == types.FloatType

def test_look_behind_replace():
    config_str = """
    {
        "first_name": "Howard",
        "second_name": "Gayle",
        "full_name": "${first_name} ${second_name}"
    }
    """

    config = Config(config_str)
    assert "<NAME>" == config.full_name

def test_look_ahead_replace():
    config_str = """
    {
        "first_name": "Hugo",
        "full_name": "${first_name} ${second_name}",
        "second_name": "Sanchez"
    }
    """

    config = Config(config_str)
    assert "<NAME>" == config.full_name

def test_list_variable():
    config_str = """
    {
        "nums": [1,2,3,4],
        "nando": "Nando",
        "names": ["Stevie", "${nando}", "Robbie"]
    }
    """

    config = Config(config_str)
    assert type(config.nums) == types.ListType
    assert [1,2,3,4] == config.nums
    assert type(config.names) == types.ListType
    assert ['Stevie', 'Nando', 'Robbie']  == config.names

def test_dict_variable():
    config_str = """
    {
        "d": { "planet": "${e}" },
        "e": "mars"
    }
    """

    config = Config(config_str)
    assert type(config.d) == types.DictType
    assert { "planet": "mars" } == config.d

def test_dict_in_list_variable():
    config_str = """
    {
        "f": { "title": "The 39 Steps" },
        "g": { "title": "Catch-22" },
        "h": "[ ${f}, ${g} ]"
    }
    """

    config = Config(config_str)
    assert type(config.h) == types.ListType
    assert config.h == [ { u'title': u'The 39 Steps' }, { u'title': u'Catch-22'} ]
    assert type(config.h[0]) == types.DictType

def test_eval_safety():
    config_str = """
    {
        "filename": "exp(len('/tmp/ssc.txt'))"
    }
    """
    try:
        config = Config(config_str)
    except NameError:
        """Expected error"""
    config = Config(config_str, unrestricted=True)


def test_eval_maths():
    config_str = """ 
    {
        "a": 10,
        "b": 20,
        "c": "${a} + ${b}"
    }
    """
    config = Config(config_str)
    assert 10 == config.a
    assert type(config.a) == types.IntType
    assert 20 == config.b
    assert 30 == config.c

def test_python_keywords():
    config_str = """
    {
        "class": "MyClassName"
    }
    """

    config = Config(config_str)
    assert "MyClassName" == config['class']

def test_invalid_type_expression():
    config_str = """
    {
        "number": 10,
        "string": "My String",
        "invalid": "exp(${number} / ${string})"
    }
    """

    config = Config(config_str)

def test_missing_reference():
    config_str = """
    {
        "ping": "${pong}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_self_reference():
    config_str = """
    {
        "fubar": "${fubar}"
    }
    """

    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_cyclic_reference():
    config_str = """
    {
        "a": "${b}",
        "b": "${a}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_complex_cyclic_reference():
    config_str = """
    {
        "a": "${c}",
        "b": "${a}",
        "c": "${b}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_complex_references():
    config_str = """
    {
        "a": "simple",
        "b": "${a} ${c}",
        "c": "${d}",
        "d": "${a} ${e}",
        "e": "as pie"
    }
    """

    config = Config(config_str)

    assert "simple" == config.a
    assert "simple simple as pie" == config.b
    assert "simple as pie" == config.c
    assert "simple as pie" == config.d
    assert "as pie" == config.e

def test_math():
    config_str = """
    {
        "a": 10,
        "b": 2,
        "c": "${a} / ${b}",
        "d": "${c} * ${a}",
        "e": "${d} + 50",
        "f": "${e} - 25"
    }
    """
    config = Config(config_str)
    assert 10 == config.a
    assert 2 == config.b
    assert 5 == config.c
    assert 50 == config.d
    assert 100 == config.e
    assert 75 == config.f

def test_file():
    import os
    dirname = os.path.dirname(__file__)
    config = FileConfig(os.path.join(dirname,'test.cfg'))

    assert "admin" == config.db_username
    assert "password" == config.db_password
    assert "http://localhost" == config.host

def test_simple_variable():
    config_str = """
    /* We can use single line */
    /* and multi-line
       Javascript like comments */
    {
        "var_a": "AAAA", // or simple single line comments too
        /* we can mix comments everywhere */
        "var_b": "You must escape \/* slashes in name or value strings *\/",
        "var_c": "A longer string \/\/ like this one"
    }
    """

    config = Config(config_str)
    assert "AAAA" == config.var_a
    assert "You must escape /* slashes in name or value strings */" == config.var_b
    assert "A longer string // like this one" == config.var_c

def test_restricted():
    config_str = """
    {
        "myfile": "open('README.txt').read()"
    }
    """
    config = Config(config_str)
    assert config.myfile == "open('README.txt').read()"

def test_unrestricted():
    config_str = """
    {
        "myfile": "open('README.txt').read()"
    }
    """
    config = Config(config_str, unrestricted=True)
    assert config.myfile != "open('README.txt').read()"
    assert "Simple Configuration" in config.myfile
    assert os.path.exists('myfile.txt')

if __name__ == "__main__":
    import sys
    import traceback

    tests = [a for a in dir() if a[0:4] == 'test']
    attrs = locals()
    for test in tests:
        print test.ljust(30, '.'),
        try:
            attrs[test]()
            print "OK"
        except AssertionError, e:
            print "ERROR"<commit_msg>Remove old piece of test code that checked for file.<commit_after>import types

from configpy import Config, FileConfig

def test_no_variables():
    config = Config("{}")

def test_simple_variable():
    config_str = """
    {
        "var_a": "AAAA",
        "var_b": 10,
        "var_c": "A longer string",
        "var_d": 10.04
    }
    """

    config = Config(config_str)
    assert "AAAA" == config.var_a
    assert type(config.var_a) == types.UnicodeType
    assert 10 == config.var_b
    assert type(config.var_b) == types.IntType
    assert 10 == config.var_b
    assert "A longer string" == config.var_c
    assert 10.04 == config.var_d
    assert type(config.var_d) == types.FloatType

def test_look_behind_replace():
    config_str = """
    {
        "first_name": "Howard",
        "second_name": "Gayle",
        "full_name": "${first_name} ${second_name}"
    }
    """

    config = Config(config_str)
    assert "<NAME>" == config.full_name

def test_look_ahead_replace():
    config_str = """
    {
        "first_name": "Hugo",
        "full_name": "${first_name} ${second_name}",
        "second_name": "Sanchez"
    }
    """

    config = Config(config_str)
    assert "<NAME>" == config.full_name

def test_list_variable():
    config_str = """
    {
        "nums": [1,2,3,4],
        "nando": "Nando",
        "names": ["Stevie", "${nando}", "Robbie"]
    }
    """

    config = Config(config_str)
    assert type(config.nums) == types.ListType
    assert [1,2,3,4] == config.nums
    assert type(config.names) == types.ListType
    assert ['Stevie', 'Nando', 'Robbie']  == config.names

def test_dict_variable():
    config_str = """
    {
        "d": { "planet": "${e}" },
        "e": "mars"
    }
    """

    config = Config(config_str)
    assert type(config.d) == types.DictType
    assert { "planet": "mars" } == config.d

def test_dict_in_list_variable():
    config_str = """
    {
        "f": { "title": "The 39 Steps" },
        "g": { "title": "Catch-22" },
        "h": "[ ${f}, ${g} ]"
    }
    """

    config = Config(config_str)
    assert type(config.h) == types.ListType
    assert config.h == [ { u'title': u'The 39 Steps' }, { u'title': u'Catch-22'} ]
    assert type(config.h[0]) == types.DictType

def test_eval_safety():
    config_str = """
    {
        "filename": "exp(len('/tmp/ssc.txt'))"
    }
    """
    try:
        config = Config(config_str)
    except NameError:
        """Expected error"""
    config = Config(config_str, unrestricted=True)


def test_eval_maths():
    config_str = """ 
    {
        "a": 10,
        "b": 20,
        "c": "${a} + ${b}"
    }
    """
    config = Config(config_str)
    assert 10 == config.a
    assert type(config.a) == types.IntType
    assert 20 == config.b
    assert 30 == config.c

def test_python_keywords():
    config_str = """
    {
        "class": "MyClassName"
    }
    """

    config = Config(config_str)
    assert "MyClassName" == config['class']

def test_invalid_type_expression():
    config_str = """
    {
        "number": 10,
        "string": "My String",
        "invalid": "exp(${number} / ${string})"
    }
    """

    config = Config(config_str)

def test_missing_reference():
    config_str = """
    {
        "ping": "${pong}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_self_reference():
    config_str = """
    {
        "fubar": "${fubar}"
    }
    """

    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_cyclic_reference():
    config_str = """
    {
        "a": "${b}",
        "b": "${a}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_complex_cyclic_reference():
    config_str = """
    {
        "a": "${c}",
        "b": "${a}",
        "c": "${b}"
    }
    """
    try:
        config = Config(config_str)
    except KeyError:
        """Expected error"""

def test_complex_references():
    config_str = """
    {
        "a": "simple",
        "b": "${a} ${c}",
        "c": "${d}",
        "d": "${a} ${e}",
        "e": "as pie"
    }
    """

    config = Config(config_str)

    assert "simple" == config.a
    assert "simple simple as pie" == config.b
    assert "simple as pie" == config.c
    assert "simple as pie" == config.d
    assert "as pie" == config.e

def test_math():
    config_str = """
    {
        "a": 10,
        "b": 2,
        "c": "${a} / ${b}",
        "d": "${c} * ${a}",
        "e": "${d} + 50",
        "f": "${e} - 25"
    }
    """
    config = Config(config_str)
    assert 10 == config.a
    assert 2 == config.b
    assert 5 == config.c
    assert 50 == config.d
    assert 100 == config.e
    assert 75 == config.f

def test_file():
    import os
    dirname = os.path.dirname(__file__)
    config = FileConfig(os.path.join(dirname,'test.cfg'))

    assert "admin" == config.db_username
    assert "password" == config.db_password
    assert "http://localhost" == config.host

def test_simple_variable():
    config_str = """
    /* We can use single line */
    /* and multi-line
       Javascript like comments */
    {
        "var_a": "AAAA", // or simple single line comments too
        /* we can mix comments everywhere */
        "var_b": "You must escape \/* slashes in name or value strings *\/",
        "var_c": "A longer string \/\/ like this one"
    }
    """

    config = Config(config_str)
    assert "AAAA" == config.var_a
    assert "You must escape /* slashes in name or value strings */" == config.var_b
    assert "A longer string // like this one" == config.var_c

def test_restricted():
    config_str = """
    {
        "myfile": "open('README.txt').read()"
    }
    """
    config = Config(config_str)
    assert config.myfile == "open('README.txt').read()"

def test_unrestricted():
    config_str = """
    {
        "myfile": "open('README.txt').read()"
    }
    """
    config = Config(config_str, unrestricted=True)
    assert config.myfile != "open('README.txt').read()"
    assert "Simple Configuration" in config.myfile

if __name__ == "__main__":
    import sys
    import traceback

    tests = [a for a in dir() if a[0:4] == 'test']
    attrs = locals()
    for test in tests:
        print test.ljust(30, '.'),
        try:
            attrs[test]()
            print "OK"
        except AssertionError, e:
            print "ERROR"
{'fraction_non_alphanumeric': 0.1729091049501892, 'fraction_numerical': 0.010039385280716658, 'mean_word_length': 2.2126023319275614, 'pattern_counts': {'":': 108, '<': 7, '<?xml version=': 0, '>': 7, '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': '2827090', 'n_tokens_mistral': 4488, 'n_tokens_neox': 4307, 'n_words': 1001}