Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rosidlcpp_generator_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ install(TARGETS ${PROJECT_NAME}

install(DIRECTORY include/ DESTINATION include)

if(BUILD_TESTING)
find_package(ament_cmake_gtest REQUIRED)

ament_add_gtest(rosidlcpp_generator_core_test test/unit/rosidlcpp_generator_core_test.cpp)
target_link_libraries(rosidlcpp_generator_core_test ${PROJECT_NAME})
target_include_directories(rosidlcpp_generator_core_test PUBLIC include)
endif()

ament_export_include_directories("include/${PROJECT_NAME}")
ament_export_libraries(${PROJECT_NAME})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
namespace nlohmann {

std::string format_as(const json& j) {
return j.dump();
// dump() would wrap strings in double quotes, which is never what the generators want when
// interpolating names and namespaces into generated code.
return j.is_string() ? j.get<std::string>() : j.dump();
}

std::string format_as(const detail::iter_impl<const json>& j) {
return j->dump();
return format_as(*j);
}

} // namespace nlohmann
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2025 Anthony Welte
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <nlohmann/json.hpp>

#include <rosidlcpp_generator_core/generator_utils.hpp>

using nlohmann::json;

// Tests for format_as(const json&)

TEST(FormatAsJsonTest, StringValueIsNotQuoted) {
json j = "hello";
EXPECT_EQ(nlohmann::format_as(j), "hello");
}

TEST(FormatAsJsonTest, StringValueWithSpecialCharacters) {
json j = "msg/UUID";
EXPECT_EQ(nlohmann::format_as(j), "msg/UUID");
}

TEST(FormatAsJsonTest, IntegerValueIsDumped) {
json j = 42;
EXPECT_EQ(nlohmann::format_as(j), "42");
}

TEST(FormatAsJsonTest, BoolValueIsDumped) {
json j = true;
EXPECT_EQ(nlohmann::format_as(j), "true");
}

TEST(FormatAsJsonTest, NullValueIsDumped) {
json j = nullptr;
EXPECT_EQ(nlohmann::format_as(j), "null");
}

TEST(FormatAsJsonTest, ArrayValueIsDumped) {
json j = json::array({1, 2, 3});
EXPECT_EQ(nlohmann::format_as(j), "[1,2,3]");
}

TEST(FormatAsJsonTest, ObjectValueIsDumped) {
json j = {{"key", "value"}};
EXPECT_EQ(nlohmann::format_as(j), "{\"key\":\"value\"}");
}

// Tests for format_as(const detail::iter_impl<const json>&)

TEST(FormatAsIteratorTest, StringValueIsNotQuoted) {
json obj = {{"name", "msg"}};
auto it = obj.cbegin();
EXPECT_EQ(nlohmann::format_as(it), "msg");
}

TEST(FormatAsIteratorTest, IntegerValueIsDumped) {
json obj = {{"count", 7}};
auto it = obj.cbegin();
EXPECT_EQ(nlohmann::format_as(it), "7");
}

TEST(FormatAsIteratorTest, ArrayElementStringNotQuoted) {
json arr = json::array({"foo", "bar"});
auto it = arr.cbegin();
EXPECT_EQ(nlohmann::format_as(it), "foo");
++it;
EXPECT_EQ(nlohmann::format_as(it), "bar");
}