|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "docfx/doxygen2markdown.h" |
| 16 | +#include <gmock/gmock.h> |
| 17 | +#include <sstream> |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +using ::testing::HasSubstr; |
| 22 | +using ::testing::IsEmpty; |
| 23 | +using ::testing::Not; |
| 24 | + |
| 25 | +TEST(Doxygen2Markdown, PlainText) { |
| 26 | + pugi::xml_document doc; |
| 27 | + doc.load_string(R"xml(<?xml version="1.0" standalone="yes"?> |
| 28 | + <doxygen version="1.9.1" xml:lang="en-US"> |
| 29 | + <para id="plain-text">test-only-value 42</para> |
| 30 | + </doxygen>)xml"); |
| 31 | + auto selected = doc.select_node("//*[@id='plain-text']"); |
| 32 | + ASSERT_TRUE(selected); |
| 33 | + std::ostringstream os; |
| 34 | + ASSERT_TRUE(AppendIfPlainText(os, *selected.node().children().begin())); |
| 35 | + EXPECT_EQ(os.str(), "test-only-value 42"); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(Doxygen2Markdown, ComputerOutput) { |
| 39 | + pugi::xml_document doc; |
| 40 | + doc.load_string(R"xml(<?xml version="1.0" standalone="yes"?> |
| 41 | + <doxygen version="1.9.1" xml:lang="en-US"> |
| 42 | + <computeroutput id="test-node">int f() { return 42; }</computeroutput> |
| 43 | + </doxygen>)xml"); |
| 44 | + auto selected = doc.select_node("//*[@id='test-node']"); |
| 45 | + std::ostringstream os; |
| 46 | + ASSERT_TRUE(AppendIfComputerOutput(os, selected.node())); |
| 47 | + EXPECT_EQ(os.str(), "`int f() { return 42; }`"); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(Doxygen2Markdown, Paragraph) { |
| 51 | + pugi::xml_document doc; |
| 52 | + doc.load_string(R"xml(<?xml version="1.0" standalone="yes"?> |
| 53 | + <doxygen version="1.9.1" xml:lang="en-US"> |
| 54 | + <para id='test-node'>Try using <computeroutput id="test-node">int f() { return 42; }</computeroutput> in your code.</para> |
| 55 | + </doxygen>)xml"); |
| 56 | + auto selected = doc.select_node("//*[@id='test-node']"); |
| 57 | + std::ostringstream os; |
| 58 | + ASSERT_TRUE(AppendIfParagraph(os, selected.node())); |
| 59 | + EXPECT_EQ(os.str(), "Try using `int f() { return 42; }` in your code.\n"); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(Doxygen2Markdown, ParagraphWithUnknown) { |
| 63 | + pugi::xml_document doc; |
| 64 | + doc.load_string(R"xml(<?xml version="1.0" standalone="yes"?> |
| 65 | + <doxygen version="1.9.1" xml:lang="en-US"> |
| 66 | + <para id='test-node'>Uh oh: <itemizedlist></itemizedlist></para> |
| 67 | + </doxygen>)xml"); |
| 68 | + auto selected = doc.select_node("//*[@id='test-node']"); |
| 69 | + std::ostringstream os; |
| 70 | + EXPECT_THROW(AppendIfParagraph(os, selected.node()), std::runtime_error); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(Doxygen2Markdown, ParagraphWithUnknownOutput) { |
| 74 | + pugi::xml_document doc; |
| 75 | + doc.load_string(R"xml(<?xml version="1.0" standalone="yes"?> |
| 76 | + <doxygen version="1.9.1" xml:lang="en-US"> |
| 77 | + <para id='test-node'>Uh oh: |
| 78 | + <itemizedlist a1="attr1" a2="attr2"> |
| 79 | + <listitem>1</listitem> |
| 80 | + <listitem>2</listitem> |
| 81 | + </itemizedlist></para> |
| 82 | + </doxygen>)xml"); |
| 83 | + auto selected = doc.select_node("//*[@id='test-node']"); |
| 84 | + std::ostringstream os; |
| 85 | + EXPECT_THROW( |
| 86 | + try { |
| 87 | + AppendIfParagraph(os, selected.node()); |
| 88 | + } catch (std::runtime_error const& ex) { |
| 89 | + EXPECT_THAT(ex.what(), Not(HasSubstr("\n"))); |
| 90 | + EXPECT_THAT(ex.what(), |
| 91 | + HasSubstr("<itemizedlist a1=\"attr1\" a2=\"attr2\">")); |
| 92 | + EXPECT_THAT(ex.what(), HasSubstr("<listitem>1</listitem>")); |
| 93 | + EXPECT_THAT(ex.what(), HasSubstr("<listitem>2</listitem>")); |
| 94 | + EXPECT_THAT(ex.what(), HasSubstr("</itemizedlist>")); |
| 95 | + throw; |
| 96 | + }, |
| 97 | + std::runtime_error); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace |
0 commit comments