buffet: extract common unittest helper functions into separate file

CreateValue, CreateDictionaryValue and ValueToString() functions
were duplicated in two unit test files in buffet. These functions
will be needed for even more unit tests in the future, so extract
them into their own unittest_utils.cc/.h so they can be reused
in other unit tests.

BUG=None
TEST=USE=buffet P2_TEST_FILTER="buffet::*" FEATURES=test emerge-link platform2

Change-Id: I76ec6d13ed71e78dc02230167041cddc7de653ca
Reviewed-on: https://chromium-review.googlesource.com/208463
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp
index 17a85bc..0174e5b 100644
--- a/buffet/buffet.gyp
+++ b/buffet/buffet.gyp
@@ -84,6 +84,7 @@
         'buffet_testrunner.cc',
         'commands/object_schema_unittest.cc',
         'commands/schema_utils_unittest.cc',
+        'commands/unittest_utils.cc',
         'data_encoding_unittest.cc',
         'device_registration_info_unittest.cc',
         'error_unittest.cc',
diff --git a/buffet/commands/object_schema.cc b/buffet/commands/object_schema.cc
index b978ec9..bf75a3c 100644
--- a/buffet/commands/object_schema.cc
+++ b/buffet/commands/object_schema.cc
@@ -7,7 +7,6 @@
 #include <algorithm>
 #include <limits>
 
-#include <base/json/json_writer.h>
 #include <base/logging.h>
 #include <base/values.h>
 
diff --git a/buffet/commands/object_schema_unittest.cc b/buffet/commands/object_schema_unittest.cc
index 64f4600..6921b56 100644
--- a/buffet/commands/object_schema_unittest.cc
+++ b/buffet/commands/object_schema_unittest.cc
@@ -14,38 +14,11 @@
 
 #include "buffet/commands/object_schema.h"
 #include "buffet/commands/prop_types.h"
+#include "buffet/commands/unittest_utils.h"
 
-namespace {
-// Helper method to create base::Value from a string as a smart pointer.
-// For ease of definition in C++ code, double-quotes in the source definition
-// are replaced with apostrophes.
-std::unique_ptr<base::Value> CreateValue(const char* json) {
-  std::string json2(json);
-  // Convert apostrophes to double-quotes so JSONReader can parse the string.
-  std::replace(json2.begin(), json2.end(), '\'', '"');
-  return std::unique_ptr<base::Value>(base::JSONReader::Read(json2));
-}
-
-// Helper method to create a JSON dictionary object from a string.
-std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) {
-  std::string json2(json);
-  std::replace(json2.begin(), json2.end(), '\'', '"');
-  base::Value* value = base::JSONReader::Read(json2);
-  base::DictionaryValue* dict;
-  value->GetAsDictionary(&dict);
-  return std::unique_ptr<base::DictionaryValue>(dict);
-}
-
-// Converts a JSON value to a string. It also converts double-quotes to
-// apostrophes for easy comparisons in C++ source code.
-std::string ValueToString(const base::Value* value) {
-  std::string json;
-  base::JSONWriter::Write(value, &json);
-  std::replace(json.begin(), json.end(), '"', '\'');
-  return json;
-}
-
-}  // namespace
+using buffet::unittests::CreateValue;
+using buffet::unittests::CreateDictionaryValue;
+using buffet::unittests::ValueToString;
 
 TEST(CommandSchema, IntPropType_Empty) {
   buffet::IntPropType prop;
diff --git a/buffet/commands/schema_utils_unittest.cc b/buffet/commands/schema_utils_unittest.cc
index 6918186..e20532d 100644
--- a/buffet/commands/schema_utils_unittest.cc
+++ b/buffet/commands/schema_utils_unittest.cc
@@ -6,8 +6,6 @@
 #include <string>
 #include <vector>
 
-#include <base/json/json_reader.h>
-#include <base/json/json_writer.h>
 #include <base/values.h>
 #include <gtest/gtest.h>
 
@@ -15,38 +13,10 @@
 #include "buffet/commands/prop_types.h"
 #include "buffet/commands/prop_values.h"
 #include "buffet/commands/schema_utils.h"
+#include "buffet/commands/unittest_utils.h"
 
-namespace {
-// Helper method to create base::Value from a string as a smart pointer.
-// For ease of definition in C++ code, double-quotes in the source definition
-// are replaced with apostrophes.
-std::unique_ptr<base::Value> CreateValue(const char* json) {
-  std::string json2(json);
-  // Convert apostrophes to double-quotes so JSONReader can parse the string.
-  std::replace(json2.begin(), json2.end(), '\'', '"');
-  return std::unique_ptr<base::Value>(base::JSONReader::Read(json2));
-}
-
-// Helper method to create a JSON dictionary object from a string.
-std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) {
-  std::string json2(json);
-  std::replace(json2.begin(), json2.end(), '\'', '"');
-  base::Value* value = base::JSONReader::Read(json2);
-  base::DictionaryValue* dict;
-  value->GetAsDictionary(&dict);
-  return std::unique_ptr<base::DictionaryValue>(dict);
-}
-
-// Converts a JSON value to a string. It also converts double-quotes to
-// apostrophes for easy comparisons in C++ source code.
-std::string ValueToString(const base::Value* value) {
-  std::string json;
-  base::JSONWriter::Write(value, &json);
-  std::replace(json.begin(), json.end(), '"', '\'');
-  return json;
-}
-
-}  // namespace
+using buffet::unittests::CreateValue;
+using buffet::unittests::ValueToString;
 
 TEST(CommandSchemaUtils, TypedValueToJson_Scalar) {
   EXPECT_EQ("true",
diff --git a/buffet/commands/unittest_utils.cc b/buffet/commands/unittest_utils.cc
new file mode 100644
index 0000000..8d4c369
--- /dev/null
+++ b/buffet/commands/unittest_utils.cc
@@ -0,0 +1,37 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "buffet/commands/unittest_utils.h"
+
+#include <base/json/json_reader.h>
+#include <base/json/json_writer.h>
+
+namespace buffet {
+namespace unittests {
+
+std::unique_ptr<base::Value> CreateValue(const char* json) {
+  std::string json2(json);
+  // Convert apostrophes to double-quotes so JSONReader can parse the string.
+  std::replace(json2.begin(), json2.end(), '\'', '"');
+  return std::unique_ptr<base::Value>(base::JSONReader::Read(json2));
+}
+
+std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json) {
+  std::string json2(json);
+  std::replace(json2.begin(), json2.end(), '\'', '"');
+  base::Value* value = base::JSONReader::Read(json2);
+  base::DictionaryValue* dict;
+  value->GetAsDictionary(&dict);
+  return std::unique_ptr<base::DictionaryValue>(dict);
+}
+
+std::string ValueToString(const base::Value* value) {
+  std::string json;
+  base::JSONWriter::Write(value, &json);
+  std::replace(json.begin(), json.end(), '"', '\'');
+  return json;
+}
+
+}  // namespace unittests
+}  // namespace buffet
diff --git a/buffet/commands/unittest_utils.h b/buffet/commands/unittest_utils.h
new file mode 100644
index 0000000..e14e4d1
--- /dev/null
+++ b/buffet/commands/unittest_utils.h
@@ -0,0 +1,31 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BUFFET_COMMANDS_UNITTEST_UTILS_H_
+#define BUFFET_COMMANDS_UNITTEST_UTILS_H_
+
+#include <memory>
+#include <string>
+
+#include <base/values.h>
+
+namespace buffet {
+namespace unittests {
+
+// Helper method to create base::Value from a string as a smart pointer.
+// For ease of definition in C++ code, double-quotes in the source definition
+// are replaced with apostrophes.
+  std::unique_ptr<base::Value> CreateValue(const char* json);
+
+// Helper method to create a JSON dictionary object from a string.
+std::unique_ptr<base::DictionaryValue> CreateDictionaryValue(const char* json);
+
+// Converts a JSON value to a string. It also converts double-quotes to
+// apostrophes for easy comparisons in C++ source code.
+std::string ValueToString(const base::Value* value);
+
+}  // namespace unittests
+}  // namespace buffet
+
+#endif  // BUFFET_COMMANDS_UNITTEST_UTILS_H_