OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 ASSERT_TRUE(root.get()); | 188 ASSERT_TRUE(root.get()); |
189 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); | 189 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |
190 ASSERT_TRUE(root_expected.get()); | 190 ASSERT_TRUE(root_expected.get()); |
191 ASSERT_TRUE(root->Equals(root_expected.get())); | 191 ASSERT_TRUE(root->Equals(root_expected.get())); |
192 } | 192 } |
193 | 193 |
194 namespace { | 194 namespace { |
195 | 195 |
196 void ValidateJsonList(const std::string& json) { | 196 void ValidateJsonList(const std::string& json) { |
197 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); | 197 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); |
198 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 198 ASSERT_TRUE(root.get()); |
199 ListValue* list = static_cast<ListValue*>(root.get()); | 199 ListValue* list = root->AsList(); |
| 200 ASSERT_TRUE(list); |
200 ASSERT_EQ(1U, list->GetSize()); | 201 ASSERT_EQ(1U, list->GetSize()); |
201 Value* elt = NULL; | 202 Value* elt = NULL; |
202 ASSERT_TRUE(list->Get(0, &elt)); | 203 ASSERT_TRUE(list->Get(0, &elt)); |
203 int value = 0; | 204 int value = 0; |
204 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 205 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
205 ASSERT_EQ(1, value); | 206 ASSERT_EQ(1, value); |
206 } | 207 } |
207 | 208 |
208 } // namespace | 209 } // namespace |
209 | 210 |
210 TEST(JSONValueSerializerTest, JSONReaderComments) { | 211 TEST(JSONValueSerializerTest, JSONReaderComments) { |
211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 212 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 213 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 214 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 215 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
215 ValidateJsonList("[ 1 /* one */ ] /* end */"); | 216 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
216 ValidateJsonList("[ 1 //// ,2\r\n ]"); | 217 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
217 | 218 |
218 scoped_ptr<Value> root; | 219 scoped_ptr<Value> root; |
219 | 220 |
220 // It's ok to have a comment in a string. | 221 // It's ok to have a comment in a string. |
221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); | 222 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); |
222 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 223 ASSERT_TRUE(root.get()); |
223 ListValue* list = static_cast<ListValue*>(root.get()); | 224 ListValue* list = root->AsList(); |
| 225 ASSERT_TRUE(list); |
224 ASSERT_EQ(1U, list->GetSize()); | 226 ASSERT_EQ(1U, list->GetSize()); |
225 Value* elt = NULL; | 227 Value* elt = NULL; |
226 ASSERT_TRUE(list->Get(0, &elt)); | 228 ASSERT_TRUE(list->Get(0, &elt)); |
227 std::string value; | 229 std::string value; |
228 ASSERT_TRUE(elt && elt->GetAsString(&value)); | 230 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
229 ASSERT_EQ("// ok\n /* foo */ ", value); | 231 ASSERT_EQ("// ok\n /* foo */ ", value); |
230 | 232 |
231 // You can't nest comments. | 233 // You can't nest comments. |
232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); | 234 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); |
233 ASSERT_FALSE(root.get()); | 235 ASSERT_FALSE(root.get()); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 FilePath source_file_path; | 331 FilePath source_file_path; |
330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 332 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
331 source_file_path = source_file_path.Append( | 333 source_file_path = source_file_path.Append( |
332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 334 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
333 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 335 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
334 JSONFileValueSerializer serializer(source_file_path); | 336 JSONFileValueSerializer serializer(source_file_path); |
335 scoped_ptr<Value> root; | 337 scoped_ptr<Value> root; |
336 root.reset(serializer.Deserialize(NULL, NULL)); | 338 root.reset(serializer.Deserialize(NULL, NULL)); |
337 ASSERT_TRUE(root.get()); | 339 ASSERT_TRUE(root.get()); |
338 } | 340 } |
OLD | NEW |