frappy/test/test_lib_enum.py
Enrico Faulhaber 574a66c65b rework EnumType to use better Enum's
unfortunately IntEnum can't be bent like we would need it (extensible).
So we had to write our own....

The members of the Enum still behave like ints, but also have
.name and .value attributes, should they be needed.

needed adoptions to correctly use (and test) the EnumType are included.

Change-Id: Ie019d2f449a244c4fab00554b6c6daaac8948b59
Reviewed-on: https://forge.frm2.tum.de/review/17843
Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de>
Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
2018-06-07 17:57:46 +02:00

81 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
# *****************************************************************************
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors:
# Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
#
# *****************************************************************************
"""test Enum type."""
import sys
sys.path.insert(0, sys.path[0] + '/..')
# no fixtures needed
import pytest
from secop.lib.enum import EnumMember, Enum
def test_EnumMember():
with pytest.raises(TypeError):
a = EnumMember(None, 'name', 'value')
with pytest.raises(TypeError):
a = EnumMember(None, 'name', 1)
e1=Enum('X')
with pytest.raises(ValueError):
a = EnumMember(e1, 'a', 'value')
a = EnumMember(e1, 'a', 1)
with pytest.raises(TypeError):
a.value = 2
with pytest.raises(TypeError):
a.value += 2
with pytest.raises(TypeError):
a += 2
# this shall work
assert 2 == (a + 1) # pylint: disable=C0122
assert (a - 1) == 0
assert a
assert a + a
assert (2 - a) == 1
assert -a == -1 # numeric negation
assert ~a == -2 # bitmask like NOT
assert (a & 3) == 1
assert (a | 6) == 7
assert (a ^ 7) == 6
assert a < 2
assert a > 0
assert a != 3
assert a == 1
def test_Enum():
e1 = Enum('e1')
e2 = Enum('e2', e1, a=1, b=3)
e3 = Enum('e3', e2, c='a')
assert e3.c == 2
with pytest.raises(TypeError):
e2.c = 2
assert e3.a < e2.b
assert e2.b > e3.a
assert e3.c >= e2.a
assert e3.b <= e2.b