Skip to content

Commit b9a09ed

Browse files
authored
refactor(docfx): split function classifiers (#11464)
1 parent a672420 commit b9a09ed

10 files changed

Lines changed: 502 additions & 15 deletions

.typos.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ extend-exclude = [
3131
"google/cloud/storage/testing/constants.h",
3232
"google/cloud/storage/tests/unified_credentials_integration_test.cc",
3333
"google/cloud/storage/tools/make_jwt_assertion_for_test_data.py",
34+
# Input data for DocFX tests. It contains many long hex strings that confuse
35+
# the spell checker.
36+
"docfx/testing/inputs.cc",
3437
]

docfx/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ add_library(
5151
doxygen_groups.h
5252
doxygen_pages.cc
5353
doxygen_pages.h
54+
function_classifiers.cc
55+
function_classifiers.h
5456
generate_metadata.cc
5557
generate_metadata.h
5658
parse_arguments.cc
@@ -98,6 +100,7 @@ set(unit_tests
98100
doxygen2yaml_test.cc
99101
doxygen_groups_test.cc
100102
doxygen_pages_test.cc
103+
function_classifiers_test.cc
101104
generate_metadata_test.cc
102105
parse_arguments_test.cc
103106
public_docs_test.cc

docfx/docfx.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ docfx_hdrs = [
2727
"doxygen_errors.h",
2828
"doxygen_groups.h",
2929
"doxygen_pages.h",
30+
"function_classifiers.h",
3031
"generate_metadata.h",
3132
"parse_arguments.h",
3233
"public_docs.h",
@@ -45,6 +46,7 @@ docfx_srcs = [
4546
"doxygen_errors.cc",
4647
"doxygen_groups.cc",
4748
"doxygen_pages.cc",
49+
"function_classifiers.cc",
4850
"generate_metadata.cc",
4951
"parse_arguments.cc",
5052
"public_docs.cc",

docfx/doxygen2yaml.cc

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "docfx/doxygen2references.h"
1919
#include "docfx/doxygen2syntax.h"
2020
#include "docfx/doxygen_errors.h"
21+
#include "docfx/function_classifiers.h"
2122
#include "docfx/public_docs.h"
2223
#include "docfx/yaml_emit.h"
2324
#include <algorithm>
@@ -119,21 +120,6 @@ void AppendDescription(YAML::Emitter& yaml, pugi::xml_node const& node) {
119120
}
120121
}
121122

122-
bool IsOperator(pugi::xml_node const& node) {
123-
auto const name = std::string_view{node.child("name").child_value()};
124-
return name.find("operator") != std::string_view::npos;
125-
}
126-
127-
bool IsConstructor(pugi::xml_node const& node) {
128-
auto is_empty = [](auto const& child) {
129-
auto const name = std::string_view{child.name()};
130-
if (name == "ref") return std::string_view{child.child_value()}.empty();
131-
return std::string_view{child.value()}.empty();
132-
};
133-
auto type = node.child("type");
134-
return std::all_of(type.begin(), type.end(), is_empty);
135-
}
136-
137123
} // namespace
138124

139125
std::vector<TocEntry> CompoundToc(Config const& cfg,

docfx/function_classifiers.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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/function_classifiers.h"
16+
#include <algorithm>
17+
#include <string_view>
18+
19+
namespace docfx {
20+
21+
bool IsOperator(pugi::xml_node const& node) {
22+
auto const name = std::string_view{node.child("name").child_value()};
23+
return name.find("operator") != std::string_view::npos;
24+
}
25+
26+
bool IsConstructor(pugi::xml_node const& node) {
27+
auto is_empty = [](auto const& child) {
28+
auto const name = std::string_view{child.name()};
29+
if (name == "ref") return std::string_view{child.child_value()}.empty();
30+
return std::string_view{child.value()}.empty();
31+
};
32+
auto type = node.child("type");
33+
return std::all_of(type.begin(), type.end(), is_empty);
34+
}
35+
36+
} // namespace docfx

docfx/function_classifiers.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#ifndef GOOGLE_CLOUD_CPP_DOCFX_FUNCTION_CLASSIFIERS_H
16+
#define GOOGLE_CLOUD_CPP_DOCFX_FUNCTION_CLASSIFIERS_H
17+
18+
#include <pugixml.hpp>
19+
20+
namespace docfx {
21+
22+
// Determine if a function is a constructor.
23+
bool IsConstructor(pugi::xml_node const& node);
24+
25+
// Determine if a function is an operator.
26+
bool IsOperator(pugi::xml_node const& node);
27+
28+
} // namespace docfx
29+
30+
#endif // GOOGLE_CLOUD_CPP_DOCFX_FUNCTION_CLASSIFIERS_H

docfx/function_classifiers_test.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/function_classifiers.h"
16+
#include "docfx/testing/inputs.h"
17+
#include <gmock/gmock.h>
18+
19+
namespace docfx {
20+
namespace {
21+
22+
TEST(FunctionClassifiers, IsConstructor) {
23+
pugi::xml_document doc;
24+
doc.load_string(docfx_testing::StatusClassXml().c_str());
25+
26+
pugi::xpath_variable_set vars;
27+
vars.add("id", pugi::xpath_type_string);
28+
auto query = pugi::xpath_query("//memberdef[@id = string($id)]", &vars);
29+
30+
vars.set("id", docfx_testing::StatusDefaultConstructorId().c_str());
31+
auto ctor = doc.select_node(query);
32+
ASSERT_TRUE(ctor);
33+
EXPECT_TRUE(IsConstructor(ctor.node()));
34+
35+
vars.set("id", docfx_testing::StatusMessageFunctionId().c_str());
36+
auto msg = doc.select_node(query);
37+
ASSERT_TRUE(msg);
38+
EXPECT_FALSE(IsConstructor(msg.node()));
39+
}
40+
41+
TEST(FunctionClassifiers, IsOperator) {
42+
pugi::xml_document doc;
43+
doc.load_string(docfx_testing::StatusClassXml().c_str());
44+
45+
pugi::xpath_variable_set vars;
46+
vars.add("id", pugi::xpath_type_string);
47+
auto query = pugi::xpath_query("//memberdef[@id = string($id)]", &vars);
48+
49+
vars.set("id", docfx_testing::StatusOperatorEqualId().c_str());
50+
auto eq = doc.select_node(query);
51+
ASSERT_TRUE(eq);
52+
EXPECT_TRUE(IsOperator(eq.node()));
53+
54+
vars.set("id", docfx_testing::StatusDefaultConstructorId().c_str());
55+
auto ctor = doc.select_node(query);
56+
ASSERT_TRUE(ctor);
57+
EXPECT_FALSE(IsOperator(ctor.node()));
58+
59+
vars.set("id", docfx_testing::StatusMessageFunctionId().c_str());
60+
auto msg = doc.select_node(query);
61+
ASSERT_TRUE(msg);
62+
EXPECT_FALSE(IsConstructor(msg.node()));
63+
}
64+
65+
} // namespace
66+
} // namespace docfx

0 commit comments

Comments
 (0)