| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
| 7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "base/string_piece.h" |
| 9 #include "base/utf_string_conversions.h" |
| 8 #include "base/values.h" | 10 #include "base/values.h" |
| 9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 10 | 12 |
| 11 namespace base { | 13 namespace base { |
| 12 | 14 |
| 13 TEST(JSONReaderTest, Reading) { | 15 TEST(JSONReaderTest, Reading) { |
| 14 // some whitespace checking | 16 // some whitespace checking |
| 15 scoped_ptr<Value> root; | 17 scoped_ptr<Value> root; |
| 16 root.reset(JSONReader().JsonToValue(" null ", false, false)); | 18 root.reset(JSONReader().JsonToValue(" null ", false, false)); |
| 17 ASSERT_TRUE(root.get()); | 19 ASSERT_TRUE(root.get()); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Invalid number formats | 163 // Invalid number formats |
| 162 root.reset(JSONReader().JsonToValue("4.3.1", false, false)); | 164 root.reset(JSONReader().JsonToValue("4.3.1", false, false)); |
| 163 ASSERT_FALSE(root.get()); | 165 ASSERT_FALSE(root.get()); |
| 164 root.reset(JSONReader().JsonToValue("4e3.1", false, false)); | 166 root.reset(JSONReader().JsonToValue("4e3.1", false, false)); |
| 165 ASSERT_FALSE(root.get()); | 167 ASSERT_FALSE(root.get()); |
| 166 | 168 |
| 167 // Test string parser | 169 // Test string parser |
| 168 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false)); | 170 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false)); |
| 169 ASSERT_TRUE(root.get()); | 171 ASSERT_TRUE(root.get()); |
| 170 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 172 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 171 std::wstring str_val; | 173 std::string str_val; |
| 172 ASSERT_TRUE(root->GetAsString(&str_val)); | 174 ASSERT_TRUE(root->GetAsString(&str_val)); |
| 173 ASSERT_EQ(L"hello world", str_val); | 175 ASSERT_EQ("hello world", str_val); |
| 174 | 176 |
| 175 // Empty string | 177 // Empty string |
| 176 root.reset(JSONReader().JsonToValue("\"\"", false, false)); | 178 root.reset(JSONReader().JsonToValue("\"\"", false, false)); |
| 177 ASSERT_TRUE(root.get()); | 179 ASSERT_TRUE(root.get()); |
| 178 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 180 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 179 str_val.clear(); | 181 str_val.clear(); |
| 180 ASSERT_TRUE(root->GetAsString(&str_val)); | 182 ASSERT_TRUE(root->GetAsString(&str_val)); |
| 181 ASSERT_EQ(L"", str_val); | 183 ASSERT_EQ("", str_val); |
| 182 | 184 |
| 183 // Test basic string escapes | 185 // Test basic string escapes |
| 184 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", | 186 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", |
| 185 false, false)); | 187 false, false)); |
| 186 ASSERT_TRUE(root.get()); | 188 ASSERT_TRUE(root.get()); |
| 187 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 189 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 188 str_val.clear(); | 190 str_val.clear(); |
| 189 ASSERT_TRUE(root->GetAsString(&str_val)); | 191 ASSERT_TRUE(root->GetAsString(&str_val)); |
| 190 ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val); | 192 ASSERT_EQ(" \"\\/\b\f\n\r\t\v", str_val); |
| 191 | 193 |
| 192 // Test hex and unicode escapes including the null character. | 194 // Test hex and unicode escapes including the null character. |
| 193 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false, | 195 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false, |
| 194 false)); | 196 false)); |
| 195 ASSERT_TRUE(root.get()); | 197 ASSERT_TRUE(root.get()); |
| 196 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 198 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 197 str_val.clear(); | 199 str_val.clear(); |
| 198 ASSERT_TRUE(root->GetAsString(&str_val)); | 200 ASSERT_TRUE(root->GetAsString(&str_val)); |
| 199 ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val); | 201 ASSERT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val)); |
| 200 | 202 |
| 201 // Test invalid strings | 203 // Test invalid strings |
| 202 root.reset(JSONReader().JsonToValue("\"no closing quote", false, false)); | 204 root.reset(JSONReader().JsonToValue("\"no closing quote", false, false)); |
| 203 ASSERT_FALSE(root.get()); | 205 ASSERT_FALSE(root.get()); |
| 204 root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false, | 206 root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false, |
| 205 false)); | 207 false)); |
| 206 ASSERT_FALSE(root.get()); | 208 ASSERT_FALSE(root.get()); |
| 207 root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false, | 209 root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false, |
| 208 false)); | 210 false)); |
| 209 ASSERT_FALSE(root.get()); | 211 ASSERT_FALSE(root.get()); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 ASSERT_TRUE(root.get()); | 303 ASSERT_TRUE(root.get()); |
| 302 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 304 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 303 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); | 305 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); |
| 304 real_val = 0.0; | 306 real_val = 0.0; |
| 305 ASSERT_TRUE(dict_val->GetReal("number", &real_val)); | 307 ASSERT_TRUE(dict_val->GetReal("number", &real_val)); |
| 306 ASSERT_DOUBLE_EQ(9.87654321, real_val); | 308 ASSERT_DOUBLE_EQ(9.87654321, real_val); |
| 307 Value* null_val = NULL; | 309 Value* null_val = NULL; |
| 308 ASSERT_TRUE(dict_val->Get("null", &null_val)); | 310 ASSERT_TRUE(dict_val->Get("null", &null_val)); |
| 309 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); | 311 ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); |
| 310 str_val.clear(); | 312 str_val.clear(); |
| 311 ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); | 313 ASSERT_TRUE(dict_val->GetString("S", &str_val)); |
| 312 ASSERT_EQ(L"str", str_val); | 314 ASSERT_EQ("str", str_val); |
| 313 | 315 |
| 314 root2.reset(JSONReader::Read( | 316 root2.reset(JSONReader::Read( |
| 315 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); | 317 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); |
| 316 ASSERT_TRUE(root2.get()); | 318 ASSERT_TRUE(root2.get()); |
| 317 EXPECT_TRUE(root->Equals(root2.get())); | 319 EXPECT_TRUE(root->Equals(root2.get())); |
| 318 | 320 |
| 319 // Test newline equivalence. | 321 // Test newline equivalence. |
| 320 root2.reset(JSONReader::Read( | 322 root2.reset(JSONReader::Read( |
| 321 "{\n" | 323 "{\n" |
| 322 " \"number\":9.87654321,\n" | 324 " \"number\":9.87654321,\n" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 list = static_cast<ListValue*>(root.get()); | 439 list = static_cast<ListValue*>(root.get()); |
| 438 ASSERT_EQ(5001U, list->GetSize()); | 440 ASSERT_EQ(5001U, list->GetSize()); |
| 439 | 441 |
| 440 // Test utf8 encoded input | 442 // Test utf8 encoded input |
| 441 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", | 443 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", |
| 442 false, false)); | 444 false, false)); |
| 443 ASSERT_TRUE(root.get()); | 445 ASSERT_TRUE(root.get()); |
| 444 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); | 446 ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 445 str_val.clear(); | 447 str_val.clear(); |
| 446 ASSERT_TRUE(root->GetAsString(&str_val)); | 448 ASSERT_TRUE(root->GetAsString(&str_val)); |
| 447 ASSERT_EQ(L"\x7f51\x9875", str_val); | 449 ASSERT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); |
| 448 | 450 |
| 449 // Test invalid utf8 encoded input | 451 // Test invalid utf8 encoded input |
| 450 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", | 452 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", |
| 451 false, false)); | 453 false, false)); |
| 452 ASSERT_FALSE(root.get()); | 454 ASSERT_FALSE(root.get()); |
| 453 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", | 455 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", |
| 454 false, false)); | 456 false, false)); |
| 455 ASSERT_FALSE(root.get()); | 457 ASSERT_FALSE(root.get()); |
| 456 | 458 |
| 457 // Test invalid root objects. | 459 // Test invalid root objects. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 | 557 |
| 556 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 558 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
| 557 &error_code, &error_message)); | 559 &error_code, &error_message)); |
| 558 EXPECT_FALSE(root.get()); | 560 EXPECT_FALSE(root.get()); |
| 559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 561 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
| 560 error_message); | 562 error_message); |
| 561 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 563 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
| 562 } | 564 } |
| 563 | 565 |
| 564 } // namespace base | 566 } // namespace base |
| OLD | NEW |