|
| 1 | +@testset "object" begin |
| 2 | + @testset "pyis" begin |
| 3 | + x = pylist() |
| 4 | + y = Py(x) |
| 5 | + z = pylist() |
| 6 | + @test pyis(x, x) |
| 7 | + @test pyis(x, y) |
| 8 | + @test !pyis(x, z) |
| 9 | + end |
| 10 | + @testset "pyrepr" begin |
| 11 | + @test pyrepr(String, pybuiltins.None) == "None" |
| 12 | + @test pyrepr(String, pyint(123)) == "123" |
| 13 | + @test pyrepr(String, "hello") == "'hello'" |
| 14 | + end |
| 15 | + @testset "pyascii" begin |
| 16 | + @test pyascii(String, pybuiltins.None) == "None" |
| 17 | + @test pyascii(String, pyint(234)) == "234" |
| 18 | + @test pyascii(String, "hello") == "'hello'" |
| 19 | + end |
| 20 | + @testset "pyhasattr" begin |
| 21 | + @test pyhasattr(pybuiltins.None, "__class__") |
| 22 | + @test !pyhasattr(pybuiltins.None, "not_an_attr") |
| 23 | + end |
| 24 | + @testset "pygetattr" begin |
| 25 | + @test pyisinstance(pygetattr(pybuiltins.None, "__class__"), pybuiltins.type) |
| 26 | + @test_throws PyException pygetattr(pybuiltins.None, "not_an_attr") |
| 27 | + @test pyis(pygetattr(pybuiltins.None, "not_an_attr", pybuiltins.None), pybuiltins.None) |
| 28 | + end |
| 29 | + @testset "pysetattr" begin |
| 30 | + x = pytype("Foo", (), ())() |
| 31 | + @test_throws PyException pysetattr(pybuiltins.None, "not_an_attr", 123) |
| 32 | + pysetattr(x, "foo", 123) |
| 33 | + @test pyeq(Bool, pygetattr(x, "foo"), 123) |
| 34 | + end |
| 35 | + @testset "pydelattr" begin |
| 36 | + x = pytype("Foo", (), ())() |
| 37 | + @test !pyhasattr(x, "foo") |
| 38 | + pysetattr(x, "foo", 123) |
| 39 | + @test pyhasattr(x, "foo") |
| 40 | + pydelattr(x, "foo") |
| 41 | + @test !pyhasattr(x, "foo") |
| 42 | + @test_throws PyException pydelattr(pybuiltins.None, "__class__") |
| 43 | + @test_throws PyException pydelattr(pybuiltins.None, "not_an_attr") |
| 44 | + end |
| 45 | + @testset "pyissubclass" begin |
| 46 | + @test pyissubclass(pybuiltins.object, pybuiltins.object) |
| 47 | + @test pyissubclass(pybuiltins.int, pybuiltins.object) |
| 48 | + @test pyissubclass(pybuiltins.bool, pybuiltins.object) |
| 49 | + @test pyissubclass(pybuiltins.bool, pybuiltins.int) |
| 50 | + @test !pyissubclass(pybuiltins.int, pybuiltins.bool) |
| 51 | + @test !pyissubclass(pybuiltins.object, pybuiltins.int) |
| 52 | + @test !pyissubclass(pybuiltins.object, pybuiltins.bool) |
| 53 | + @test_throws PyException pyissubclass(pybuiltins.None, pybuiltins.None) |
| 54 | + @test_throws PyException pyissubclass(pybuiltins.object, pybuiltins.None) |
| 55 | + @test_throws PyException pyissubclass(pybuiltins.None, pybuiltins.object) |
| 56 | + end |
| 57 | + @testset "pyisinstance" begin |
| 58 | + o = pybuiltins.object() |
| 59 | + i = pybuiltins.int() |
| 60 | + b = pybuiltins.bool() |
| 61 | + @test pyisinstance(o, pybuiltins.object) |
| 62 | + @test pyisinstance(i, pybuiltins.object) |
| 63 | + @test pyisinstance(b, pybuiltins.object) |
| 64 | + @test pyisinstance(b, pybuiltins.int) |
| 65 | + @test !pyisinstance(i, pybuiltins.bool) |
| 66 | + @test !pyisinstance(o, pybuiltins.int) |
| 67 | + @test !pyisinstance(o, pybuiltins.bool) |
| 68 | + @test_throws PyException pyisinstance(o, pybuiltins.None) |
| 69 | + end |
| 70 | + @testset "pyhash" begin |
| 71 | + @test pyhash(pybuiltins.None) isa Integer |
| 72 | + @test pyhash(pybuiltins.None) == pyhash(pybuiltins.None) |
| 73 | + @test pyhash(pytuple((1, 2, 3))) == pyhash(pytuple((1, 2, 3))) |
| 74 | + end |
| 75 | + @testset "pytruth" begin |
| 76 | + @test !pytruth(pybuiltins.None) |
| 77 | + @test pytruth(pybuiltins.object) |
| 78 | + for n in -2:2 |
| 79 | + @test pytruth(pyint(n)) == (n != 0) |
| 80 | + end |
| 81 | + for n in 0:3 |
| 82 | + @test pytruth(pylist(zeros(n))) == (n != 0) |
| 83 | + end |
| 84 | + end |
| 85 | + @testset "pynot" begin |
| 86 | + @test pynot(pybuiltins.None) |
| 87 | + @test !pynot(pybuiltins.int) |
| 88 | + for n in -2:2 |
| 89 | + @test pynot(pyfloat(n)) == (n == 0) |
| 90 | + end |
| 91 | + for n in 0:3 |
| 92 | + @test pynot(pylist(zeros(n))) == (n == 0) |
| 93 | + end |
| 94 | + end |
| 95 | + @testset "pylen" begin |
| 96 | + for n in 0:3 |
| 97 | + @test pylen(pytuple(zeros(n))) == n |
| 98 | + @test pylen(pylist(zeros(n))) == n |
| 99 | + end |
| 100 | + @test_throws PyException pylen(pybuiltins.None) |
| 101 | + @test_throws PyException pylen(pyint(0)) |
| 102 | + end |
| 103 | + @testset "pygetitem" begin |
| 104 | + x = pyrange(3) |
| 105 | + @test_throws PyException pygetitem(x, pybuiltins.None) |
| 106 | + @test_throws PyException pygetitem(x, 3) |
| 107 | + @test_throws PyException pygetitem(x, -4) |
| 108 | + @test pyeq(Bool, pygetitem(x, 0), 0) |
| 109 | + @test pyeq(Bool, pygetitem(x, 1), 1) |
| 110 | + @test pyeq(Bool, pygetitem(x, 2), 2) |
| 111 | + @test pyeq(Bool, pygetitem(x, -1), 2) |
| 112 | + @test pyeq(Bool, pygetitem(x, -2), 1) |
| 113 | + @test pyeq(Bool, pygetitem(x, -3), 0) |
| 114 | + end |
| 115 | + @testset "pysetitem" begin |
| 116 | + x = pydict() |
| 117 | + @test_throws PyException pygetitem(x, "foo") |
| 118 | + pysetitem(x, "foo", 123) |
| 119 | + @test pyeq(Bool, pygetitem(x, "foo"), 123) |
| 120 | + pysetitem(x, "foo", 0) |
| 121 | + @test pyeq(Bool, pygetitem(x, "foo"), 0) |
| 122 | + end |
| 123 | + @testset "pydelitem" begin |
| 124 | + x = pydict() |
| 125 | + pysetitem(x, "foo", 123) |
| 126 | + @test pylen(x) == 1 |
| 127 | + pydelitem(x, "foo") |
| 128 | + @test pylen(x) == 0 |
| 129 | + end |
| 130 | + @testset "pydir" begin |
| 131 | + x = pytype("Foo", (), (foo=1, bar=2))() |
| 132 | + d = pydir(x) |
| 133 | + @test pycontains(d, "__class__") |
| 134 | + @test pycontains(d, "foo") |
| 135 | + @test pycontains(d, "bar") |
| 136 | + @test !pycontains(d, "baz") |
| 137 | + end |
| 138 | + @testset "pycall" begin |
| 139 | + @test pyeq(Bool, pycall(pybuiltins.int), 0) |
| 140 | + @test_throws PyException pycall(pybuiltins.None) |
| 141 | + @test pyeq(Bool, pycall(pybuiltins.int, "10"), 10) |
| 142 | + @test_throws PyException pycall(pybuiltins.int, "10", "20") |
| 143 | + @test pyeq(Bool, pycall(pybuiltins.int, "10", base=16), 16) |
| 144 | + @test_throws PyException pycall(pybuiltins.int, "10", base="16") |
| 145 | + @test_throws PyException pycall(pybuiltins.int, "10", bad_argument=0) |
| 146 | + end |
| 147 | + @testset "pyeq" begin |
| 148 | + @test pyis(pyeq(pybuiltins.None, pybuiltins.None), pybuiltins.True) |
| 149 | + @test pyeq(Bool, pybuiltins.None, pybuiltins.None) |
| 150 | + @test pyis(pyeq(pybuiltins.None, pybuiltins.int), pybuiltins.False) |
| 151 | + @test !pyeq(Bool, pybuiltins.None, pybuiltins.int) |
| 152 | + end |
| 153 | + @testset "pyne" begin |
| 154 | + @test pyis(pyne(pybuiltins.None, pybuiltins.None), pybuiltins.False) |
| 155 | + @test !pyne(Bool, pybuiltins.None, pybuiltins.None) |
| 156 | + @test pyis(pyne(pybuiltins.None, pybuiltins.int), pybuiltins.True) |
| 157 | + @test pyne(Bool, pybuiltins.None, pybuiltins.int) |
| 158 | + end |
| 159 | + @testset "pylt" begin |
| 160 | + for a in -1:1 |
| 161 | + for b in -1:1 |
| 162 | + @test pylt(Bool, pyint(a), pyint(b)) == (a < b) |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + @testset "pyle" begin |
| 167 | + for a in -1:1 |
| 168 | + for b in -1:1 |
| 169 | + @test pyle(Bool, pyint(a), pyint(b)) == (a ≤ b) |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + @testset "pygt" begin |
| 174 | + for a in -1:1 |
| 175 | + for b in -1:1 |
| 176 | + @test pygt(Bool, pyint(a), pyint(b)) == (a > b) |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + @testset "pyge" begin |
| 181 | + for a in -1:1 |
| 182 | + for b in -1:1 |
| 183 | + @test pyge(Bool, pyint(a), pyint(b)) == (a ≥ b) |
| 184 | + end |
| 185 | + end |
| 186 | + end |
| 187 | + @testset "pycontains" begin |
| 188 | + x = pyrange(3) |
| 189 | + @test pycontains(x, 0) |
| 190 | + @test pycontains(x, 1) |
| 191 | + @test pycontains(x, 2) |
| 192 | + @test !pycontains(x, 3) |
| 193 | + @test !pycontains(x, -1) |
| 194 | + @test !pycontains(x, pybuiltins.None) |
| 195 | + end |
| 196 | + @testset "pyin" begin |
| 197 | + x = pyrange(3) |
| 198 | + @test pyin(0, x) |
| 199 | + @test pyin(1, x) |
| 200 | + @test pyin(2, x) |
| 201 | + @test !pyin(3, x) |
| 202 | + @test !pyin(-1, x) |
| 203 | + @test !pyin(pybuiltins.None, x) |
| 204 | + end |
| 205 | +end |
0 commit comments