|
| 1 | +@testset "import" begin |
| 2 | + sys = pyimport("sys") |
| 3 | + os = pyimport("os") |
| 4 | + @test pyeq(Bool, sys.__name__, "sys") |
| 5 | + @test pyeq(Bool, os.__name__, "os") |
| 6 | + sysos = pyimport("sys", "os") |
| 7 | + @test sysos isa Tuple{Py, Py} |
| 8 | + @test pyis(sysos[1], sys) |
| 9 | + @test pyis(sysos[2], os) |
| 10 | + ver = pyimport("sys" => "version") |
| 11 | + @test pyis(ver, sys.version) |
| 12 | + path = pyimport("sys" => "path") |
| 13 | + @test pyis(path, sys.path) |
| 14 | + verpath = pyimport("sys" => ("version", "path")) |
| 15 | + @test verpath isa Tuple{Py, Py} |
| 16 | + @test pyis(verpath[1], ver) |
| 17 | + @test pyis(verpath[2], path) |
| 18 | +end |
| 19 | + |
| 20 | +@testset "consts" begin |
| 21 | + @test pybuiltins.None isa Py |
| 22 | + @test pystr(String, pybuiltins.None) == "None" |
| 23 | +end |
| 24 | + |
| 25 | +@testset "str" begin |
| 26 | + @test pyisinstance(pystr("foo"), pybuiltins.str) |
| 27 | + @test pyeq(Bool, pystr(pystr("foo")), pystr("foo")) |
| 28 | + @test pyeq(Bool, pystr(SubString("foobarbaz", 4:6)), pystr("bar")) |
| 29 | + @test pyeq(Bool, pystr('x'), pystr("x")) |
| 30 | + @test pystr(String, pybuiltins.None) === "None" |
| 31 | + @test pystr(String, pyint(123)) === "123" |
| 32 | + @test pystr(String, pystr("foo")) === "foo" |
| 33 | +end |
| 34 | + |
| 35 | +@testset "bytes" begin |
| 36 | + @test pyisinstance(pybytes(UInt8[1,2,3]), pybuiltins.bytes) |
| 37 | + @test pyeq(Bool, pybytes(pylist([1,2,3])), pybytes(UInt8[1,2,3])) |
| 38 | + @test pyeq(Bool, pybytes(b"foo"), pystr("foo").encode("ascii")) |
| 39 | + @test pyeq(Bool, pybytes(codeunits(SubString("foobarbaz", 4:6))), pystr("bar").encode("ascii")) |
| 40 | + @test pybytes(Vector, pylist([1,2,3])) == UInt8[1,2,3] |
| 41 | + @test pybytes(Vector{UInt8}, pylist([1,2,3])) == UInt8[1,2,3] |
| 42 | + @test pybytes(Base.CodeUnits, pystr("foo").encode("ascii")) == b"foo" |
| 43 | + @test pybytes(Base.CodeUnits{UInt8,String}, pystr("bar").encode("ascii")) == b"bar" |
| 44 | +end |
| 45 | + |
| 46 | +@testset "tuple" begin |
| 47 | + z = pytuple() |
| 48 | + @test pyisinstance(z, pybuiltins.tuple) |
| 49 | + @test pylen(z) == 0 |
| 50 | + x = pytuple((1,2,3)) |
| 51 | + @test pyisinstance(x, pybuiltins.tuple) |
| 52 | + @test pylen(x) == 3 |
| 53 | + @test pyeq(Bool, pygetitem(x, 0), 1) |
| 54 | + @test pyeq(Bool, pygetitem(x, 1), 2) |
| 55 | + @test pyeq(Bool, pygetitem(x, 2), 3) |
| 56 | + @test pyeq(Bool, pytuple([1,2,3]), x) |
| 57 | + @test pyeq(Bool, pytuple(i+1 for i in 0:10 if i<3), x) |
| 58 | + @test pyeq(Bool, pytuple(pytuple((1,2,3))), x) |
| 59 | + @test pyeq(Bool, pytuple(pylist([1,2,3])), x) |
| 60 | +end |
| 61 | + |
| 62 | +@testset "list" begin |
| 63 | + z = pylist() |
| 64 | + @test pyisinstance(z, pybuiltins.list) |
| 65 | + @test pylen(z) == 0 |
| 66 | + x = pylist((1,2,3)) |
| 67 | + @test pyisinstance(x, pybuiltins.list) |
| 68 | + @test pylen(x) == 3 |
| 69 | + @test pyeq(Bool, pygetitem(x, 0), 1) |
| 70 | + @test pyeq(Bool, pygetitem(x, 1), 2) |
| 71 | + @test pyeq(Bool, pygetitem(x, 2), 3) |
| 72 | + @test pyeq(Bool, pylist([1,2,3]), x) |
| 73 | + @test pyeq(Bool, pylist(i+1 for i in 0:10 if i<3), x) |
| 74 | + @test pyeq(Bool, pylist(pylist((1,2,3))), x) |
| 75 | + @test pyeq(Bool, pylist(pytuple([1,2,3])), x) |
| 76 | + @test pyeq(Bool, pycollist([1,2,3]), pylist([1,2,3])) |
| 77 | + @test pyeq(Bool, pycollist([1 2; 3 4]), pylist((pylist([1,3]), pylist([2,4])))) |
| 78 | + @test pyeq(Bool, pyrowlist([1,2,3]), pylist([1,2,3])) |
| 79 | + @test pyeq(Bool, pyrowlist([1 2; 3 4]), pylist((pylist([1,2]), pylist([3,4])))) |
| 80 | +end |
| 81 | + |
| 82 | +@testset "dict" begin |
| 83 | + z = pydict() |
| 84 | + @test pyisinstance(z, pybuiltins.dict) |
| 85 | + @test pylen(z) == 0 |
| 86 | + x = pydict(foo=1, bar=2) |
| 87 | + @test pyisinstance(x, pybuiltins.dict) |
| 88 | + @test pylen(x) == 2 |
| 89 | + @test pyeq(Bool, pygetitem(x, "foo"), 1) |
| 90 | + @test pyeq(Bool, pygetitem(x, "bar"), 2) |
| 91 | + @test pyeq(Bool, pydict(["foo"=>1, "bar"=>2]), x) |
| 92 | + @test pyeq(Bool, pydict([("foo"=>1), ("bar"=>2)]), x) |
| 93 | + @test pyeq(Bool, pydict(Dict("foo"=>1, "bar"=>2)), x) |
| 94 | + @test pyeq(Bool, pydict((foo=1, bar=2)), x) |
| 95 | + @test pyeq(Bool, pydict(x), x) |
| 96 | +end |
| 97 | + |
| 98 | +@testset "bool" begin |
| 99 | + @test pyis(pybool(), pybuiltins.False) |
| 100 | + @test pyis(pybool(false), pybuiltins.False) |
| 101 | + @test pyis(pybool(true), pybuiltins.True) |
| 102 | + @test pyis(pybool(0.0), pybuiltins.False) |
| 103 | + @test pyis(pybool(-1.2), pybuiltins.True) |
| 104 | + @test pyis(pybool(pybuiltins.None), pybuiltins.False) |
| 105 | + @test pyis(pybool(pylist()), pybuiltins.False) |
| 106 | + @test pyis(pybool(pylist([1,2,3])), pybuiltins.True) |
| 107 | +end |
| 108 | + |
| 109 | +@testset "int" begin |
| 110 | + @test pyisinstance(pyint(), pybuiltins.int) |
| 111 | + @test pystr(String, pyint()) == "0" |
| 112 | + x = 123 |
| 113 | + y = pyint(x) |
| 114 | + @test pyisinstance(y, pybuiltins.int) |
| 115 | + @test pystr(String, y) == string(x) |
| 116 | + x = BigInt(123) << 200 |
| 117 | + y = pyint(x) |
| 118 | + @test pyisinstance(y, pybuiltins.int) |
| 119 | + @test pystr(String, y) == string(x) |
| 120 | + x = UInt(123) |
| 121 | + y = pyint(x) |
| 122 | + @test pyisinstance(y, pybuiltins.int) |
| 123 | + @test pystr(String, y) == string(x) |
| 124 | + x = UInt128(123) << 100 |
| 125 | + y = pyint(x) |
| 126 | + @test pyisinstance(y, pybuiltins.int) |
| 127 | + @test pystr(String, y) == string(x) |
| 128 | + @test pyeq(Bool, pyint(pyint(123)), pyint(123)) |
| 129 | + @test pyeq(Bool, pyint(pyfloat(12.3)), pyint(12)) |
| 130 | +end |
| 131 | + |
| 132 | +@testset "float" begin |
| 133 | + y = pyfloat() |
| 134 | + @test pyisinstance(y, pybuiltins.float) |
| 135 | + @test pyeq(Bool, y, pyint(0)) |
| 136 | + x = 123 |
| 137 | + y = pyfloat(x) |
| 138 | + @test pyisinstance(y, pybuiltins.float) |
| 139 | + @test pyeq(Bool, y, pyint(x)) |
| 140 | + x = 0.25 |
| 141 | + y = pyfloat(x) |
| 142 | + @test pyisinstance(y, pybuiltins.float) |
| 143 | + @test pyeq(Bool, y, pytruediv(1, 4)) |
| 144 | + x = 1//4 |
| 145 | + y = pyfloat(x) |
| 146 | + @test pyisinstance(y, pybuiltins.float) |
| 147 | + @test pyeq(Bool, y, pyfloat(float(x))) |
| 148 | + @test pyeq(Bool, pyfloat(pyfloat(12.3)), pyfloat(12.3)) |
| 149 | + @test pyeq(Bool, pyfloat(pyint(123)), pyfloat(123)) |
| 150 | +end |
| 151 | + |
| 152 | +@testset "complex" begin |
| 153 | + y = pycomplex() |
| 154 | + @test pyisinstance(y, pybuiltins.complex) |
| 155 | + @test pyeq(Bool, y, pyint(0)) |
| 156 | + x = 12.3 |
| 157 | + y = pycomplex(x) |
| 158 | + @test pyisinstance(y, pybuiltins.complex) |
| 159 | + @test pyeq(Bool, y, pyfloat(x)) |
| 160 | + xr, xi = 12, 34 |
| 161 | + y = pycomplex(xr, xi) |
| 162 | + @test pyisinstance(y, pybuiltins.complex) |
| 163 | + @test pyeq(Bool, y.real, pyfloat(xr)) |
| 164 | + @test pyeq(Bool, y.imag, pyfloat(xi)) |
| 165 | + x = Complex(12, 34) |
| 166 | + y = pycomplex(x) |
| 167 | + @test pyisinstance(y, pybuiltins.complex) |
| 168 | + @test pyeq(Bool, y.real, pyfloat(real(x))) |
| 169 | + @test pyeq(Bool, y.imag, pyfloat(imag(x))) |
| 170 | + @test pyeq(Bool, pycomplex(y), y) |
| 171 | + @test pyeq(Bool, pycomplex(pyint(12), pyint(34)), y) |
| 172 | +end |
| 173 | + |
| 174 | +@testset "set" begin |
| 175 | + y = pyset() |
| 176 | + yf = pyfrozenset() |
| 177 | + @test pyisinstance(y, pybuiltins.set) |
| 178 | + @test pylen(y) == 0 |
| 179 | + @test pyisinstance(yf, pybuiltins.frozenset) |
| 180 | + @test pylen(yf) == 0 |
| 181 | + @test pyeq(Bool, y, yf) |
| 182 | + x = [1,2,3,2,1] |
| 183 | + y = pyset(x) |
| 184 | + yf = pyfrozenset(x) |
| 185 | + @test pyisinstance(y, pybuiltins.set) |
| 186 | + @test pylen(y) == 3 |
| 187 | + @test pycontains(y, 1) |
| 188 | + @test pycontains(y, 2) |
| 189 | + @test pycontains(y, 3) |
| 190 | + @test pyeq(Bool, pyset(y), y) |
| 191 | + @test pyisinstance(yf, pybuiltins.frozenset) |
| 192 | + @test pylen(yf) == 3 |
| 193 | + @test pycontains(yf, 1) |
| 194 | + @test pycontains(yf, 2) |
| 195 | + @test pycontains(yf, 3) |
| 196 | + @test pyeq(Bool, pyfrozenset(y), y) |
| 197 | + @test pyeq(Bool, y, yf) |
| 198 | +end |
| 199 | + |
| 200 | +@testset "slice" begin |
| 201 | + x = pyslice(12) |
| 202 | + @test pyisinstance(x, pybuiltins.slice) |
| 203 | + @test pyeq(Bool, x.start, pybuiltins.None) |
| 204 | + @test pyeq(Bool, x.stop, 12) |
| 205 | + @test pyeq(Bool, x.step, pybuiltins.None) |
| 206 | + x = pyslice(12, 34) |
| 207 | + @test pyisinstance(x, pybuiltins.slice) |
| 208 | + @test pyeq(Bool, x.start, 12) |
| 209 | + @test pyeq(Bool, x.stop, 34) |
| 210 | + @test pyeq(Bool, x.step, pybuiltins.None) |
| 211 | + x = pyslice(12, 34, 56) |
| 212 | + @test pyisinstance(x, pybuiltins.slice) |
| 213 | + @test pyeq(Bool, x.start, 12) |
| 214 | + @test pyeq(Bool, x.stop, 34) |
| 215 | + @test pyeq(Bool, x.step, 56) |
| 216 | +end |
| 217 | + |
| 218 | +@testset "range" begin |
| 219 | + x = pyrange(123) |
| 220 | + @test pyisinstance(x, pybuiltins.range) |
| 221 | + @test pyeq(Bool, x.start, 0) |
| 222 | + @test pyeq(Bool, x.stop, 123) |
| 223 | + @test pyeq(Bool, x.step, 1) |
| 224 | + x = pyrange(12, 123) |
| 225 | + @test pyisinstance(x, pybuiltins.range) |
| 226 | + @test pyeq(Bool, x.start, 12) |
| 227 | + @test pyeq(Bool, x.stop, 123) |
| 228 | + @test pyeq(Bool, x.step, 1) |
| 229 | + x = pyrange(12, 123, 3) |
| 230 | + @test pyisinstance(x, pybuiltins.range) |
| 231 | + @test pyeq(Bool, x.start, 12) |
| 232 | + @test pyeq(Bool, x.stop, 123) |
| 233 | + @test pyeq(Bool, x.step, 3) |
| 234 | +end |
| 235 | + |
| 236 | +@testset "none" begin |
| 237 | +end |
| 238 | + |
| 239 | +@testset "type" begin |
| 240 | + x = pytype(pyint()) |
| 241 | + @test pyisinstance(x, pybuiltins.type) |
| 242 | + @test pyis(x, pybuiltins.int) |
| 243 | + x = pytype(pybuiltins.type) |
| 244 | + @test pyisinstance(x, pybuiltins.type) |
| 245 | + @test pyis(x, pybuiltins.type) |
| 246 | + x = pytype("Foo", (), (foo=1, bar=2)) |
| 247 | + @test pyisinstance(x, pybuiltins.type) |
| 248 | + @test pyeq(Bool, x.__name__, "Foo") |
| 249 | + @test pyeq(Bool, x.foo, 1) |
| 250 | + @test pyeq(Bool, x.bar, 2) |
| 251 | + x = pyclass("Bar", (), foo=3, bar=4) |
| 252 | + @test pyisinstance(x, pybuiltins.type) |
| 253 | + @test pyeq(Bool, x.__name__, "Bar") |
| 254 | + @test pyeq(Bool, x.foo, 3) |
| 255 | + @test pyeq(Bool, x.bar, 4) |
| 256 | +end |
| 257 | + |
| 258 | +@testset "fraction" begin |
| 259 | + # TODO |
| 260 | +end |
| 261 | + |
| 262 | +@testset "method" begin |
| 263 | + # TODO |
| 264 | +end |
| 265 | + |
| 266 | +@testset "datetime" begin |
| 267 | + # TODO |
| 268 | +end |
| 269 | + |
| 270 | +@testset "code" begin |
| 271 | + # TODO |
| 272 | +end |
0 commit comments