Skip to content

Commit d9562d0

Browse files
author
Christopher Doris
committed
tests/bugfixes for juliaraw and juliadict
1 parent 6477ac0 commit d9562d0

File tree

2 files changed

+78
-9
lines changed

2 files changed

+78
-9
lines changed

src/cpython/juliadict.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ Base.iterate(x::DictPairSet) =
108108
(r = iterate(x.dict); r === nothing ? nothing : (Tuple(r[1]), r[2]))
109109
Base.iterate(x::DictPairSet, st) =
110110
(r = iterate(x.dict, st); r === nothing ? nothing : (Tuple(r[1]), r[2]))
111-
Base.in(v, x::DictPairSet) = v in x.dict
111+
Base.in(v::Pair, x::DictPairSet) = v in x.dict
112+
Base.in(v::Tuple{Any,Any}, x::DictPairSet) = Pair(v[1], v[2]) in x.dict
112113

113114
const PyJuliaDictValue_Type = LazyPyObject() do
114115
c = []
@@ -122,37 +123,37 @@ const PyJuliaDictValue_Type = LazyPyObject() do
122123
PyMethodDef(
123124
name = cacheptr!(c, "keys"),
124125
flags = Py_METH_NOARGS,
125-
meth = @cfunctionOO(pyjldict_keys),
126+
meth = @cfunctionOOO(pyjldict_keys),
126127
),
127128
PyMethodDef(
128129
name = cacheptr!(c, "values"),
129130
flags = Py_METH_NOARGS,
130-
meth = @cfunctionOO(pyjldict_values),
131+
meth = @cfunctionOOO(pyjldict_values),
131132
),
132133
PyMethodDef(
133134
name = cacheptr!(c, "items"),
134135
flags = Py_METH_NOARGS,
135-
meth = @cfunctionOO(pyjldict_items),
136+
meth = @cfunctionOOO(pyjldict_items),
136137
),
137138
PyMethodDef(
138139
name = cacheptr!(c, "get"),
139140
flags = Py_METH_VARARGS,
140-
meth = @cfunctionOO(pyjldict_get),
141+
meth = @cfunctionOOO(pyjldict_get),
141142
),
142143
PyMethodDef(
143144
name = cacheptr!(c, "clear"),
144145
flags = Py_METH_NOARGS,
145-
meth = @cfunctionOO(pyjldict_clear),
146+
meth = @cfunctionOOO(pyjldict_clear),
146147
),
147148
PyMethodDef(
148149
name = cacheptr!(c, "pop"),
149150
flags = Py_METH_VARARGS,
150-
meth = @cfunctionOO(pyjldict_pop),
151+
meth = @cfunctionOOO(pyjldict_pop),
151152
),
152153
PyMethodDef(
153154
name = cacheptr!(c, "popitem"),
154155
flags = Py_METH_NOARGS,
155-
meth = @cfunctionOO(pyjldict_popitem),
156+
meth = @cfunctionOOO(pyjldict_popitem),
156157
),
157158
# PyMethodDef(
158159
# name = cacheptr!(c, "update"),
@@ -162,7 +163,7 @@ const PyJuliaDictValue_Type = LazyPyObject() do
162163
PyMethodDef(
163164
name = cacheptr!(c, "setdefault"),
164165
flags = Py_METH_VARARGS,
165-
meth = @cfunctionOO(pyjldict_setdefault),
166+
meth = @cfunctionOOO(pyjldict_setdefault),
166167
),
167168
PyMethodDef(),
168169
]),

test/runtests.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,35 @@ end
824824
@test pyjlgetvalue(v) === value
825825
end
826826

827+
@testset "juliaraw" begin
828+
xo = pyjlraw(nothing)
829+
@test @pyv `repr($xo) == "<jl nothing>"`::Bool
830+
@test @pyv `str($xo) == "nothing"`::Bool
831+
x = Struct1("foo", 12)
832+
xo = pyjlraw(x)
833+
@test (@pyv `$xo.x`::Any) === x.x
834+
@test (@pyv `$xo.y`::Any) === x.y
835+
@py `$xo.x = "bar"`
836+
@test x.x == "bar"
837+
@test @pyv `"x" in dir($xo)`::Bool
838+
@test @pyv `"y" in dir($xo)`::Bool
839+
xo = pyjlraw(+)
840+
@test (@pyv `$xo(2,3)`::Any) === 5
841+
x = [1,2,3]
842+
xo = pyjlraw(x)
843+
@test @pyv `len($xo) == 3`::Bool
844+
@test (@pyv `$xo[1]`::Any) === x[1]
845+
@test (@pyv `$xo[$(pyjlraw(1:3))]`::Any) == [1,2,3]
846+
@py `$xo[1] = 0`
847+
@test x == [0,2,3]
848+
x = [1 2; 3 4]
849+
xo = pyjlraw(x)
850+
@test @pyv `len($xo) == 4`::Bool
851+
@test (@pyv `$xo[1,2]`::Any) === x[1,2]
852+
@py `$xo[1,2] = 0`
853+
@test x == [1 0; 3 4]
854+
end
855+
827856
@testset "juliaany" begin
828857
for value in Any[nothing, missing, (), identity, push!]
829858
@test @pyv `type($(pyjl(value))).__name__ == "AnyValue"`::Bool
@@ -948,6 +977,45 @@ end
948977
@test @pyv `list($xmv) == [0,3,2,4]`::Bool
949978
end
950979

980+
@testset "juliavector" begin
981+
# TODO
982+
end
983+
984+
@testset "juliaset" begin
985+
# TODO
986+
end
987+
988+
@testset "juliadict" begin
989+
x = Dict("foo"=>1, "bar"=>2, "baz"=>3)
990+
@test @pyv `set($x) == {"foo", "bar", "baz"}`::Bool
991+
@test @pyv `set($x.keys()) == {"foo", "bar", "baz"}`::Bool
992+
@test @pyv `set($x.values()) == {1, 2, 3}`::Bool
993+
@test @pyv `set($x.items()) == {("foo", 1), ("bar", 2), ("baz", 3)}`::Bool
994+
@test @pyv `len($x) == 3`::Bool
995+
@test @pyv `len($x.items()) == 3`::Bool
996+
@test @pyv `"foo" in $x`::Bool
997+
@test @pyv `"hello" not in $x`::Bool
998+
@test @pyv `1 not in $x`::Bool
999+
@test @pyv `("foo", 1) in $x.items()`::Bool
1000+
@test @pyv `$x.get("bar") == 2`::Bool
1001+
@test @pyv `$x.get("hello") is None`::Bool
1002+
@test @pyv `$x.get(1) is None`::Bool
1003+
@test @pyv `$x.get("hello", "MISSING") == "MISSING"`::Bool
1004+
@test @pyv `$x.setdefault("foo", 0) == 1`::Bool
1005+
@test x["foo"] == 1
1006+
@test @pyv `$x.setdefault("bax", 0) == 0`::Bool
1007+
@test x["bax"] == 0
1008+
@test @pyv `$x.pop("bax", None) == 0`::Bool
1009+
@test @pyv `$x.pop("bax", None) is None`::Bool
1010+
@test_throws Exception @pyv `$x.pop("bax")`
1011+
@py `$x.clear()`
1012+
@test isempty(x)
1013+
x["foo"] = 1
1014+
@test @pyv `$x.popitem() == ("foo", 1)`::Bool
1015+
@test_throws Exception @pyv `$x.popitem()`
1016+
@test isempty(x)
1017+
end
1018+
9511019
@testset "juliaio" begin
9521020
for value in Any[stdin, stdout, IOBuffer()]
9531021
@test @pyv `type($(pyjl(value))).__name__ == "BufferedIOValue"`::Bool

0 commit comments

Comments
 (0)