File: test_tools.py

package info (click to toggle)
python-easydev 0.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 504 kB
  • sloc: python: 2,130; javascript: 49; makefile: 13
file content (167 lines) | stat: -rw-r--r-- 3,605 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from easydev import tools as tools
from easydev import TempFile


def test_check_range():
    tools.check_range(1, 0,1)
    tools.check_range(0, 0,1)
    tools.check_range(0.5, 0,1)

    try:
        tools.check_range(1, 0,1, strict=True)
        assert False
    except:
        assert True
    try:
        tools.check_range(0, 0,1, strict=True)
        assert False
    except:
        assert True

    try:
        tools.check_range(10, 0,1, strict=False)
        assert False
    except:
        assert True
    try:
        tools.check_range(-10, 0,1, strict=False)
        assert False
    except:
        assert True


def test_swapdict():
    assert {1:'a'} == tools.swapdict({'a':1})

    # if the are non-unique values, we can catch the error or no:
    try:
        tools.swapdict({'a':1, 'b':1})
        assert False
    except:
        assert True
    tools.swapdict({'a':1, 'b':1}, check_ambiguity=False)

def test_tools():
    tools.shellcmd('ls')
    tools.shellcmd('ls', show=False)
    tools.shellcmd('ls', show=True)
    output = tools.shellcmd('ls', verbose=True)
    tools.shellcmd('lssssssss', verbose=True, ignore_errors=True)

    tools.execute('ls')

def test_tools2():
    try:
        tools.shellcmd('lsss', verbose=False)
        assert False
    except:
        assert True


def test_checkParams():
    tools.checkParam(1, [1, 2])
    try:
        tools.checkParam(0, [1, 2])
        assert False
    except:
        assert True

    try:
        tools.checkParam(0, 0)
        assert False
    except TypeError:
        assert True


def test_check_param_in_list():
    tools.check_param_in_list(1, [0,1,5], "test")
    try:
        tools.check_param_in_list(10, [0,1,5])
        assert False
    except:
        assert True
    try:
        tools.check_param_in_list(10, [0,1,5], 'testt')
        assert False
    except:
        assert True


def test_precision():
    assert tools.precision(2.123) == 2.12
    assert tools.precision(2.123, 1) == 2.1
    assert tools.precision(2.123,3) == 2.123
    assert tools.precision(2123,-2) == 2100


def test_attrdict():

    a = tools.AttrDict(value=1)
    assert a.value == 1
    assert 'value' in list(a.keys())
    assert 1 in (a.values())

    a.description = 'test'
    assert a['description'] == 'test'

    a['output'] = 'txt'
    assert a.output == 'txt'


    d = {'a':{'b':1}, 'aa':2}
    ad = tools.AttrDict(**d)
    assert ad.a.b == 1
    ad.a.b = 2
    assert ad.a.b == 2

    ad['d'] = 4
    assert ad.d == 4

    try:
        ad.update(1)
        assert False
    except:
        assert True

    # check json capabilities
    fh = TempFile()
    js = ad.to_json()
    ad.to_json(filename=fh.name)
    ad.from_json(fh.name)
    fh.delete()


def test_devtools():
    d = tools.DevTools()
    d.check_param_in_list(1, [1,2])
    d.check_range(1,0,2)
    assert d.list2string(['a', 'b']) == 'a,b'
    assert d.swapdict({'a':1}) == {1:'a'}
    d.to_json({'a':1})
    assert d.to_list('a') == ['a']
    import tempfile, os
    d.mkdirs(tempfile.mkdtemp() + os.sep + "test")
    try:
        d.check_exists("ttttttttttt")
        assert False
    except:
        assert True
    d.mkdir(tempfile.mkdtemp())


def test_mkdirs():
    import tempfile, os
    tools.mkdirs(tempfile.mkdtemp() + os.sep + "test")
    try:
        tools.mkdirs(tempfile.mkdtemp() + os.sep + "test")
        assert False
    except:
        assert True

    # without / , was not working but is now part of the API
    tools.mkdirs(tempfile.mkdtemp())

def test_touch():
    with TempFile() as fh:
        fh.name
        tools.touch(fh.name)