Skip to content

Commit 8877892

Browse files
author
Christopher Doris
committed
tests for PySet
1 parent 008074f commit 8877892

File tree

3 files changed

+122
-6
lines changed

3 files changed

+122
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ __pycache__
55
build/
66
dist/
77
.CondaPkg/
8+
/jltest.*

test/convert.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ end
169169
@test t4 === (x=6, y=7)
170170
end
171171

172+
@testitem "mapping → PyDict" begin
173+
x1 = pyconvert(PyDict, pydict([1=>11, 2=>22, 3=>33]))
174+
@test x1 isa PyDict{Py,Py}
175+
@test isequal(x1, Dict([Py(1)=>Py(11), Py(2)=>Py(22), Py(3)=>Py(33)]))
176+
x2 = pyconvert(PyDict{Int,Int}, pydict([1=>11, 2=>22, 3=>33]))
177+
@test x2 isa PyDict{Int,Int}
178+
@test x2 == Dict(1=>11, 2=>22, 3=>33)
179+
end
180+
172181
@testitem "mapping → Dict" begin
173182
x1 = pyconvert(Dict, pydict(["a"=>1, "b"=>2]))
174183
@test x1 isa Dict{String, Int}
@@ -178,6 +187,24 @@ end
178187
@test x2 == Dict('c'=>3.0, 'd'=>4.0)
179188
end
180189

190+
@testitem "sequence → PyList" begin
191+
x1 = pyconvert(PyList, pylist([1, 2, 3]))
192+
@test x1 isa PyList{Py}
193+
@test isequal(x1, [Py(1), Py(2), Py(3)])
194+
x2 = pyconvert(PyList{Int}, pylist([1, 2, 3]))
195+
@test x2 isa PyList{Int}
196+
@test x2 == [1, 2, 3]
197+
end
198+
199+
@testitem "set → PySet" begin
200+
x1 = pyconvert(PySet, pyset([1, 2, 3]))
201+
@test x1 isa PySet{Py}
202+
@test isequal(x1, Set([Py(1), Py(2), Py(3)]))
203+
x2 = pyconvert(PySet{Int}, pyset([1, 2, 3]))
204+
@test x2 isa PySet{Int}
205+
@test x2 == Set([1, 2, 3])
206+
end
207+
181208
@testitem "date → Date" begin
182209
using Dates
183210
x1 = pyconvert(Date, pydate(2001, 2, 3))

test/pywrap.jl

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,101 @@ end
272272
end
273273

274274
@testitem "PySet" begin
275+
x = pyset([1, 2, 3])
276+
y = PySet(x)
277+
z = PySet{Int}(x)
278+
e = PySet{String}()
279+
@testset "construct" begin
280+
@test y isa PySet{Py}
281+
@test z isa PySet{Int}
282+
@test e isa PySet{String}
283+
@test PythonCall.ispy(y)
284+
@test PythonCall.ispy(z)
285+
@test PythonCall.ispy(e)
286+
@test Py(y) === x
287+
@test Py(z) === x
288+
@test Py(e) !== x
289+
end
290+
@testset "length" begin
291+
@test length(y) == 3
292+
@test length(z) == 3
293+
@test length(e) == 0
294+
end
295+
@testset "isempty" begin
296+
@test !isempty(y)
297+
@test !isempty(z)
298+
@test isempty(e)
299+
end
300+
@testset "in" begin
301+
@test 1 in z
302+
@test 3 in z
303+
@test !(4 in z)
304+
@test !(1 in e)
305+
@test 2.0 in z
306+
@test !(2.1 in z)
307+
end
308+
@testset "push!" begin
309+
a = PySet{String}(["a"])
310+
@test a == Set(["a"])
311+
push!(a, "b")
312+
@test a == Set(["a", "b"])
313+
push!(a, "a")
314+
@test a == Set(["a", "b"])
315+
end
316+
@testset "delete!" begin
317+
a = PySet{Int}()
318+
@test a == Set()
319+
delete!(a, 0)
320+
@test a == Set()
321+
delete!(a, nothing)
322+
@test a == Set()
323+
push!(a, 1, 2, 3)
324+
@test a == Set([1, 2, 3])
325+
delete!(a, 2)
326+
@test a == Set([1, 3])
327+
delete!(a, 1.2)
328+
@test a == Set([1, 3])
329+
delete!(a, 3.0)
330+
@test a == Set([1])
331+
delete!(a, nothing)
332+
@test a == Set([1])
333+
end
334+
@testset "pop!" begin
335+
a = PySet{Int}()
336+
@test_throws Exception pop!(a)
337+
push!(a, 1, -1)
338+
x = pop!(a)
339+
@test x in [1, -1]
340+
@test a == Set([-x])
341+
y = pop!(a)
342+
@test x == -y
343+
@test a == Set()
344+
@test_throws Exception pop!(a)
345+
push!(a, 1, 2, 3)
346+
@test pop!(a, 1) == 1
347+
@test a == Set([2, 3])
348+
@test pop!(a, 2.0) == 2
349+
@test a == Set([3])
350+
@test_throws Exception pop!(a, 1)
351+
@test_throws Exception pop!(a, 1.0)
352+
@test_throws Exception pop!(a, nothing)
353+
@test a == Set([3])
354+
@test pop!(a, 1, 99) === 99
355+
@test pop!(a, 1.0, 99) === 99
356+
@test pop!(a, nothing, 99) === 99
357+
end
358+
@testset "empty!" begin
359+
a = PySet{Int}([1, 2, 3])
360+
@test a == Set([1, 2, 3])
361+
@test empty!(a) === a
362+
@test a == Set()
363+
end
275364
@testset "copy" begin
276-
x = PySet{Int}([1,2,3])
277-
y = copy(x)
278-
@test y isa PySet{Int}
279-
push!(y, 99)
280-
@test x == Set([1, 2, 3])
281-
@test y == Set([1, 2, 3, 99])
365+
z2 = copy(z)
366+
@test z2 isa PySet{Int}
367+
push!(z2, 99)
368+
@test z == Set([1, 2, 3])
369+
@test z2 == Set([1, 2, 3, 99])
282370
end
283371
end
284372

0 commit comments

Comments
 (0)