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
2e76daa5
Commit
2e76daa5
authored
Nov 15, 2016
by
Jason Rhinelander
Committed by
Wenzel Jakob
Nov 15, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable testing for <optional> when available (#501)
parent
425b4970
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
6 deletions
+38
-6
tests/test_python_types.cpp
+19
-4
tests/test_python_types.py
+19
-2
No files found.
tests/test_python_types.cpp
View file @
2e76daa5
...
@@ -290,11 +290,10 @@ test_initializer python_types([](py::module &m) {
...
@@ -290,11 +290,10 @@ test_initializer python_types([](py::module &m) {
return
d
;
return
d
;
});
});
// this only tests std::experimental::optional for now
bool
has_optional
=
false
,
has_exp_optional
=
false
;
bool
has_optional
=
false
;
#ifdef PYBIND11_HAS_OPTIONAL
#ifdef PYBIND11_HAS_EXP_OPTIONAL
has_optional
=
true
;
has_optional
=
true
;
using
opt_int
=
std
::
experimental
::
optional
<
int
>
;
using
opt_int
=
std
::
optional
<
int
>
;
m
.
def
(
"double_or_zero"
,
[](
const
opt_int
&
x
)
->
int
{
m
.
def
(
"double_or_zero"
,
[](
const
opt_int
&
x
)
->
int
{
return
x
.
value_or
(
0
)
*
2
;
return
x
.
value_or
(
0
)
*
2
;
});
});
...
@@ -303,7 +302,23 @@ test_initializer python_types([](py::module &m) {
...
@@ -303,7 +302,23 @@ test_initializer python_types([](py::module &m) {
});
});
m
.
def
(
"test_nullopt"
,
[](
opt_int
x
)
{
m
.
def
(
"test_nullopt"
,
[](
opt_int
x
)
{
return
x
.
value_or
(
42
);
return
x
.
value_or
(
42
);
},
py
::
arg_v
(
"x"
,
std
::
nullopt
,
"None"
));
#endif
#ifdef PYBIND11_HAS_EXP_OPTIONAL
has_exp_optional
=
true
;
using
opt_int
=
std
::
experimental
::
optional
<
int
>
;
m
.
def
(
"double_or_zero_exp"
,
[](
const
opt_int
&
x
)
->
int
{
return
x
.
value_or
(
0
)
*
2
;
});
m
.
def
(
"half_or_none_exp"
,
[](
int
x
)
->
opt_int
{
return
x
?
opt_int
(
x
/
2
)
:
opt_int
();
});
m
.
def
(
"test_nullopt_exp"
,
[](
opt_int
x
)
{
return
x
.
value_or
(
42
);
},
py
::
arg_v
(
"x"
,
std
::
experimental
::
nullopt
,
"None"
));
},
py
::
arg_v
(
"x"
,
std
::
experimental
::
nullopt
,
"None"
));
#endif
#endif
m
.
attr
(
"has_optional"
)
=
py
::
cast
(
has_optional
);
m
.
attr
(
"has_optional"
)
=
py
::
cast
(
has_optional
);
m
.
attr
(
"has_exp_optional"
)
=
py
::
cast
(
has_exp_optional
);
});
});
tests/test_python_types.py
View file @
2e76daa5
import
pytest
import
pytest
from
pybind11_tests
import
ExamplePythonTypes
,
ConstructorStats
,
has_optional
from
pybind11_tests
import
ExamplePythonTypes
,
ConstructorStats
,
has_optional
,
has_exp_optional
def
test_static
():
def
test_static
():
...
@@ -297,7 +297,7 @@ def test_accessors():
...
@@ -297,7 +297,7 @@ def test_accessors():
assert
d
[
"var"
]
==
99
assert
d
[
"var"
]
==
99
@pytest.mark.skipif
(
not
has_optional
,
reason
=
'no <
experimental/
optional>'
)
@pytest.mark.skipif
(
not
has_optional
,
reason
=
'no <optional>'
)
def
test_optional
():
def
test_optional
():
from
pybind11_tests
import
double_or_zero
,
half_or_none
,
test_nullopt
from
pybind11_tests
import
double_or_zero
,
half_or_none
,
test_nullopt
...
@@ -313,3 +313,20 @@ def test_optional():
...
@@ -313,3 +313,20 @@ def test_optional():
assert
test_nullopt
(
None
)
==
42
assert
test_nullopt
(
None
)
==
42
assert
test_nullopt
(
42
)
==
42
assert
test_nullopt
(
42
)
==
42
assert
test_nullopt
(
43
)
==
43
assert
test_nullopt
(
43
)
==
43
@pytest.mark.skipif
(
not
has_exp_optional
,
reason
=
'no <experimental/optional>'
)
def
test_exp_optional
():
from
pybind11_tests
import
double_or_zero_exp
,
half_or_none_exp
,
test_nullopt_exp
assert
double_or_zero_exp
(
None
)
==
0
assert
double_or_zero_exp
(
42
)
==
84
pytest
.
raises
(
TypeError
,
double_or_zero_exp
,
'foo'
)
assert
half_or_none_exp
(
0
)
is
None
assert
half_or_none_exp
(
42
)
==
21
pytest
.
raises
(
TypeError
,
half_or_none_exp
,
'foo'
)
assert
test_nullopt_exp
()
==
42
assert
test_nullopt_exp
(
None
)
==
42
assert
test_nullopt_exp
(
42
)
==
42
assert
test_nullopt_exp
(
43
)
==
43
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