brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · a845612 Raw
120 lines · cpp
1//===-- XMLTest.cpp -------------------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Host/XML.h"10#include "gtest/gtest.h"11 12using namespace lldb_private;13 14#if LLDB_ENABLE_LIBXML215 16static 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());22 23  uint64_t uint_val;24  EXPECT_EQ(node.GetElementTextAsUnsigned(uint_val, 66, 0),25            expected_uint_success);26  EXPECT_EQ(uint_val, expected_uint);27 28  double double_val;29  EXPECT_EQ(node.GetElementTextAsFloat(double_val, 66.0),30            expected_double_success);31  EXPECT_EQ(double_val, expected_double);32 33  XMLNode attr_node = root.FindFirstChildElementWithName("attr");34  ASSERT_TRUE(node.IsValid());35 36  EXPECT_EQ(37      attr_node.GetAttributeValueAsUnsigned(element_name, uint_val, 66, 0),38      expected_uint_success);39  EXPECT_EQ(uint_val, expected_uint);40}41 42#define ASSERT_GET(element_name, ...)                                          \43  {                                                                            \44    SCOPED_TRACE("at element/attribute " element_name);                        \45    assertGetElement(root, element_name, __VA_ARGS__);                         \46  }47 48TEST(XML, GetAs) {49  std::string test_xml =50      "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"51      "<test>\n"52      "  <empty/>\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"61      "  <attr empty=\"\"\n"62      "        text=\"123foo\"\n"63      "        positive-int=\"11\"\n"64      "        negative-int=\"-11\"\n"65      "        positive-overflow=\"18446744073709551616\"\n"66      "        negative-overflow=\"-9223372036854775809\"\n"67      "        hex=\"0x1234\"\n"68      "        positive-float=\"12.5\"\n"69      "        negative-float=\"-12.5\"\n"70      "       />\n"71      "</test>\n";72 73  XMLDocument doc;74  ASSERT_TRUE(doc.ParseMemory(test_xml.data(), test_xml.size()));75 76  XMLNode root = doc.GetRootElement();77  ASSERT_TRUE(root.IsValid());78 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);88}89 90#else // !LLDB_ENABLE_LIBXML291 92TEST(XML, GracefulNoXML) {93  std::string test_xml =94      "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"95      "<test>\n"96      "  <text attribute=\"123\">123</text>\n"97      "</test>\n";98 99  XMLDocument doc;100  ASSERT_FALSE(doc.ParseMemory(test_xml.data(), test_xml.size()));101 102  XMLNode root = doc.GetRootElement();103  EXPECT_FALSE(root.IsValid());104 105  XMLNode node = root.FindFirstChildElementWithName("text");106  EXPECT_FALSE(node.IsValid());107 108  uint64_t uint_val;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);113 114  double double_val;115  EXPECT_FALSE(node.GetElementTextAsFloat(double_val, 66.0));116  EXPECT_EQ(double_val, 66.0);117}118 119#endif // LLDB_ENABLE_LIBXML2120