Add PrintTo for dynamic
authorTom Jackson <tjackson@fb.com>
Thu, 21 Jan 2016 22:19:37 +0000 (14:19 -0800)
committerfacebook-github-bot-0 <folly-bot@fb.com>
Thu, 21 Jan 2016 23:21:02 +0000 (15:21 -0800)
Summary: Making `EXPECT_EQ(dyn1, dyn2)` easier to debug

Reviewed By: luciang, ot, yfeldblum

Differential Revision: D2848318

fb-gh-sync-id: 0c7cdd292665a493f2b792798df4e6966c1f28db

folly/json.cpp
folly/json.h
folly/test/JsonTest.cpp

index 63fa2ba08dc7236577d73de4e781f04be132ce3c..438cdf2ed7a4bc833c059a7b6c05e0d569c033ee 100644 (file)
@@ -833,6 +833,15 @@ void dynamic::print_as_pseudo_json(std::ostream& out) const {
   out << json::serialize(*this, opts);
 }
 
+void PrintTo(const dynamic& dyn, std::ostream* os) {
+  json::serialization_opts opts;
+  opts.allow_nan_inf = true;
+  opts.allow_non_string_keys = true;
+  opts.pretty_formatting = true;
+  opts.sort_keys = true;
+  *os << json::serialize(dyn, opts);
+}
+
 //////////////////////////////////////////////////////////////////////
 
 }
index ead3afd01bec45a6618fdb6da217bb06d1269a4d..3d229f3f1f61d01eff6a950a2d30c34c81f986c0 100644 (file)
@@ -41,6 +41,8 @@
 #ifndef FOLLY_JSON_H_
 #define FOLLY_JSON_H_
 
+#include <iosfwd>
+
 #include <folly/dynamic.h>
 #include <folly/FBString.h>
 #include <folly/Range.h>
@@ -158,6 +160,11 @@ fbstring toJson(dynamic const&);
  */
 fbstring toPrettyJson(dynamic const&);
 
+/*
+ * Printer for GTest.
+ * Uppercase name to fill GTest's API, which calls this method through ADL.
+ */
+void PrintTo(const dynamic&, std::ostream*);
 //////////////////////////////////////////////////////////////////////
 
 }
index d514e8f3af84630844b48ba89f740353060ad371..74838a0329762aa873cbb70d313a0db42985935b 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <limits>
+#include <strstream>
 
+#include <boost/next_prior.hpp>
 #include <folly/json.h>
-
-#include <gtest/gtest.h>
 #include <gflags/gflags.h>
-#include <limits>
-#include <boost/next_prior.hpp>
+#include <gtest/gtest.h>
 
 using folly::dynamic;
 using folly::parseJson;
@@ -465,6 +465,63 @@ TEST(Json, SortKeys) {
   EXPECT_EQ(sorted_keys, folly::json::serialize(value, opts_on));
 }
 
+TEST(Json, PrintTo) {
+  std::ostrstream oss;
+
+  dynamic value = dynamic::object
+    ("foo", "bar")
+    ("junk", 12)
+    ("another", 32.2)
+    (true, false) // include non-string keys
+    (false, true)
+    (2, 3)
+    (0, 1)
+    (1, 2)
+    (1.5, 2.25)
+    (0.5, 0.25)
+    (0, 1)
+    (1, 2)
+    ("a",
+      {
+        dynamic::object("a", "b")
+                       ("c", "d"),
+        12.5,
+        "Yo Dawg",
+        { "heh" },
+        nullptr
+      }
+    )
+    ;
+
+  std::string expected =
+      R"({
+  false : true,
+  true : false,
+  0.5 : 0.25,
+  1.5 : 2.25,
+  0 : 1,
+  1 : 2,
+  2 : 3,
+  "a" : [
+    {
+      "a" : "b",
+      "c" : "d"
+    },
+    12.5,
+    "Yo Dawg",
+    [
+      "heh"
+    ],
+    null
+  ],
+  "another" : 32.2,
+  "foo" : "bar",
+  "junk" : 12
+})";
+  PrintTo(value, &oss);
+  EXPECT_EQ(expected, oss.str());
+}
+
 int main(int argc, char** argv) {
   testing::InitGoogleTest(&argc, argv);
   gflags::ParseCommandLineFlags(&argc, &argv, true);