File: test_configparser.py

package info (click to toggle)
python-pyqtgraph 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,168 kB
  • sloc: python: 54,831; makefile: 128; ansic: 40; sh: 2
file content (88 lines) | stat: -rw-r--r-- 2,148 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy as np

from pyqtgraph import configfile


def test_longArrays(tmpdir):
    """
    Test config saving and loading of long arrays.
    """
    arr = np.arange(20)

    tf = tmpdir.join("config.cfg")
    configfile.writeConfigFile({'arr': arr}, tf)
    config = configfile.readConfigFile(tf)
    assert all(config['arr'] == arr)


def test_multipleParameters(tmpdir):
    """
    Test config saving and loading of multiple parameters.
    """

    par1 = [1,2,3]
    par2 = "Test"
    par3 = {'a':3,'b':'c'}

    tf = tmpdir.join("config.cfg")
    configfile.writeConfigFile({'par1':par1, 'par2':par2, 'par3':par3}, tf)
    config = configfile.readConfigFile(tf)

    assert config['par1'] == par1
    assert config['par2'] == par2
    assert config['par3'] == par3


def test_duplicate_keys_error(tmpdir):
    """
    Test that an error is raised when duplicate keys are present in the config file.
    """

    tf = tmpdir.join("config.cfg")
    with open(tf, 'w') as f:
        f.write('a: 1\n')
        f.write('a: 2\n')

    try:
        configfile.readConfigFile(tf)
    except configfile.ParseError as e:
        assert 'Duplicate key' in str(e)
    else:
        assert False, "Expected ParseError"


def test_line_numbers_acconut_for_comments_and_blanks(tmpdir):
    """
    Test that line numbers in ParseError account for comments and blank lines.
    """

    tf = tmpdir.join("config.cfg")
    with open(tf, 'w') as f:
        f.write('a: 1\n')
        f.write('\n')
        f.write('# comment\n')
        f.write('a: 2\n')

    try:
        configfile.readConfigFile(tf)
    except configfile.ParseError as e:
        assert 'at line 4' in str(e)
    else:
        assert False, "Expected ParseError"


def test_comment_indentation_is_ignored(tmpdir):
    """
    Test that comment indentation is ignored.
    """

    tf = tmpdir.join("config.cfg")
    with open(tf, 'w') as f:
        f.write('a:\n')
        f.write('        # comment\n')
        f.write('    b:\n')
        f.write('# more comments\n')
        f.write('        c: 2\n')

    retval = configfile.readConfigFile(tf)
    assert retval['a']['b']['c'] == 2