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() && root->AsList()); |
199 ListValue* list = static_cast<ListValue*>(root.get()); | 199 ListValue* list = static_cast<ListValue*>(root.get()); |
Evan Martin
2011/08/23 18:15:26
ASSERT_TRUE(root.get());
ListValue* list = root->A
tfarina
2011/08/23 18:26:36
Done.
| |
200 ASSERT_EQ(1U, list->GetSize()); | 200 ASSERT_EQ(1U, list->GetSize()); |
201 Value* elt = NULL; | 201 Value* elt = NULL; |
202 ASSERT_TRUE(list->Get(0, &elt)); | 202 ASSERT_TRUE(list->Get(0, &elt)); |
203 int value = 0; | 203 int value = 0; |
204 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 204 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
205 ASSERT_EQ(1, value); | 205 ASSERT_EQ(1, value); |
206 } | 206 } |
207 | 207 |
208 } // namespace | 208 } // namespace |
209 | 209 |
210 TEST(JSONValueSerializerTest, JSONReaderComments) { | 210 TEST(JSONValueSerializerTest, JSONReaderComments) { |
211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 211 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 212 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 213 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 214 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
215 ValidateJsonList("[ 1 /* one */ ] /* end */"); | 215 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
216 ValidateJsonList("[ 1 //// ,2\r\n ]"); | 216 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
217 | 217 |
218 scoped_ptr<Value> root; | 218 scoped_ptr<Value> root; |
219 | 219 |
220 // It's ok to have a comment in a string. | 220 // It's ok to have a comment in a string. |
221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); | 221 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); |
222 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 222 ASSERT_TRUE(root.get() && root->AsList()); |
223 ListValue* list = static_cast<ListValue*>(root.get()); | 223 ListValue* list = static_cast<ListValue*>(root.get()); |
224 ASSERT_EQ(1U, list->GetSize()); | 224 ASSERT_EQ(1U, list->GetSize()); |
225 Value* elt = NULL; | 225 Value* elt = NULL; |
226 ASSERT_TRUE(list->Get(0, &elt)); | 226 ASSERT_TRUE(list->Get(0, &elt)); |
227 std::string value; | 227 std::string value; |
228 ASSERT_TRUE(elt && elt->GetAsString(&value)); | 228 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
229 ASSERT_EQ("// ok\n /* foo */ ", value); | 229 ASSERT_EQ("// ok\n /* foo */ ", value); |
230 | 230 |
231 // You can't nest comments. | 231 // You can't nest comments. |
232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); | 232 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 FilePath source_file_path; | 329 FilePath source_file_path; |
330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
331 source_file_path = source_file_path.Append( | 331 source_file_path = source_file_path.Append( |
332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
333 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 333 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
334 JSONFileValueSerializer serializer(source_file_path); | 334 JSONFileValueSerializer serializer(source_file_path); |
335 scoped_ptr<Value> root; | 335 scoped_ptr<Value> root; |
336 root.reset(serializer.Deserialize(NULL, NULL)); | 336 root.reset(serializer.Deserialize(NULL, NULL)); |
337 ASSERT_TRUE(root.get()); | 337 ASSERT_TRUE(root.get()); |
338 } | 338 } |
OLD | NEW |