| 1 | # -*- coding: utf-8 -*- |
| 2 | # <sure - utility belt for automated testing in python> |
| 3 | # Copyright (C) <2010-2018> Gabriel Falcão <[email protected]> |
| 4 | # |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 3 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | |
| 19 | def stub(base_class=None, **attributes): |
| 20 | """creates a python class on-the-fly with the given keyword-arguments |
| 21 | as class-attributes accessible with .attrname. |
| 22 | |
| 23 | The new class inherits from |
| 24 | Use this to mock rather than stub. |
| 25 | """ |
| 26 | if base_class is None: |
| 27 | base_class = object |
| 28 | |
| 29 | members = { |
| 30 | "__init__": lambda self: None, |
| 31 | "__new__": lambda *args, **kw: object.__new__( |
| 32 | *args, *kw |
| 33 | ), # remove __new__ and metaclass behavior from object |
| 34 | "__metaclass__": None, |
| 35 | } |
| 36 | members.update(attributes) |
| 37 | # let's create a python class on-the-fly :) |
| 38 | return type(f"{base_class.__name__}Stub", (base_class,), members)() |