1 //===-- XMLTest.cpp -------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/XML.h"
10 #include "gtest/gtest.h"
12 using namespace lldb_private
;
14 #if LLDB_ENABLE_LIBXML2
16 static void assertGetElement(XMLNode
&root
, const char *element_name
,
17 bool expected_uint_success
, uint64_t expected_uint
,
18 bool expected_double_success
,
19 double expected_double
) {
20 XMLNode node
= root
.FindFirstChildElementWithName(element_name
);
21 ASSERT_TRUE(node
.IsValid());
24 EXPECT_EQ(node
.GetElementTextAsUnsigned(uint_val
, 66, 0),
25 expected_uint_success
);
26 EXPECT_EQ(uint_val
, expected_uint
);
29 EXPECT_EQ(node
.GetElementTextAsFloat(double_val
, 66.0),
30 expected_double_success
);
31 EXPECT_EQ(double_val
, expected_double
);
33 XMLNode attr_node
= root
.FindFirstChildElementWithName("attr");
34 ASSERT_TRUE(node
.IsValid());
37 attr_node
.GetAttributeValueAsUnsigned(element_name
, uint_val
, 66, 0),
38 expected_uint_success
);
39 EXPECT_EQ(uint_val
, expected_uint
);
42 #define ASSERT_GET(element_name, ...) \
44 SCOPED_TRACE("at element/attribute " element_name); \
45 assertGetElement(root, element_name, __VA_ARGS__); \
49 std::string test_xml
=
50 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
53 " <text>123foo</text>\n"
54 " <positive-int>11</positive-int>\n"
55 " <negative-int>-11</negative-int>\n"
56 " <positive-overflow>18446744073709551616</positive-overflow>\n"
57 " <negative-overflow>-9223372036854775809</negative-overflow>\n"
58 " <hex>0x1234</hex>\n"
59 " <positive-float>12.5</positive-float>\n"
60 " <negative-float>-12.5</negative-float>\n"
63 " positive-int=\"11\"\n"
64 " negative-int=\"-11\"\n"
65 " positive-overflow=\"18446744073709551616\"\n"
66 " negative-overflow=\"-9223372036854775809\"\n"
68 " positive-float=\"12.5\"\n"
69 " negative-float=\"-12.5\"\n"
74 ASSERT_TRUE(doc
.ParseMemory(test_xml
.data(), test_xml
.size()));
76 XMLNode root
= doc
.GetRootElement();
77 ASSERT_TRUE(root
.IsValid());
79 ASSERT_GET("empty", false, 66, false, 66.0);
80 ASSERT_GET("text", false, 66, false, 66.0);
81 ASSERT_GET("positive-int", true, 11, true, 11.0);
82 ASSERT_GET("negative-int", false, 66, true, -11.0);
83 ASSERT_GET("positive-overflow", false, 66, true, 18446744073709551616.0);
84 ASSERT_GET("negative-overflow", false, 66, true, -9223372036854775809.0);
85 ASSERT_GET("hex", true, 0x1234, true, 4660.0);
86 ASSERT_GET("positive-float", false, 66, true, 12.5);
87 ASSERT_GET("negative-float", false, 66, true, -12.5);
90 #else // !LLDB_ENABLE_LIBXML2
92 TEST(XML
, GracefulNoXML
) {
93 std::string test_xml
=
94 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
96 " <text attribute=\"123\">123</text>\n"
100 ASSERT_FALSE(doc
.ParseMemory(test_xml
.data(), test_xml
.size()));
102 XMLNode root
= doc
.GetRootElement();
103 EXPECT_FALSE(root
.IsValid());
105 XMLNode node
= root
.FindFirstChildElementWithName("text");
106 EXPECT_FALSE(node
.IsValid());
109 EXPECT_FALSE(node
.GetElementTextAsUnsigned(uint_val
, 66, 0));
110 EXPECT_EQ(uint_val
, 66U);
111 EXPECT_FALSE(node
.GetAttributeValueAsUnsigned("attribute", uint_val
, 66, 0));
112 EXPECT_EQ(uint_val
, 66U);
115 EXPECT_FALSE(node
.GetElementTextAsFloat(double_val
, 66.0));
116 EXPECT_EQ(double_val
, 66.0);
119 #endif // LLDB_ENABLE_LIBXML2