From e374e2503b7b843c457d30596a2428ebfc26bb0b Mon Sep 17 00:00:00 2001 From: Christopher Rowley Date: Fri, 16 Jun 2023 15:40:02 +0100 Subject: [PATCH] Jlvaluetruth (#327) * add jlwrap tests * define __bool__ on jlwrap objects --------- Co-authored-by: Christopher Doris --- src/jlwrap/any.jl | 2 + src/jlwrap/array.jl | 2 + src/jlwrap/dict.jl | 2 + src/jlwrap/set.jl | 2 + test/jlwrap.jl | 124 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+) diff --git a/src/jlwrap/any.jl b/src/jlwrap/any.jl index e0c98751..5458faa5 100644 --- a/src/jlwrap/any.jl +++ b/src/jlwrap/any.jl @@ -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): diff --git a/src/jlwrap/array.jl b/src/jlwrap/array.jl index 1c9eb26f..bbfbd08e 100644 --- a/src/jlwrap/array.jl +++ b/src/jlwrap/array.jl @@ -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): diff --git a/src/jlwrap/dict.jl b/src/jlwrap/dict.jl index 8a8a18f2..6ed73d7b 100644 --- a/src/jlwrap/dict.jl +++ b/src/jlwrap/dict.jl @@ -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): diff --git a/src/jlwrap/set.jl b/src/jlwrap/set.jl index 8622e29c..c4ff1b1f 100644 --- a/src/jlwrap/set.jl +++ b/src/jlwrap/set.jl @@ -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): diff --git a/test/jlwrap.jl b/test/jlwrap.jl index cc5999eb..44f7d7ca 100644 --- a/test/jlwrap.jl +++ b/test/jlwrap.jl @@ -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" @@ -123,6 +134,50 @@ 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) @@ -130,3 +185,72 @@ end 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