Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
a3daf87d
Commit
a3daf87d
authored
Mar 14, 2020
by
fatvlady
Committed by
Wenzel Jakob
Jun 29, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add failing optional test
parent
8e85fadf
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
0 deletions
+50
-0
tests/test_stl.cpp
+40
-0
tests/test_stl.py
+10
-0
No files found.
tests/test_stl.cpp
View file @
a3daf87d
...
...
@@ -50,6 +50,17 @@ namespace std {
}
template
<
template
<
typename
>
typename
OptionalImpl
,
typename
T
>
struct
OptionalHolder
{
OptionalHolder
()
=
default
;
bool
member_initialized
()
const
{
return
member
&&
member
->
initialized
;
}
OptionalImpl
<
T
>
member
=
T
{};
};
TEST_SUBMODULE
(
stl
,
m
)
{
// test_vector
m
.
def
(
"cast_vector"
,
[]()
{
return
std
::
vector
<
int
>
{
1
};
});
...
...
@@ -154,6 +165,23 @@ TEST_SUBMODULE(stl, m) {
.
def
(
py
::
init
<>
())
.
def
(
py
::
init
<
int
>
());
struct
MoveOutDetector
{
MoveOutDetector
()
=
default
;
MoveOutDetector
(
const
MoveOutDetector
&
)
=
default
;
MoveOutDetector
(
MoveOutDetector
&&
other
)
noexcept
:
initialized
(
other
.
initialized
)
{
// steal underlying resource
other
.
initialized
=
false
;
}
bool
initialized
=
true
;
};
py
::
class_
<
MoveOutDetector
>
(
m
,
"MoveOutDetector"
,
"Class with move tracking"
)
.
def
(
py
::
init
<>
())
.
def_readonly
(
"initialized"
,
&
MoveOutDetector
::
initialized
);
#ifdef PYBIND11_HAS_OPTIONAL
// test_optional
m
.
attr
(
"has_optional"
)
=
true
;
...
...
@@ -175,6 +203,12 @@ TEST_SUBMODULE(stl, m) {
m
.
def
(
"nodefer_none_optional"
,
[](
std
::
optional
<
int
>
)
{
return
true
;
});
m
.
def
(
"nodefer_none_optional"
,
[](
py
::
none
)
{
return
false
;
});
using
opt_holder
=
OptionalHolder
<
std
::
optional
,
MoveOutDetector
>
;
py
::
class_
<
opt_holder
>
(
m
,
"OptionalHolder"
,
"Class with optional member"
)
.
def
(
py
::
init
<>
())
.
def_readonly
(
"member"
,
&
opt_holder
::
member
)
.
def
(
"member_initialized"
,
&
opt_holder
::
member_initialized
);
#endif
#ifdef PYBIND11_HAS_EXP_OPTIONAL
...
...
@@ -195,6 +229,12 @@ TEST_SUBMODULE(stl, m) {
m
.
def
(
"test_no_assign_exp"
,
[](
const
exp_opt_no_assign
&
x
)
{
return
x
?
x
->
value
:
42
;
},
py
::
arg_v
(
"x"
,
std
::
experimental
::
nullopt
,
"None"
));
using
opt_exp_holder
=
OptionalHolder
<
std
::
experimental
::
optional
,
MoveOutDetector
>
;
py
::
class_
<
opt_exp_holder
>
(
m
,
"OptionalExpHolder"
,
"Class with optional member"
)
.
def
(
py
::
init
<>
())
.
def_readonly
(
"member"
,
&
opt_exp_holder
::
member
)
.
def
(
"member_initialized"
,
&
opt_exp_holder
::
member_initialized
);
#endif
#ifdef PYBIND11_HAS_VARIANT
...
...
tests/test_stl.py
View file @
a3daf87d
...
...
@@ -127,6 +127,11 @@ def test_optional():
assert
m
.
nodefer_none_optional
(
None
)
holder
=
m
.
OptionalHolder
()
mvalue
=
holder
.
member
assert
mvalue
.
initialized
assert
holder
.
member_initialized
()
@pytest.mark.skipif
(
not
hasattr
(
m
,
"has_exp_optional"
),
reason
=
'no <experimental/optional>'
)
def
test_exp_optional
():
...
...
@@ -148,6 +153,11 @@ def test_exp_optional():
assert
m
.
test_no_assign_exp
(
m
.
NoAssign
(
43
))
==
43
pytest
.
raises
(
TypeError
,
m
.
test_no_assign_exp
,
43
)
holder
=
m
.
OptionalExpHolder
()
mvalue
=
holder
.
member
assert
mvalue
.
initialized
assert
holder
.
member_initialized
()
@pytest.mark.skipif
(
not
hasattr
(
m
,
"load_variant"
),
reason
=
'no <variant>'
)
def
test_variant
(
doc
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment