summaryrefslogtreecommitdiffstats
path: root/base/json/string_escape_unittest.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 20:00:20 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-23 20:00:20 +0000
commit93d49d70b89b22ec46d0b00b7950297f64865d56 (patch)
tree3243e0209c738df0c18c2ba615667f757fcaec94 /base/json/string_escape_unittest.cc
parenta9f607e33a91604bc63bd8c846aefbda9bf0dfa9 (diff)
downloadchromium_src-93d49d70b89b22ec46d0b00b7950297f64865d56.zip
chromium_src-93d49d70b89b22ec46d0b00b7950297f64865d56.tar.gz
chromium_src-93d49d70b89b22ec46d0b00b7950297f64865d56.tar.bz2
Move the json-related files into a separate json directory. This hopefully also
makes the naming of string_escape more clear (it's actually JSON-specific). Move the files into the base namespace. TEST=none BUG=none Review URL: http://codereview.chromium.org/316016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29934 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/json/string_escape_unittest.cc')
-rw-r--r--base/json/string_escape_unittest.cc99
1 files changed, 99 insertions, 0 deletions
diff --git a/base/json/string_escape_unittest.cc b/base/json/string_escape_unittest.cc
new file mode 100644
index 0000000..a3d6262
--- /dev/null
+++ b/base/json/string_escape_unittest.cc
@@ -0,0 +1,99 @@
+// Copyright (c) 2006-2008 The Chromium 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 "base/json/string_escape.h"
+#include "base/string_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+namespace {
+
+const struct json_narrow_test_data {
+ const char* to_escape;
+ const char* escaped;
+} json_narrow_cases[] = {
+ {"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
+ {"a\b\f\n\r\t\v\1\\.\"z",
+ "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
+ {"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
+};
+
+}
+
+TEST(StringEscapeTest, JsonDoubleQuoteNarrow) {
+ for (size_t i = 0; i < arraysize(json_narrow_cases); ++i) {
+ std::string in = json_narrow_cases[i].to_escape;
+ std::string out;
+ JsonDoubleQuote(in, false, &out);
+ EXPECT_EQ(std::string(json_narrow_cases[i].escaped), out);
+ }
+
+ std::string in = json_narrow_cases[0].to_escape;
+ std::string out;
+ JsonDoubleQuote(in, false, &out);
+
+ // test quoting
+ std::string out_quoted;
+ JsonDoubleQuote(in, true, &out_quoted);
+ EXPECT_EQ(out.length() + 2, out_quoted.length());
+ EXPECT_EQ(out_quoted.find(out), 1U);
+
+ // now try with a NULL in the string
+ std::string null_prepend = "test";
+ null_prepend.push_back(0);
+ in = null_prepend + in;
+ std::string expected = "test\\u0000";
+ expected += json_narrow_cases[0].escaped;
+ out.clear();
+ JsonDoubleQuote(in, false, &out);
+ EXPECT_EQ(expected, out);
+}
+
+namespace {
+
+const struct json_wide_test_data {
+ const wchar_t* to_escape;
+ const char* escaped;
+} json_wide_cases[] = {
+ {L"b\uffb1\u00ff", "b\\uFFB1\\u00FF"},
+ {L"\b\001aZ\"\\wee", "\\b\\u0001aZ\\\"\\\\wee"},
+ {L"a\b\f\n\r\t\v\1\\.\"z",
+ "a\\b\\f\\n\\r\\t\\u000B\\u0001\\\\.\\\"z"},
+ {L"b\x0f\x7f\xf0\xff!", "b\\u000F\\u007F\\u00F0\\u00FF!"},
+};
+
+}
+
+TEST(StringEscapeTest, JsonDoubleQuoteWide) {
+
+ for (size_t i = 0; i < arraysize(json_wide_cases); ++i) {
+ std::string out;
+ string16 in = WideToUTF16(json_wide_cases[i].to_escape);
+ JsonDoubleQuote(in, false, &out);
+ EXPECT_EQ(std::string(json_wide_cases[i].escaped), out);
+ }
+
+ string16 in = WideToUTF16(json_wide_cases[0].to_escape);
+ std::string out;
+ JsonDoubleQuote(in, false, &out);
+
+ // test quoting
+ std::string out_quoted;
+ JsonDoubleQuote(in, true, &out_quoted);
+ EXPECT_EQ(out.length() + 2, out_quoted.length());
+ EXPECT_EQ(out_quoted.find(out), 1U);
+
+ // now try with a NULL in the string
+ string16 null_prepend = WideToUTF16(L"test");
+ null_prepend.push_back(0);
+ in = null_prepend + in;
+ std::string expected = "test\\u0000";
+ expected += json_wide_cases[0].escaped;
+ out.clear();
+ JsonDoubleQuote(in, false, &out);
+ EXPECT_EQ(expected, out);
+}
+
+} // namespace base