OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2008 Google Inc. All rights reserved. | 3 // Copyright 2008 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 using Google.Protobuf.Reflection; | 33 using Google.Protobuf.Reflection; |
34 using Google.Protobuf.TestProtos; | 34 using Google.Protobuf.TestProtos; |
35 using Google.Protobuf.WellKnownTypes; | 35 using Google.Protobuf.WellKnownTypes; |
36 using NUnit.Framework; | 36 using NUnit.Framework; |
37 using System; | 37 using System; |
38 | 38 |
39 namespace Google.Protobuf | 39 namespace Google.Protobuf |
40 { | 40 { |
41 /// <summary> | 41 /// <summary> |
42 /// Unit tests for JSON parsing. Some tests are ignored at the moment as the
desired behaviour | 42 /// Unit tests for JSON parsing. |
43 /// isn't fully known, either in terms of which exceptions should be thrown
or whether they should | |
44 /// count as valid values. | |
45 /// </summary> | 43 /// </summary> |
46 public class JsonParserTest | 44 public class JsonParserTest |
47 { | 45 { |
48 // Sanity smoke test | 46 // Sanity smoke test |
49 [Test] | 47 [Test] |
50 public void AllTypesRoundtrip() | 48 public void AllTypesRoundtrip() |
51 { | 49 { |
52 AssertRoundtrip(SampleMessages.CreateFullTestAllTypes()); | 50 AssertRoundtrip(SampleMessages.CreateFullTestAllTypes()); |
53 } | 51 } |
54 | 52 |
(...skipping 12 matching lines...) Expand all Loading... |
67 [TestCase("1.5")] | 65 [TestCase("1.5")] |
68 public void IntegerMapKeysAreStrict(string keyText) | 66 public void IntegerMapKeysAreStrict(string keyText) |
69 { | 67 { |
70 // Test that integer parsing is strict. We assume that if this is co
rrect for int32, | 68 // Test that integer parsing is strict. We assume that if this is co
rrect for int32, |
71 // it's correct for other numeric key types. | 69 // it's correct for other numeric key types. |
72 var json = "{ \"mapInt32Int32\": { \"" + keyText + "\" : \"1\" } }"; | 70 var json = "{ \"mapInt32Int32\": { \"" + keyText + "\" : \"1\" } }"; |
73 Assert.Throws<InvalidProtocolBufferException>(() => JsonParser.Defau
lt.Parse<TestMap>(json)); | 71 Assert.Throws<InvalidProtocolBufferException>(() => JsonParser.Defau
lt.Parse<TestMap>(json)); |
74 } | 72 } |
75 | 73 |
76 [Test] | 74 [Test] |
| 75 public void OriginalFieldNameAccepted() |
| 76 { |
| 77 var json = "{ \"single_int32\": 10 }"; |
| 78 var expected = new TestAllTypes { SingleInt32 = 10 }; |
| 79 Assert.AreEqual(expected, TestAllTypes.Parser.ParseJson(json)); |
| 80 } |
| 81 |
| 82 [Test] |
77 public void SourceContextRoundtrip() | 83 public void SourceContextRoundtrip() |
78 { | 84 { |
79 AssertRoundtrip(new SourceContext { FileName = "foo.proto" }); | 85 AssertRoundtrip(new SourceContext { FileName = "foo.proto" }); |
80 } | 86 } |
81 | 87 |
82 [Test] | 88 [Test] |
83 public void SingularWrappers_DefaultNonNullValues() | 89 public void SingularWrappers_DefaultNonNullValues() |
84 { | 90 { |
85 var message = new TestWellKnownTypes | 91 var message = new TestWellKnownTypes |
86 { | 92 { |
(...skipping 24 matching lines...) Expand all Loading... |
111 Int64Field = 2, | 117 Int64Field = 2, |
112 Uint32Field = 3, | 118 Uint32Field = 3, |
113 Uint64Field = 4 | 119 Uint64Field = 4 |
114 }; | 120 }; |
115 AssertRoundtrip(message); | 121 AssertRoundtrip(message); |
116 } | 122 } |
117 | 123 |
118 [Test] | 124 [Test] |
119 public void SingularWrappers_ExplicitNulls() | 125 public void SingularWrappers_ExplicitNulls() |
120 { | 126 { |
121 var message = new TestWellKnownTypes(); | 127 // When we parse the "valueField": null part, we remember it... basi
cally, it's one case |
| 128 // where explicit default values don't fully roundtrip. |
| 129 var message = new TestWellKnownTypes { ValueField = Value.ForNull()
}; |
122 var json = new JsonFormatter(new JsonFormatter.Settings(true)).Forma
t(message); | 130 var json = new JsonFormatter(new JsonFormatter.Settings(true)).Forma
t(message); |
123 var parsed = JsonParser.Default.Parse<TestWellKnownTypes>(json); | 131 var parsed = JsonParser.Default.Parse<TestWellKnownTypes>(json); |
124 Assert.AreEqual(message, parsed); | 132 Assert.AreEqual(message, parsed); |
125 } | 133 } |
126 | 134 |
127 [Test] | 135 [Test] |
128 [TestCase(typeof(Int32Value), "32", 32)] | 136 [TestCase(typeof(Int32Value), "32", 32)] |
129 [TestCase(typeof(Int64Value), "32", 32L)] | 137 [TestCase(typeof(Int64Value), "32", 32L)] |
130 [TestCase(typeof(UInt32Value), "32", 32U)] | 138 [TestCase(typeof(UInt32Value), "32", 32U)] |
131 [TestCase(typeof(UInt64Value), "32", 32UL)] | 139 [TestCase(typeof(UInt64Value), "32", 32UL)] |
132 [TestCase(typeof(StringValue), "\"foo\"", "foo")] | 140 [TestCase(typeof(StringValue), "\"foo\"", "foo")] |
133 [TestCase(typeof(FloatValue), "1.5", 1.5f)] | 141 [TestCase(typeof(FloatValue), "1.5", 1.5f)] |
134 [TestCase(typeof(DoubleValue), "1.5", 1.5d)] | 142 [TestCase(typeof(DoubleValue), "1.5", 1.5d)] |
135 public void Wrappers_Standalone(System.Type wrapperType, string json, ob
ject expectedValue) | 143 public void Wrappers_Standalone(System.Type wrapperType, string json, ob
ject expectedValue) |
136 { | 144 { |
137 IMessage parsed = (IMessage) Activator.CreateInstance(wrapperType); | 145 IMessage parsed = (IMessage)Activator.CreateInstance(wrapperType); |
138 IMessage expected = (IMessage) Activator.CreateInstance(wrapperType)
; | 146 IMessage expected = (IMessage)Activator.CreateInstance(wrapperType); |
139 JsonParser.Default.Merge(parsed, "null"); | 147 JsonParser.Default.Merge(parsed, "null"); |
140 Assert.AreEqual(expected, parsed); | 148 Assert.AreEqual(expected, parsed); |
141 | 149 |
142 JsonParser.Default.Merge(parsed, json); | 150 JsonParser.Default.Merge(parsed, json); |
143 expected.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumbe
r].Accessor.SetValue(expected, expectedValue); | 151 expected.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumbe
r].Accessor.SetValue(expected, expectedValue); |
144 Assert.AreEqual(expected, parsed); | 152 Assert.AreEqual(expected, parsed); |
145 } | 153 } |
146 | 154 |
147 [Test] | 155 [Test] |
| 156 public void ExplicitNullValue() |
| 157 { |
| 158 string json = "{\"valueField\": null}"; |
| 159 var message = JsonParser.Default.Parse<TestWellKnownTypes>(json); |
| 160 Assert.AreEqual(new TestWellKnownTypes { ValueField = Value.ForNull(
) }, message); |
| 161 } |
| 162 |
| 163 [Test] |
148 public void BytesWrapper_Standalone() | 164 public void BytesWrapper_Standalone() |
149 { | 165 { |
150 ByteString data = ByteString.CopyFrom(1, 2, 3); | 166 ByteString data = ByteString.CopyFrom(1, 2, 3); |
151 // Can't do this with attributes... | 167 // Can't do this with attributes... |
152 var parsed = JsonParser.Default.Parse<BytesValue>("\"" + data.ToBase
64() + "\""); | 168 var parsed = JsonParser.Default.Parse<BytesValue>(WrapInQuotes(data.
ToBase64())); |
153 var expected = new BytesValue { Value = data }; | 169 var expected = new BytesValue { Value = data }; |
154 Assert.AreEqual(expected, parsed); | 170 Assert.AreEqual(expected, parsed); |
155 } | 171 } |
156 | 172 |
157 [Test] | 173 [Test] |
158 public void RepeatedWrappers() | 174 public void RepeatedWrappers() |
159 { | 175 { |
160 var message = new RepeatedWellKnownTypes | 176 var message = new RepeatedWellKnownTypes |
161 { | 177 { |
162 BoolField = { true, false }, | 178 BoolField = { true, false }, |
163 BytesField = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom
(4, 5, 6), ByteString.Empty }, | 179 BytesField = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom
(4, 5, 6), ByteString.Empty }, |
164 DoubleField = { 12.5, -1.5, 0d }, | 180 DoubleField = { 12.5, -1.5, 0d }, |
165 FloatField = { 123.25f, -20f, 0f }, | 181 FloatField = { 123.25f, -20f, 0f }, |
166 Int32Field = { int.MaxValue, int.MinValue, 0 }, | 182 Int32Field = { int.MaxValue, int.MinValue, 0 }, |
167 Int64Field = { long.MaxValue, long.MinValue, 0L }, | 183 Int64Field = { long.MaxValue, long.MinValue, 0L }, |
168 StringField = { "First", "Second", "" }, | 184 StringField = { "First", "Second", "" }, |
169 Uint32Field = { uint.MaxValue, uint.MinValue, 0U }, | 185 Uint32Field = { uint.MaxValue, uint.MinValue, 0U }, |
170 Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL }, | 186 Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL }, |
171 }; | 187 }; |
172 AssertRoundtrip(message); | 188 AssertRoundtrip(message); |
173 } | 189 } |
174 | 190 |
175 [Test] | 191 [Test] |
| 192 public void RepeatedField_NullElementProhibited() |
| 193 { |
| 194 string json = "{ \"repeated_foreign_message\": [null] }"; |
| 195 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
| 196 } |
| 197 |
| 198 [Test] |
| 199 public void RepeatedField_NullOverallValueAllowed() |
| 200 { |
| 201 string json = "{ \"repeated_foreign_message\": null }"; |
| 202 Assert.AreEqual(new TestAllTypes(), TestAllTypes.Parser.ParseJson(js
on)); |
| 203 } |
| 204 |
| 205 [Test] |
| 206 [TestCase("{ \"mapInt32Int32\": { \"10\": null }")] |
| 207 [TestCase("{ \"mapStringString\": { \"abc\": null }")] |
| 208 [TestCase("{ \"mapInt32ForeignMessage\": { \"10\": null }")] |
| 209 public void MapField_NullValueProhibited(string json) |
| 210 { |
| 211 Assert.Throws<InvalidProtocolBufferException>(() => TestMap.Parser.P
arseJson(json)); |
| 212 } |
| 213 |
| 214 [Test] |
| 215 public void MapField_NullOverallValueAllowed() |
| 216 { |
| 217 string json = "{ \"mapInt32Int32\": null }"; |
| 218 Assert.AreEqual(new TestMap(), TestMap.Parser.ParseJson(json)); |
| 219 } |
| 220 |
| 221 [Test] |
176 public void IndividualWrapperTypes() | 222 public void IndividualWrapperTypes() |
177 { | 223 { |
178 Assert.AreEqual(new StringValue { Value = "foo" }, StringValue.Parse
r.ParseJson("\"foo\"")); | 224 Assert.AreEqual(new StringValue { Value = "foo" }, StringValue.Parse
r.ParseJson("\"foo\"")); |
179 Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.Pars
eJson("1")); | 225 Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.Pars
eJson("1")); |
180 // Can parse strings directly too | 226 // Can parse strings directly too |
181 Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.Pars
eJson("\"1\"")); | 227 Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.Pars
eJson("\"1\"")); |
182 } | 228 } |
183 | 229 |
184 private static void AssertRoundtrip<T>(T message) where T : IMessage<T>,
new() | 230 private static void AssertRoundtrip<T>(T message) where T : IMessage<T>,
new() |
185 { | 231 { |
186 var clone = message.Clone(); | 232 var clone = message.Clone(); |
187 var json = message.ToString(); | 233 var json = JsonFormatter.Default.Format(message); |
188 var parsed = JsonParser.Default.Parse<T>(json); | 234 var parsed = JsonParser.Default.Parse<T>(json); |
189 Assert.AreEqual(clone, parsed); | 235 Assert.AreEqual(clone, parsed); |
190 } | 236 } |
191 | 237 |
192 [Test] | 238 [Test] |
193 [TestCase("0", 0)] | 239 [TestCase("0", 0)] |
194 [TestCase("-0", 0)] // Not entirely clear whether we intend to allow thi
s... | 240 [TestCase("-0", 0)] // Not entirely clear whether we intend to allow thi
s... |
195 [TestCase("1", 1)] | 241 [TestCase("1", 1)] |
196 [TestCase("-1", -1)] | 242 [TestCase("-1", -1)] |
197 [TestCase("2147483647", 2147483647)] | 243 [TestCase("2147483647", 2147483647)] |
198 [TestCase("-2147483648", -2147483648)] | 244 [TestCase("-2147483648", -2147483648)] |
199 public void StringToInt32_Valid(string jsonValue, int expectedParsedValu
e) | 245 public void StringToInt32_Valid(string jsonValue, int expectedParsedValu
e) |
200 { | 246 { |
201 string json = "{ \"singleInt32\": \"" + jsonValue + "\"}"; | 247 string json = "{ \"singleInt32\": \"" + jsonValue + "\"}"; |
202 var parsed = TestAllTypes.Parser.ParseJson(json); | 248 var parsed = TestAllTypes.Parser.ParseJson(json); |
203 Assert.AreEqual(expectedParsedValue, parsed.SingleInt32); | 249 Assert.AreEqual(expectedParsedValue, parsed.SingleInt32); |
204 } | 250 } |
205 | 251 |
206 [Test] | 252 [Test] |
207 [TestCase("+0")] | 253 [TestCase("+0")] |
| 254 [TestCase(" 1")] |
| 255 [TestCase("1 ")] |
208 [TestCase("00")] | 256 [TestCase("00")] |
209 [TestCase("-00")] | 257 [TestCase("-00")] |
210 [TestCase("--1")] | 258 [TestCase("--1")] |
211 [TestCase("+1")] | 259 [TestCase("+1")] |
212 [TestCase("1.5")] | 260 [TestCase("1.5")] |
213 [TestCase("1e10")] | 261 [TestCase("1e10")] |
214 [TestCase("2147483648")] | 262 [TestCase("2147483648")] |
215 [TestCase("-2147483649")] | 263 [TestCase("-2147483649")] |
216 public void StringToInt32_Invalid(string jsonValue) | 264 public void StringToInt32_Invalid(string jsonValue) |
217 { | 265 { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 } | 359 } |
312 | 360 |
313 [Test] | 361 [Test] |
314 [TestCase("1.7977e308")] | 362 [TestCase("1.7977e308")] |
315 [TestCase("-1.7977e308")] | 363 [TestCase("-1.7977e308")] |
316 [TestCase("1e309")] | 364 [TestCase("1e309")] |
317 [TestCase("1,0")] | 365 [TestCase("1,0")] |
318 [TestCase("1.0.0")] | 366 [TestCase("1.0.0")] |
319 [TestCase("+1")] | 367 [TestCase("+1")] |
320 [TestCase("00")] | 368 [TestCase("00")] |
| 369 [TestCase("01")] |
| 370 [TestCase("-00")] |
| 371 [TestCase("-01")] |
321 [TestCase("--1")] | 372 [TestCase("--1")] |
| 373 [TestCase(" Infinity")] |
| 374 [TestCase(" -Infinity")] |
| 375 [TestCase("NaN ")] |
| 376 [TestCase("Infinity ")] |
| 377 [TestCase("-Infinity ")] |
| 378 [TestCase(" NaN")] |
| 379 [TestCase("INFINITY")] |
| 380 [TestCase("nan")] |
322 [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity c
hecking... | 381 [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity c
hecking... |
323 public void StringToDouble_Invalid(string jsonValue) | 382 public void StringToDouble_Invalid(string jsonValue) |
324 { | 383 { |
325 string json = "{ \"singleDouble\": \"" + jsonValue + "\"}"; | 384 string json = "{ \"singleDouble\": \"" + jsonValue + "\"}"; |
326 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); | 385 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
327 } | 386 } |
328 | 387 |
329 [Test] | 388 [Test] |
330 [TestCase("0", 0f)] | 389 [TestCase("0", 0f)] |
331 [TestCase("1", 1f)] | 390 [TestCase("1", 1f)] |
(...skipping 24 matching lines...) Expand all Loading... |
356 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); | 415 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
357 } | 416 } |
358 | 417 |
359 [Test] | 418 [Test] |
360 [TestCase("0", 0)] | 419 [TestCase("0", 0)] |
361 [TestCase("-0", 0)] // Not entirely clear whether we intend to allow thi
s... | 420 [TestCase("-0", 0)] // Not entirely clear whether we intend to allow thi
s... |
362 [TestCase("1", 1)] | 421 [TestCase("1", 1)] |
363 [TestCase("-1", -1)] | 422 [TestCase("-1", -1)] |
364 [TestCase("2147483647", 2147483647)] | 423 [TestCase("2147483647", 2147483647)] |
365 [TestCase("-2147483648", -2147483648)] | 424 [TestCase("-2147483648", -2147483648)] |
| 425 [TestCase("1e1", 10)] |
| 426 [TestCase("-1e1", -10)] |
| 427 [TestCase("10.00", 10)] |
| 428 [TestCase("-10.00", -10)] |
366 public void NumberToInt32_Valid(string jsonValue, int expectedParsedValu
e) | 429 public void NumberToInt32_Valid(string jsonValue, int expectedParsedValu
e) |
367 { | 430 { |
368 string json = "{ \"singleInt32\": " + jsonValue + "}"; | 431 string json = "{ \"singleInt32\": " + jsonValue + "}"; |
369 var parsed = TestAllTypes.Parser.ParseJson(json); | 432 var parsed = TestAllTypes.Parser.ParseJson(json); |
370 Assert.AreEqual(expectedParsedValue, parsed.SingleInt32); | 433 Assert.AreEqual(expectedParsedValue, parsed.SingleInt32); |
371 } | 434 } |
372 | 435 |
373 [Test] | 436 [Test] |
374 [TestCase("+0", typeof(InvalidJsonException))] | 437 [TestCase("+0", typeof(InvalidJsonException))] |
375 [TestCase("00", typeof(InvalidJsonException))] | 438 [TestCase("00", typeof(InvalidJsonException))] |
376 [TestCase("-00", typeof(InvalidJsonException))] | 439 [TestCase("-00", typeof(InvalidJsonException))] |
377 [TestCase("--1", typeof(InvalidJsonException))] | 440 [TestCase("--1", typeof(InvalidJsonException))] |
378 [TestCase("+1", typeof(InvalidJsonException))] | 441 [TestCase("+1", typeof(InvalidJsonException))] |
379 [TestCase("1.5", typeof(InvalidProtocolBufferException), Ignore = true,
Reason = "Desired behaviour unclear")] | 442 [TestCase("1.5", typeof(InvalidProtocolBufferException))] |
| 443 // Value is out of range |
380 [TestCase("1e10", typeof(InvalidProtocolBufferException))] | 444 [TestCase("1e10", typeof(InvalidProtocolBufferException))] |
381 [TestCase("2147483648", typeof(InvalidProtocolBufferException))] | 445 [TestCase("2147483648", typeof(InvalidProtocolBufferException))] |
382 [TestCase("-2147483649", typeof(InvalidProtocolBufferException))] | 446 [TestCase("-2147483649", typeof(InvalidProtocolBufferException))] |
383 public void NumberToInt32_Invalid(string jsonValue, System.Type expected
ExceptionType) | 447 public void NumberToInt32_Invalid(string jsonValue, System.Type expected
ExceptionType) |
384 { | 448 { |
385 string json = "{ \"singleInt32\": " + jsonValue + "}"; | 449 string json = "{ \"singleInt32\": " + jsonValue + "}"; |
386 Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.Parse
Json(json)); | 450 Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.Parse
Json(json)); |
387 } | 451 } |
388 | 452 |
389 [Test] | 453 [Test] |
(...skipping 14 matching lines...) Expand all Loading... |
404 public void NumberToUInt32_Invalid(string jsonValue) | 468 public void NumberToUInt32_Invalid(string jsonValue) |
405 { | 469 { |
406 string json = "{ \"singleUint32\": " + jsonValue + "}"; | 470 string json = "{ \"singleUint32\": " + jsonValue + "}"; |
407 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); | 471 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
408 } | 472 } |
409 | 473 |
410 [Test] | 474 [Test] |
411 [TestCase("0", 0L)] | 475 [TestCase("0", 0L)] |
412 [TestCase("1", 1L)] | 476 [TestCase("1", 1L)] |
413 [TestCase("-1", -1L)] | 477 [TestCase("-1", -1L)] |
414 [TestCase("9223372036854775807", 9223372036854775807, Ignore = true, Rea
son = "Desired behaviour unclear")] | 478 // long.MaxValue isn't actually representable as a double. This string v
alue is the highest |
415 [TestCase("-9223372036854775808", -9223372036854775808, Ignore = true, R
eason = "Desired behaviour unclear")] | 479 // representable value which isn't greater than long.MaxValue. |
| 480 [TestCase("9223372036854774784", 9223372036854774784)] |
| 481 [TestCase("-9223372036854775808", -9223372036854775808)] |
416 public void NumberToInt64_Valid(string jsonValue, long expectedParsedVal
ue) | 482 public void NumberToInt64_Valid(string jsonValue, long expectedParsedVal
ue) |
417 { | 483 { |
418 string json = "{ \"singleInt64\": " + jsonValue + "}"; | 484 string json = "{ \"singleInt64\": " + jsonValue + "}"; |
419 var parsed = TestAllTypes.Parser.ParseJson(json); | 485 var parsed = TestAllTypes.Parser.ParseJson(json); |
420 Assert.AreEqual(expectedParsedValue, parsed.SingleInt64); | 486 Assert.AreEqual(expectedParsedValue, parsed.SingleInt64); |
421 } | 487 } |
422 | 488 |
423 // Assume that anything non-bounds-related is covered in the Int32 case | 489 // Assume that anything non-bounds-related is covered in the Int32 case |
424 [Test] | 490 [Test] |
425 [TestCase("-9223372036854775809", Ignore = true, Reason = "Desired behav
iour unclear")] | 491 [TestCase("9223372036854775808")] |
426 [TestCase("9223372036854775808", Ignore = true, Reason = "Desired behavi
our unclear")] | 492 // Theoretical bound would be -9223372036854775809, but when that is par
sed to a double |
| 493 // we end up with the exact value of long.MinValue due to lack of precis
ion. The value here |
| 494 // is the "next double down". |
| 495 [TestCase("-9223372036854780000")] |
427 public void NumberToInt64_Invalid(string jsonValue) | 496 public void NumberToInt64_Invalid(string jsonValue) |
428 { | 497 { |
429 string json = "{ \"singleInt64\": " + jsonValue + "}"; | 498 string json = "{ \"singleInt64\": " + jsonValue + "}"; |
430 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); | 499 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
431 } | 500 } |
432 | 501 |
433 [Test] | 502 [Test] |
434 [TestCase("0", 0UL)] | 503 [TestCase("0", 0UL)] |
435 [TestCase("1", 1UL)] | 504 [TestCase("1", 1UL)] |
436 [TestCase("18446744073709551615", 18446744073709551615, Ignore = true, R
eason = "Desired behaviour unclear")] | 505 // ulong.MaxValue isn't representable as a double. This value is the lar
gest double within |
| 506 // the range of ulong. |
| 507 [TestCase("18446744073709549568", 18446744073709549568UL)] |
437 public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedV
alue) | 508 public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedV
alue) |
438 { | 509 { |
439 string json = "{ \"singleUint64\": " + jsonValue + "}"; | 510 string json = "{ \"singleUint64\": " + jsonValue + "}"; |
440 var parsed = TestAllTypes.Parser.ParseJson(json); | 511 var parsed = TestAllTypes.Parser.ParseJson(json); |
441 Assert.AreEqual(expectedParsedValue, parsed.SingleUint64); | 512 Assert.AreEqual(expectedParsedValue, parsed.SingleUint64); |
442 } | 513 } |
443 | 514 |
444 // Assume that anything non-bounds-related is covered in the Int32 case | 515 // Assume that anything non-bounds-related is covered in the Int32 case |
445 [Test] | 516 [Test] |
446 [TestCase("-1")] | 517 [TestCase("-1")] |
(...skipping 21 matching lines...) Expand all Loading... |
468 [TestCase("1.79769e308", 1.79769e308)] | 539 [TestCase("1.79769e308", 1.79769e308)] |
469 [TestCase("-1.79769e308", -1.79769e308)] | 540 [TestCase("-1.79769e308", -1.79769e308)] |
470 public void NumberToDouble_Valid(string jsonValue, double expectedParsed
Value) | 541 public void NumberToDouble_Valid(string jsonValue, double expectedParsed
Value) |
471 { | 542 { |
472 string json = "{ \"singleDouble\": " + jsonValue + "}"; | 543 string json = "{ \"singleDouble\": " + jsonValue + "}"; |
473 var parsed = TestAllTypes.Parser.ParseJson(json); | 544 var parsed = TestAllTypes.Parser.ParseJson(json); |
474 Assert.AreEqual(expectedParsedValue, parsed.SingleDouble); | 545 Assert.AreEqual(expectedParsedValue, parsed.SingleDouble); |
475 } | 546 } |
476 | 547 |
477 [Test] | 548 [Test] |
478 [TestCase("1.7977e308", Ignore = true, Reason = "Desired behaviour uncle
ar")] | 549 [TestCase("1.7977e308")] |
479 [TestCase("-1.7977e308", Ignore = true, Reason = "Desired behaviour uncl
ear")] | 550 [TestCase("-1.7977e308")] |
480 [TestCase("1e309", Ignore = true, Reason = "Desired behaviour unclear")] | 551 [TestCase("1e309")] |
481 [TestCase("1,0")] | 552 [TestCase("1,0")] |
482 [TestCase("1.0.0")] | 553 [TestCase("1.0.0")] |
483 [TestCase("+1")] | 554 [TestCase("+1")] |
484 [TestCase("00")] | 555 [TestCase("00")] |
485 [TestCase("--1")] | 556 [TestCase("--1")] |
486 [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity c
hecking... | 557 [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity c
hecking... |
487 public void NumberToDouble_Invalid(string jsonValue) | 558 public void NumberToDouble_Invalid(string jsonValue) |
488 { | 559 { |
489 string json = "{ \"singleDouble\": " + jsonValue + "}"; | 560 string json = "{ \"singleDouble\": " + jsonValue + "}"; |
490 Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJ
son(json)); | 561 Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJ
son(json)); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 [TestCase("2015-10-08T20:46:23.123456789-18:00", "2015-10-09T14:46:23.12
3456789Z")] | 629 [TestCase("2015-10-08T20:46:23.123456789-18:00", "2015-10-09T14:46:23.12
3456789Z")] |
559 | 630 |
560 // Leap years and min/max | 631 // Leap years and min/max |
561 [TestCase("2016-02-29T14:46:23.123456789Z", null)] | 632 [TestCase("2016-02-29T14:46:23.123456789Z", null)] |
562 [TestCase("2000-02-29T14:46:23.123456789Z", null)] | 633 [TestCase("2000-02-29T14:46:23.123456789Z", null)] |
563 [TestCase("0001-01-01T00:00:00Z", null)] | 634 [TestCase("0001-01-01T00:00:00Z", null)] |
564 [TestCase("9999-12-31T23:59:59.999999999Z", null)] | 635 [TestCase("9999-12-31T23:59:59.999999999Z", null)] |
565 public void Timestamp_Valid(string jsonValue, string expectedFormatted) | 636 public void Timestamp_Valid(string jsonValue, string expectedFormatted) |
566 { | 637 { |
567 expectedFormatted = expectedFormatted ?? jsonValue; | 638 expectedFormatted = expectedFormatted ?? jsonValue; |
568 string json = "\"" + jsonValue + "\""; | 639 string json = WrapInQuotes(jsonValue); |
569 var parsed = Timestamp.Parser.ParseJson(json); | 640 var parsed = Timestamp.Parser.ParseJson(json); |
570 Assert.AreEqual(expectedFormatted, parsed.ToString()); | 641 Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString()); |
571 } | 642 } |
572 | 643 |
573 [Test] | 644 [Test] |
574 [TestCase("2015-10-09 14:46:23.123456789Z", Description = "No T between
date and time")] | 645 [TestCase("2015-10-09 14:46:23.123456789Z", Description = "No T between
date and time")] |
575 [TestCase("2015/10/09T14:46:23.123456789Z", Description = "Wrong date se
parators")] | 646 [TestCase("2015/10/09T14:46:23.123456789Z", Description = "Wrong date se
parators")] |
576 [TestCase("2015-10-09T14.46.23.123456789Z", Description = "Wrong time se
parators")] | 647 [TestCase("2015-10-09T14.46.23.123456789Z", Description = "Wrong time se
parators")] |
577 [TestCase("2015-10-09T14:46:23,123456789Z", Description = "Wrong fractio
nal second separators (valid ISO-8601 though)")] | 648 [TestCase("2015-10-09T14:46:23,123456789Z", Description = "Wrong fractio
nal second separators (valid ISO-8601 though)")] |
578 [TestCase(" 2015-10-09T14:46:23.123456789Z", Description = "Whitespace a
t start")] | 649 [TestCase(" 2015-10-09T14:46:23.123456789Z", Description = "Whitespace a
t start")] |
579 [TestCase("2015-10-09T14:46:23.123456789Z ", Description = "Whitespace a
t end")] | 650 [TestCase("2015-10-09T14:46:23.123456789Z ", Description = "Whitespace a
t end")] |
580 [TestCase("2015-10-09T14:46:23.1234567890", Description = "Too many digi
ts")] | 651 [TestCase("2015-10-09T14:46:23.1234567890", Description = "Too many digi
ts")] |
581 [TestCase("2015-10-09T14:46:23.123456789", Description = "No offset")] | 652 [TestCase("2015-10-09T14:46:23.123456789", Description = "No offset")] |
582 [TestCase("2015-13-09T14:46:23.123456789Z", Description = "Invalid month
")] | 653 [TestCase("2015-13-09T14:46:23.123456789Z", Description = "Invalid month
")] |
583 [TestCase("2015-10-32T14:46:23.123456789Z", Description = "Invalid day")
] | 654 [TestCase("2015-10-32T14:46:23.123456789Z", Description = "Invalid day")
] |
584 [TestCase("2015-10-09T24:00:00.000000000Z", Description = "Invalid hour
(valid ISO-8601 though)")] | 655 [TestCase("2015-10-09T24:00:00.000000000Z", Description = "Invalid hour
(valid ISO-8601 though)")] |
585 [TestCase("2015-10-09T14:60:23.123456789Z", Description = "Invalid minut
es")] | 656 [TestCase("2015-10-09T14:60:23.123456789Z", Description = "Invalid minut
es")] |
586 [TestCase("2015-10-09T14:46:60.123456789Z", Description = "Invalid secon
ds")] | 657 [TestCase("2015-10-09T14:46:60.123456789Z", Description = "Invalid secon
ds")] |
587 [TestCase("2015-10-09T14:46:23.123456789+18:01", Description = "Offset t
oo large (positive)")] | 658 [TestCase("2015-10-09T14:46:23.123456789+18:01", Description = "Offset t
oo large (positive)")] |
588 [TestCase("2015-10-09T14:46:23.123456789-18:01", Description = "Offset t
oo large (negative)")] | 659 [TestCase("2015-10-09T14:46:23.123456789-18:01", Description = "Offset t
oo large (negative)")] |
589 [TestCase("2015-10-09T14:46:23.123456789-00:00", Description = "Local of
fset (-00:00) makes no sense here")] | 660 [TestCase("2015-10-09T14:46:23.123456789-00:00", Description = "Local of
fset (-00:00) makes no sense here")] |
590 [TestCase("0001-01-01T00:00:00+00:01", Description = "Value before earli
est when offset applied")] | 661 [TestCase("0001-01-01T00:00:00+00:01", Description = "Value before earli
est when offset applied")] |
591 [TestCase("9999-12-31T23:59:59.999999999-00:01", Description = "Value af
ter latest when offset applied")] | 662 [TestCase("9999-12-31T23:59:59.999999999-00:01", Description = "Value af
ter latest when offset applied")] |
592 [TestCase("2100-02-29T14:46:23.123456789Z", Description = "Feb 29th on a
non-leap-year")] | 663 [TestCase("2100-02-29T14:46:23.123456789Z", Description = "Feb 29th on a
non-leap-year")] |
593 public void Timestamp_Invalid(string jsonValue) | 664 public void Timestamp_Invalid(string jsonValue) |
594 { | 665 { |
595 string json = "\"" + jsonValue + "\""; | 666 string json = WrapInQuotes(jsonValue); |
596 Assert.Throws<InvalidProtocolBufferException>(() => Timestamp.Parser
.ParseJson(json)); | 667 Assert.Throws<InvalidProtocolBufferException>(() => Timestamp.Parser
.ParseJson(json)); |
597 } | 668 } |
598 | 669 |
599 [Test] | 670 [Test] |
600 public void StructValue_Null() | 671 public void StructValue_Null() |
601 { | 672 { |
602 Assert.AreEqual(new Value { NullValue = 0 }, Value.Parser.ParseJson(
"null")); | 673 Assert.AreEqual(new Value { NullValue = 0 }, Value.Parser.ParseJson(
"null")); |
603 } | 674 } |
604 | 675 |
605 [Test] | 676 [Test] |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
659 [TestCase("0.123s", null)] | 730 [TestCase("0.123s", null)] |
660 [TestCase("-0.123s", null)] | 731 [TestCase("-0.123s", null)] |
661 [TestCase("123456.123s", null)] | 732 [TestCase("123456.123s", null)] |
662 [TestCase("-123456.123s", null)] | 733 [TestCase("-123456.123s", null)] |
663 // Upper and lower bounds | 734 // Upper and lower bounds |
664 [TestCase("315576000000s", null)] | 735 [TestCase("315576000000s", null)] |
665 [TestCase("-315576000000s", null)] | 736 [TestCase("-315576000000s", null)] |
666 public void Duration_Valid(string jsonValue, string expectedFormatted) | 737 public void Duration_Valid(string jsonValue, string expectedFormatted) |
667 { | 738 { |
668 expectedFormatted = expectedFormatted ?? jsonValue; | 739 expectedFormatted = expectedFormatted ?? jsonValue; |
669 string json = "\"" + jsonValue + "\""; | 740 string json = WrapInQuotes(jsonValue); |
670 var parsed = Duration.Parser.ParseJson(json); | 741 var parsed = Duration.Parser.ParseJson(json); |
671 Assert.AreEqual(expectedFormatted, parsed.ToString()); | 742 Assert.AreEqual(WrapInQuotes(expectedFormatted), parsed.ToString()); |
672 } | 743 } |
673 | 744 |
674 // The simplest way of testing that the value has parsed correctly is to
reformat it, | 745 // The simplest way of testing that the value has parsed correctly is to
reformat it, |
675 // as we trust the formatting. In many cases that will give the same res
ult as the input, | 746 // as we trust the formatting. In many cases that will give the same res
ult as the input, |
676 // so in those cases we accept an expectedFormatted value of null. Somet
imes the results | 747 // so in those cases we accept an expectedFormatted value of null. Somet
imes the results |
677 // will be different though, due to a different number of digits being p
rovided. | 748 // will be different though, due to a different number of digits being p
rovided. |
678 [Test] | 749 [Test] |
679 [TestCase("1.1234567890s", Description = "Too many digits")] | 750 [TestCase("1.1234567890s", Description = "Too many digits")] |
680 [TestCase("1.123456789", Description = "No suffix")] | 751 [TestCase("1.123456789", Description = "No suffix")] |
681 [TestCase("1.123456789ss", Description = "Too much suffix")] | 752 [TestCase("1.123456789ss", Description = "Too much suffix")] |
682 [TestCase("1.123456789S", Description = "Upper case suffix")] | 753 [TestCase("1.123456789S", Description = "Upper case suffix")] |
683 [TestCase("+1.123456789s", Description = "Leading +")] | 754 [TestCase("+1.123456789s", Description = "Leading +")] |
684 [TestCase(".123456789s", Description = "No integer before the fraction")
] | 755 [TestCase(".123456789s", Description = "No integer before the fraction")
] |
685 [TestCase("1,123456789s", Description = "Comma as decimal separator")] | 756 [TestCase("1,123456789s", Description = "Comma as decimal separator")] |
686 [TestCase("1x1.123456789s", Description = "Non-digit in integer part")] | 757 [TestCase("1x1.123456789s", Description = "Non-digit in integer part")] |
687 [TestCase("1.1x3456789s", Description = "Non-digit in fractional part")] | 758 [TestCase("1.1x3456789s", Description = "Non-digit in fractional part")] |
688 [TestCase(" 1.123456789s", Description = "Whitespace before fraction")] | 759 [TestCase(" 1.123456789s", Description = "Whitespace before fraction")] |
689 [TestCase("1.123456789s ", Description = "Whitespace after value")] | 760 [TestCase("1.123456789s ", Description = "Whitespace after value")] |
690 [TestCase("01.123456789s", Description = "Leading zero (positive)")] | 761 [TestCase("01.123456789s", Description = "Leading zero (positive)")] |
691 [TestCase("-01.123456789s", Description = "Leading zero (negative)")] | 762 [TestCase("-01.123456789s", Description = "Leading zero (negative)")] |
692 [TestCase("--0.123456789s", Description = "Double minus sign")] | 763 [TestCase("--0.123456789s", Description = "Double minus sign")] |
693 // Violate upper/lower bounds in various ways | 764 // Violate upper/lower bounds in various ways |
694 [TestCase("315576000001s", Description = "Integer part too large")] | 765 [TestCase("315576000001s", Description = "Integer part too large")] |
695 [TestCase("315576000000.000000001s", Description = "Integer part is uppe
r bound; non-zero fraction")] | |
696 [TestCase("3155760000000s", Description = "Integer part too long (positi
ve)")] | 766 [TestCase("3155760000000s", Description = "Integer part too long (positi
ve)")] |
697 [TestCase("-3155760000000s", Description = "Integer part too long (negat
ive)")] | 767 [TestCase("-3155760000000s", Description = "Integer part too long (negat
ive)")] |
698 public void Duration_Invalid(string jsonValue) | 768 public void Duration_Invalid(string jsonValue) |
699 { | 769 { |
700 string json = "\"" + jsonValue + "\""; | 770 string json = WrapInQuotes(jsonValue); |
701 Assert.Throws<InvalidProtocolBufferException>(() => Duration.Parser.
ParseJson(json)); | 771 Assert.Throws<InvalidProtocolBufferException>(() => Duration.Parser.
ParseJson(json)); |
702 } | 772 } |
703 | 773 |
704 // Not as many tests for field masks as I'd like; more to be added when
we have more | 774 // Not as many tests for field masks as I'd like; more to be added when
we have more |
705 // detailed specifications. | 775 // detailed specifications. |
706 | 776 |
707 [Test] | 777 [Test] |
708 [TestCase("")] | 778 [TestCase("")] |
709 [TestCase("foo", "foo")] | 779 [TestCase("foo", "foo")] |
710 [TestCase("foo,bar", "foo", "bar")] | 780 [TestCase("foo,bar", "foo", "bar")] |
711 [TestCase("foo.bar", "foo.bar")] | 781 [TestCase("foo.bar", "foo.bar")] |
712 [TestCase("fooBar", "foo_bar")] | 782 [TestCase("fooBar", "foo_bar")] |
713 [TestCase("fooBar.bazQux", "foo_bar.baz_qux")] | 783 [TestCase("fooBar.bazQux", "foo_bar.baz_qux")] |
714 public void FieldMask_Valid(string jsonValue, params string[] expectedPa
ths) | 784 public void FieldMask_Valid(string jsonValue, params string[] expectedPa
ths) |
715 { | 785 { |
716 string json = "\"" + jsonValue + "\""; | 786 string json = WrapInQuotes(jsonValue); |
717 var parsed = FieldMask.Parser.ParseJson(json); | 787 var parsed = FieldMask.Parser.ParseJson(json); |
718 CollectionAssert.AreEqual(expectedPaths, parsed.Paths); | 788 CollectionAssert.AreEqual(expectedPaths, parsed.Paths); |
719 } | 789 } |
720 | 790 |
721 [Test] | 791 [Test] |
| 792 [TestCase("foo_bar")] |
| 793 public void FieldMask_Invalid(string jsonValue) |
| 794 { |
| 795 string json = WrapInQuotes(jsonValue); |
| 796 Assert.Throws<InvalidProtocolBufferException>(() => FieldMask.Parser
.ParseJson(json)); |
| 797 } |
| 798 |
| 799 [Test] |
722 public void Any_RegularMessage() | 800 public void Any_RegularMessage() |
723 { | 801 { |
724 var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); | 802 var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); |
725 var formatter = new JsonFormatter(new JsonFormatter.Settings(false,
TypeRegistry.FromMessages(TestAllTypes.Descriptor))); | 803 var formatter = new JsonFormatter(new JsonFormatter.Settings(false,
TypeRegistry.FromMessages(TestAllTypes.Descriptor))); |
726 var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessa
ge = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; | 804 var message = new TestAllTypes { SingleInt32 = 10, SingleNestedMessa
ge = new TestAllTypes.Types.NestedMessage { Bb = 20 } }; |
727 var original = Any.Pack(message); | 805 var original = Any.Pack(message); |
728 var json = formatter.Format(original); // This is tested in JsonForm
atterTest | 806 var json = formatter.Format(original); // This is tested in JsonForm
atterTest |
729 var parser = new JsonParser(new JsonParser.Settings(10, registry)); | 807 var parser = new JsonParser(new JsonParser.Settings(10, registry)); |
730 Assert.AreEqual(original, parser.Parse<Any>(json)); | 808 Assert.AreEqual(original, parser.Parse<Any>(json)); |
731 string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessag
e\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllT
ypes\" }"; | 809 string valueFirstJson = "{ \"singleInt32\": 10, \"singleNestedMessag
e\": { \"bb\": 20 }, \"@type\": \"type.googleapis.com/protobuf_unittest.TestAllT
ypes\" }"; |
732 Assert.AreEqual(original, parser.Parse<Any>(valueFirstJson)); | 810 Assert.AreEqual(original, parser.Parse<Any>(valueFirstJson)); |
733 } | 811 } |
734 | 812 |
735 [Test] | 813 [Test] |
| 814 public void Any_CustomPrefix() |
| 815 { |
| 816 var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor); |
| 817 var message = new TestAllTypes { SingleInt32 = 10 }; |
| 818 var original = Any.Pack(message, "custom.prefix/middle-part"); |
| 819 var parser = new JsonParser(new JsonParser.Settings(10, registry)); |
| 820 string json = "{ \"@type\": \"custom.prefix/middle-part/protobuf_uni
ttest.TestAllTypes\", \"singleInt32\": 10 }"; |
| 821 Assert.AreEqual(original, parser.Parse<Any>(json)); |
| 822 } |
| 823 |
| 824 [Test] |
736 public void Any_UnknownType() | 825 public void Any_UnknownType() |
737 { | 826 { |
738 string json = "{ \"@type\": \"type.googleapis.com/bogus\" }"; | 827 string json = "{ \"@type\": \"type.googleapis.com/bogus\" }"; |
739 Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(
json)); | 828 Assert.Throws<InvalidOperationException>(() => Any.Parser.ParseJson(
json)); |
740 } | 829 } |
741 | 830 |
742 [Test] | 831 [Test] |
| 832 public void Any_NoTypeUrl() |
| 833 { |
| 834 string json = "{ \"foo\": \"bar\" }"; |
| 835 Assert.Throws<InvalidProtocolBufferException>(() => Any.Parser.Parse
Json(json)); |
| 836 } |
| 837 |
| 838 [Test] |
743 public void Any_WellKnownType() | 839 public void Any_WellKnownType() |
744 { | 840 { |
745 var registry = TypeRegistry.FromMessages(Timestamp.Descriptor); | 841 var registry = TypeRegistry.FromMessages(Timestamp.Descriptor); |
746 var formatter = new JsonFormatter(new JsonFormatter.Settings(false,
registry)); | 842 var formatter = new JsonFormatter(new JsonFormatter.Settings(false,
registry)); |
747 var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.U
tc).ToTimestamp(); | 843 var timestamp = new DateTime(1673, 6, 19, 12, 34, 56, DateTimeKind.U
tc).ToTimestamp(); |
748 var original = Any.Pack(timestamp); | 844 var original = Any.Pack(timestamp); |
749 var json = formatter.Format(original); // This is tested in JsonForm
atterTest | 845 var json = formatter.Format(original); // This is tested in JsonForm
atterTest |
750 var parser = new JsonParser(new JsonParser.Settings(10, registry)); | 846 var parser = new JsonParser(new JsonParser.Settings(10, registry)); |
751 Assert.AreEqual(original, parser.Parse<Any>(json)); | 847 Assert.AreEqual(original, parser.Parse<Any>(json)); |
752 string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@t
ype\": \"type.googleapis.com/google.protobuf.Timestamp\" }"; | 848 string valueFirstJson = "{ \"value\": \"1673-06-19T12:34:56Z\", \"@t
ype\": \"type.googleapis.com/google.protobuf.Timestamp\" }"; |
(...skipping 29 matching lines...) Expand all Loading... |
782 { | 878 { |
783 string data64 = CodedInputStreamTest.MakeRecursiveMessage(64).ToStri
ng(); | 879 string data64 = CodedInputStreamTest.MakeRecursiveMessage(64).ToStri
ng(); |
784 string data65 = CodedInputStreamTest.MakeRecursiveMessage(65).ToStri
ng(); | 880 string data65 = CodedInputStreamTest.MakeRecursiveMessage(65).ToStri
ng(); |
785 | 881 |
786 var parser64 = new JsonParser(new JsonParser.Settings(64)); | 882 var parser64 = new JsonParser(new JsonParser.Settings(64)); |
787 CodedInputStreamTest.AssertMessageDepth(parser64.Parse<TestRecursive
Message>(data64), 64); | 883 CodedInputStreamTest.AssertMessageDepth(parser64.Parse<TestRecursive
Message>(data64), 64); |
788 Assert.Throws<InvalidProtocolBufferException>(() => parser64.Parse<T
estRecursiveMessage>(data65)); | 884 Assert.Throws<InvalidProtocolBufferException>(() => parser64.Parse<T
estRecursiveMessage>(data65)); |
789 | 885 |
790 var parser63 = new JsonParser(new JsonParser.Settings(63)); | 886 var parser63 = new JsonParser(new JsonParser.Settings(63)); |
791 Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<T
estRecursiveMessage>(data64)); | 887 Assert.Throws<InvalidProtocolBufferException>(() => parser63.Parse<T
estRecursiveMessage>(data64)); |
| 888 } |
792 | 889 |
| 890 [Test] |
| 891 [TestCase("AQI")] |
| 892 [TestCase("_-==")] |
| 893 public void Bytes_InvalidBase64(string badBase64) |
| 894 { |
| 895 string json = "{ \"singleBytes\": \"" + badBase64 + "\" }"; |
| 896 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
| 897 } |
| 898 |
| 899 [Test] |
| 900 [TestCase("\"FOREIGN_BAR\"", ForeignEnum.ForeignBar)] |
| 901 [TestCase("5", ForeignEnum.ForeignBar)] |
| 902 [TestCase("100", (ForeignEnum)100)] |
| 903 public void EnumValid(string value, ForeignEnum expectedValue) |
| 904 { |
| 905 string json = "{ \"singleForeignEnum\": " + value + " }"; |
| 906 var parsed = TestAllTypes.Parser.ParseJson(json); |
| 907 Assert.AreEqual(new TestAllTypes { SingleForeignEnum = expectedValue
}, parsed); |
| 908 } |
| 909 |
| 910 [Test] |
| 911 [TestCase("\"NOT_A_VALID_VALUE\"")] |
| 912 [TestCase("5.5")] |
| 913 public void Enum_Invalid(string value) |
| 914 { |
| 915 string json = "{ \"singleForeignEnum\": " + value + " }"; |
| 916 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
| 917 } |
| 918 |
| 919 [Test] |
| 920 public void OneofDuplicate_Invalid() |
| 921 { |
| 922 string json = "{ \"oneofString\": \"x\", \"oneofUint32\": 10 }"; |
| 923 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseJson(json)); |
| 924 } |
| 925 |
| 926 /// <summary> |
| 927 /// Various tests use strings which have quotes round them for parsing o
r as the result |
| 928 /// of formatting, but without those quotes being specified in the tests
(for the sake of readability). |
| 929 /// This method simply returns the input, wrapped in double quotes. |
| 930 /// </summary> |
| 931 internal static string WrapInQuotes(string text) |
| 932 { |
| 933 return '"' + text + '"'; |
793 } | 934 } |
794 } | 935 } |
795 } | 936 } |
OLD | NEW |