Skip to content

Commit d6f9d64

Browse files
committed
tests/class_reverse_op: Test for reverse arith ops special methods.
This test should be run only if support for reverse ops is enabled, so the corresponding feature_check is added to run-tests.
1 parent eb84a83 commit d6f9d64

5 files changed

Lines changed: 35 additions & 0 deletions

File tree

ports/qemu-arm/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
1919
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
2020
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
21+
#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1)
2122
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
2223
#define MICROPY_PY_BUILTINS_FROZENSET (1)
2324
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)

tests/basics/class_reverse_op.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class A:
2+
3+
def __init__(self, v):
4+
self.v = v
5+
6+
def __add__(self, o):
7+
if isinstance(o, A):
8+
return A(self.v + o.v)
9+
return A(self.v + o)
10+
11+
def __radd__(self, o):
12+
return A(self.v + o)
13+
14+
def __repr__(self):
15+
return "A(%s)" % self.v
16+
17+
print(A(3) + 1)
18+
print(2 + A(5))

tests/feature_check/reverse_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo:
2+
3+
def __radd__(self, other):
4+
pass
5+
6+
try:
7+
5 + Foo()
8+
except TypeError:
9+
print("TypeError")

tests/feature_check/reverse_ops.py.exp

Whitespace-only changes.

tests/run-tests

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def run_tests(pyb, tests, args, base_path="."):
207207
skip_set_type = False
208208
skip_async = False
209209
skip_const = False
210+
skip_revops = False
210211

211212
# Check if micropython.native is supported, and skip such tests if it's not
212213
native = run_feature_check(pyb, args, base_path, 'native_check.py')
@@ -233,6 +234,11 @@ def run_tests(pyb, tests, args, base_path="."):
233234
if native == b'CRASH':
234235
skip_const = True
235236

237+
# Check if __rOP__ special methods are supported, and skip such tests if it's not
238+
native = run_feature_check(pyb, args, base_path, 'reverse_ops.py')
239+
if native == b'TypeError\n':
240+
skip_revops = True
241+
236242
# Check if emacs repl is supported, and skip such tests if it's not
237243
t = run_feature_check(pyb, args, base_path, 'repl_emacs_check.py')
238244
if not 'True' in str(t, 'ascii'):
@@ -360,6 +366,7 @@ def run_tests(pyb, tests, args, base_path="."):
360366
skip_it |= skip_set_type and is_set_type
361367
skip_it |= skip_async and is_async
362368
skip_it |= skip_const and is_const
369+
skip_it |= skip_revops and test_name.startswith("class_reverse_op")
363370

364371
if skip_it:
365372
print("skip ", test_file)

0 commit comments

Comments
 (0)