-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d3c199
commit 64d8551
Showing
10 changed files
with
436 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <limits> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "drake/common/temp_directory.h" | ||
#include "drake/common/test_utilities/expect_throws_message.h" | ||
#include "drake/common/yaml/test/example_structs.h" | ||
#include "drake/common/yaml/yaml_io.h" | ||
|
||
namespace drake { | ||
namespace yaml { | ||
namespace test { | ||
namespace { | ||
|
||
GTEST_TEST(YamlJsonTest, WriteScalars) { | ||
AllScalarsStruct data; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"some_bool":false,)""" | ||
R"""("some_double":1.2345,)""" | ||
R"""("some_float":1.2345,)""" | ||
R"""("some_int32":12,)""" | ||
R"""("some_int64":14,)""" | ||
R"""("some_string":"kNominalString",)""" | ||
R"""("some_uint32":12,)""" | ||
R"""("some_uint64":15})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, StringEscaping) { | ||
StringStruct data; | ||
data.value = "foo\n\t\"bar"; | ||
EXPECT_EQ(SaveJsonString(data), | ||
R"""({"value":"foo\u000a\u0009\u0022bar"})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, NonFinite) { | ||
DoubleStruct data; | ||
|
||
data.value = std::numeric_limits<double>::infinity(); | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":Infinity})"""); | ||
|
||
data.value = -data.value; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":-Infinity})"""); | ||
|
||
data.value = std::numeric_limits<double>::quiet_NaN(); | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":NaN})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, WriteSequence) { | ||
VectorStruct data; | ||
data.value = {1.0, 2.0}; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":[1.0,2.0]})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, WriteMapping) { | ||
MapStruct data; | ||
data.value.clear(); | ||
data.value["a"] = 1.0; | ||
data.value["b"] = 2.0; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":{"a":1.0,"b":2.0}})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, WriteNested) { | ||
OuterStruct data; | ||
EXPECT_EQ( | ||
SaveJsonString(data), | ||
R"""({"inner_struct":{"inner_value":1.2345},"outer_value":1.2345})"""); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, WriteOptional) { | ||
OptionalStruct data; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":1.2345})"""); | ||
|
||
data.value.reset(); | ||
EXPECT_EQ(SaveJsonString(data), "{}"); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, WriteVariant) { | ||
VariantStruct data; | ||
|
||
data.value = std::string{"foo"}; | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":"foo"})"""); | ||
|
||
// It would be plausible here to use the `_tag` convention to annotate | ||
// variant tags, matching what we do in our yaml.py conventions. | ||
data.value = DoubleStruct{}; | ||
DRAKE_EXPECT_THROWS_MESSAGE(SaveJsonString(data), | ||
".*SaveJsonString.*mapping.*tag.*"); | ||
} | ||
|
||
struct IntVariant { | ||
template <typename Archive> | ||
void Serialize(Archive* a) { | ||
a->Visit(DRAKE_NVP(value)); | ||
} | ||
|
||
std::variant<int, uint64_t> value{0}; | ||
}; | ||
|
||
GTEST_TEST(YamlJsonTest, WriteVariantScalar) { | ||
IntVariant data; | ||
|
||
data.value.emplace<int>(1); | ||
EXPECT_EQ(SaveJsonString(data), R"""({"value":1})"""); | ||
|
||
// There is no syntax that could express this in JSON. | ||
data.value.emplace<uint64_t>(22); | ||
DRAKE_EXPECT_THROWS_MESSAGE(SaveJsonString(data), | ||
".*SaveJsonString.*scalar.*22.*tag.*"); | ||
} | ||
|
||
GTEST_TEST(YamlJsonTest, FileRoundTrip) { | ||
DoubleStruct data; | ||
data.value = 22.25; | ||
const std::string filename = temp_directory() + "/file_round_trip.json"; | ||
SaveJsonFile(filename, data); | ||
const auto readback = LoadYamlFile<DoubleStruct>(filename); | ||
EXPECT_EQ(readback.value, 22.25); | ||
} | ||
|
||
} // namespace | ||
} // namespace test | ||
} // namespace yaml | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.