From d6f9d64d97af245d042cbab8438dde25a29c504f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 10 Sep 2017 17:05:31 +0300 Subject: [PATCH] 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. --- ports/qemu-arm/mpconfigport.h | 1 + tests/basics/class_reverse_op.py | 18 ++++++++++++++++++ tests/feature_check/reverse_ops.py | 9 +++++++++ tests/feature_check/reverse_ops.py.exp | 0 tests/run-tests | 7 +++++++ 5 files changed, 35 insertions(+) create mode 100644 tests/basics/class_reverse_op.py create mode 100644 tests/feature_check/reverse_ops.py create mode 100644 tests/feature_check/reverse_ops.py.exp diff --git a/ports/qemu-arm/mpconfigport.h b/ports/qemu-arm/mpconfigport.h index a12165a92a..51706b9277 100644 --- a/ports/qemu-arm/mpconfigport.h +++ b/ports/qemu-arm/mpconfigport.h @@ -18,6 +18,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_CAN_OVERRIDE_BUILTINS (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (1) +#define MICROPY_PY_REVERSE_SPECIAL_METHODS (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_BUILTINS_FROZENSET (1) #define MICROPY_PY_BUILTINS_MEMORYVIEW (1) diff --git a/tests/basics/class_reverse_op.py b/tests/basics/class_reverse_op.py new file mode 100644 index 0000000000..d41c55c9d7 --- /dev/null +++ b/tests/basics/class_reverse_op.py @@ -0,0 +1,18 @@ +class A: + + def __init__(self, v): + self.v = v + + def __add__(self, o): + if isinstance(o, A): + return A(self.v + o.v) + return A(self.v + o) + + def __radd__(self, o): + return A(self.v + o) + + def __repr__(self): + return "A(%s)" % self.v + +print(A(3) + 1) +print(2 + A(5)) diff --git a/tests/feature_check/reverse_ops.py b/tests/feature_check/reverse_ops.py new file mode 100644 index 0000000000..668748bc57 --- /dev/null +++ b/tests/feature_check/reverse_ops.py @@ -0,0 +1,9 @@ +class Foo: + + def __radd__(self, other): + pass + +try: + 5 + Foo() +except TypeError: + print("TypeError") diff --git a/tests/feature_check/reverse_ops.py.exp b/tests/feature_check/reverse_ops.py.exp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/run-tests b/tests/run-tests index 14df1e986d..c9f9efe77d 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -207,6 +207,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_set_type = False skip_async = False skip_const = False + skip_revops = False # Check if micropython.native is supported, and skip such tests if it's not native = run_feature_check(pyb, args, base_path, 'native_check.py') @@ -233,6 +234,11 @@ def run_tests(pyb, tests, args, base_path="."): if native == b'CRASH': skip_const = True + # Check if __rOP__ special methods are supported, and skip such tests if it's not + native = run_feature_check(pyb, args, base_path, 'reverse_ops.py') + if native == b'TypeError\n': + skip_revops = True + # Check if emacs repl is supported, and skip such tests if it's not t = run_feature_check(pyb, args, base_path, 'repl_emacs_check.py') if not 'True' in str(t, 'ascii'): @@ -360,6 +366,7 @@ def run_tests(pyb, tests, args, base_path="."): skip_it |= skip_set_type and is_set_type skip_it |= skip_async and is_async skip_it |= skip_const and is_const + skip_it |= skip_revops and test_name.startswith("class_reverse_op") if skip_it: print("skip ", test_file)