File: test_funchelper.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (73 lines) | stat: -rw-r--r-- 1,928 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
#------------------------------------------------------------------------------
# Copyright (c) 2018-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from textwrap import dedent

import pytest

from enaml.core.funchelper import call_func
from utils import compile_source

source = dedent("""\
from enaml.widgets.window import Window

enamldef MyWindow(Window): main:

    func call(*args, **kwargs):
        return kwargs.get('a') or a

""")
tester = compile_source(source, 'MyWindow')()

func = tester.call.__func__


def test_calling_function():
    """Test calling a function with locals.

    """
    assert call_func(func, (), {}, {'a': 1}) == 1
    assert call_func(func, (1,), {}, {'a': 1}) == 1


def test_calling_function_kwarg():
    """Test calling a function with kwargs.

    """
    assert call_func(func, (), {'b': 2}, {'a': 1}) == 1


def test_handling_none_as_locals():
    """Test passing None as locals.

    """
    assert call_func(func, (), {'a': 1}, None) == 1


def test_handling_wrong_arguments():
    """Test handling incorrect arguments.

    """
    with pytest.raises(TypeError) as excinfo:
        call_func(None, None, None, None)
    assert 'Python function' in excinfo.exconly()

    with pytest.raises(TypeError) as excinfo:
        call_func(func, None, None, None)
    assert 'tuple' in excinfo.exconly()

    with pytest.raises(TypeError) as excinfo:
        call_func(func, (), None, None)
    assert 'dict' in excinfo.exconly()

    with pytest.raises(TypeError) as excinfo:
        call_func(func, (), {}, 1)
    assert 'mapping' in excinfo.exconly()

    with pytest.raises(TypeError) as excinfo:
        call_func(func, ())
    assert 'must have 3 or 4 arguments' in excinfo.exconly()