xref: /llvm-project/lldb/test/API/lang/cpp/default-template-args/TestDefaultTemplateArgs.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test default template arguments.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestDefaultTemplateArgs(TestBase):
12    @no_debug_info_test
13    def test(self):
14        self.build()
15        lldbutil.run_to_source_breakpoint(
16            self, "// break here", lldb.SBFileSpec("main.cpp")
17        )
18
19        # Declare a template with a template argument that has a default argument.
20        self.expect(
21            "expr --top-level -- template<typename T = int> struct $X { int v; };"
22        )
23
24        # The type we display to the user should omit the argument with the default
25        # value.
26        result = self.expect_expr("$X<> x; x", result_type="$X<>")
27        # The internal name should also always show all arguments (even if they
28        # have their default value).
29        self.assertEqual(result.GetTypeName(), "$X<int>")
30
31        # Test the template but this time specify a non-default value for the
32        # template argument.
33        # Both internal type name and the one we display to the user should
34        # show the non-default value in the type name.
35        result = self.expect_expr("$X<long> x; x", result_type="$X<long>")
36        self.assertEqual(result.GetTypeName(), "$X<long>")
37
38        # Test that the formatters are using the internal type names that
39        # always include all template arguments.
40        self.expect("type summary add '$X<int>' --summary-string 'summary1'")
41        self.expect_expr("$X<> x; x", result_summary="summary1")
42        self.expect("type summary add '$X<long>' --summary-string 'summary2'")
43        self.expect_expr("$X<long> x; x", result_summary="summary2")
44