JsonParserTest.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.TestProtos;
  33. using Google.Protobuf.WellKnownTypes;
  34. using NUnit.Framework;
  35. using System;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Unit tests for JSON parsing. Some tests are ignored at the moment as the desired behaviour
  40. /// isn't fully known, either in terms of which exceptions should be thrown or whether they should
  41. /// count as valid values.
  42. /// </summary>
  43. public class JsonParserTest
  44. {
  45. // Sanity smoke test
  46. [Test]
  47. public void AllTypesRoundtrip()
  48. {
  49. AssertRoundtrip(SampleMessages.CreateFullTestAllTypes());
  50. }
  51. [Test]
  52. public void Maps()
  53. {
  54. AssertRoundtrip(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } });
  55. AssertRoundtrip(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } });
  56. AssertRoundtrip(new TestMap { MapBoolBool = { { false, true }, { true, false } } });
  57. }
  58. [Test]
  59. [TestCase(" 1 ")]
  60. [TestCase("+1")]
  61. [TestCase("1,000")]
  62. [TestCase("1.5")]
  63. public void IntegerMapKeysAreStrict(string keyText)
  64. {
  65. // Test that integer parsing is strict. We assume that if this is correct for int32,
  66. // it's correct for other numeric key types.
  67. var json = "{ \"mapInt32Int32\": { \"" + keyText + "\" : \"1\" } }";
  68. Assert.Throws<InvalidProtocolBufferException>(() => JsonParser.Default.Parse<TestMap>(json));
  69. }
  70. [Test]
  71. public void SourceContextRoundtrip()
  72. {
  73. AssertRoundtrip(new SourceContext { FileName = "foo.proto" });
  74. }
  75. [Test]
  76. public void SingularWrappers_DefaultNonNullValues()
  77. {
  78. var message = new TestWellKnownTypes
  79. {
  80. StringField = "",
  81. BytesField = ByteString.Empty,
  82. BoolField = false,
  83. FloatField = 0f,
  84. DoubleField = 0d,
  85. Int32Field = 0,
  86. Int64Field = 0,
  87. Uint32Field = 0,
  88. Uint64Field = 0
  89. };
  90. AssertRoundtrip(message);
  91. }
  92. [Test]
  93. public void SingularWrappers_NonDefaultValues()
  94. {
  95. var message = new TestWellKnownTypes
  96. {
  97. StringField = "x",
  98. BytesField = ByteString.CopyFrom(1, 2, 3),
  99. BoolField = true,
  100. FloatField = 12.5f,
  101. DoubleField = 12.25d,
  102. Int32Field = 1,
  103. Int64Field = 2,
  104. Uint32Field = 3,
  105. Uint64Field = 4
  106. };
  107. AssertRoundtrip(message);
  108. }
  109. [Test]
  110. public void SingularWrappers_ExplicitNulls()
  111. {
  112. var message = new TestWellKnownTypes();
  113. var json = new JsonFormatter(new JsonFormatter.Settings(true)).Format(message);
  114. var parsed = JsonParser.Default.Parse<TestWellKnownTypes>(json);
  115. Assert.AreEqual(message, parsed);
  116. }
  117. [Test]
  118. [TestCase(typeof(Int32Value), "32", 32)]
  119. [TestCase(typeof(Int64Value), "32", 32L)]
  120. [TestCase(typeof(UInt32Value), "32", 32U)]
  121. [TestCase(typeof(UInt64Value), "32", 32UL)]
  122. [TestCase(typeof(StringValue), "\"foo\"", "foo")]
  123. [TestCase(typeof(FloatValue), "1.5", 1.5f)]
  124. [TestCase(typeof(DoubleValue), "1.5", 1.5d)]
  125. public void Wrappers_Standalone(System.Type wrapperType, string json, object expectedValue)
  126. {
  127. IMessage parsed = (IMessage) Activator.CreateInstance(wrapperType);
  128. IMessage expected = (IMessage) Activator.CreateInstance(wrapperType);
  129. JsonParser.Default.Merge(parsed, "null");
  130. Assert.AreEqual(expected, parsed);
  131. JsonParser.Default.Merge(parsed, json);
  132. expected.Descriptor.Fields[Wrappers.WrapperValueFieldNumber].Accessor.SetValue(expected, expectedValue);
  133. Assert.AreEqual(expected, parsed);
  134. }
  135. [Test]
  136. public void BytesWrapper_Standalone()
  137. {
  138. ByteString data = ByteString.CopyFrom(1, 2, 3);
  139. // Can't do this with attributes...
  140. var parsed = JsonParser.Default.Parse<BytesValue>("\"" + data.ToBase64() + "\"");
  141. var expected = new BytesValue { Value = data };
  142. Assert.AreEqual(expected, parsed);
  143. }
  144. [Test]
  145. public void RepeatedWrappers()
  146. {
  147. var message = new RepeatedWellKnownTypes
  148. {
  149. BoolField = { true, false },
  150. BytesField = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
  151. DoubleField = { 12.5, -1.5, 0d },
  152. FloatField = { 123.25f, -20f, 0f },
  153. Int32Field = { int.MaxValue, int.MinValue, 0 },
  154. Int64Field = { long.MaxValue, long.MinValue, 0L },
  155. StringField = { "First", "Second", "" },
  156. Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
  157. Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
  158. };
  159. AssertRoundtrip(message);
  160. }
  161. [Test]
  162. public void IndividualWrapperTypes()
  163. {
  164. Assert.AreEqual(new StringValue { Value = "foo" }, StringValue.Parser.ParseJson("\"foo\""));
  165. Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.ParseJson("1"));
  166. // Can parse strings directly too
  167. Assert.AreEqual(new Int32Value { Value = 1 }, Int32Value.Parser.ParseJson("\"1\""));
  168. }
  169. private static void AssertRoundtrip<T>(T message) where T : IMessage<T>, new()
  170. {
  171. var clone = message.Clone();
  172. var json = message.ToString();
  173. var parsed = JsonParser.Default.Parse<T>(json);
  174. Assert.AreEqual(clone, parsed);
  175. }
  176. [Test]
  177. [TestCase("0", 0)]
  178. [TestCase("-0", 0)] // Not entirely clear whether we intend to allow this...
  179. [TestCase("1", 1)]
  180. [TestCase("-1", -1)]
  181. [TestCase("2147483647", 2147483647)]
  182. [TestCase("-2147483648", -2147483648)]
  183. public void StringToInt32_Valid(string jsonValue, int expectedParsedValue)
  184. {
  185. string json = "{ \"singleInt32\": \"" + jsonValue + "\"}";
  186. var parsed = TestAllTypes.Parser.ParseJson(json);
  187. Assert.AreEqual(expectedParsedValue, parsed.SingleInt32);
  188. }
  189. [Test]
  190. [TestCase("+0")]
  191. [TestCase("00")]
  192. [TestCase("-00")]
  193. [TestCase("--1")]
  194. [TestCase("+1")]
  195. [TestCase("1.5")]
  196. [TestCase("1e10")]
  197. [TestCase("2147483648")]
  198. [TestCase("-2147483649")]
  199. public void StringToInt32_Invalid(string jsonValue)
  200. {
  201. string json = "{ \"singleInt32\": \"" + jsonValue + "\"}";
  202. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  203. }
  204. [Test]
  205. [TestCase("0", 0U)]
  206. [TestCase("1", 1U)]
  207. [TestCase("4294967295", 4294967295U)]
  208. public void StringToUInt32_Valid(string jsonValue, uint expectedParsedValue)
  209. {
  210. string json = "{ \"singleUint32\": \"" + jsonValue + "\"}";
  211. var parsed = TestAllTypes.Parser.ParseJson(json);
  212. Assert.AreEqual(expectedParsedValue, parsed.SingleUint32);
  213. }
  214. // Assume that anything non-bounds-related is covered in the Int32 case
  215. [Test]
  216. [TestCase("-1")]
  217. [TestCase("4294967296")]
  218. public void StringToUInt32_Invalid(string jsonValue)
  219. {
  220. string json = "{ \"singleUint32\": \"" + jsonValue + "\"}";
  221. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  222. }
  223. [Test]
  224. [TestCase("0", 0L)]
  225. [TestCase("1", 1L)]
  226. [TestCase("-1", -1L)]
  227. [TestCase("9223372036854775807", 9223372036854775807)]
  228. [TestCase("-9223372036854775808", -9223372036854775808)]
  229. public void StringToInt64_Valid(string jsonValue, long expectedParsedValue)
  230. {
  231. string json = "{ \"singleInt64\": \"" + jsonValue + "\"}";
  232. var parsed = TestAllTypes.Parser.ParseJson(json);
  233. Assert.AreEqual(expectedParsedValue, parsed.SingleInt64);
  234. }
  235. // Assume that anything non-bounds-related is covered in the Int32 case
  236. [Test]
  237. [TestCase("-9223372036854775809")]
  238. [TestCase("9223372036854775808")]
  239. public void StringToInt64_Invalid(string jsonValue)
  240. {
  241. string json = "{ \"singleInt64\": \"" + jsonValue + "\"}";
  242. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  243. }
  244. [Test]
  245. [TestCase("0", 0UL)]
  246. [TestCase("1", 1UL)]
  247. [TestCase("18446744073709551615", 18446744073709551615)]
  248. public void StringToUInt64_Valid(string jsonValue, ulong expectedParsedValue)
  249. {
  250. string json = "{ \"singleUint64\": \"" + jsonValue + "\"}";
  251. var parsed = TestAllTypes.Parser.ParseJson(json);
  252. Assert.AreEqual(expectedParsedValue, parsed.SingleUint64);
  253. }
  254. // Assume that anything non-bounds-related is covered in the Int32 case
  255. [Test]
  256. [TestCase("-1")]
  257. [TestCase("18446744073709551616")]
  258. public void StringToUInt64_Invalid(string jsonValue)
  259. {
  260. string json = "{ \"singleUint64\": \"" + jsonValue + "\"}";
  261. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  262. }
  263. [Test]
  264. [TestCase("0", 0d)]
  265. [TestCase("1", 1d)]
  266. [TestCase("1.000000", 1d)]
  267. [TestCase("1.0000000000000000000000001", 1d)] // We don't notice that we haven't preserved the exact value
  268. [TestCase("-1", -1d)]
  269. [TestCase("1e1", 10d)]
  270. [TestCase("1e01", 10d)] // Leading decimals are allowed in exponents
  271. [TestCase("1E1", 10d)] // Either case is fine
  272. [TestCase("-1e1", -10d)]
  273. [TestCase("1.5e1", 15d)]
  274. [TestCase("-1.5e1", -15d)]
  275. [TestCase("15e-1", 1.5d)]
  276. [TestCase("-15e-1", -1.5d)]
  277. [TestCase("1.79769e308", 1.79769e308)]
  278. [TestCase("-1.79769e308", -1.79769e308)]
  279. [TestCase("Infinity", double.PositiveInfinity)]
  280. [TestCase("-Infinity", double.NegativeInfinity)]
  281. [TestCase("NaN", double.NaN)]
  282. public void StringToDouble_Valid(string jsonValue, double expectedParsedValue)
  283. {
  284. string json = "{ \"singleDouble\": \"" + jsonValue + "\"}";
  285. var parsed = TestAllTypes.Parser.ParseJson(json);
  286. Assert.AreEqual(expectedParsedValue, parsed.SingleDouble);
  287. }
  288. [Test]
  289. [TestCase("1.7977e308")]
  290. [TestCase("-1.7977e308")]
  291. [TestCase("1e309")]
  292. [TestCase("1,0")]
  293. [TestCase("1.0.0")]
  294. [TestCase("+1")]
  295. [TestCase("00")]
  296. [TestCase("--1")]
  297. [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking...
  298. public void StringToDouble_Invalid(string jsonValue)
  299. {
  300. string json = "{ \"singleDouble\": \"" + jsonValue + "\"}";
  301. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  302. }
  303. [Test]
  304. [TestCase("0", 0f)]
  305. [TestCase("1", 1f)]
  306. [TestCase("1.000000", 1f)]
  307. [TestCase("-1", -1f)]
  308. [TestCase("3.402823e38", 3.402823e38f)]
  309. [TestCase("-3.402823e38", -3.402823e38f)]
  310. [TestCase("1.5e1", 15f)]
  311. [TestCase("15e-1", 1.5f)]
  312. public void StringToFloat_Valid(string jsonValue, float expectedParsedValue)
  313. {
  314. string json = "{ \"singleFloat\": \"" + jsonValue + "\"}";
  315. var parsed = TestAllTypes.Parser.ParseJson(json);
  316. Assert.AreEqual(expectedParsedValue, parsed.SingleFloat);
  317. }
  318. [Test]
  319. [TestCase("3.402824e38")]
  320. [TestCase("-3.402824e38")]
  321. [TestCase("1,0")]
  322. [TestCase("1.0.0")]
  323. [TestCase("+1")]
  324. [TestCase("00")]
  325. [TestCase("--1")]
  326. public void StringToFloat_Invalid(string jsonValue)
  327. {
  328. string json = "{ \"singleFloat\": \"" + jsonValue + "\"}";
  329. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  330. }
  331. [Test]
  332. [TestCase("0", 0)]
  333. [TestCase("-0", 0)] // Not entirely clear whether we intend to allow this...
  334. [TestCase("1", 1)]
  335. [TestCase("-1", -1)]
  336. [TestCase("2147483647", 2147483647)]
  337. [TestCase("-2147483648", -2147483648)]
  338. public void NumberToInt32_Valid(string jsonValue, int expectedParsedValue)
  339. {
  340. string json = "{ \"singleInt32\": " + jsonValue + "}";
  341. var parsed = TestAllTypes.Parser.ParseJson(json);
  342. Assert.AreEqual(expectedParsedValue, parsed.SingleInt32);
  343. }
  344. [Test]
  345. [TestCase("+0", typeof(InvalidJsonException))]
  346. [TestCase("00", typeof(InvalidJsonException))]
  347. [TestCase("-00", typeof(InvalidJsonException))]
  348. [TestCase("--1", typeof(InvalidJsonException))]
  349. [TestCase("+1", typeof(InvalidJsonException))]
  350. [TestCase("1.5", typeof(InvalidProtocolBufferException), Ignore = true, Reason = "Desired behaviour unclear")]
  351. [TestCase("1e10", typeof(InvalidProtocolBufferException))]
  352. [TestCase("2147483648", typeof(InvalidProtocolBufferException))]
  353. [TestCase("-2147483649", typeof(InvalidProtocolBufferException))]
  354. public void NumberToInt32_Invalid(string jsonValue, System.Type expectedExceptionType)
  355. {
  356. string json = "{ \"singleInt32\": " + jsonValue + "}";
  357. Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.ParseJson(json));
  358. }
  359. [Test]
  360. [TestCase("0", 0U)]
  361. [TestCase("1", 1U)]
  362. [TestCase("4294967295", 4294967295U)]
  363. public void NumberToUInt32_Valid(string jsonValue, uint expectedParsedValue)
  364. {
  365. string json = "{ \"singleUint32\": " + jsonValue + "}";
  366. var parsed = TestAllTypes.Parser.ParseJson(json);
  367. Assert.AreEqual(expectedParsedValue, parsed.SingleUint32);
  368. }
  369. // Assume that anything non-bounds-related is covered in the Int32 case
  370. [Test]
  371. [TestCase("-1")]
  372. [TestCase("4294967296")]
  373. public void NumberToUInt32_Invalid(string jsonValue)
  374. {
  375. string json = "{ \"singleUint32\": " + jsonValue + "}";
  376. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  377. }
  378. [Test]
  379. [TestCase("0", 0L)]
  380. [TestCase("1", 1L)]
  381. [TestCase("-1", -1L)]
  382. [TestCase("9223372036854775807", 9223372036854775807, Ignore = true, Reason = "Desired behaviour unclear")]
  383. [TestCase("-9223372036854775808", -9223372036854775808, Ignore = true, Reason = "Desired behaviour unclear")]
  384. public void NumberToInt64_Valid(string jsonValue, long expectedParsedValue)
  385. {
  386. string json = "{ \"singleInt64\": " + jsonValue + "}";
  387. var parsed = TestAllTypes.Parser.ParseJson(json);
  388. Assert.AreEqual(expectedParsedValue, parsed.SingleInt64);
  389. }
  390. // Assume that anything non-bounds-related is covered in the Int32 case
  391. [Test]
  392. [TestCase("-9223372036854775809", Ignore = true, Reason = "Desired behaviour unclear")]
  393. [TestCase("9223372036854775808", Ignore = true, Reason = "Desired behaviour unclear")]
  394. public void NumberToInt64_Invalid(string jsonValue)
  395. {
  396. string json = "{ \"singleInt64\": " + jsonValue + "}";
  397. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  398. }
  399. [Test]
  400. [TestCase("0", 0UL)]
  401. [TestCase("1", 1UL)]
  402. [TestCase("18446744073709551615", 18446744073709551615, Ignore = true, Reason = "Desired behaviour unclear")]
  403. public void NumberToUInt64_Valid(string jsonValue, ulong expectedParsedValue)
  404. {
  405. string json = "{ \"singleUint64\": " + jsonValue + "}";
  406. var parsed = TestAllTypes.Parser.ParseJson(json);
  407. Assert.AreEqual(expectedParsedValue, parsed.SingleUint64);
  408. }
  409. // Assume that anything non-bounds-related is covered in the Int32 case
  410. [Test]
  411. [TestCase("-1")]
  412. [TestCase("18446744073709551616")]
  413. public void NumberToUInt64_Invalid(string jsonValue)
  414. {
  415. string json = "{ \"singleUint64\": " + jsonValue + "}";
  416. Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseJson(json));
  417. }
  418. [Test]
  419. [TestCase("0", 0d)]
  420. [TestCase("1", 1d)]
  421. [TestCase("1.000000", 1d)]
  422. [TestCase("1.0000000000000000000000001", 1d)] // We don't notice that we haven't preserved the exact value
  423. [TestCase("-1", -1d)]
  424. [TestCase("1e1", 10d)]
  425. [TestCase("1e01", 10d)] // Leading decimals are allowed in exponents
  426. [TestCase("1E1", 10d)] // Either case is fine
  427. [TestCase("-1e1", -10d)]
  428. [TestCase("1.5e1", 15d)]
  429. [TestCase("-1.5e1", -15d)]
  430. [TestCase("15e-1", 1.5d)]
  431. [TestCase("-15e-1", -1.5d)]
  432. [TestCase("1.79769e308", 1.79769e308)]
  433. [TestCase("-1.79769e308", -1.79769e308)]
  434. public void NumberToDouble_Valid(string jsonValue, double expectedParsedValue)
  435. {
  436. string json = "{ \"singleDouble\": " + jsonValue + "}";
  437. var parsed = TestAllTypes.Parser.ParseJson(json);
  438. Assert.AreEqual(expectedParsedValue, parsed.SingleDouble);
  439. }
  440. [Test]
  441. [TestCase("1.7977e308", Ignore = true, Reason = "Desired behaviour unclear")]
  442. [TestCase("-1.7977e308", Ignore = true, Reason = "Desired behaviour unclear")]
  443. [TestCase("1e309", Ignore = true, Reason = "Desired behaviour unclear")]
  444. [TestCase("1,0")]
  445. [TestCase("1.0.0")]
  446. [TestCase("+1")]
  447. [TestCase("00")]
  448. [TestCase("--1")]
  449. [TestCase("\u00BD")] // 1/2 as a single Unicode character. Just sanity checking...
  450. public void NumberToDouble_Invalid(string jsonValue)
  451. {
  452. string json = "{ \"singleDouble\": " + jsonValue + "}";
  453. Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJson(json));
  454. }
  455. [Test]
  456. [TestCase("0", 0f)]
  457. [TestCase("1", 1f)]
  458. [TestCase("1.000000", 1f)]
  459. [TestCase("-1", -1f)]
  460. [TestCase("3.402823e38", 3.402823e38f)]
  461. [TestCase("-3.402823e38", -3.402823e38f)]
  462. [TestCase("1.5e1", 15f)]
  463. [TestCase("15e-1", 1.5f)]
  464. public void NumberToFloat_Valid(string jsonValue, float expectedParsedValue)
  465. {
  466. string json = "{ \"singleFloat\": " + jsonValue + "}";
  467. var parsed = TestAllTypes.Parser.ParseJson(json);
  468. Assert.AreEqual(expectedParsedValue, parsed.SingleFloat);
  469. }
  470. [Test]
  471. [TestCase("3.402824e38", typeof(InvalidProtocolBufferException))]
  472. [TestCase("-3.402824e38", typeof(InvalidProtocolBufferException))]
  473. [TestCase("1,0", typeof(InvalidJsonException))]
  474. [TestCase("1.0.0", typeof(InvalidJsonException))]
  475. [TestCase("+1", typeof(InvalidJsonException))]
  476. [TestCase("00", typeof(InvalidJsonException))]
  477. [TestCase("--1", typeof(InvalidJsonException))]
  478. public void NumberToFloat_Invalid(string jsonValue, System.Type expectedExceptionType)
  479. {
  480. string json = "{ \"singleFloat\": " + jsonValue + "}";
  481. Assert.Throws(expectedExceptionType, () => TestAllTypes.Parser.ParseJson(json));
  482. }
  483. // The simplest way of testing that the value has parsed correctly is to reformat it,
  484. // as we trust the formatting. In many cases that will give the same result as the input,
  485. // so in those cases we accept an expectedFormatted value of null. Sometimes the results
  486. // will be different though, due to a different number of digits being provided.
  487. [Test]
  488. // Z offset
  489. [TestCase("2015-10-09T14:46:23.123456789Z", null)]
  490. [TestCase("2015-10-09T14:46:23.123456Z", null)]
  491. [TestCase("2015-10-09T14:46:23.123Z", null)]
  492. [TestCase("2015-10-09T14:46:23Z", null)]
  493. [TestCase("2015-10-09T14:46:23.123456000Z", "2015-10-09T14:46:23.123456Z")]
  494. [TestCase("2015-10-09T14:46:23.1234560Z", "2015-10-09T14:46:23.123456Z")]
  495. [TestCase("2015-10-09T14:46:23.123000000Z", "2015-10-09T14:46:23.123Z")]
  496. [TestCase("2015-10-09T14:46:23.1230Z", "2015-10-09T14:46:23.123Z")]
  497. [TestCase("2015-10-09T14:46:23.00Z", "2015-10-09T14:46:23Z")]
  498. // +00:00 offset
  499. [TestCase("2015-10-09T14:46:23.123456789+00:00", "2015-10-09T14:46:23.123456789Z")]
  500. [TestCase("2015-10-09T14:46:23.123456+00:00", "2015-10-09T14:46:23.123456Z")]
  501. [TestCase("2015-10-09T14:46:23.123+00:00", "2015-10-09T14:46:23.123Z")]
  502. [TestCase("2015-10-09T14:46:23+00:00", "2015-10-09T14:46:23Z")]
  503. [TestCase("2015-10-09T14:46:23.123456000+00:00", "2015-10-09T14:46:23.123456Z")]
  504. [TestCase("2015-10-09T14:46:23.1234560+00:00", "2015-10-09T14:46:23.123456Z")]
  505. [TestCase("2015-10-09T14:46:23.123000000+00:00", "2015-10-09T14:46:23.123Z")]
  506. [TestCase("2015-10-09T14:46:23.1230+00:00", "2015-10-09T14:46:23.123Z")]
  507. [TestCase("2015-10-09T14:46:23.00+00:00", "2015-10-09T14:46:23Z")]
  508. // Other offsets (assume by now that the subsecond handling is okay)
  509. [TestCase("2015-10-09T15:46:23.123456789+01:00", "2015-10-09T14:46:23.123456789Z")]
  510. [TestCase("2015-10-09T13:46:23.123456789-01:00", "2015-10-09T14:46:23.123456789Z")]
  511. [TestCase("2015-10-09T15:16:23.123456789+00:30", "2015-10-09T14:46:23.123456789Z")]
  512. [TestCase("2015-10-09T14:16:23.123456789-00:30", "2015-10-09T14:46:23.123456789Z")]
  513. [TestCase("2015-10-09T16:31:23.123456789+01:45", "2015-10-09T14:46:23.123456789Z")]
  514. [TestCase("2015-10-09T13:01:23.123456789-01:45", "2015-10-09T14:46:23.123456789Z")]
  515. [TestCase("2015-10-10T08:46:23.123456789+18:00", "2015-10-09T14:46:23.123456789Z")]
  516. [TestCase("2015-10-08T20:46:23.123456789-18:00", "2015-10-09T14:46:23.123456789Z")]
  517. // Leap years and min/max
  518. [TestCase("2016-02-29T14:46:23.123456789Z", null)]
  519. [TestCase("2000-02-29T14:46:23.123456789Z", null)]
  520. [TestCase("0001-01-01T00:00:00Z", null)]
  521. [TestCase("9999-12-31T23:59:59.999999999Z", null)]
  522. public void Timestamp_Valid(string jsonValue, string expectedFormatted)
  523. {
  524. expectedFormatted = expectedFormatted ?? jsonValue;
  525. string json = "\"" + jsonValue + "\"";
  526. var parsed = Timestamp.Parser.ParseJson(json);
  527. Assert.AreEqual(expectedFormatted, parsed.ToString());
  528. }
  529. [Test]
  530. [TestCase("2015-10-09 14:46:23.123456789Z", Description = "No T between date and time")]
  531. [TestCase("2015/10/09T14:46:23.123456789Z", Description = "Wrong date separators")]
  532. [TestCase("2015-10-09T14.46.23.123456789Z", Description = "Wrong time separators")]
  533. [TestCase("2015-10-09T14:46:23,123456789Z", Description = "Wrong fractional second separators (valid ISO-8601 though)")]
  534. [TestCase(" 2015-10-09T14:46:23.123456789Z", Description = "Whitespace at start")]
  535. [TestCase("2015-10-09T14:46:23.123456789Z ", Description = "Whitespace at end")]
  536. [TestCase("2015-10-09T14:46:23.1234567890", Description = "Too many digits")]
  537. [TestCase("2015-10-09T14:46:23.123456789", Description = "No offset")]
  538. [TestCase("2015-13-09T14:46:23.123456789Z", Description = "Invalid month")]
  539. [TestCase("2015-10-32T14:46:23.123456789Z", Description = "Invalid day")]
  540. [TestCase("2015-10-09T24:00:00.000000000Z", Description = "Invalid hour (valid ISO-8601 though)")]
  541. [TestCase("2015-10-09T14:60:23.123456789Z", Description = "Invalid minutes")]
  542. [TestCase("2015-10-09T14:46:60.123456789Z", Description = "Invalid seconds")]
  543. [TestCase("2015-10-09T14:46:23.123456789+18:01", Description = "Offset too large (positive)")]
  544. [TestCase("2015-10-09T14:46:23.123456789-18:01", Description = "Offset too large (negative)")]
  545. [TestCase("2015-10-09T14:46:23.123456789-00:00", Description = "Local offset (-00:00) makes no sense here")]
  546. [TestCase("0001-01-01T00:00:00+00:01", Description = "Value before earliest when offset applied")]
  547. [TestCase("9999-12-31T23:59:59.999999999-00:01", Description = "Value after latest when offset applied")]
  548. [TestCase("2100-02-29T14:46:23.123456789Z", Description = "Feb 29th on a non-leap-year")]
  549. public void Timestamp_Invalid(string jsonValue)
  550. {
  551. string json = "\"" + jsonValue + "\"";
  552. Assert.Throws<InvalidProtocolBufferException>(() => Timestamp.Parser.ParseJson(json));
  553. }
  554. [Test]
  555. public void StructValue_Null()
  556. {
  557. Assert.AreEqual(new Value { NullValue = 0 }, Value.Parser.ParseJson("null"));
  558. }
  559. [Test]
  560. public void StructValue_String()
  561. {
  562. Assert.AreEqual(new Value { StringValue = "hi" }, Value.Parser.ParseJson("\"hi\""));
  563. }
  564. [Test]
  565. public void StructValue_Bool()
  566. {
  567. Assert.AreEqual(new Value { BoolValue = true }, Value.Parser.ParseJson("true"));
  568. Assert.AreEqual(new Value { BoolValue = false }, Value.Parser.ParseJson("false"));
  569. }
  570. [Test]
  571. public void StructValue_List()
  572. {
  573. Assert.AreEqual(Value.ForList(Value.ForNumber(1), Value.ForString("x")), Value.Parser.ParseJson("[1, \"x\"]"));
  574. }
  575. [Test]
  576. public void ParseListValue()
  577. {
  578. Assert.AreEqual(new ListValue { Values = { Value.ForNumber(1), Value.ForString("x") } }, ListValue.Parser.ParseJson("[1, \"x\"]"));
  579. }
  580. [Test]
  581. public void StructValue_Struct()
  582. {
  583. Assert.AreEqual(
  584. Value.ForStruct(new Struct { Fields = { { "x", Value.ForNumber(1) }, { "y", Value.ForString("z") } } }),
  585. Value.Parser.ParseJson("{ \"x\": 1, \"y\": \"z\" }"));
  586. }
  587. [Test]
  588. public void ParseStruct()
  589. {
  590. Assert.AreEqual(new Struct { Fields = { { "x", Value.ForNumber(1) }, { "y", Value.ForString("z") } } },
  591. Struct.Parser.ParseJson("{ \"x\": 1, \"y\": \"z\" }"));
  592. }
  593. // TODO for duration parsing: upper and lower bounds.
  594. // +/- 315576000000 seconds
  595. [Test]
  596. [TestCase("1.123456789s", null)]
  597. [TestCase("1.123456s", null)]
  598. [TestCase("1.123s", null)]
  599. [TestCase("1.12300s", "1.123s")]
  600. [TestCase("1.12345s", "1.123450s")]
  601. [TestCase("1s", null)]
  602. [TestCase("-1.123456789s", null)]
  603. [TestCase("-1.123456s", null)]
  604. [TestCase("-1.123s", null)]
  605. [TestCase("-1s", null)]
  606. [TestCase("0.123s", null)]
  607. [TestCase("-0.123s", null)]
  608. [TestCase("123456.123s", null)]
  609. [TestCase("-123456.123s", null)]
  610. // Upper and lower bounds
  611. [TestCase("315576000000s", null)]
  612. [TestCase("-315576000000s", null)]
  613. public void Duration_Valid(string jsonValue, string expectedFormatted)
  614. {
  615. expectedFormatted = expectedFormatted ?? jsonValue;
  616. string json = "\"" + jsonValue + "\"";
  617. var parsed = Duration.Parser.ParseJson(json);
  618. Assert.AreEqual(expectedFormatted, parsed.ToString());
  619. }
  620. // The simplest way of testing that the value has parsed correctly is to reformat it,
  621. // as we trust the formatting. In many cases that will give the same result as the input,
  622. // so in those cases we accept an expectedFormatted value of null. Sometimes the results
  623. // will be different though, due to a different number of digits being provided.
  624. [Test]
  625. [TestCase("1.1234567890s", Description = "Too many digits")]
  626. [TestCase("1.123456789", Description = "No suffix")]
  627. [TestCase("1.123456789ss", Description = "Too much suffix")]
  628. [TestCase("1.123456789S", Description = "Upper case suffix")]
  629. [TestCase("+1.123456789s", Description = "Leading +")]
  630. [TestCase(".123456789s", Description = "No integer before the fraction")]
  631. [TestCase("1,123456789s", Description = "Comma as decimal separator")]
  632. [TestCase("1x1.123456789s", Description = "Non-digit in integer part")]
  633. [TestCase("1.1x3456789s", Description = "Non-digit in fractional part")]
  634. [TestCase(" 1.123456789s", Description = "Whitespace before fraction")]
  635. [TestCase("1.123456789s ", Description = "Whitespace after value")]
  636. [TestCase("01.123456789s", Description = "Leading zero (positive)")]
  637. [TestCase("-01.123456789s", Description = "Leading zero (negative)")]
  638. [TestCase("--0.123456789s", Description = "Double minus sign")]
  639. // Violate upper/lower bounds in various ways
  640. [TestCase("315576000001s", Description = "Integer part too large")]
  641. [TestCase("315576000000.000000001s", Description = "Integer part is upper bound; non-zero fraction")]
  642. [TestCase("3155760000000s", Description = "Integer part too long (positive)")]
  643. [TestCase("-3155760000000s", Description = "Integer part too long (negative)")]
  644. public void Duration_Invalid(string jsonValue)
  645. {
  646. string json = "\"" + jsonValue + "\"";
  647. Assert.Throws<InvalidProtocolBufferException>(() => Duration.Parser.ParseJson(json));
  648. }
  649. // Not as many tests for field masks as I'd like; more to be added when we have more
  650. // detailed specifications.
  651. [Test]
  652. [TestCase("")]
  653. [TestCase("foo", "foo")]
  654. [TestCase("foo,bar", "foo", "bar")]
  655. [TestCase("foo.bar", "foo.bar")]
  656. [TestCase("fooBar", "foo_bar")]
  657. [TestCase("fooBar.bazQux", "foo_bar.baz_qux")]
  658. public void FieldMask_Valid(string jsonValue, params string[] expectedPaths)
  659. {
  660. string json = "\"" + jsonValue + "\"";
  661. var parsed = FieldMask.Parser.ParseJson(json);
  662. CollectionAssert.AreEqual(expectedPaths, parsed.Paths);
  663. }
  664. [Test]
  665. public void DataAfterObject()
  666. {
  667. string json = "{} 10";
  668. Assert.Throws<InvalidJsonException>(() => TestAllTypes.Parser.ParseJson(json));
  669. }
  670. }
  671. }