Skip to content

Commit e374e25

Browse files
authored
Jlvaluetruth (#327)
* add jlwrap tests * define __bool__ on jlwrap objects --------- Co-authored-by: Christopher Doris <github.com/cjdoris>
1 parent fb69ab6 commit e374e25

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

src/jlwrap/any.jl

+2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ function init_jlwrap_any()
206206
return ValueBase.__dir__(self) + self._jl_callmethod($(pyjl_methodnum(pyjlany_dir)))
207207
def __call__(self, *args, **kwargs):
208208
return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
209+
def __bool__(self):
210+
return True
209211
def __len__(self):
210212
return self._jl_callmethod($(pyjl_methodnum(pyjlany_op(length))))
211213
def __getitem__(self, k):

src/jlwrap/array.jl

+2
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ function init_jlwrap_array()
304304
return self._jl_callmethod($(pyjl_methodnum(Py copy)))
305305
def reshape(self, shape):
306306
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_reshape)), shape)
307+
def __bool__(self):
308+
return bool(len(self))
307309
def __getitem__(self, k):
308310
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_getitem)), k)
309311
def __setitem__(self, k, v):

src/jlwrap/dict.jl

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function init_jlwrap_dict()
3838
class DictValue(AnyValue):
3939
__slots__ = ()
4040
_jl_undefined_ = object()
41+
def __bool__(self):
42+
return bool(len(self))
4143
def __iter__(self):
4244
return self._jl_callmethod($(pyjl_methodnum(pyjldict_iter)))
4345
def __contains__(self, key):

src/jlwrap/set.jl

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ function init_jlwrap_set()
7979
$("\n"^(@__LINE__()-1))
8080
class SetValue(AnyValue):
8181
__slots__ = ()
82+
def __bool__(self):
83+
return bool(len(self))
8284
def add(self, value):
8385
return self._jl_callmethod($(pyjl_methodnum(pyjlset_add)), value)
8486
def discard(self, value):

test/jlwrap.jl

+124
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@
1717
Base.:(>>)(x::Foo, y::Foo) = "$(x.value) >> $(y.value)"
1818
Base.:(&)(x::Foo, y::Foo) = "$(x.value) & $(y.value)"
1919
Base.:(|)(x::Foo, y::Foo) = "$(x.value) | $(y.value)"
20+
@testset "type" begin
21+
@test pyis(pytype(pyjl(Foo(1))), PythonCall.pyjlanytype)
22+
@test pyis(pytype(pyjl(nothing)), PythonCall.pyjlanytype)
23+
@test pyis(pytype(pyjl(missing)), PythonCall.pyjlanytype)
24+
end
25+
@testset "bool" begin
26+
@test pytruth(pyjl(Foo(0)))
27+
@test pytruth(pyjl(Foo(1)))
28+
@test pytruth(pyjl(nothing))
29+
@test pytruth(pyjl(missing))
30+
end
2031
@testset "pos" begin
2132
z = pyjl(+Foo(1))
2233
@test pyconvert(String, z) == "+ 1"
@@ -123,10 +134,123 @@
123134
end
124135
end
125136

137+
@testitem "array" begin
138+
@testset "type" begin
139+
@test pyis(pytype(pyjl(fill(nothing))), PythonCall.pyjlarraytype)
140+
@test pyis(pytype(pyjl([1 2; 3 4])), PythonCall.pyjlarraytype)
141+
end
142+
@testset "bool" begin
143+
@test !pytruth(pyjl(fill(nothing, 0, 1)))
144+
@test !pytruth(pyjl(fill(nothing, 1, 0)))
145+
@test pytruth(pyjl(fill(nothing)))
146+
@test pytruth(pyjl(fill(nothing, 1, 2)))
147+
@test pytruth(pyjl(fill(nothing, 1, 2, 3)))
148+
end
149+
end
150+
151+
@testitem "base" begin
152+
153+
end
154+
155+
@testitem "callback" begin
156+
157+
end
158+
159+
@testitem "dict" begin
160+
@testset "type" begin
161+
@test pyis(pytype(pyjl(Dict())), PythonCall.pyjldicttype)
162+
end
163+
@testset "bool" begin
164+
@test !pytruth(pyjl(Dict()))
165+
@test pytruth(pyjl(Dict("one"=>1, "two"=>2)))
166+
end
167+
end
168+
169+
@testitem "io" begin
170+
@testset "type" begin
171+
@test pyis(pytype(pyjl(devnull)), PythonCall.pyjlbinaryiotype)
172+
@test pyis(pytype(pybinaryio(devnull)), PythonCall.pyjlbinaryiotype)
173+
@test pyis(pytype(pytextio(devnull)), PythonCall.pyjltextiotype)
174+
end
175+
@testset "bool" begin
176+
@test pytruth(pybinaryio(devnull))
177+
@test pytruth(pytextio(devnull))
178+
end
179+
end
180+
126181
@testitem "iter" begin
127182
x1 = [1,2,3,4,5]
128183
x2 = pyjl(x1)
129184
x3 = pylist(x2)
130185
x4 = pyconvert(Vector{Int}, x3)
131186
@test x1 == x4
132187
end
188+
189+
@testitem "module" begin
190+
@testset "type" begin
191+
@test pyis(pytype(pyjl(PythonCall)), PythonCall.pyjlmoduletype)
192+
end
193+
@testset "bool" begin
194+
@test pytruth(pyjl(PythonCall))
195+
end
196+
end
197+
198+
@testitem "number" begin
199+
@testset "type" begin
200+
@test pyis(pytype(pyjl(false)), PythonCall.pyjlintegertype)
201+
@test pyis(pytype(pyjl(0)), PythonCall.pyjlintegertype)
202+
@test pyis(pytype(pyjl(0//1)), PythonCall.pyjlrationaltype)
203+
@test pyis(pytype(pyjl(0.0)), PythonCall.pyjlrealtype)
204+
@test pyis(pytype(pyjl(Complex(0.0))), PythonCall.pyjlcomplextype)
205+
end
206+
@testset "bool" begin
207+
@test !pytruth(pyjl(false))
208+
@test !pytruth(pyjl(0))
209+
@test !pytruth(pyjl(0//1))
210+
@test !pytruth(pyjl(0.0))
211+
@test !pytruth(pyjl(Complex(0.0)))
212+
@test pytruth(pyjl(true))
213+
@test pytruth(pyjl(3))
214+
@test pytruth(pyjl(5//2))
215+
@test pytruth(pyjl(2.3))
216+
@test pytruth(pyjl(Complex(1.2, 3.4)))
217+
end
218+
end
219+
220+
@testitem "objectarray" begin
221+
222+
end
223+
224+
@testitem "raw" begin
225+
226+
end
227+
228+
@testitem "set" begin
229+
@testset "type" begin
230+
@test pyis(pytype(pyjl(Set())), PythonCall.pyjlsettype)
231+
end
232+
@testset "bool" begin
233+
@test !pytruth(pyjl(Set()))
234+
@test pytruth(pyjl(Set([1,2,3])))
235+
end
236+
end
237+
238+
@testitem "type" begin
239+
@testset "type" begin
240+
@test pyis(pytype(pyjl(Int)), PythonCall.pyjltypetype)
241+
end
242+
@testset "bool" begin
243+
@test pytruth(pyjl(Int))
244+
end
245+
end
246+
247+
@testitem "vector" begin
248+
@testset "type" begin
249+
@test pyis(pytype(pyjl([1, 2, 3, 4])), PythonCall.pyjlvectortype)
250+
end
251+
@testset "bool" begin
252+
@test !pytruth(pyjl([]))
253+
@test pytruth(pyjl([1]))
254+
@test pytruth(pyjl([1,2]))
255+
end
256+
end

0 commit comments

Comments
 (0)