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