Add ICU message format support
[chromium-blink-merge.git] / base / json / json_parser_unittest.cc
blobd88f9ea795225ebea04ec6a2f24ad64eb714f8b7
1 // Copyright (c) 2012 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.
5 #include "base/json/json_parser.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
13 namespace internal {
15 class JSONParserTest : public testing::Test {
16 public:
17 JSONParser* NewTestParser(const std::string& input) {
18 JSONParser* parser = new JSONParser(JSON_PARSE_RFC);
19 parser->start_pos_ = input.data();
20 parser->pos_ = parser->start_pos_;
21 parser->end_pos_ = parser->start_pos_ + input.length();
22 return parser;
25 void TestLastThree(JSONParser* parser) {
26 EXPECT_EQ(',', *parser->NextChar());
27 EXPECT_EQ('|', *parser->NextChar());
28 EXPECT_EQ('\0', *parser->NextChar());
29 EXPECT_EQ(parser->end_pos_, parser->pos_);
33 TEST_F(JSONParserTest, NextChar) {
34 std::string input("Hello world");
35 scoped_ptr<JSONParser> parser(NewTestParser(input));
37 EXPECT_EQ('H', *parser->pos_);
38 for (size_t i = 1; i < input.length(); ++i) {
39 EXPECT_EQ(input[i], *parser->NextChar());
41 EXPECT_EQ(parser->end_pos_, parser->NextChar());
44 TEST_F(JSONParserTest, ConsumeString) {
45 std::string input("\"test\",|");
46 scoped_ptr<JSONParser> parser(NewTestParser(input));
47 scoped_ptr<Value> value(parser->ConsumeString());
48 EXPECT_EQ('"', *parser->pos_);
50 TestLastThree(parser.get());
52 ASSERT_TRUE(value.get());
53 std::string str;
54 EXPECT_TRUE(value->GetAsString(&str));
55 EXPECT_EQ("test", str);
58 TEST_F(JSONParserTest, ConsumeList) {
59 std::string input("[true, false],|");
60 scoped_ptr<JSONParser> parser(NewTestParser(input));
61 scoped_ptr<Value> value(parser->ConsumeList());
62 EXPECT_EQ(']', *parser->pos_);
64 TestLastThree(parser.get());
66 ASSERT_TRUE(value.get());
67 base::ListValue* list;
68 EXPECT_TRUE(value->GetAsList(&list));
69 EXPECT_EQ(2u, list->GetSize());
72 TEST_F(JSONParserTest, ConsumeDictionary) {
73 std::string input("{\"abc\":\"def\"},|");
74 scoped_ptr<JSONParser> parser(NewTestParser(input));
75 scoped_ptr<Value> value(parser->ConsumeDictionary());
76 EXPECT_EQ('}', *parser->pos_);
78 TestLastThree(parser.get());
80 ASSERT_TRUE(value.get());
81 base::DictionaryValue* dict;
82 EXPECT_TRUE(value->GetAsDictionary(&dict));
83 std::string str;
84 EXPECT_TRUE(dict->GetString("abc", &str));
85 EXPECT_EQ("def", str);
88 TEST_F(JSONParserTest, ConsumeLiterals) {
89 // Literal |true|.
90 std::string input("true,|");
91 scoped_ptr<JSONParser> parser(NewTestParser(input));
92 scoped_ptr<Value> value(parser->ConsumeLiteral());
93 EXPECT_EQ('e', *parser->pos_);
95 TestLastThree(parser.get());
97 ASSERT_TRUE(value.get());
98 bool bool_value = false;
99 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
100 EXPECT_TRUE(bool_value);
102 // Literal |false|.
103 input = "false,|";
104 parser.reset(NewTestParser(input));
105 value.reset(parser->ConsumeLiteral());
106 EXPECT_EQ('e', *parser->pos_);
108 TestLastThree(parser.get());
110 ASSERT_TRUE(value.get());
111 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
112 EXPECT_FALSE(bool_value);
114 // Literal |null|.
115 input = "null,|";
116 parser.reset(NewTestParser(input));
117 value.reset(parser->ConsumeLiteral());
118 EXPECT_EQ('l', *parser->pos_);
120 TestLastThree(parser.get());
122 ASSERT_TRUE(value.get());
123 EXPECT_TRUE(value->IsType(Value::TYPE_NULL));
126 TEST_F(JSONParserTest, ConsumeNumbers) {
127 // Integer.
128 std::string input("1234,|");
129 scoped_ptr<JSONParser> parser(NewTestParser(input));
130 scoped_ptr<Value> value(parser->ConsumeNumber());
131 EXPECT_EQ('4', *parser->pos_);
133 TestLastThree(parser.get());
135 ASSERT_TRUE(value.get());
136 int number_i;
137 EXPECT_TRUE(value->GetAsInteger(&number_i));
138 EXPECT_EQ(1234, number_i);
140 // Negative integer.
141 input = "-1234,|";
142 parser.reset(NewTestParser(input));
143 value.reset(parser->ConsumeNumber());
144 EXPECT_EQ('4', *parser->pos_);
146 TestLastThree(parser.get());
148 ASSERT_TRUE(value.get());
149 EXPECT_TRUE(value->GetAsInteger(&number_i));
150 EXPECT_EQ(-1234, number_i);
152 // Double.
153 input = "12.34,|";
154 parser.reset(NewTestParser(input));
155 value.reset(parser->ConsumeNumber());
156 EXPECT_EQ('4', *parser->pos_);
158 TestLastThree(parser.get());
160 ASSERT_TRUE(value.get());
161 double number_d;
162 EXPECT_TRUE(value->GetAsDouble(&number_d));
163 EXPECT_EQ(12.34, number_d);
165 // Scientific.
166 input = "42e3,|";
167 parser.reset(NewTestParser(input));
168 value.reset(parser->ConsumeNumber());
169 EXPECT_EQ('3', *parser->pos_);
171 TestLastThree(parser.get());
173 ASSERT_TRUE(value.get());
174 EXPECT_TRUE(value->GetAsDouble(&number_d));
175 EXPECT_EQ(42000, number_d);
177 // Negative scientific.
178 input = "314159e-5,|";
179 parser.reset(NewTestParser(input));
180 value.reset(parser->ConsumeNumber());
181 EXPECT_EQ('5', *parser->pos_);
183 TestLastThree(parser.get());
185 ASSERT_TRUE(value.get());
186 EXPECT_TRUE(value->GetAsDouble(&number_d));
187 EXPECT_EQ(3.14159, number_d);
189 // Positive scientific.
190 input = "0.42e+3,|";
191 parser.reset(NewTestParser(input));
192 value.reset(parser->ConsumeNumber());
193 EXPECT_EQ('3', *parser->pos_);
195 TestLastThree(parser.get());
197 ASSERT_TRUE(value.get());
198 EXPECT_TRUE(value->GetAsDouble(&number_d));
199 EXPECT_EQ(420, number_d);
202 TEST_F(JSONParserTest, ErrorMessages) {
203 // Error strings should not be modified in case of success.
204 std::string error_message;
205 int error_code = 0;
206 scoped_ptr<Value> root;
207 root.reset(JSONReader::DeprecatedReadAndReturnError(
208 "[42]", JSON_PARSE_RFC, &error_code, &error_message));
209 EXPECT_TRUE(error_message.empty());
210 EXPECT_EQ(0, error_code);
212 // Test line and column counting
213 const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
214 // error here ----------------------------------^
215 root.reset(JSONReader::DeprecatedReadAndReturnError(
216 big_json, JSON_PARSE_RFC, &error_code, &error_message));
217 EXPECT_FALSE(root.get());
218 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
219 error_message);
220 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
222 error_code = 0;
223 error_message = "";
224 // Test line and column counting with "\r\n" line ending
225 const char big_json_crlf[] =
226 "[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
227 // error here ----------------------^
228 root.reset(JSONReader::DeprecatedReadAndReturnError(
229 big_json_crlf, JSON_PARSE_RFC, &error_code, &error_message));
230 EXPECT_FALSE(root.get());
231 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
232 error_message);
233 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
235 // Test each of the error conditions
236 root.reset(JSONReader::DeprecatedReadAndReturnError(
237 "{},{}", JSON_PARSE_RFC, &error_code, &error_message));
238 EXPECT_FALSE(root.get());
239 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
240 JSONReader::kUnexpectedDataAfterRoot), error_message);
241 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
243 std::string nested_json;
244 for (int i = 0; i < 101; ++i) {
245 nested_json.insert(nested_json.begin(), '[');
246 nested_json.append(1, ']');
248 root.reset(JSONReader::DeprecatedReadAndReturnError(
249 nested_json, JSON_PARSE_RFC, &error_code, &error_message));
250 EXPECT_FALSE(root.get());
251 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 100, JSONReader::kTooMuchNesting),
252 error_message);
253 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
255 root.reset(JSONReader::DeprecatedReadAndReturnError(
256 "[1,]", JSON_PARSE_RFC, &error_code, &error_message));
257 EXPECT_FALSE(root.get());
258 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
259 error_message);
260 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
262 root.reset(JSONReader::DeprecatedReadAndReturnError(
263 "{foo:\"bar\"}", JSON_PARSE_RFC, &error_code, &error_message));
264 EXPECT_FALSE(root.get());
265 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
266 JSONReader::kUnquotedDictionaryKey), error_message);
267 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
269 root.reset(JSONReader::DeprecatedReadAndReturnError(
270 "{\"foo\":\"bar\",}", JSON_PARSE_RFC, &error_code, &error_message));
271 EXPECT_FALSE(root.get());
272 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
273 error_message);
275 root.reset(JSONReader::DeprecatedReadAndReturnError(
276 "[nu]", JSON_PARSE_RFC, &error_code, &error_message));
277 EXPECT_FALSE(root.get());
278 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
279 error_message);
280 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
282 root.reset(JSONReader::DeprecatedReadAndReturnError(
283 "[\"xxx\\xq\"]", JSON_PARSE_RFC, &error_code, &error_message));
284 EXPECT_FALSE(root.get());
285 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
286 error_message);
287 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
289 root.reset(JSONReader::DeprecatedReadAndReturnError(
290 "[\"xxx\\uq\"]", JSON_PARSE_RFC, &error_code, &error_message));
291 EXPECT_FALSE(root.get());
292 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
293 error_message);
294 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
296 root.reset(JSONReader::DeprecatedReadAndReturnError(
297 "[\"xxx\\q\"]", JSON_PARSE_RFC, &error_code, &error_message));
298 EXPECT_FALSE(root.get());
299 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
300 error_message);
301 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
304 TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
305 // This test strings contains a 4 byte unicode character (a smiley!) that the
306 // reader should be able to handle (the character is \xf0\x9f\x98\x87).
307 const char kUtf8Data[] =
308 "[\"😇\",[],[],[],{\"google:suggesttype\":[]}]";
309 std::string error_message;
310 int error_code = 0;
311 scoped_ptr<Value> root(JSONReader::DeprecatedReadAndReturnError(
312 kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message));
313 EXPECT_TRUE(root.get()) << error_message;
316 TEST_F(JSONParserTest, DecodeUnicodeNonCharacter) {
317 // Tests Unicode code points (encoded as escaped UTF-16) that are not valid
318 // characters.
319 EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]"));
320 EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]"));
321 EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]"));
324 } // namespace internal
325 } // namespace base