test_hacked_modules.py 3.41 KB
Newer Older
Romain Casati's avatar
Romain Casati committed
1
2
3
from pathlib import Path
import re

Romain Casati's avatar
Romain Casati committed
4
5
_test_data = Path(__file__).parent / "data"

Romain Casati's avatar
Romain Casati committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19

def test_all(selenium):
    # ensure all hacked modules are tested
    tested = set(g[len('test_'):] for g in globals()
                 if g.startswith('test_') and g != 'test_all')
    result = selenium.run_basthon("""
    from basthon import _hack_modules
    set(g[len('hack_'):] for g in dir(_hack_modules)
        if g.startswith('hack_'))""")['result']
    hacked = eval(result['result']['text/plain'])
    assert tested == hacked


def test_PIL(selenium):
Romain Casati's avatar
Romain Casati committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    result = selenium.run_basthon("""
    from PIL import Image, ImageDraw
    w, h = 120, 90
    bbox = [(10, 10), (w - 10, h - 10)]

    img = Image.new("RGB", (w, h), "#f9f9f9")
    dctx = ImageDraw.Draw(img)
    dctx.rectangle(bbox, fill="#ddddff", outline="blue")
    img.show()
    """)
    assert result['stderr'] == ""
    assert result['display']['display_type'] == 'multiple'
    img = result['display']['content']['image/png']
    with open(_test_data / "pil.png") as f:
        target = f.read()
    assert target == img
Romain Casati's avatar
Romain Casati committed
36
37
38


def test_matplotlib(selenium):
Romain Casati's avatar
Romain Casati committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    selenium.run_basthon("""
    import matplotlib.pyplot as plt

    plt.figure()
    plt.plot([0, 1], [0, 1])
    plt.show()
    """, return_data=False)
    # can't access content like this
    # elem = result['content']
    # because of selenium's StaleElementReferenceException
    # bypassing it via JS
    html = selenium.run_js("return window._basthon_eval_data.display.content.outerHTML")
    html = re.sub('matplotlib_[0-9a-f]+', 'matplotlib_', html)
    with open(_test_data / 'matplotlib.html') as f:
        target = f.read()
    assert html == target
Romain Casati's avatar
Romain Casati committed
55
56
57


def test_folium(selenium):
Romain Casati's avatar
Romain Casati committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    result = selenium.run_basthon("""
    import folium

    m = folium.Map(location=[47.228382, 2.062796],
                   zoom_start=17)

    m.display()
    """)
    assert result['stderr'] == ""
    assert result['display']['display_type'] == 'html'
    map = re.sub('map_[0-9a-f]+', 'map_', result['display']['content'])
    map = re.sub('tile_layer_[0-9a-f]+', 'tile_layer_', map)
    with open(_test_data / "folium.html") as f:
        target = f.read()
    assert target == map
Romain Casati's avatar
Romain Casati committed
73
74
75


def test_pandas(selenium):
Romain Casati's avatar
Romain Casati committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    result = selenium.run_basthon("""
    import pandas as pd

    df = pd.DataFrame({
        'language': ["Python", "C", "Java"],
        'verbosity': [0.1, 0.5, 0.9]
    })

    df.display()
    """)
    assert result['stderr'] == ""
    assert result['display']['display_type'] == 'html'
    with open(_test_data / "pandas.html") as f:
        assert result['display']['content'] == f.read()
Romain Casati's avatar
Romain Casati committed
90
91
92


def test_sympy(selenium):
Romain Casati's avatar
Romain Casati committed
93
94
95
96
97
98
99
100
    result = selenium.run_basthon("""
    import sympy
    from sympy.abc import x
    sympy.pretty_print(sympy.sqrt(x ** 2 + sympy.pi))
    """)
    assert result['stderr'] == ""
    assert result['display']['display_type'] == 'sympy'
    assert result['display']['content'] == "$$\\sqrt{x^{2} + \\pi}$$"
Romain Casati's avatar
Romain Casati committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114


def test_turtle(selenium):
    selenium.run_basthon("""
    import turtle
    turtle.forward(100)
    turtle.done()
    """, return_data=False)
    # can't access content like this
    # elem = result['content']
    # because of selenium's StaleElementReferenceException
    # bypassing it via JS
    svg = selenium.run_js("return window._basthon_eval_data.display.content.outerHTML")
    svg = re.sub('\"af_[0-9a-f]+_', '\"af_', svg)
Romain Casati's avatar
Romain Casati committed
115
    with open(_test_data / 'turtle.svg') as f:
Romain Casati's avatar
Romain Casati committed
116
117
        target = f.read()
    assert svg == target