2020-02-04 12:18:31 -08:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import jc.parsers.ini
|
|
|
|
|
|
|
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
class MyTests(unittest.TestCase):
|
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
# input
|
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-test.ini'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_test = f.read()
|
2020-02-04 12:18:31 -08:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-iptelserver.ini'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_iptelserver = f.read()
|
2020-02-04 12:18:31 -08:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-double-quote.ini'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_double_quote = f.read()
|
2022-06-15 12:50:58 -07:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-single-quote.ini'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_single_quote = f.read()
|
2022-06-15 12:50:58 -07:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
# output
|
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-test.json'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_test_json = json.loads(f.read())
|
2020-02-04 12:18:31 -08:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-iptelserver.json'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_iptelserver_json = json.loads(f.read())
|
2020-02-04 12:18:31 -08:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-double-quote.json'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_double_quote_json = json.loads(f.read())
|
2022-06-15 12:50:58 -07:00
|
|
|
|
2022-09-22 08:39:01 -07:00
|
|
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/ini-single-quote.json'), 'r', encoding='utf-8') as f:
|
|
|
|
generic_ini_single_quote_json = json.loads(f.read())
|
2022-06-15 12:50:58 -07:00
|
|
|
|
2022-09-23 10:58:28 -07:00
|
|
|
|
2020-06-10 17:54:06 -07:00
|
|
|
def test_ini_nodata(self):
|
|
|
|
"""
|
|
|
|
Test the test ini file with no data
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.ini.parse('', quiet=True), {})
|
|
|
|
|
2020-02-04 12:18:31 -08:00
|
|
|
def test_ini_test(self):
|
|
|
|
"""
|
|
|
|
Test the test ini file
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.ini.parse(self.generic_ini_test, quiet=True), self.generic_ini_test_json)
|
|
|
|
|
|
|
|
def test_ini_iptelserver(self):
|
|
|
|
"""
|
|
|
|
Test the iptelserver ini file
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.ini.parse(self.generic_ini_iptelserver, quiet=True), self.generic_ini_iptelserver_json)
|
|
|
|
|
2022-04-04 11:38:52 -07:00
|
|
|
def test_ini_duplicate_keys(self):
|
|
|
|
"""
|
|
|
|
Test input that contains duplicate keys. Only the last value should be used.
|
|
|
|
"""
|
|
|
|
data = '''
|
2023-01-03 12:11:05 -08:00
|
|
|
[section]
|
2022-04-04 11:38:52 -07:00
|
|
|
duplicate_key: value1
|
|
|
|
another_key = foo
|
|
|
|
duplicate_key = value2
|
|
|
|
'''
|
2023-01-03 12:11:05 -08:00
|
|
|
expected = {'section': {'duplicate_key': 'value2', 'another_key': 'foo'}}
|
2022-04-04 11:38:52 -07:00
|
|
|
self.assertEqual(jc.parsers.ini.parse(data, quiet=True), expected)
|
|
|
|
|
2022-06-15 12:50:58 -07:00
|
|
|
def test_ini_doublequote(self):
|
|
|
|
"""
|
|
|
|
Test ini file with double quotes around a value
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.ini.parse(self.generic_ini_double_quote, quiet=True), self.generic_ini_double_quote_json)
|
|
|
|
|
|
|
|
def test_ini_singlequote(self):
|
|
|
|
"""
|
|
|
|
Test ini file with single quotes around a value
|
|
|
|
"""
|
|
|
|
self.assertEqual(jc.parsers.ini.parse(self.generic_ini_single_quote, quiet=True), self.generic_ini_single_quote_json)
|
|
|
|
|
|
|
|
|
2020-02-04 12:18:31 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|