File: test_evaluate_js.py

package info (click to toggle)
python-pywebview 3.3.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 29,536 kB
  • sloc: python: 5,703; javascript: 888; cs: 130; sh: 55; makefile: 3
file content (144 lines) | stat: -rwxr-xr-x 2,565 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
import pytest
import threading
from .util import run_test, get_test_name
import webview


@pytest.fixture
def window():
    return webview.create_window('Evaluate JS test')


def test_mixed(window):
    run_test(webview, window, mixed_test)


def test_array(window):
    run_test(webview, window, array_test)


def test_object(window):
    run_test(webview, window, object_test)


def test_string(window):
    run_test(webview, window, string_test)


def test_int(window):
    run_test(webview, window, int_test)


def test_float(window):
    run_test(webview, window, float_test)


def test_undefined(window):
    run_test(webview, window, undefined_test)


def test_null(window):
    run_test(webview, window, null_test)


def test_nan(window):
    run_test(webview, window, nan_test)


def mixed_test(window):
    result = window.evaluate_js("""
        document.body.style.backgroundColor = '#212121';
        // comment
        function test() {
            return 2 + 2;
        }
        test();
    """)
    assert result == 4


def array_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return [undefined, 1, 'two', 3.00001, {four: true}]
    }
    getValue()
    """)
    assert result == [None, 1, 'two', 3.00001, {'four': True}]


def object_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return {1: 2, 'test': true, obj: {2: false, 3: 3.1}}
    }

    getValue()
    """)
    assert result == {'1': 2, 'test': True, 'obj': {'2': False, '3': 3.1}}


def string_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return "this is only a test"
    }

    getValue()
    """)
    assert result == u'this is only a test'


def int_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return 23
    }

    getValue()
    """)
    assert result == 23


def float_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return 23.23443
    }

    getValue()
    """)
    assert result == 23.23443


def undefined_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return undefined
    }

    getValue()
    """)
    assert result is None


def null_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return null
    }

    getValue()
    """)
    assert result is None


def nan_test(window):
    result = window.evaluate_js("""
    function getValue() {
        return NaN
    }

    getValue()
    """)
    assert result is None