Skip to content

Commit

Permalink
common/yaml: Add BUILD and tweaks for Drake
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri committed Mar 12, 2019
1 parent b34a136 commit 677868f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
53 changes: 53 additions & 0 deletions common/yaml/dev/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- mode: python -*-
# vi: set ft=python :

load(
"@drake//tools/skylark:drake_cc.bzl",
"drake_cc_googletest",
"drake_cc_library",
"drake_cc_package_library",
)
load("//tools/lint:lint.bzl", "add_lint_tests")

package(default_visibility = ["//visibility:public"])

drake_cc_package_library(
name = "dev",
deps = [
":visitor",
":yaml_read_archive",
],
)

drake_cc_library(
name = "visitor",
hdrs = ["visitor.h"],
deps = [
"//common:essential",
],
)

drake_cc_library(
name = "yaml_read_archive",
srcs = ["yaml_read_archive.cc"],
hdrs = ["yaml_read_archive.h"],
deps = [
":visitor",
"//common:essential",
"//common:nice_type_name",
"@yaml_cpp",
],
)

# === test/ ===

drake_cc_googletest(
name = "yaml_read_archive_test",
deps = [
":visitor",
":yaml_read_archive",
"//common/test_utilities",
],
)

add_lint_tests()
34 changes: 17 additions & 17 deletions common/yaml/dev/test/yaml_read_archive_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "common/yaml_read_archive.h"
#include "drake/common/yaml/dev/yaml_read_archive.h"

#include <cmath>
#include <limits>
Expand All @@ -10,7 +10,7 @@

#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "common/visitor.h"
#include "drake/common/yaml/dev/visitor.h"

using anzu::common::YamlReadArchive;

Expand Down Expand Up @@ -197,7 +197,7 @@ class YamlReadArchiveTest : public ::testing::Test {
};

TEST_F(YamlReadArchiveTest, Double) {
const auto test = [this](const std::string& value, double expected) {
const auto test = [](const std::string& value, double expected) {
const auto& x = AcceptNoThrow<DoubleStruct>(LoadSingleValue(value));
EXPECT_EQ(x.value, expected);
};
Expand Down Expand Up @@ -226,8 +226,8 @@ TEST_F(YamlReadArchiveTest, Double) {
}

TEST_F(YamlReadArchiveTest, StdArray) {
const auto test = [this](const std::string& value,
const std::array<double, 3>& expected) {
const auto test = [](const std::string& value,
const std::array<double, 3>& expected) {
const auto& x = AcceptNoThrow<ArrayStruct>(LoadSingleValue(value));
EXPECT_EQ(x.value, expected);
};
Expand All @@ -236,8 +236,8 @@ TEST_F(YamlReadArchiveTest, StdArray) {
}

TEST_F(YamlReadArchiveTest, StdVector) {
const auto test = [this](const std::string& value,
const std::vector<double>& expected) {
const auto test = [](const std::string& value,
const std::vector<double>& expected) {
const auto& x = AcceptNoThrow<VectorStruct>(LoadSingleValue(value));
EXPECT_EQ(x.value, expected);
};
Expand All @@ -246,8 +246,8 @@ TEST_F(YamlReadArchiveTest, StdVector) {
}

TEST_F(YamlReadArchiveTest, Optional) {
const auto test = [this](const std::string& doc,
const drake::optional<double>& expected) {
const auto test = [](const std::string& doc,
const drake::optional<double>& expected) {
const auto& x = AcceptNoThrow<OptionalStruct>(Load(doc));
if (expected.has_value()) {
ASSERT_TRUE(x.value.has_value()) << *expected;
Expand All @@ -263,8 +263,8 @@ TEST_F(YamlReadArchiveTest, Optional) {
}

TEST_F(YamlReadArchiveTest, Variant) {
const auto test = [this](const std::string& doc,
const Variant3& expected) {
const auto test = [](const std::string& doc,
const Variant3& expected) {
const auto& x = AcceptNoThrow<VariantStruct>(Load(doc));
EXPECT_EQ(x.value, expected) << doc;
};
Expand All @@ -275,8 +275,8 @@ TEST_F(YamlReadArchiveTest, Variant) {
}

TEST_F(YamlReadArchiveTest, EigenVector) {
const auto test = [this](const std::string& value,
const Eigen::VectorXd& expected) {
const auto test = [](const std::string& value,
const Eigen::VectorXd& expected) {
const auto& vec = AcceptNoThrow<EigenVecStruct>(LoadSingleValue(value));
const auto& vec3 = AcceptNoThrow<EigenVec3Struct>(LoadSingleValue(value));
EXPECT_TRUE(drake::CompareMatrices(vec.value, expected));
Expand All @@ -287,8 +287,8 @@ TEST_F(YamlReadArchiveTest, EigenVector) {
}

TEST_F(YamlReadArchiveTest, EigenVectorX) {
const auto test = [this](const std::string& value,
const Eigen::VectorXd& expected) {
const auto test = [](const std::string& value,
const Eigen::VectorXd& expected) {
const auto& x = AcceptNoThrow<EigenVecStruct>(LoadSingleValue(value));
EXPECT_TRUE(drake::CompareMatrices(x.value, expected));
};
Expand All @@ -299,8 +299,8 @@ TEST_F(YamlReadArchiveTest, EigenVectorX) {

TEST_F(YamlReadArchiveTest, EigenMatrix) {
using Matrix34d = Eigen::Matrix<double, 3, 4>;
const auto test = [this](const std::string& doc,
const Eigen::MatrixXd& expected) {
const auto test = [](const std::string& doc,
const Eigen::MatrixXd& expected) {
const auto& mat = AcceptNoThrow<EigenMatrixStruct>(Load(doc));
const auto& mat34 = AcceptNoThrow<EigenMatrix34Struct>(Load(doc));
EXPECT_TRUE(drake::CompareMatrices(mat.value, expected));
Expand Down
2 changes: 1 addition & 1 deletion common/yaml/dev/yaml_read_archive.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "common/yaml_read_archive.h"
#include "drake/common/yaml/dev/yaml_read_archive.h"

#include <algorithm>

Expand Down
4 changes: 2 additions & 2 deletions common/yaml/dev/yaml_read_archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "drake/common/drake_throw.h"
#include "drake/common/drake_variant.h"
#include "drake/common/nice_type_name.h"
#include "common/visitor.h"
#include "drake/common/yaml/dev/visitor.h"

namespace anzu {
namespace common {
Expand Down Expand Up @@ -224,7 +224,7 @@ class YamlReadArchive final {
}

// Steps through Types to extract 'size_t I' and 'typename T' for the Impl.
template <template <typename...> typename Variant, typename... Types>
template <template <typename...> class Variant, typename... Types>
void VariantHelper(
const std::string& tag, const char* name, Variant<Types...>* storage) {
VariantHelperImpl<0, Variant<Types...>, Types...>(tag, name, storage);
Expand Down
1 change: 1 addition & 0 deletions tools/install/libdrake/build_components.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ LIBDRAKE_COMPONENTS = [
"//common",
"//common/proto",
"//common/trajectories",
"//common/yaml/dev",
"//common:drake_marker_shared_library", # unpackaged
"//common:text_logging_gflags_h", # unpackaged
"//examples/acrobot:acrobot_input", # unpackaged
Expand Down

0 comments on commit 677868f

Please sign in to comment.