-
Notifications
You must be signed in to change notification settings - Fork 2.2k
WIP: Adding string constructor for enum #1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ def test_unscoped_enum(): | |
|
||
assert int(m.UnscopedEnum.ETwo) == 2 | ||
assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo" | ||
assert str(m.UnscopedEnum("ETwo")) == "UnscopedEnum.ETwo" | ||
|
||
# order | ||
assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo | ||
|
@@ -70,8 +71,28 @@ def test_unscoped_enum(): | |
assert not (2 < m.UnscopedEnum.EOne) | ||
|
||
|
||
def test_converstion_enum(): | ||
assert m.test_conversion_enum(m.ConversionEnum.Convert1) == "ConversionEnum::Convert1" | ||
assert m.test_conversion_enum(m.ConversionEnum("Convert1")) == "ConversionEnum::Convert1" | ||
assert m.test_conversion_enum("Convert1") == "ConversionEnum::Convert1" | ||
|
||
|
||
def test_conversion_enum_raises(): | ||
with pytest.raises(ValueError) as excinfo: | ||
m.ConversionEnum("Convert0") | ||
assert str(excinfo.value) == "\"Convert0\" is not a valid value for enum type ConversionEnum" | ||
|
||
|
||
def test_conversion_enum_raises_implicit(): | ||
with pytest.raises(ValueError) as excinfo: | ||
m.test_conversion_enum("Convert0") | ||
assert str(excinfo.value) == "\"Convert0\" is not a valid value for enum type ConversionEnum" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test fails, since it gives a generic message and |
||
|
||
|
||
def test_scoped_enum(): | ||
assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three" | ||
with pytest.raises(TypeError): | ||
m.test_scoped_enum("Three") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a check here against the specific exception message, via: with pytest.raises(TypeError) as excinfo:
# ... the test call
assert str(excinfo.value) == "the expected exception message" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with that is this gives the wrong error message. I need help figuring out why the error message is being swallowed. I've tried stepping through it in Xcode, but finding where exceptions (at least this one) is being caught is tricky. I think it's inside a constructor/destructor somewhere. I think that directly calling the constructor works, but not if it's part of an implicit conversion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests for errors, one that passes (direct construction was okay) and one that fails (implicit construction). |
||
z = m.ScopedEnum.Two | ||
assert m.test_scoped_enum(z) == "ScopedEnum::Two" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't your code, but let's clean it up anyway: instead of
auto m_entries_ptr = m_entries.inc_ref().ptr();
being a raw, incremented pointer, we can change it to:auto entries = m_entries;
. Then all the lambdas (this, plus the others that do areinterpret_borrow
) can just captureentries
and can simplify theirreinterpret_borrow<dict>(m_entries_ptr)
to justentries
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, much nicer the new way. Thanks!