Skip to content

Commit 19991df

Browse files
author
Christopher Doris
committed
tests for gui, ipython, serialization, tables, macros
1 parent 868c1d6 commit 19991df

File tree

1 file changed

+351
-6
lines changed

1 file changed

+351
-6
lines changed

test/compat.jl

Lines changed: 351 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,356 @@
1818
end
1919

2020
@testitem "gui" begin
21-
@test PythonCall.fix_qt_plugin_path() isa Bool
22-
@test PythonCall.fix_qt_plugin_path() === false
23-
for g in [:pyqt4, :pyqt5, :pyside, :pyside2, :gtk, :gtk3, :wx, :tkinter]
24-
# TODO: actually test the various GUIs somehow?
25-
@test_throws PyException PythonCall.event_loop_on(g)
26-
@test PythonCall.event_loop_off(g) === nothing
21+
@testset "fix_qt_plugin_path" begin
22+
@test PythonCall.fix_qt_plugin_path() isa Bool
23+
# second time is a no-op
24+
@test PythonCall.fix_qt_plugin_path() === false
25+
end
26+
@testset "event_loop_on/off" begin
27+
for g in [:pyqt4, :pyqt5, :pyside, :pyside2, :gtk, :gtk3, :wx]
28+
# TODO: actually test the various GUIs somehow?
29+
@show g
30+
@test_throws PyException PythonCall.event_loop_on(g)
31+
@test PythonCall.event_loop_off(g) === nothing
32+
end
33+
end
34+
end
35+
36+
@testitem "ipython" begin
37+
@testset "PythonDisplay" begin
38+
sys = pyimport("sys")
39+
io = pyimport("io")
40+
pystdout = sys.stdout
41+
fp = sys.stdout = io.StringIO()
42+
try
43+
d = PythonCall._compat.PythonDisplay()
44+
@test display(d, 123) === nothing
45+
fp.seek(0)
46+
@test pyconvert(String, fp.read()) == "123\n"
47+
finally
48+
sys.stdout = pystdout
49+
end
50+
end
51+
@testset "IPythonDisplay" begin
52+
# TODO
53+
end
54+
end
55+
56+
@testitem "multimedia" begin
57+
# TODO
58+
end
59+
60+
@testitem "PyCall.jl" begin
61+
# TODO
62+
end
63+
64+
@testitem "Serialization.jl" begin
65+
using Serialization
66+
@testset "Py" begin
67+
for x in Py[Py(123), Py(1.23), Py("hello"), pylist([1, 2, 3]), pytuple([1, 2, 3]), Py(nothing), Py([1, 2, 3]), Py(:hello)]
68+
io = IOBuffer()
69+
serialize(io, x)
70+
seekstart(io)
71+
y = deserialize(io)
72+
@test y isa Py
73+
@test pyis(pytype(x), pytype(y))
74+
@test pyeq(Bool, x, y)
75+
end
76+
end
77+
@testset "PyException" begin
78+
for e in Py[pybuiltins.ValueError("hello")]
79+
io = IOBuffer()
80+
x = PyException(e)
81+
serialize(io, x)
82+
seekstart(io)
83+
y = deserialize(io)
84+
@test y isa PyException
85+
@test pyeq(Bool, y.t, x.t)
86+
@test pyeq(Bool, y.v.args, x.v.args)
87+
end
88+
end
89+
end
90+
91+
@testitem "Tables.jl" begin
92+
@testset "pytable" begin
93+
x = (x = [1,2,3], y = ["a", "b", "c"])
94+
# pandas
95+
# TODO: install pandas and test properly
96+
@test_throws PyException pytable(x, :pandas)
97+
# columns
98+
y = pytable(x, :columns)
99+
@test pyeq(Bool, y, pydict(x=[1,2,3], y=["a","b","c"]))
100+
# rows
101+
y = pytable(x, :rows)
102+
@test pyeq(Bool, y, pylist([(1, "a"), (2, "b"), (3, "c")]))
103+
@test all(pyisinstance(y.x, pybuiltins.int) for y in y)
104+
@test all(pyisinstance(y.y, pybuiltins.str) for y in y)
105+
# rowdicts
106+
y = pytable(x, :rowdicts)
107+
@test pyeq(Bool, y, pylist([pydict(x=1, y="a"), pydict(x=2, y="b"), pydict(x=3, y="c")]))
108+
end
109+
end
110+
111+
@testitem "@pyconst" begin
112+
f() = @pyconst "hello"
113+
g() = @pyconst "hello"
114+
@test f() === f()
115+
@test f() === f()
116+
@test g() === g()
117+
@test g() !== f()
118+
@test f() isa Py
119+
@test pyeq(Bool, f(), "hello")
120+
end
121+
122+
@testitem "@py" begin
123+
@testset "literals" begin
124+
# int
125+
x = @py(123)
126+
@test x isa Py
127+
@test pyis(pytype(x), pybuiltins.int)
128+
@test pyeq(Bool, x, 123)
129+
# uint
130+
x = @py(0x123)
131+
@test x isa Py
132+
@test pyis(pytype(x), pybuiltins.int)
133+
@test pyeq(Bool, x, 0x123)
134+
# int128
135+
x = @py(12345678901234567890)
136+
@test x isa Py
137+
@test pyis(pytype(x), pybuiltins.int)
138+
@test pyeq(Bool, x, 12345678901234567890)
139+
# uint128
140+
x = @py(0x12345678901234567890)
141+
@test x isa Py
142+
@test pyis(pytype(x), pybuiltins.int)
143+
@test pyeq(Bool, x, 0x12345678901234567890)
144+
# bigint
145+
x = @py(big"1234567890123456789012345678901234567890")
146+
@test x isa Py
147+
@test pyis(pytype(x), pybuiltins.int)
148+
@test pyeq(Bool, x, big"1234567890123456789012345678901234567890")
149+
x = @py(1234567890123456789012345678901234567890)
150+
@test x isa Py
151+
@test pyis(pytype(x), pybuiltins.int)
152+
@test pyeq(Bool, x, big"1234567890123456789012345678901234567890")
153+
# None
154+
x = @py(None)
155+
@test pyis(x, pybuiltins.None)
156+
# True
157+
x = @py(True)
158+
@test x isa Py
159+
@test pyis(x, pybuiltins.True)
160+
x = @py(true)
161+
@test x isa Py
162+
@test pyis(x, pybuiltins.True)
163+
# False
164+
x = @py(False)
165+
@test x isa Py
166+
@test pyis(x, pybuiltins.False)
167+
x = @py(false)
168+
@test x isa Py
169+
@test pyis(x, pybuiltins.False)
170+
# str
171+
x = @py("hello")
172+
@test x isa Py
173+
@test pyis(pytype(x), pybuiltins.str)
174+
@test pyeq(Bool, x, "hello")
175+
# float
176+
x = @py(1.23)
177+
@test x isa Py
178+
@test pyis(pytype(x), pybuiltins.float)
179+
@test pyeq(Bool, x, 1.23)
180+
# tuple
181+
x = @py tuple()
182+
@test x isa Py
183+
@test pyis(pytype(x), pybuiltins.tuple)
184+
@test pyeq(Bool, x, ())
185+
x = @py (1, 2, 3)
186+
@test x isa Py
187+
@test pyis(pytype(x), pybuiltins.tuple)
188+
@test pyeq(Bool, x, (1, 2, 3))
189+
# list
190+
x = @py list()
191+
@test x isa Py
192+
@test pyis(pytype(x), pybuiltins.list)
193+
@test pyeq(Bool, x, pylist())
194+
x = @py [1, 2, 3]
195+
@test x isa Py
196+
@test pyis(pytype(x), pybuiltins.list)
197+
@test pyeq(Bool, x, pylist([1, 2, 3]))
198+
# dict
199+
x = @py dict()
200+
@test x isa Py
201+
@test pyis(pytype(x), pybuiltins.dict)
202+
@test pyeq(Bool, x, pydict())
203+
x = @py {}
204+
@test x isa Py
205+
@test pyis(pytype(x), pybuiltins.dict)
206+
@test pyeq(Bool, x, pydict())
207+
x = @py {x=1, y=2}
208+
@test x isa Py
209+
@test pyis(pytype(x), pybuiltins.dict)
210+
@test pyeq(Bool, x, pydict(x=1, y=2))
211+
x = @py {"x": 1, "y": 2}
212+
@test x isa Py
213+
@test pyis(pytype(x), pybuiltins.dict)
214+
@test pyeq(Bool, x, pydict(x=1, y=2))
215+
# set
216+
x = @py set()
217+
@test x isa Py
218+
@test pyis(pytype(x), pybuiltins.set)
219+
@test pyeq(Bool, x, pyset())
220+
x = @py {1, 2, 3}
221+
@test x isa Py
222+
@test pyis(pytype(x), pybuiltins.set)
223+
@test pyeq(Bool, x, pyset([1, 2, 3]))
224+
end
225+
@testset "__file__" begin
226+
x = @py __file__
227+
@test x isa Py
228+
@test pyis(pytype(x), pybuiltins.str)
229+
@test pyeq(Bool, x, @__FILE__)
230+
end
231+
@testset "__line__" begin
232+
x = @py __line__
233+
@test x isa Py
234+
@test pyis(pytype(x), pybuiltins.int)
235+
@test pyeq(Bool, x, @__LINE__() - 3)
236+
end
237+
@testset "builtins" begin
238+
x = @py int
239+
@test pyis(x, pybuiltins.int)
240+
x = @py float
241+
@test pyis(x, pybuiltins.float)
242+
x = @py ValueError
243+
@test pyis(x, pybuiltins.ValueError)
244+
end
245+
@testset "variables" begin
246+
x = 123
247+
y = @py x
248+
@test y === x
249+
end
250+
@testset "arithmetic" begin
251+
x = @py 1 + 2 + 3
252+
@test pyeq(Bool, x, 6)
253+
x = @py "foo" + "bar"
254+
@test pyeq(Bool, x, "foobar")
255+
x = @py 9 - 2
256+
@test pyeq(Bool, x, 7)
257+
x = @py 2 * 3 * 4
258+
@test pyeq(Bool, x, 24)
259+
x = @py "foo" * 3
260+
@test pyeq(Bool, x, "foofoofoo")
261+
x = @py 10 / 4
262+
@test pyeq(Bool, x, 2.5)
263+
x = @py 1 << 10
264+
@test pyeq(Bool, x, 1024)
265+
x = @py -(10)
266+
@test pyeq(Bool, x, -10)
267+
end
268+
@testset "attrs" begin
269+
t = pytype("Test", (pybuiltins.object,), [])
270+
x = t()
271+
@py x.foo = "foo"
272+
@test pyeq(Bool, x.foo, "foo")
273+
end
274+
@testset "items" begin
275+
x = pylist([1, 2, 3])
276+
@test pyeq(Bool, @py(x[0]), 1)
277+
@test pyeq(Bool, @py(x[-1]), 3)
278+
@test pyeq(Bool, @py(x[0:2]), pylist([1, 2]))
279+
@py x[1] = 0
280+
@test pyeq(Bool, x, pylist([1, 0, 3]))
281+
end
282+
@testset "assign" begin
283+
@py x = 12
284+
@test x isa Py
285+
@test pyis(pytype(x), pybuiltins.int)
286+
@test pyeq(Bool, x, 12)
287+
end
288+
@testset "@jl" begin
289+
x = @py @jl "foo"^3
290+
@test x == "foofoofoo"
291+
end
292+
@testset "begin" begin
293+
z = @py begin
294+
x = "foo"
295+
y = 4
296+
x * y
297+
end
298+
@test x isa Py
299+
@test pyeq(Bool, x, "foo")
300+
@test y isa Py
301+
@test pyeq(Bool, y, 4)
302+
@test z isa Py
303+
@test pyeq(Bool, z, "foofoofoofoo")
304+
end
305+
@testset "import" begin
306+
@py import sys
307+
@test pyis(sys, pyimport("sys"))
308+
@py import sys as _sys
309+
@test pyis(_sys, sys)
310+
@py import sys as _sys2, sys as _sys3
311+
@test pyis(_sys2, sys)
312+
@test pyis(_sys3, sys)
313+
@py import sys: version_info
314+
@test pyis(version_info, sys.version_info)
315+
@py import sys: modules as _mods, version_info as _ver
316+
@test pyis(_mods, sys.modules)
317+
@test pyis(_ver, sys.version_info)
318+
end
319+
@testset "short-circuit" begin
320+
x = @py 3 && pylist([1,2])
321+
@test pyeq(Bool, x, pylist([1, 2]))
322+
x = @py None && True
323+
@test pyis(x, pybuiltins.None)
324+
x = @py None || 0 || pyset()
325+
@test pyeq(Bool, x, pyset())
326+
x = @py pydict() || 8 || ""
327+
@test pyeq(Bool, x, 8)
328+
end
329+
@testset "if" begin
330+
x = @py if 1 == 2; "a"; end
331+
@test x isa Py
332+
@test pyis(x, pybuiltins.None)
333+
x = @py if 1 < 2; "a"; end
334+
@test x isa Py
335+
@test pyeq(Bool, x, "a")
336+
x = @py if 1 == 2; "a"; else; "b"; end
337+
@test x isa Py
338+
@test pyeq(Bool, x, "b")
339+
x = @py if 1 < 2; "a"; else; "b"; end
340+
@test x isa Py
341+
@test pyeq(Bool, x, "a")
342+
x = @py if 1 == 2; "a"; elseif 1 < 2; "b"; end
343+
@test x isa Py
344+
@test pyeq(Bool, x, "b")
345+
x = @py if 1 < 2; "a"; elseif 2 < 3; "b"; end
346+
@test x isa Py
347+
@test pyeq(Bool, x, "a")
348+
x = @py if 1 == 2; "a"; elseif 2 == 3; "b"; end
349+
@test x isa Py
350+
@test pyis(x, pybuiltins.None)
351+
end
352+
@testset "for" begin
353+
x = pydict(x=1, y=2)
354+
y = pylist()
355+
@py for k in x
356+
y.append(k)
357+
end
358+
@test pyeq(Bool, y, pylist(["x", "y"]))
359+
end
360+
@testset "while" begin
361+
x = pylist([1, 2, 3, 4])
362+
y = pylist()
363+
@py while len(x) > 2
364+
y.append(x.pop())
365+
end
366+
@test pyeq(Bool, x, pylist([1, 2]))
367+
@test pyeq(Bool, y, pylist([4, 3]))
368+
end
369+
@testset "string interpolation" begin
370+
x = @py """$(None)$(True)$("foo"*2)"""
371+
@test pyeq(Bool, x, "NoneTruefoofoo")
27372
end
28373
end

0 commit comments

Comments
 (0)