<commit_before>#
# Copyright (C) 2013 <NAME> <<EMAIL>>
# License: MIT
#
import anyconfig.backend.configobj_ as T
import os
import sys
import tempfile
import unittest


CONF_0 = """\
# This is the 'initial_comment'
# Which may be several lines
keyword1 = value1
'keyword 2' = 'value 2'

[ "section 1" ]
# This comment goes with keyword 3
keyword 3 = value 3
'keyword 4' = value4, value 5, 'value 6'

    [[ sub-section ]]    # an inline comment
    # sub-section is inside "section 1"
    'keyword 5' = 'value 7'
    'keyword 6' = '''A multiline value,
that spans more than one line :-)
The line breaks are included in the value.'''

        [[[ sub-sub-section ]]]
        # sub-sub-section is *in* 'sub-section'
        # which is in 'section 1'
        'keyword 7' = 'value 8'

[section 2]    # an inline comment
keyword8 = "value 9"
keyword9 = value10     # an inline comment
# The 'final_comment'
# Which also may be several lines
"""


class Test_ConfigObjParser(unittest.TestCase):

    def setUp(self):
        (_, conf) = tempfile.mkstemp(prefix="ac-bc-test-")
        open(conf, 'w').write(CONF_0)
        self.config_path = conf

    def tearDown(self):
        os.remove(self.config_path)

    def test_00_supports(self):
        self.assertTrue(T.ConfigObjParser.supports("/a/b/c/d.ini"))
        self.assertFalse(T.ConfigObjParser.supports("/a/b/c/d.json"))

    def test_10_loads(self):
        c = T.ConfigObjParser.loads(CONF_0)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_20_load(self):
        c = T.ConfigObjParser.load(self.config_path)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_30_dumps(self):
        c = T.ConfigObjParser.loads(CONF_0)
        s = T.ConfigObjParser.dumps(c)
        c = T.ConfigObjParser.loads(s)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_40_dump(self):
        c = T.ConfigObjParser.loads(CONF_0)
        T.ConfigObjParser.dump(c, self.config_path)
        c = T.ConfigObjParser.load(self.config_path)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

# vim:sw=4:ts=4:et:
<commit_msg>follow module name changes, etc.<commit_after>#
# Copyright (C) 2013, 2014 <NAME> <<EMAIL>>
# License: MIT
#
import anyconfig_configobj_backend.configobj_ as TT
import os
import tempfile
import unittest


CONF_0 = """\
# This is the 'initial_comment'
# Which may be several lines
keyword1 = value1
'keyword 2' = 'value 2'

[ "section 1" ]
# This comment goes with keyword 3
keyword 3 = value 3
'keyword 4' = value4, value 5, 'value 6'

    [[ sub-section ]]    # an inline comment
    # sub-section is inside "section 1"
    'keyword 5' = 'value 7'
    'keyword 6' = '''A multiline value,
that spans more than one line :-)
The line breaks are included in the value.'''

        [[[ sub-sub-section ]]]
        # sub-sub-section is *in* 'sub-section'
        # which is in 'section 1'
        'keyword 7' = 'value 8'

[section 2]    # an inline comment
keyword8 = "value 9"
keyword9 = value10     # an inline comment
# The 'final_comment'
# Which also may be several lines
"""


class Test_ConfigObjParser(unittest.TestCase):

    def setUp(self):
        (_, conf) = tempfile.mkstemp(prefix="ac-bc-test-")
        open(conf, 'w').write(CONF_0)
        self.config_path = conf

    def tearDown(self):
        os.remove(self.config_path)

    def test_00_supports(self):
        self.assertTrue(TT.ConfigObjParser.supports("/a/b/c/d.ini"))
        self.assertFalse(TT.ConfigObjParser.supports("/a/b/c/d.json"))

    def test_10_loads(self):
        c = TT.ConfigObjParser.loads(CONF_0)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_20_load(self):
        c = TT.ConfigObjParser.load(self.config_path)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_30_dumps(self):
        c = TT.ConfigObjParser.loads(CONF_0)
        s = TT.ConfigObjParser.dumps(c)
        c = TT.ConfigObjParser.loads(s)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

    def test_40_dump(self):
        c = TT.ConfigObjParser.loads(CONF_0)
        TT.ConfigObjParser.dump(c, self.config_path)
        c = TT.ConfigObjParser.load(self.config_path)

        self.assertEquals(c['keyword1'], 'value1')
        self.assertEquals(c['keyword 2'], 'value 2')
        self.assertEquals(c['section 1']['keyword 3'], 'value 3')
        self.assertEquals(c['section 1']['keyword 4'],
                          ['value4', 'value 5', 'value 6'])
        self.assertEquals(c['section 1']['sub-section']['keyword 5'],
                          'value 7')
        self.assertEquals(c['section 1']['sub-section']['keyword 6'],
                          """A multiline value,
that spans more than one line :-)
The line breaks are included in the value.""")
        self.assertEquals(
            c['section 1']['sub-section']['sub-sub-section']['keyword 7'],
            'value 8'
        )
        self.assertEquals(c['section 2']['keyword8'], 'value 9')
        self.assertEquals(c['section 2']['keyword9'], 'value10')

# vim:sw=4:ts=4:et:

{'fraction_non_alphanumeric': 0.16299352902560255, 'fraction_numerical': 0.02926005814498734, 'mean_word_length': 2.5264550264550265, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4931380', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3061, 'n_words': 787}