license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include "testing/gtest/include/gtest/gtest.h" |
| 6 | #include "base/string_escape.h" |
| 7 | |
| 8 | TEST(StringEscapeTest, JavascriptDoubleQuote) { |
| 9 | static const char* kToEscape = "\b\001aZ\"\\wee"; |
| 10 | static const char* kEscaped = "\\b\\x01aZ\\\"\\\\wee"; |
deanm@google.com | a593736 | 2008-08-19 21:34:51 +0900 | [diff] [blame] | 11 | static const char* kEscapedQuoted = "\"\\b\\x01aZ\\\"\\\\wee\""; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | static const wchar_t* kUToEscape = L"\b\x0001" L"a\x123fZ\"\\wee"; |
| 13 | static const char* kUEscaped = "\\b\\x01a\\u123FZ\\\"\\\\wee"; |
| 14 | static const char* kUEscapedQuoted = "\"\\b\\x01a\\u123FZ\\\"\\\\wee\""; |
| 15 | |
| 16 | std::string out; |
| 17 | |
| 18 | // Test wide unicode escaping |
| 19 | out = "testy: "; |
| 20 | string_escape::JavascriptDoubleQuote(std::wstring(kUToEscape), false, &out); |
| 21 | ASSERT_EQ(std::string("testy: ") + kUEscaped, out); |
| 22 | |
| 23 | out = "testy: "; |
| 24 | string_escape::JavascriptDoubleQuote(std::wstring(kUToEscape), true, &out); |
| 25 | ASSERT_EQ(std::string("testy: ") + kUEscapedQuoted, out); |
| 26 | |
| 27 | // Test null and high bit / negative unicode values |
| 28 | std::wstring wstr(L"TeSt"); |
| 29 | wstr.push_back(0); |
| 30 | wstr.push_back(0xffb1); |
| 31 | wstr.push_back(0x00ff); |
| 32 | |
| 33 | out = "testy: "; |
| 34 | string_escape::JavascriptDoubleQuote(wstr, false, &out); |
| 35 | ASSERT_EQ("testy: TeSt\\x00\\uFFB1\\xFF", out); |
| 36 | |
| 37 | // Test escaping of 7bit ascii |
| 38 | out = "testy: "; |
| 39 | string_escape::JavascriptDoubleQuote(std::string(kToEscape), false, &out); |
| 40 | ASSERT_EQ(std::string("testy: ") + kEscaped, out); |
| 41 | |
deanm@google.com | a593736 | 2008-08-19 21:34:51 +0900 | [diff] [blame] | 42 | out = "testy: "; |
| 43 | string_escape::JavascriptDoubleQuote(std::string(kToEscape), true, &out); |
| 44 | ASSERT_EQ(std::string("testy: ") + kEscapedQuoted, out); |
| 45 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 46 | // Test null, non-printable, and non-7bit |
| 47 | std::string str("TeSt"); |
| 48 | str.push_back(0); |
| 49 | str.push_back(15); |
| 50 | str.push_back(127); |
| 51 | str.push_back(-16); |
| 52 | str.push_back(-128); |
| 53 | str.push_back('!'); |
| 54 | |
| 55 | out = "testy: "; |
| 56 | string_escape::JavascriptDoubleQuote(str, false, &out); |
| 57 | ASSERT_EQ("testy: TeSt\\x00\\x0F\\x7F\xf0\x80!", out); |
deanm@google.com | a593736 | 2008-08-19 21:34:51 +0900 | [diff] [blame] | 58 | |
| 59 | // Test escape sequences |
| 60 | out = "testy: "; |
| 61 | string_escape::JavascriptDoubleQuote("a\b\f\n\r\t\v\1\\.\"z", false, &out); |
| 62 | ASSERT_EQ("testy: a\\b\\f\\n\\r\\t\\v\\x01\\\\.\\\"z", out); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 63 | } |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 64 | |