Skip to content

Commit b5822bb

Browse files
Enum conversion with normalized names (robotframework#3632)
Implements robotframework#3611.
1 parent 6a04aad commit b5822bb

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

atest/testdata/keywords/type_conversion/Annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class MyEnum(Enum):
1212
FOO = 1
1313
bar = 'xxx'
14-
14+
foo = 'yyy'
1515

1616
class Unknown(object):
1717
pass

atest/testdata/keywords/type_conversion/KeywordDecorator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Enum(object):
1818
class MyEnum(Enum):
1919
FOO = 1
2020
bar = 'xxx'
21+
foo = 'yyy'
2122

2223

2324
class Unknown(object):

atest/testdata/keywords/type_conversion/annotations.robot

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,15 @@ Invalid timedelta
165165
Enum
166166

167167
Enum FOO MyEnum.FOO
168-
Enum bar MyEnum.bar
168+
Enum foo MyEnum.foo
169+
Enum b a r MyEnum.bar
170+
Enum BAr MyEnum.bar
171+
Enum B_A_r MyEnum.bar
169172

170173
Invalid Enum
171174
[Template] Conversion Should Fail
172-
Enum foobar type=MyEnum error=MyEnum does not have member 'foobar'. Available: 'FOO' and 'bar'
173-
Enum BAR type=MyEnum error=MyEnum does not have member 'BAR'. Available: 'FOO' and 'bar'
175+
Enum foobar type=MyEnum error=MyEnum does not have member 'foobar'. Available: 'FOO', 'bar' and 'foo'
176+
Enum bar! type=MyEnum error=MyEnum does not have member 'bar!'. Available: 'FOO', 'bar' and 'foo'
174177

175178
NoneType
176179
NoneType None None

atest/testdata/keywords/type_conversion/keyword_decorator.robot

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,16 @@ Invalid timedelta
167167
Enum
168168
[Tags] require-enum
169169
Enum FOO MyEnum.FOO
170-
Enum bar MyEnum.bar
170+
Enum foo MyEnum.foo
171+
Enum b a r MyEnum.bar
172+
Enum BAr MyEnum.bar
173+
Enum B_A_r MyEnum.bar
171174

172175
Invalid Enum
173176
[Tags] require-enum
174177
[Template] Conversion Should Fail
175-
Enum foobar type=MyEnum error=MyEnum does not have member 'foobar'. Available: 'FOO' and 'bar'
176-
Enum BAR type=MyEnum error=MyEnum does not have member 'BAR'. Available: 'FOO' and 'bar'
178+
Enum foobar type=MyEnum error=MyEnum does not have member 'foobar'. Available: 'FOO', 'bar' and 'foo'
179+
Enum bar! type=MyEnum error=MyEnum does not have member 'bar!'. Available: 'FOO', 'bar' and 'foo'
177180

178181
NoneType
179182
NoneType None None

doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,11 +1394,13 @@ case-insensitive.
13941394
+-------------+---------------+------------+----------------------------------------------------------------+--------------------------------------+
13951395
| Enum_ | | | The specified type must be an enumeration (a subclass of | .. sourcecode:: python |
13961396
| | | | Enum_) and arguments themselves must match its members. | |
1397-
| | | | | class Color(Enum): |
1398-
| | | | | RED = 1 |
1397+
| | | | Starting from RF 3.2.2, matching members is case-, underscore- | class Color(Enum): |
1398+
| | | | and whitespace-insensitive. | RED = 1 |
13991399
| | | | | GREEN = 2 |
1400+
| | | | | DARKGREEN = 3 |
14001401
| | | | | |
1401-
| | | | | | `GREEN` |
1402+
| | | | | | `GREEN` (Color.GREEN) |
1403+
| | | | | | `Dark Green` (Color.DARKGREEN) |
14021404
+-------------+---------------+------------+----------------------------------------------------------------+--------------------------------------+
14031405
| NoneType_ | | | String `NONE` (case-insensitively) is converted to `None` | | `None` |
14041406
| | | | object, other values are passed as-is. Mainly relevant when | |

src/robot/running/arguments/typeconverters.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Enum(object):
3030

3131
from robot.libraries.DateTime import convert_date, convert_time
3232
from robot.utils import (FALSE_STRINGS, IRONPYTHON, TRUE_STRINGS, PY_VERSION,
33-
PY2, seq2str, type_name, unicode)
33+
PY2, normalize, seq2str, type_name, unicode)
3434

3535

3636
class TypeConverter(object):
@@ -263,6 +263,10 @@ def _convert(self, value, explicit_type=True):
263263
return getattr(self._enum, value)
264264
except AttributeError:
265265
members = self._get_members(self._enum)
266+
normalized_value = normalize(value, ignore='_')
267+
for member in members:
268+
if normalize(member, ignore='_') == normalized_value:
269+
return getattr(self._enum, member)
266270
raise ValueError("%s does not have member '%s'. Available: %s"
267271
% (self.type_name, value, seq2str(members)))
268272

0 commit comments

Comments
 (0)