PageRenderTime 57ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Json.Test.Integration/JsonValueDynamicTests.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 1098 lines | 920 code | 129 blank | 49 comment | 57 complexity | 7743a82938232fb2816ba85a25de9a57 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Dynamic;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using System.Runtime.Serialization.Json;
  6. using Xunit;
  7. namespace System.Json
  8. {
  9. /// <summary>
  10. /// Tests for the dynamic support for <see cref="JsonValue"/>.
  11. /// </summary>
  12. public class JsonValueDynamicTests
  13. {
  14. string teamNameValue = "WCF RIA Base";
  15. string[] teamMembersValues = { "Carlos", "Chris", "Joe", "Miguel", "Yavor" };
  16. /// <summary>
  17. /// Tests for the dynamic getters in <see cref="JsonObject"/> instances.
  18. /// </summary>
  19. [Fact]
  20. public void JsonObjectDynamicGetters()
  21. {
  22. dynamic team = new JsonObject();
  23. team["TeamSize"] = this.teamMembersValues.Length;
  24. team["TeamName"] = this.teamNameValue;
  25. team["TeamMascots"] = null;
  26. team["TeamMembers"] = new JsonArray
  27. {
  28. this.teamMembersValues[0], this.teamMembersValues[1], this.teamMembersValues[2],
  29. this.teamMembersValues[3], this.teamMembersValues[4]
  30. };
  31. Assert.Equal(this.teamMembersValues.Length, (int)team.TeamSize);
  32. Assert.Equal(this.teamNameValue, (string)team.TeamName);
  33. Assert.NotNull(team.TeamMascots);
  34. Assert.True(team.TeamMascots is JsonValue); // default
  35. for (int i = 0; i < this.teamMembersValues.Length; i++)
  36. {
  37. Assert.Equal(this.teamMembersValues[i], (string)team.TeamMembers[i]);
  38. }
  39. for (int i = 0; i < this.teamMembersValues.Length; i++)
  40. {
  41. Assert.Equal(this.teamMembersValues[i], (string)team.TeamMembers[i]);
  42. }
  43. // Negative tests for getters
  44. JsonValueTests.ExpectException<InvalidCastException>(delegate { int fail = (int)team.NonExistentProp; });
  45. }
  46. /// <summary>
  47. /// Tests for the dynamic setters in <see cref="JsonObject"/> instances.
  48. /// </summary>
  49. [Fact]
  50. public void JsonObjectDynamicSetters()
  51. {
  52. dynamic team = new JsonObject();
  53. team.TeamSize = this.teamMembersValues.Length;
  54. team.TeamName = this.teamNameValue;
  55. team.TeamMascots = null;
  56. team.TeamMembers = new JsonArray
  57. {
  58. this.teamMembersValues[0], this.teamMembersValues[1], this.teamMembersValues[2],
  59. this.teamMembersValues[3], this.teamMembersValues[4]
  60. };
  61. Assert.Equal(this.teamMembersValues.Length, (int)team["TeamSize"]);
  62. Assert.Equal(this.teamNameValue, (string)team["TeamName"]);
  63. Assert.NotNull(team["TeamMascots"]);
  64. Assert.True(team["TeamMascots"] is JsonValue);
  65. for (int i = 0; i < this.teamMembersValues.Length; i++)
  66. {
  67. Assert.Equal(this.teamMembersValues[i], (string)team["TeamMembers"][i]);
  68. }
  69. // Could not come up with negative setter
  70. }
  71. /// <summary>
  72. /// Tests for the dynamic indexers in <see cref="JsonArray"/> instances.
  73. /// </summary>
  74. [Fact]
  75. public void JsonArrayDynamicSanity()
  76. {
  77. // Sanity test for JsonArray to ensure [] still works even if dynamic
  78. dynamic people = new JsonArray();
  79. foreach (string member in this.teamMembersValues)
  80. {
  81. people.Add(member);
  82. }
  83. Assert.Equal(this.teamMembersValues[0], (string)people[0]);
  84. Assert.Equal(this.teamMembersValues[1], (string)people[1]);
  85. Assert.Equal(this.teamMembersValues[2], (string)people[2]);
  86. Assert.Equal(this.teamMembersValues[3], (string)people[3]);
  87. Assert.Equal(this.teamMembersValues[4], (string)people[4]);
  88. // Note: this test and the above execute the dynamic binder differently.
  89. for (int i = 0; i < people.Count; i++)
  90. {
  91. Assert.Equal(this.teamMembersValues[i], (string)people[i]);
  92. }
  93. people.Add(this.teamMembersValues.Length);
  94. people.Add(this.teamNameValue);
  95. Assert.Equal(this.teamMembersValues.Length, (int)people[5]);
  96. Assert.Equal(this.teamNameValue, (string)people[6]);
  97. }
  98. /// <summary>
  99. /// Tests for calling methods in dynamic references to <see cref="JsonValue"/> instances.
  100. /// </summary>
  101. [Fact]
  102. public void DynamicMethodCalling()
  103. {
  104. JsonObject jo = new JsonObject();
  105. dynamic dyn = jo;
  106. dyn.Foo = "bar";
  107. Assert.Equal(1, jo.Count);
  108. Assert.Equal(1, dyn.Count);
  109. dyn.Remove("Foo");
  110. Assert.Equal(0, jo.Count);
  111. }
  112. /// <summary>
  113. /// Tests for using boolean operators in dynamic references to <see cref="JsonValue"/> instances.
  114. /// </summary>
  115. [Fact(Skip = "Ignore")]
  116. public void DynamicBooleanOperators()
  117. {
  118. JsonValue jv;
  119. dynamic dyn;
  120. foreach (bool value in new bool[] { true, false })
  121. {
  122. jv = value;
  123. dyn = jv;
  124. Log.Info("IsTrue, {0}", jv);
  125. if (dyn)
  126. {
  127. Assert.True(value, "Boolean evaluation should not enter 'if' clause.");
  128. }
  129. else
  130. {
  131. Assert.False(value, "Boolean evaluation should not enter 'else' clause.");
  132. }
  133. }
  134. foreach (string value in new string[] { "true", "false", "True", "False" })
  135. {
  136. bool isTrueValue = value.Equals("true", StringComparison.InvariantCultureIgnoreCase);
  137. jv = new JsonPrimitive(value);
  138. dyn = jv;
  139. Log.Info("IsTrue, {0}", jv);
  140. if (dyn)
  141. {
  142. Assert.True(isTrueValue, "Boolean evaluation should not enter 'if' clause.");
  143. }
  144. else
  145. {
  146. Assert.False(isTrueValue, "Boolean evaluation should not enter 'else' clause.");
  147. }
  148. }
  149. foreach (bool first in new bool[] { false, true })
  150. {
  151. dynamic dyn1 = new JsonPrimitive(first);
  152. Log.Info("Negation, {0}", first);
  153. Assert.Equal(!first, !dyn1);
  154. foreach (bool second in new bool[] { false, true })
  155. {
  156. dynamic dyn2 = new JsonPrimitive(second);
  157. Log.Info("Boolean AND, {0} && {1}", first, second);
  158. Assert.Equal(first && second, (bool)(dyn1 && dyn2));
  159. Log.Info("Boolean OR, {0} && {1}", first, second);
  160. Assert.Equal(first || second, (bool)(dyn1 || dyn2));
  161. }
  162. }
  163. Log.Info("Invalid boolean operator usage");
  164. dynamic boolDyn = new JsonPrimitive(true);
  165. dynamic intDyn = new JsonPrimitive(1);
  166. dynamic strDyn = new JsonPrimitive("hello");
  167. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", !intDyn); });
  168. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", !strDyn); });
  169. JsonValueTests.ExpectException<InvalidCastException>(() => { Log.Info("{0}", intDyn && intDyn); });
  170. JsonValueTests.ExpectException<InvalidCastException>(() => { Log.Info("{0}", intDyn || true); });
  171. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", boolDyn && 1); });
  172. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", boolDyn && intDyn); });
  173. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", boolDyn && "hello"); });
  174. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", boolDyn && strDyn); });
  175. JsonValueTests.ExpectException<FormatException>(() => { Log.Info("{0}", strDyn && boolDyn); });
  176. JsonValueTests.ExpectException<FormatException>(() => { Log.Info("{0}", strDyn || true); });
  177. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", !intDyn.NotHere); });
  178. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", !intDyn.NotHere && true); });
  179. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info("{0}", !intDyn.NotHere || false); });
  180. }
  181. /// <summary>
  182. /// Tests for using relational operators in dynamic references to <see cref="JsonValue"/> instances.
  183. /// </summary>
  184. [Fact(Skip = "Ignore")]
  185. public void DynamicRelationalOperators()
  186. {
  187. JsonValue jv = new JsonObject { { "one", 1 }, { "one_point_two", 1.2 }, { "decimal_one_point_one", 1.1m }, { "trueValue", true }, { "str", "hello" } };
  188. dynamic dyn = jv;
  189. JsonValue defaultJsonValue = jv.ValueOrDefault(-1);
  190. Log.Info("Equality");
  191. Assert.True(dyn.one == 1);
  192. Assert.True(dyn.one_point_two == 1.2);
  193. Assert.False(dyn.one == 1.2);
  194. Assert.False(dyn.one_point_two == 1);
  195. Assert.False(dyn.one == 2);
  196. Assert.False(dyn.one_point_two == 1.3);
  197. Assert.True(dyn.one == 1m);
  198. Assert.False(dyn.one == 2m);
  199. Assert.True(dyn.decimal_one_point_one == 1.1m);
  200. Assert.True(dyn.NotHere == null);
  201. Assert.True(dyn.NotHere == dyn.NotHere);
  202. Assert.True(dyn.NotHere == defaultJsonValue);
  203. // DISABLED, 197375, Assert.False(dyn.NotHere == 1);
  204. Assert.False(dyn.NotHere == jv);
  205. Log.Info("Inequality");
  206. Assert.False(dyn.one != 1);
  207. Assert.False(dyn.one_point_two != 1.2);
  208. Assert.True(dyn.one != 1.2);
  209. Assert.True(dyn.one_point_two != 1);
  210. Assert.True(dyn.one != 2);
  211. Assert.True(dyn.one_point_two != 1.3);
  212. Assert.False(dyn.one != 1m);
  213. Assert.True(dyn.one != 2m);
  214. Assert.False(dyn.NotHere != null);
  215. Assert.False(dyn.NotHere != dyn.NotHere);
  216. Assert.False(dyn.NotHere != defaultJsonValue);
  217. // DISABLED, 197375, Assert.True(dyn.NotHere != 1);
  218. Assert.True(dyn.NotHere != jv);
  219. Log.Info("Less than");
  220. Assert.True(dyn.one < 2);
  221. Assert.False(dyn.one < 1);
  222. Assert.False(dyn.one < 0);
  223. Assert.True(dyn.one_point_two < 1.3);
  224. Assert.False(dyn.one_point_two < 1.2);
  225. Assert.False(dyn.one_point_two < 1.1);
  226. Assert.True(dyn.one < 1.1);
  227. Assert.Equal(1 < 1.0, dyn.one < 1.0);
  228. Assert.False(dyn.one < 0.9);
  229. Assert.True(dyn.one_point_two < 2);
  230. Assert.False(dyn.one_point_two < 1);
  231. Assert.Equal(1.2 < 1.2f, dyn.one_point_two < 1.2f);
  232. Log.Info("Greater than");
  233. Assert.False(dyn.one > 2);
  234. Assert.False(dyn.one > 1);
  235. Assert.True(dyn.one > 0);
  236. Assert.False(dyn.one_point_two > 1.3);
  237. Assert.False(dyn.one_point_two > 1.2);
  238. Assert.True(dyn.one_point_two > 1.1);
  239. Assert.False(dyn.one > 1.1);
  240. Assert.Equal(1 > 1.0, dyn.one > 1.0);
  241. Assert.True(dyn.one > 0.9);
  242. Assert.False(dyn.one_point_two > 2);
  243. Assert.True(dyn.one_point_two > 1);
  244. Assert.Equal(1.2 > 1.2f, dyn.one_point_two > 1.2f);
  245. Log.Info("Less than or equals");
  246. Assert.True(dyn.one <= 2);
  247. Assert.True(dyn.one <= 1);
  248. Assert.False(dyn.one <= 0);
  249. Assert.True(dyn.one_point_two <= 1.3);
  250. Assert.True(dyn.one_point_two <= 1.2);
  251. Assert.False(dyn.one_point_two <= 1.1);
  252. Assert.True(dyn.one <= 1.1);
  253. Assert.Equal(1 <= 1.0, dyn.one <= 1.0);
  254. Assert.False(dyn.one <= 0.9);
  255. Assert.True(dyn.one_point_two <= 2);
  256. Assert.False(dyn.one_point_two <= 1);
  257. Assert.Equal(1.2 <= 1.2f, dyn.one_point_two <= 1.2f);
  258. Log.Info("Greater than or equals");
  259. Assert.False(dyn.one >= 2);
  260. Assert.True(dyn.one >= 1);
  261. Assert.True(dyn.one >= 0);
  262. Assert.False(dyn.one_point_two >= 1.3);
  263. Assert.True(dyn.one_point_two >= 1.2);
  264. Assert.True(dyn.one_point_two >= 1.1);
  265. Assert.False(dyn.one >= 1.1);
  266. Assert.Equal(1 >= 1.0, dyn.one >= 1.0);
  267. Assert.True(dyn.one >= 0.9);
  268. Assert.False(dyn.one_point_two >= 2);
  269. Assert.True(dyn.one_point_two >= 1);
  270. Assert.Equal(1.2 >= 1.2f, dyn.one_point_two >= 1.2f);
  271. Log.Info("Invalid number conversions");
  272. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info(dyn.decimal_one_point_one == 1.1); });
  273. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info(dyn.one != (uint)2); });
  274. Log.Info("Invalid data types for relational operators");
  275. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info(dyn.trueValue >= dyn.trueValue); });
  276. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info(dyn.NotHere < dyn.NotHere); });
  277. JsonValueTests.ExpectException<InvalidOperationException>(() => { Log.Info(dyn.str < "Jello"); });
  278. // DISABLED, 197315
  279. Log.Info("Conversions from string");
  280. jv = new JsonObject { { "one", "1" }, { "twelve_point_two", "1.22e1" } };
  281. dyn = jv;
  282. Assert.True(dyn.one == 1);
  283. Assert.True(dyn.twelve_point_two == 1.22e1);
  284. Assert.True(dyn.one >= 0.5f);
  285. Assert.True(dyn.twelve_point_two <= 13);
  286. Assert.True(dyn.one < 2);
  287. Assert.Equal(dyn.twelve_point_two.ReadAs<int>() > 12, dyn.twelve_point_two > 12);
  288. }
  289. /// <summary>
  290. /// Tests for using arithmetic operators in dynamic references to <see cref="JsonValue"/> instances.
  291. /// </summary>
  292. [Fact(Skip = "Ignore")]
  293. public void ArithmeticOperators()
  294. {
  295. int seed = MethodBase.GetCurrentMethod().Name.GetHashCode();
  296. Log.Info("Seed: {0}", seed);
  297. Random rndGen = new Random(seed);
  298. int i1 = rndGen.Next(-10000, 10000);
  299. int i2 = rndGen.Next(-10000, 10000);
  300. JsonValue jv1 = i1;
  301. JsonValue jv2 = i2;
  302. Log.Info("jv1 = {0}, jv2 = {1}", jv1, jv2);
  303. dynamic dyn1 = jv1;
  304. dynamic dyn2 = jv2;
  305. string str1 = i1.ToString(CultureInfo.InvariantCulture);
  306. string str2 = i2.ToString(CultureInfo.InvariantCulture);
  307. JsonValue jvstr1 = str1;
  308. JsonValue jvstr2 = str2;
  309. Log.Info("Unary +");
  310. Assert.Equal<int>(+i1, +dyn1);
  311. Assert.Equal<int>(+i2, +dyn2);
  312. Log.Info("Unary -");
  313. Assert.Equal<int>(-i1, -dyn1);
  314. Assert.Equal<int>(-i2, -dyn2);
  315. Log.Info("Unary ~ (bitwise NOT)");
  316. Assert.Equal<int>(~i1, ~dyn1);
  317. Assert.Equal<int>(~i2, ~dyn2);
  318. Log.Info("Binary +: {0}", i1 + i2);
  319. Assert.Equal<int>(i1 + i2, dyn1 + dyn2);
  320. Assert.Equal<int>(i1 + i2, dyn2 + dyn1);
  321. Assert.Equal<int>(i1 + i2, dyn1 + i2);
  322. Assert.Equal<int>(i1 + i2, dyn2 + i1);
  323. // DISABLED, 197394
  324. // Assert.Equal<int>(i1 + i2, dyn1 + str2);
  325. // Assert.Equal<int>(i1 + i2, dyn1 + jvstr2);
  326. Log.Info("Binary -: {0}, {1}", i1 - i2, i2 - i1);
  327. Assert.Equal<int>(i1 - i2, dyn1 - dyn2);
  328. Assert.Equal<int>(i2 - i1, dyn2 - dyn1);
  329. Assert.Equal<int>(i1 - i2, dyn1 - i2);
  330. Assert.Equal<int>(i2 - i1, dyn2 - i1);
  331. Log.Info("Binary *: {0}", i1 * i2);
  332. Assert.Equal<int>(i1 * i2, dyn1 * dyn2);
  333. Assert.Equal<int>(i1 * i2, dyn2 * dyn1);
  334. Assert.Equal<int>(i1 * i2, dyn1 * i2);
  335. Assert.Equal<int>(i1 * i2, dyn2 * i1);
  336. while (i1 == 0)
  337. {
  338. i1 = rndGen.Next(-10000, 10000);
  339. jv1 = i1;
  340. dyn1 = jv1;
  341. Log.Info("Using new (non-zero) i1 value: {0}", i1);
  342. }
  343. while (i2 == 0)
  344. {
  345. i2 = rndGen.Next(-10000, 10000);
  346. jv2 = i2;
  347. dyn2 = jv2;
  348. Log.Info("Using new (non-zero) i2 value: {0}", i2);
  349. }
  350. Log.Info("Binary / (integer division): {0}, {1}", i1 / i2, i2 / i1);
  351. Assert.Equal<int>(i1 / i2, dyn1 / dyn2);
  352. Assert.Equal<int>(i2 / i1, dyn2 / dyn1);
  353. Assert.Equal<int>(i1 / i2, dyn1 / i2);
  354. Assert.Equal<int>(i2 / i1, dyn2 / i1);
  355. Log.Info("Binary % (modulo): {0}, {1}", i1 % i2, i2 % i1);
  356. Assert.Equal<int>(i1 % i2, dyn1 % dyn2);
  357. Assert.Equal<int>(i2 % i1, dyn2 % dyn1);
  358. Assert.Equal<int>(i1 % i2, dyn1 % i2);
  359. Assert.Equal<int>(i2 % i1, dyn2 % i1);
  360. Log.Info("Binary & (bitwise AND): {0}", i1 & i2);
  361. Assert.Equal<int>(i1 & i2, dyn1 & dyn2);
  362. Assert.Equal<int>(i1 & i2, dyn2 & dyn1);
  363. Assert.Equal<int>(i1 & i2, dyn1 & i2);
  364. Assert.Equal<int>(i1 & i2, dyn2 & i1);
  365. Log.Info("Binary | (bitwise OR): {0}", i1 | i2);
  366. Assert.Equal<int>(i1 | i2, dyn1 | dyn2);
  367. Assert.Equal<int>(i1 | i2, dyn2 | dyn1);
  368. Assert.Equal<int>(i1 | i2, dyn1 | i2);
  369. Assert.Equal<int>(i1 | i2, dyn2 | i1);
  370. Log.Info("Binary ^ (bitwise XOR): {0}", i1 ^ i2);
  371. Assert.Equal<int>(i1 ^ i2, dyn1 ^ dyn2);
  372. Assert.Equal<int>(i1 ^ i2, dyn2 ^ dyn1);
  373. Assert.Equal<int>(i1 ^ i2, dyn1 ^ i2);
  374. Assert.Equal<int>(i1 ^ i2, dyn2 ^ i1);
  375. i1 = rndGen.Next(1, 10);
  376. i2 = rndGen.Next(1, 10);
  377. jv1 = i1;
  378. jv2 = i2;
  379. dyn1 = jv1;
  380. dyn2 = jv2;
  381. Log.Info("New i1, i2: {0}, {1}", i1, i2);
  382. Log.Info("Left shift: {0}", i1 << i2);
  383. Assert.Equal<int>(i1 << i2, dyn1 << dyn2);
  384. Assert.Equal<int>(i1 << i2, dyn1 << i2);
  385. i1 = i1 << i2;
  386. jv1 = i1;
  387. dyn1 = jv1;
  388. Log.Info("New i1: {0}", i1);
  389. Log.Info("Right shift: {0}", i1 >> i2);
  390. Assert.Equal<int>(i1 >> i2, dyn1 >> dyn2);
  391. Assert.Equal<int>(i1 >> i2, dyn1 >> i2);
  392. i2 += 4;
  393. jv2 = i2;
  394. dyn2 = jv2;
  395. Log.Info("New i2: {0}", i2);
  396. Log.Info("Right shift: {0}", i1 >> i2);
  397. Assert.Equal<int>(i1 >> i2, dyn1 >> dyn2);
  398. Assert.Equal<int>(i1 >> i2, dyn1 >> i2);
  399. }
  400. /// <summary>
  401. /// Tests for conversions between data types in arithmetic operations.
  402. /// </summary>
  403. [Fact(Skip = "Ignore")]
  404. public void ArithmeticConversion()
  405. {
  406. JsonObject jo = new JsonObject
  407. {
  408. { "byteVal", (byte)10 },
  409. { "sbyteVal", (sbyte)10 },
  410. { "shortVal", (short)10 },
  411. { "ushortVal", (ushort)10 },
  412. { "intVal", 10 },
  413. { "uintVal", (uint)10 },
  414. { "longVal", 10L },
  415. { "ulongVal", (ulong)10 },
  416. { "charVal", (char)10 },
  417. { "decimalVal", 10m },
  418. { "doubleVal", 10.0 },
  419. { "floatVal", 10f },
  420. };
  421. dynamic dyn = jo;
  422. Log.Info("Conversion from byte");
  423. // DISABLED, 197387, ValidateResult<int>(dyn.byteVal + (byte)10, 20);
  424. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.byteVal + (sbyte)10));
  425. ValidateResult<short>(dyn.byteVal + (short)10, 20);
  426. ValidateResult<ushort>(dyn.byteVal + (ushort)10, 20);
  427. ValidateResult<int>(dyn.byteVal + (int)10, 20);
  428. ValidateResult<uint>(dyn.byteVal + (uint)10, 20);
  429. ValidateResult<long>(dyn.byteVal + 10L, 20);
  430. ValidateResult<ulong>(dyn.byteVal + (ulong)10, 20);
  431. ValidateResult<decimal>(dyn.byteVal + 10m, 20);
  432. ValidateResult<float>(dyn.byteVal + 10f, 20);
  433. ValidateResult<double>(dyn.byteVal + 10.0, 20);
  434. Log.Info("Conversion from sbyte");
  435. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.sbyteVal + (byte)10));
  436. // DISABLED, 197387, ValidateResult<int>(dyn.sbyteVal + (sbyte)10, 20);
  437. ValidateResult<short>(dyn.sbyteVal + (short)10, 20);
  438. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.sbyteVal + (ushort)10));
  439. ValidateResult<int>(dyn.sbyteVal + (int)10, 20);
  440. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.sbyteVal + (uint)10));
  441. ValidateResult<long>(dyn.sbyteVal + 10L, 20);
  442. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.sbyteVal + (ulong)10));
  443. ValidateResult<decimal>(dyn.sbyteVal + 10m, 20);
  444. ValidateResult<float>(dyn.sbyteVal + 10f, 20);
  445. ValidateResult<double>(dyn.sbyteVal + 10.0, 20);
  446. Log.Info("Conversion from short");
  447. ValidateResult<short>(dyn.shortVal + (byte)10, 20);
  448. ValidateResult<short>(dyn.shortVal + (sbyte)10, 20);
  449. ValidateResult<short>(dyn.shortVal + (short)10, 20);
  450. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.shortVal + (ushort)10));
  451. ValidateResult<int>(dyn.shortVal + (int)10, 20);
  452. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.shortVal + (uint)10));
  453. ValidateResult<long>(dyn.shortVal + 10L, 20);
  454. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.shortVal + (ulong)10));
  455. ValidateResult<decimal>(dyn.shortVal + 10m, 20);
  456. ValidateResult<float>(dyn.shortVal + 10f, 20);
  457. ValidateResult<double>(dyn.shortVal + 10.0, 20);
  458. Log.Info("Conversion from ushort");
  459. ValidateResult<ushort>(dyn.ushortVal + (byte)10, 20);
  460. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ushortVal + (sbyte)10));
  461. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ushortVal + (short)10));
  462. ValidateResult<ushort>(dyn.ushortVal + (ushort)10, 20);
  463. ValidateResult<int>(dyn.ushortVal + (int)10, 20);
  464. ValidateResult<uint>(dyn.ushortVal + (uint)10, 20);
  465. ValidateResult<long>(dyn.ushortVal + 10L, 20);
  466. ValidateResult<ulong>(dyn.ushortVal + (ulong)10, 20);
  467. ValidateResult<decimal>(dyn.ushortVal + 10m, 20);
  468. ValidateResult<float>(dyn.ushortVal + 10f, 20);
  469. ValidateResult<double>(dyn.ushortVal + 10.0, 20);
  470. Log.Info("Conversion from int");
  471. ValidateResult<int>(dyn.intVal + (byte)10, 20);
  472. ValidateResult<int>(dyn.intVal + (sbyte)10, 20);
  473. ValidateResult<int>(dyn.intVal + (short)10, 20);
  474. ValidateResult<int>(dyn.intVal + (ushort)10, 20);
  475. ValidateResult<int>(dyn.intVal + (int)10, 20);
  476. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.intVal + (uint)10));
  477. ValidateResult<long>(dyn.intVal + 10L, 20);
  478. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.intVal + (ulong)10));
  479. ValidateResult<decimal>(dyn.intVal + 10m, 20);
  480. ValidateResult<float>(dyn.intVal + 10f, 20);
  481. ValidateResult<double>(dyn.intVal + 10.0, 20);
  482. Log.Info("Conversion from uint");
  483. ValidateResult<uint>(dyn.uintVal + (byte)10, 20);
  484. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.uintVal + (sbyte)10));
  485. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.uintVal + (short)10));
  486. ValidateResult<uint>(dyn.uintVal + (ushort)10, 20);
  487. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.uintVal + (int)10));
  488. ValidateResult<uint>(dyn.uintVal + (uint)10, 20);
  489. ValidateResult<long>(dyn.uintVal + 10L, 20);
  490. ValidateResult<ulong>(dyn.uintVal + (ulong)10, 20);
  491. ValidateResult<decimal>(dyn.uintVal + 10m, 20);
  492. ValidateResult<float>(dyn.uintVal + 10f, 20);
  493. ValidateResult<double>(dyn.uintVal + 10.0, 20);
  494. Log.Info("Conversion from long");
  495. ValidateResult<long>(dyn.longVal + (byte)10, 20);
  496. ValidateResult<long>(dyn.longVal + (sbyte)10, 20);
  497. ValidateResult<long>(dyn.longVal + (short)10, 20);
  498. ValidateResult<long>(dyn.longVal + (ushort)10, 20);
  499. ValidateResult<long>(dyn.longVal + (int)10, 20);
  500. ValidateResult<long>(dyn.longVal + (uint)10, 20);
  501. ValidateResult<long>(dyn.longVal + 10L, 20);
  502. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.longVal + (ulong)10));
  503. ValidateResult<decimal>(dyn.longVal + 10m, 20);
  504. ValidateResult<float>(dyn.longVal + 10f, 20);
  505. ValidateResult<double>(dyn.longVal + 10.0, 20);
  506. Log.Info("Conversion from ulong");
  507. ValidateResult<ulong>(dyn.ulongVal + (byte)10, 20);
  508. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ulongVal + (sbyte)10));
  509. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ulongVal + (short)10));
  510. ValidateResult<ulong>(dyn.ulongVal + (ushort)10, 20);
  511. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ulongVal + (int)10));
  512. ValidateResult<ulong>(dyn.ulongVal + (uint)10, 20);
  513. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.ulongVal + (long)10));
  514. ValidateResult<ulong>(dyn.ulongVal + (ulong)10, 20);
  515. ValidateResult<decimal>(dyn.ulongVal + 10m, 20);
  516. ValidateResult<float>(dyn.ulongVal + 10f, 20);
  517. ValidateResult<double>(dyn.ulongVal + 10.0, 20);
  518. Log.Info("Conversion from float");
  519. ValidateResult<float>(dyn.floatVal + (byte)10, 20);
  520. ValidateResult<float>(dyn.floatVal + (sbyte)10, 20);
  521. ValidateResult<float>(dyn.floatVal + (short)10, 20);
  522. ValidateResult<float>(dyn.floatVal + (ushort)10, 20);
  523. ValidateResult<float>(dyn.floatVal + (int)10, 20);
  524. ValidateResult<float>(dyn.floatVal + (uint)10, 20);
  525. ValidateResult<float>(dyn.floatVal + 10L, 20);
  526. ValidateResult<float>(dyn.floatVal + (ulong)10, 20);
  527. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.floatVal + 10m));
  528. ValidateResult<float>(dyn.floatVal + 10f, 20);
  529. ValidateResult<double>(dyn.floatVal + 10.0, 20);
  530. Log.Info("Conversion from double");
  531. ValidateResult<double>(dyn.doubleVal + (byte)10, 20);
  532. ValidateResult<double>(dyn.doubleVal + (sbyte)10, 20);
  533. ValidateResult<double>(dyn.doubleVal + (short)10, 20);
  534. ValidateResult<double>(dyn.doubleVal + (ushort)10, 20);
  535. ValidateResult<double>(dyn.doubleVal + (int)10, 20);
  536. ValidateResult<double>(dyn.doubleVal + (uint)10, 20);
  537. ValidateResult<double>(dyn.doubleVal + 10L, 20);
  538. ValidateResult<double>(dyn.doubleVal + (ulong)10, 20);
  539. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.doubleVal + 10m));
  540. ValidateResult<double>(dyn.doubleVal + 10f, 20);
  541. ValidateResult<double>(dyn.doubleVal + 10.0, 20);
  542. Log.Info("Conversion from decimal");
  543. ValidateResult<decimal>(dyn.decimalVal + (byte)10, 20);
  544. ValidateResult<decimal>(dyn.decimalVal + (sbyte)10, 20);
  545. ValidateResult<decimal>(dyn.decimalVal + (short)10, 20);
  546. ValidateResult<decimal>(dyn.decimalVal + (ushort)10, 20);
  547. ValidateResult<decimal>(dyn.decimalVal + (int)10, 20);
  548. ValidateResult<decimal>(dyn.decimalVal + (uint)10, 20);
  549. ValidateResult<decimal>(dyn.decimalVal + 10L, 20);
  550. ValidateResult<decimal>(dyn.decimalVal + (ulong)10, 20);
  551. ValidateResult<decimal>(dyn.decimalVal + 10m, 20);
  552. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.decimalVal + 10f));
  553. JsonValueTests.ExpectException<InvalidOperationException>(() => Log.Info("{0}", dyn.decimalVal + 10.0));
  554. }
  555. /// <summary>
  556. /// Tests for implicit casts between dynamic references to <see cref="JsonPrimitive"/> instances
  557. /// and the supported CLR types.
  558. /// </summary>
  559. [Fact]
  560. public void ImplicitPrimitiveCastTests()
  561. {
  562. DateTime now = DateTime.Now;
  563. int seed = now.Year * 10000 + now.Month * 100 + now.Day;
  564. Log.Info("Seed: {0}", seed);
  565. Random rndGen = new Random(seed);
  566. int intValue = rndGen.Next(1, 127);
  567. Log.Info("Value: {0}", intValue);
  568. uint uintValue = (uint)intValue;
  569. short shortValue = (short)intValue;
  570. ushort ushortValue = (ushort)intValue;
  571. long longValue = (long)intValue;
  572. ulong ulongValue = (ulong)intValue;
  573. byte byteValue = (byte)intValue;
  574. sbyte sbyteValue = (sbyte)intValue;
  575. float floatValue = (float)intValue;
  576. double doubleValue = (double)intValue;
  577. decimal decimalValue = (decimal)intValue;
  578. string stringValue = intValue.ToString(CultureInfo.InvariantCulture);
  579. dynamic dyn = new JsonObject
  580. {
  581. { "Byte", byteValue },
  582. { "SByte", sbyteValue },
  583. { "Int16", shortValue },
  584. { "UInt16", ushortValue },
  585. { "Int32", intValue },
  586. { "UInt32", uintValue },
  587. { "Int64", longValue },
  588. { "UInt64", ulongValue },
  589. { "Double", doubleValue },
  590. { "Single", floatValue },
  591. { "Decimal", decimalValue },
  592. { "String", stringValue },
  593. { "True", "true" },
  594. { "False", "false" },
  595. };
  596. Log.Info("dyn: {0}", dyn);
  597. Log.Info("Casts to Byte");
  598. byte byteFromByte = dyn.Byte;
  599. byte byteFromSByte = dyn.SByte;
  600. byte byteFromShort = dyn.Int16;
  601. byte byteFromUShort = dyn.UInt16;
  602. byte byteFromInt = dyn.Int32;
  603. byte byteFromUInt = dyn.UInt32;
  604. byte byteFromLong = dyn.Int64;
  605. byte byteFromULong = dyn.UInt64;
  606. byte byteFromDouble = dyn.Double;
  607. byte byteFromFloat = dyn.Single;
  608. byte byteFromDecimal = dyn.Decimal;
  609. byte byteFromString = dyn.String;
  610. Assert.Equal<byte>(byteValue, byteFromByte);
  611. Assert.Equal<byte>(byteValue, byteFromSByte);
  612. Assert.Equal<byte>(byteValue, byteFromShort);
  613. Assert.Equal<byte>(byteValue, byteFromUShort);
  614. Assert.Equal<byte>(byteValue, byteFromInt);
  615. Assert.Equal<byte>(byteValue, byteFromUInt);
  616. Assert.Equal<byte>(byteValue, byteFromLong);
  617. Assert.Equal<byte>(byteValue, byteFromULong);
  618. Assert.Equal<byte>(byteValue, byteFromDouble);
  619. Assert.Equal<byte>(byteValue, byteFromFloat);
  620. Assert.Equal<byte>(byteValue, byteFromDecimal);
  621. Assert.Equal<byte>(byteValue, byteFromString);
  622. Log.Info("Casts to SByte");
  623. sbyte sbyteFromByte = dyn.Byte;
  624. sbyte sbyteFromSByte = dyn.SByte;
  625. sbyte sbyteFromShort = dyn.Int16;
  626. sbyte sbyteFromUShort = dyn.UInt16;
  627. sbyte sbyteFromInt = dyn.Int32;
  628. sbyte sbyteFromUInt = dyn.UInt32;
  629. sbyte sbyteFromLong = dyn.Int64;
  630. sbyte sbyteFromULong = dyn.UInt64;
  631. sbyte sbyteFromDouble = dyn.Double;
  632. sbyte sbyteFromFloat = dyn.Single;
  633. sbyte sbyteFromDecimal = dyn.Decimal;
  634. sbyte sbyteFromString = dyn.String;
  635. Assert.Equal<sbyte>(sbyteValue, sbyteFromByte);
  636. Assert.Equal<sbyte>(sbyteValue, sbyteFromSByte);
  637. Assert.Equal<sbyte>(sbyteValue, sbyteFromShort);
  638. Assert.Equal<sbyte>(sbyteValue, sbyteFromUShort);
  639. Assert.Equal<sbyte>(sbyteValue, sbyteFromInt);
  640. Assert.Equal<sbyte>(sbyteValue, sbyteFromUInt);
  641. Assert.Equal<sbyte>(sbyteValue, sbyteFromLong);
  642. Assert.Equal<sbyte>(sbyteValue, sbyteFromULong);
  643. Assert.Equal<sbyte>(sbyteValue, sbyteFromDouble);
  644. Assert.Equal<sbyte>(sbyteValue, sbyteFromFloat);
  645. Assert.Equal<sbyte>(sbyteValue, sbyteFromDecimal);
  646. Assert.Equal<sbyte>(sbyteValue, sbyteFromString);
  647. Log.Info("Casts to Short");
  648. short shortFromByte = dyn.Byte;
  649. short shortFromSByte = dyn.SByte;
  650. short shortFromShort = dyn.Int16;
  651. short shortFromUShort = dyn.UInt16;
  652. short shortFromInt = dyn.Int32;
  653. short shortFromUInt = dyn.UInt32;
  654. short shortFromLong = dyn.Int64;
  655. short shortFromULong = dyn.UInt64;
  656. short shortFromDouble = dyn.Double;
  657. short shortFromFloat = dyn.Single;
  658. short shortFromDecimal = dyn.Decimal;
  659. short shortFromString = dyn.String;
  660. Assert.Equal<short>(shortValue, shortFromByte);
  661. Assert.Equal<short>(shortValue, shortFromSByte);
  662. Assert.Equal<short>(shortValue, shortFromShort);
  663. Assert.Equal<short>(shortValue, shortFromUShort);
  664. Assert.Equal<short>(shortValue, shortFromInt);
  665. Assert.Equal<short>(shortValue, shortFromUInt);
  666. Assert.Equal<short>(shortValue, shortFromLong);
  667. Assert.Equal<short>(shortValue, shortFromULong);
  668. Assert.Equal<short>(shortValue, shortFromDouble);
  669. Assert.Equal<short>(shortValue, shortFromFloat);
  670. Assert.Equal<short>(shortValue, shortFromDecimal);
  671. Assert.Equal<short>(shortValue, shortFromString);
  672. Log.Info("Casts to UShort");
  673. ushort ushortFromByte = dyn.Byte;
  674. ushort ushortFromSByte = dyn.SByte;
  675. ushort ushortFromShort = dyn.Int16;
  676. ushort ushortFromUShort = dyn.UInt16;
  677. ushort ushortFromInt = dyn.Int32;
  678. ushort ushortFromUInt = dyn.UInt32;
  679. ushort ushortFromLong = dyn.Int64;
  680. ushort ushortFromULong = dyn.UInt64;
  681. ushort ushortFromDouble = dyn.Double;
  682. ushort ushortFromFloat = dyn.Single;
  683. ushort ushortFromDecimal = dyn.Decimal;
  684. ushort ushortFromString = dyn.String;
  685. Assert.Equal<ushort>(ushortValue, ushortFromByte);
  686. Assert.Equal<ushort>(ushortValue, ushortFromSByte);
  687. Assert.Equal<ushort>(ushortValue, ushortFromShort);
  688. Assert.Equal<ushort>(ushortValue, ushortFromUShort);
  689. Assert.Equal<ushort>(ushortValue, ushortFromInt);
  690. Assert.Equal<ushort>(ushortValue, ushortFromUInt);
  691. Assert.Equal<ushort>(ushortValue, ushortFromLong);
  692. Assert.Equal<ushort>(ushortValue, ushortFromULong);
  693. Assert.Equal<ushort>(ushortValue, ushortFromDouble);
  694. Assert.Equal<ushort>(ushortValue, ushortFromFloat);
  695. Assert.Equal<ushort>(ushortValue, ushortFromDecimal);
  696. Assert.Equal<ushort>(ushortValue, ushortFromString);
  697. Log.Info("Casts to Int");
  698. int intFromByte = dyn.Byte;
  699. int intFromSByte = dyn.SByte;
  700. int intFromShort = dyn.Int16;
  701. int intFromUShort = dyn.UInt16;
  702. int intFromInt = dyn.Int32;
  703. int intFromUInt = dyn.UInt32;
  704. int intFromLong = dyn.Int64;
  705. int intFromULong = dyn.UInt64;
  706. int intFromDouble = dyn.Double;
  707. int intFromFloat = dyn.Single;
  708. int intFromDecimal = dyn.Decimal;
  709. int intFromString = dyn.String;
  710. Assert.Equal<int>(intValue, intFromByte);
  711. Assert.Equal<int>(intValue, intFromSByte);
  712. Assert.Equal<int>(intValue, intFromShort);
  713. Assert.Equal<int>(intValue, intFromUShort);
  714. Assert.Equal<int>(intValue, intFromInt);
  715. Assert.Equal<int>(intValue, intFromUInt);
  716. Assert.Equal<int>(intValue, intFromLong);
  717. Assert.Equal<int>(intValue, intFromULong);
  718. Assert.Equal<int>(intValue, intFromDouble);
  719. Assert.Equal<int>(intValue, intFromFloat);
  720. Assert.Equal<int>(intValue, intFromDecimal);
  721. Assert.Equal<int>(intValue, intFromString);
  722. Log.Info("Casts to UInt");
  723. uint uintFromByte = dyn.Byte;
  724. uint uintFromSByte = dyn.SByte;
  725. uint uintFromShort = dyn.Int16;
  726. uint uintFromUShort = dyn.UInt16;
  727. uint uintFromInt = dyn.Int32;
  728. uint uintFromUInt = dyn.UInt32;
  729. uint uintFromLong = dyn.Int64;
  730. uint uintFromULong = dyn.UInt64;
  731. uint uintFromDouble = dyn.Double;
  732. uint uintFromFloat = dyn.Single;
  733. uint uintFromDecimal = dyn.Decimal;
  734. uint uintFromString = dyn.String;
  735. Assert.Equal<uint>(uintValue, uintFromByte);
  736. Assert.Equal<uint>(uintValue, uintFromSByte);
  737. Assert.Equal<uint>(uintValue, uintFromShort);
  738. Assert.Equal<uint>(uintValue, uintFromUShort);
  739. Assert.Equal<uint>(uintValue, uintFromInt);
  740. Assert.Equal<uint>(uintValue, uintFromUInt);
  741. Assert.Equal<uint>(uintValue, uintFromLong);
  742. Assert.Equal<uint>(uintValue, uintFromULong);
  743. Assert.Equal<uint>(uintValue, uintFromDouble);
  744. Assert.Equal<uint>(uintValue, uintFromFloat);
  745. Assert.Equal<uint>(uintValue, uintFromDecimal);
  746. Assert.Equal<uint>(uintValue, uintFromString);
  747. Log.Info("Casts to Long");
  748. long longFromByte = dyn.Byte;
  749. long longFromSByte = dyn.SByte;
  750. long longFromShort = dyn.Int16;
  751. long longFromUShort = dyn.UInt16;
  752. long longFromInt = dyn.Int32;
  753. long longFromUInt = dyn.UInt32;
  754. long longFromLong = dyn.Int64;
  755. long longFromULong = dyn.UInt64;
  756. long longFromDouble = dyn.Double;
  757. long longFromFloat = dyn.Single;
  758. long longFromDecimal = dyn.Decimal;
  759. long longFromString = dyn.String;
  760. Assert.Equal<long>(longValue, longFromByte);
  761. Assert.Equal<long>(longValue, longFromSByte);
  762. Assert.Equal<long>(longValue, longFromShort);
  763. Assert.Equal<long>(longValue, longFromUShort);
  764. Assert.Equal<long>(longValue, longFromInt);
  765. Assert.Equal<long>(longValue, longFromUInt);
  766. Assert.Equal<long>(longValue, longFromLong);
  767. Assert.Equal<long>(longValue, longFromULong);
  768. Assert.Equal<long>(longValue, longFromDouble);
  769. Assert.Equal<long>(longValue, longFromFloat);
  770. Assert.Equal<long>(longValue, longFromDecimal);
  771. Assert.Equal<long>(longValue, longFromString);
  772. Log.Info("Casts to ULong");
  773. ulong ulongFromByte = dyn.Byte;
  774. ulong ulongFromSByte = dyn.SByte;
  775. ulong ulongFromShort = dyn.Int16;
  776. ulong ulongFromUShort = dyn.UInt16;
  777. ulong ulongFromInt = dyn.Int32;
  778. ulong ulongFromUInt = dyn.UInt32;
  779. ulong ulongFromLong = dyn.Int64;
  780. ulong ulongFromULong = dyn.UInt64;
  781. ulong ulongFromDouble = dyn.Double;
  782. ulong ulongFromFloat = dyn.Single;
  783. ulong ulongFromDecimal = dyn.Decimal;
  784. ulong ulongFromString = dyn.String;
  785. Assert.Equal<ulong>(ulongValue, ulongFromByte);
  786. Assert.Equal<ulong>(ulongValue, ulongFromSByte);
  787. Assert.Equal<ulong>(ulongValue, ulongFromShort);
  788. Assert.Equal<ulong>(ulongValue, ulongFromUShort);
  789. Assert.Equal<ulong>(ulongValue, ulongFromInt);
  790. Assert.Equal<ulong>(ulongValue, ulongFromUInt);
  791. Assert.Equal<ulong>(ulongValue, ulongFromLong);
  792. Assert.Equal<ulong>(ulongValue, ulongFromULong);
  793. Assert.Equal<ulong>(ulongValue, ulongFromDouble);
  794. Assert.Equal<ulong>(ulongValue, ulongFromFloat);
  795. Assert.Equal<ulong>(ulongValue, ulongFromDecimal);
  796. Assert.Equal<ulong>(ulongValue, ulongFromString);
  797. Log.Info("Casts to Float");
  798. float floatFromByte = dyn.Byte;
  799. float floatFromSByte = dyn.SByte;
  800. float floatFromShort = dyn.Int16;
  801. float floatFromUShort = dyn.UInt16;
  802. float floatFromInt = dyn.Int32;
  803. float floatFromUInt = dyn.UInt32;
  804. float floatFromLong = dyn.Int64;
  805. float floatFromULong = dyn.UInt64;
  806. float floatFromDouble = dyn.Double;
  807. float floatFromFloat = dyn.Single;
  808. float floatFromDecimal = dyn.Decimal;
  809. float floatFromString = dyn.String;
  810. Assert.Equal<float>(floatValue, floatFromByte);
  811. Assert.Equal<float>(floatValue, floatFromSByte);
  812. Assert.Equal<float>(floatValue, floatFromShort);
  813. Assert.Equal<float>(floatValue, floatFromUShort);
  814. Assert.Equal<float>(floatValue, floatFromInt);
  815. Assert.Equal<float>(floatValue, floatFromUInt);
  816. Assert.Equal<float>(floatValue, floatFromLong);
  817. Assert.Equal<float>(floatValue, floatFromULong);
  818. Assert.Equal<float>(floatValue, floatFromDouble);
  819. Assert.Equal<float>(floatValue, floatFromFloat);
  820. Assert.Equal<float>(floatValue, floatFromDecimal);
  821. Assert.Equal<float>(floatValue, floatFromString);
  822. Log.Info("Casts to Double");
  823. double doubleFromByte = dyn.Byte;
  824. double doubleFromSByte = dyn.SByte;
  825. double doubleFromShort = dyn.Int16;
  826. double doubleFromUShort = dyn.UInt16;
  827. double doubleFromInt = dyn.Int32;
  828. double doubleFromUInt = dyn.UInt32;
  829. double doubleFromLong = dyn.Int64;
  830. double doubleFromULong = dyn.UInt64;
  831. double doubleFromDouble = dyn.Double;
  832. double doubleFromFloat = dyn.Single;
  833. double doubleFromDecimal = dyn.Decimal;
  834. double doubleFromString = dyn.String;
  835. Assert.Equal<double>(doubleValue, doubleFromByte);
  836. Assert.Equal<double>(doubleValue, doubleFromSByte);
  837. Assert.Equal<double>(doubleValue, doubleFromShort);
  838. Assert.Equal<double>(doubleValue, doubleFromUShort);
  839. Assert.Equal<double>(doubleValue, doubleFromInt);
  840. Assert.Equal<double>(doubleValue, doubleFromUInt);
  841. Assert.Equal<double>(doubleValue, doubleFromLong);
  842. Assert.Equal<double>(doubleValue, doubleFromULong);
  843. Assert.Equal<double>(doubleValue, doubleFromDouble);
  844. Assert.Equal<double>(doubleValue, doubleFromFloat);
  845. Assert.Equal<double>(doubleValue, doubleFromDecimal);
  846. Assert.Equal<double>(doubleValue, doubleFromString);
  847. Log.Info("Casts to Decimal");
  848. decimal decimalFromByte = dyn.Byte;
  849. decimal decimalFromSByte = dyn.SByte;
  850. decimal decimalFromShort = dyn.Int16;
  851. decimal decimalFromUShort = dyn.UInt16;
  852. decimal decimalFromInt = dyn.Int32;
  853. decimal decimalFromUInt = dyn.UInt32;
  854. decimal decimalFromLong = dyn.Int64;
  855. decimal decimalFromULong = dyn.UInt64;
  856. decimal decimalFromDouble = dyn.Double;
  857. decimal decimalFromFloat = dyn.Single;
  858. decimal decimalFromDecimal = dyn.Decimal;
  859. decimal decimalFromString = dyn.String;
  860. Assert.Equal<decimal>(decimalValue, decimalFromByte);
  861. Assert.Equal<decimal>(decimalValue, decimalFromSByte);
  862. Assert.Equal<decimal>(decimalValue, decimalFromShort);
  863. Assert.Equal<decimal>(decimalValue, decimalFromUShort);
  864. Assert.Equal<decimal>(decimalValue, decimalFromInt);
  865. Assert.Equal<decimal>(decimalValue, decimalFromUInt);
  866. Assert.Equal<decimal>(decimalValue, decimalFromLong);
  867. Assert.Equal<decimal>(decimalValue, decimalFromULong);
  868. Assert.Equal<decimal>(decimalValue, decimalFromDouble);
  869. Assert.Equal<decimal>(decimalValue, decimalFromFloat);
  870. Assert.Equal<decimal>(decimalValue, decimalFromDecimal);
  871. Assert.Equal<decimal>(decimalValue, decimalFromString);
  872. Log.Info("Casts to String");
  873. string stringFromByte = dyn.Byte;
  874. string stringFromSByte = dyn.SByte;
  875. string stringFromShort = dyn.Int16;
  876. string stringFromUShort = dyn.UInt16;
  877. string stringFromInt = dyn.Int32;
  878. string stringFromUInt = dyn.UInt32;
  879. string stringFromLong = dyn.Int64;
  880. string stringFromULong = dyn.UInt64;
  881. string stringFromDouble = dyn.Double;
  882. string stringFromFloat = dyn.Single;
  883. string stringFromDecimal = dyn.Decimal;
  884. string stringFromString = dyn.String;
  885. Assert.Equal(stringValue, stringFromByte);
  886. Assert.Equal(stringValue, stringFromSByte);
  887. Assert.Equal(stringValue, stringFromShort);
  888. Assert.Equal(stringValue, stringFromUShort);
  889. Assert.Equal(stringValue, stringFromInt);
  890. Assert.Equal(stringValue, stringFromUInt);
  891. Assert.Equal(stringValue, stringFromLong);
  892. Assert.Equal(stringValue, stringFromULong);
  893. Assert.Equal(stringValue, stringFromDouble);
  894. Assert.Equal(stringValue, stringFromFloat);
  895. Assert.Equal(stringValue, stringFromDecimal);
  896. Assert.Equal(stringValue, stringFromString);
  897. Log.Info("Casts to Boolean");
  898. bool bTrue = dyn.True;
  899. bool bFalse = dyn.False;
  900. Assert.True(bTrue);
  901. Assert.False(bFalse);
  902. }
  903. /// <summary>
  904. /// Test for creating a JsonValue from a deep-nested dynamic object.
  905. /// </summary>
  906. [Fact]
  907. public void CreateFromDeepNestedDynamic()
  908. {
  909. int count = 5000;
  910. string expected = "";
  911. dynamic dyn = new TestDynamicObject();
  912. dynamic cur = dyn;
  913. for (int i = 0; i < count; i++)
  914. {
  915. expected += "{\"" + i + "\":";
  916. cur[i.ToString()] = new TestDynamicObject();
  917. cur = cur[i.ToString()];
  918. }
  919. expected += "{}";
  920. for (int i = 0; i < count; i++)
  921. {
  922. expected += "}";
  923. }
  924. JsonValue jv = JsonValueExtensions.CreateFrom(dyn);
  925. Assert.Equal(expected, jv.ToString());
  926. }
  927. private void ValidateResult<ResultType>(dynamic value, ResultType expectedResult)
  928. {
  929. Assert.IsAssignableFrom(typeof(ResultType), value);
  930. Assert.Equal<ResultType>(expectedResult, (ResultType)value);
  931. }
  932. /// <summary>
  933. /// Concrete DynamicObject class for testing purposes.
  934. /// </summary>
  935. internal class TestDynamicObject : DynamicObject
  936. {
  937. private IDictionary<string, object> _values = new Dictionary<string, object>();
  938. public override IEnumerable<string> GetDynamicMemberNames()
  939. {
  940. return _values.Keys;
  941. }
  942. public override bool TrySetMember(SetMemberBinder binder, object value)
  943. {
  944. _values[binder.Name] = value;
  945. return true;
  946. }
  947. public override bool TryGetMember(GetMemberBinder binder, out object result)
  948. {
  949. return _values.TryGetValue(binder.Name, out result);
  950. }
  951. public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
  952. {
  953. string key = indexes[0].ToString();
  954. if (_values.ContainsKey(key))
  955. {
  956. _values[key] = value;
  957. }
  958. else
  959. {
  960. _values.Add(key, value);
  961. }
  962. return true;
  963. }
  964. public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
  965. {
  966. string key = indexes[0].ToString();
  967. if (_values.ContainsKey(key))
  968. {
  969. result = _values[key];