Skip to content

Commit

Permalink
Jlvaluetruth (#327)
Browse files Browse the repository at this point in the history
* add jlwrap tests

* define __bool__ on jlwrap objects

---------

Co-authored-by: Christopher Doris <github.com/cjdoris>
  • Loading branch information
cjdoris authored Jun 16, 2023
1 parent fb69ab6 commit e374e25
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/jlwrap/any.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ function init_jlwrap_any()
return ValueBase.__dir__(self) + self._jl_callmethod($(pyjl_methodnum(pyjlany_dir)))
def __call__(self, *args, **kwargs):
return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
def __bool__(self):
return True
def __len__(self):
return self._jl_callmethod($(pyjl_methodnum(pyjlany_op(length))))
def __getitem__(self, k):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ function init_jlwrap_array()
return self._jl_callmethod($(pyjl_methodnum(Py copy)))
def reshape(self, shape):
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_reshape)), shape)
def __bool__(self):
return bool(len(self))
def __getitem__(self, k):
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_getitem)), k)
def __setitem__(self, k, v):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function init_jlwrap_dict()
class DictValue(AnyValue):
__slots__ = ()
_jl_undefined_ = object()
def __bool__(self):
return bool(len(self))
def __iter__(self):
return self._jl_callmethod($(pyjl_methodnum(pyjldict_iter)))
def __contains__(self, key):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ function init_jlwrap_set()
$("\n"^(@__LINE__()-1))
class SetValue(AnyValue):
__slots__ = ()
def __bool__(self):
return bool(len(self))
def add(self, value):
return self._jl_callmethod($(pyjl_methodnum(pyjlset_add)), value)
def discard(self, value):
Expand Down
124 changes: 124 additions & 0 deletions test/jlwrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
Base.:(>>)(x::Foo, y::Foo) = "$(x.value) >> $(y.value)"
Base.:(&)(x::Foo, y::Foo) = "$(x.value) & $(y.value)"
Base.:(|)(x::Foo, y::Foo) = "$(x.value) | $(y.value)"
@testset "type" begin
@test pyis(pytype(pyjl(Foo(1))), PythonCall.pyjlanytype)
@test pyis(pytype(pyjl(nothing)), PythonCall.pyjlanytype)
@test pyis(pytype(pyjl(missing)), PythonCall.pyjlanytype)
end
@testset "bool" begin
@test pytruth(pyjl(Foo(0)))
@test pytruth(pyjl(Foo(1)))
@test pytruth(pyjl(nothing))
@test pytruth(pyjl(missing))
end
@testset "pos" begin
z = pyjl(+Foo(1))
@test pyconvert(String, z) == "+ 1"
Expand Down Expand Up @@ -123,10 +134,123 @@
end
end

@testitem "array" begin
@testset "type" begin
@test pyis(pytype(pyjl(fill(nothing))), PythonCall.pyjlarraytype)
@test pyis(pytype(pyjl([1 2; 3 4])), PythonCall.pyjlarraytype)
end
@testset "bool" begin
@test !pytruth(pyjl(fill(nothing, 0, 1)))
@test !pytruth(pyjl(fill(nothing, 1, 0)))
@test pytruth(pyjl(fill(nothing)))
@test pytruth(pyjl(fill(nothing, 1, 2)))
@test pytruth(pyjl(fill(nothing, 1, 2, 3)))
end
end

@testitem "base" begin

end

@testitem "callback" begin

end

@testitem "dict" begin
@testset "type" begin
@test pyis(pytype(pyjl(Dict())), PythonCall.pyjldicttype)
end
@testset "bool" begin
@test !pytruth(pyjl(Dict()))
@test pytruth(pyjl(Dict("one"=>1, "two"=>2)))
end
end

@testitem "io" begin
@testset "type" begin
@test pyis(pytype(pyjl(devnull)), PythonCall.pyjlbinaryiotype)
@test pyis(pytype(pybinaryio(devnull)), PythonCall.pyjlbinaryiotype)
@test pyis(pytype(pytextio(devnull)), PythonCall.pyjltextiotype)
end
@testset "bool" begin
@test pytruth(pybinaryio(devnull))
@test pytruth(pytextio(devnull))
end
end

@testitem "iter" begin
x1 = [1,2,3,4,5]
x2 = pyjl(x1)
x3 = pylist(x2)
x4 = pyconvert(Vector{Int}, x3)
@test x1 == x4
end

@testitem "module" begin
@testset "type" begin
@test pyis(pytype(pyjl(PythonCall)), PythonCall.pyjlmoduletype)
end
@testset "bool" begin
@test pytruth(pyjl(PythonCall))
end
end

@testitem "number" begin
@testset "type" begin
@test pyis(pytype(pyjl(false)), PythonCall.pyjlintegertype)
@test pyis(pytype(pyjl(0)), PythonCall.pyjlintegertype)
@test pyis(pytype(pyjl(0//1)), PythonCall.pyjlrationaltype)
@test pyis(pytype(pyjl(0.0)), PythonCall.pyjlrealtype)
@test pyis(pytype(pyjl(Complex(0.0))), PythonCall.pyjlcomplextype)
end
@testset "bool" begin
@test !pytruth(pyjl(false))
@test !pytruth(pyjl(0))
@test !pytruth(pyjl(0//1))
@test !pytruth(pyjl(0.0))
@test !pytruth(pyjl(Complex(0.0)))
@test pytruth(pyjl(true))
@test pytruth(pyjl(3))
@test pytruth(pyjl(5//2))
@test pytruth(pyjl(2.3))
@test pytruth(pyjl(Complex(1.2, 3.4)))
end
end

@testitem "objectarray" begin

end

@testitem "raw" begin

end

@testitem "set" begin
@testset "type" begin
@test pyis(pytype(pyjl(Set())), PythonCall.pyjlsettype)
end
@testset "bool" begin
@test !pytruth(pyjl(Set()))
@test pytruth(pyjl(Set([1,2,3])))
end
end

@testitem "type" begin
@testset "type" begin
@test pyis(pytype(pyjl(Int)), PythonCall.pyjltypetype)
end
@testset "bool" begin
@test pytruth(pyjl(Int))
end
end

@testitem "vector" begin
@testset "type" begin
@test pyis(pytype(pyjl([1, 2, 3, 4])), PythonCall.pyjlvectortype)
end
@testset "bool" begin
@test !pytruth(pyjl([]))
@test pytruth(pyjl([1]))
@test pytruth(pyjl([1,2]))
end
end

0 comments on commit e374e25

Please sign in to comment.