-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding dedicated test_const_name. (#3578)
* Adding dedicated test_const_name. Also exercises pybind11::detail::_ backward compatibility. See also: PR #3423 * Backing out tests involving int_to_str (requires C++17 or higher). * Suppressing clang-tidy errors. * Disabling test_const_name for MSVC 2015 due to bizarre failures. * Stacking @pytest.mark.parametrize (thanks to @Skylion007 for pointing out).
- Loading branch information
Ralf W. Grosse-Kunstleve
authored
Dec 29, 2021
1 parent
9b4f71d
commit 1bbaeb3
Showing
5 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2021 The Pybind Development Team. | ||
// All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
#include "pybind11_tests.h" | ||
|
||
#if defined(_MSC_VER) && _MSC_VER < 1910 | ||
|
||
// MSVC 2015 fails in bizarre ways. | ||
# define PYBIND11_SKIP_TEST_CONST_NAME | ||
|
||
#else // Only test with MSVC 2017 or newer. | ||
|
||
// IUT = Implementation Under Test | ||
# define CONST_NAME_TESTS(TEST_FUNC, IUT) \ | ||
std::string TEST_FUNC(int selector) { \ | ||
switch (selector) { \ | ||
case 0: \ | ||
return IUT("").text; \ | ||
case 1: \ | ||
return IUT("A").text; \ | ||
case 2: \ | ||
return IUT("Bd").text; \ | ||
case 3: \ | ||
return IUT("Cef").text; \ | ||
case 4: \ | ||
return IUT<int>().text; /*NOLINT(bugprone-macro-parentheses)*/ \ | ||
case 5: \ | ||
return IUT<std::string>().text; /*NOLINT(bugprone-macro-parentheses)*/ \ | ||
case 6: \ | ||
return IUT<true>("T1", "T2").text; /*NOLINT(bugprone-macro-parentheses)*/ \ | ||
case 7: \ | ||
return IUT<false>("U1", "U2").text; /*NOLINT(bugprone-macro-parentheses)*/ \ | ||
case 8: \ | ||
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \ | ||
return IUT<true>(IUT("D1"), IUT("D2")).text; \ | ||
case 9: \ | ||
/*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \ | ||
return IUT<false>(IUT("E1"), IUT("E2")).text; \ | ||
case 10: \ | ||
return IUT("KeepAtEnd").text; \ | ||
default: \ | ||
break; \ | ||
} \ | ||
throw std::runtime_error("Invalid selector value."); \ | ||
} | ||
|
||
CONST_NAME_TESTS(const_name_tests, py::detail::const_name) | ||
|
||
# ifdef PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY | ||
CONST_NAME_TESTS(underscore_tests, py::detail::_) | ||
# endif | ||
|
||
#endif // MSVC >= 2017 | ||
|
||
TEST_SUBMODULE(const_name, m) { | ||
#ifdef PYBIND11_SKIP_TEST_CONST_NAME | ||
m.attr("const_name_tests") = "PYBIND11_SKIP_TEST_CONST_NAME"; | ||
#else | ||
m.def("const_name_tests", const_name_tests); | ||
#endif | ||
|
||
#ifdef PYBIND11_SKIP_TEST_CONST_NAME | ||
m.attr("underscore_tests") = "PYBIND11_SKIP_TEST_CONST_NAME"; | ||
#elif defined(PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY) | ||
m.def("underscore_tests", underscore_tests); | ||
#else | ||
m.attr("underscore_tests") = "PYBIND11_DETAIL_UNDERSCORE_BACKWARD_COMPATIBILITY not defined."; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
import env | ||
from pybind11_tests import const_name as m | ||
|
||
|
||
@pytest.mark.parametrize("func", (m.const_name_tests, m.underscore_tests)) | ||
@pytest.mark.parametrize( | ||
"selector, expected", | ||
enumerate( | ||
( | ||
"", | ||
"A", | ||
"Bd", | ||
"Cef", | ||
"%", | ||
"%", | ||
"T1", | ||
"U2", | ||
"D1", | ||
"E2", | ||
"KeepAtEnd", | ||
) | ||
), | ||
) | ||
def test_const_name(func, selector, expected): | ||
if isinstance(func, type(u"") if env.PY2 else str): | ||
pytest.skip(func) | ||
text = func(selector) | ||
assert text == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters