File: test_feature.py

package info (click to toggle)
python-evalidate 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144 kB
  • sloc: python: 500; makefile: 3
file content (63 lines) | stat: -rw-r--r-- 1,566 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
import evalidate
import pytest

class TestSafeEval():
    def test_sum(self):
        r = evalidate.Expr('1+2').eval()
        assert(r==3)

    def test_context(self):
        ctx = {'a':100, 'b': 200 }
        r = evalidate.Expr('a+b').eval(ctx)
        assert(r==300)

    def test_call(self):
        ctx = {'a':36.6}

        m = evalidate.base_eval_model.clone()
        m.nodes.append('Call')
        m.allowed_functions.append('int')

        r = evalidate.Expr('int(a)', model=m).eval(ctx)
        assert(r==36)

    def test_normal(self):
        codelist = [
            ('1+1',2),
            ('a+b',3),
            ('int(pi)', 3)
        ]

        ctx={'a':1, 'b':2, 'pi': 3.14}

        m = evalidate.base_eval_model.clone()
        m.nodes.append('Call')
        m.allowed_functions.append('int')

        for src, r in codelist:
            result = evalidate.Expr(src, model=m).eval(ctx)
            assert(result==r)
    
    def test_stack(self):
        src='int(1)'
        m = evalidate.base_eval_model.clone()
        m.nodes.append('Call')
        m.allowed_functions.append('int')

        for i in range(199):
            src = f'int( {src} )'

        r = evalidate.Expr(src, model=m).eval()
        assert( r==1 )
    
    def test_startswith(self):

        m = evalidate.base_eval_model.clone()
        m.nodes.extend(['Call', 'Attribute'])
        m.allowed_functions.append('int')
        m.attributes.append('startswith')


        src = '"abcdef".startswith("abc")'
        r = evalidate.Expr(src, model=m).eval()
        assert r