JsonTests.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Jint.Native.Json;
  2. using Jint.Runtime;
  3. namespace Jint.Tests.Runtime
  4. {
  5. public class JsonTests
  6. {
  7. [Fact]
  8. public void CanParseTabsInProperties()
  9. {
  10. var engine = new Engine();
  11. const string script = @"JSON.parse(""{\""abc\\tdef\"": \""42\""}"");";
  12. var obj = engine.Evaluate(script).AsObject();
  13. Assert.True(obj.HasOwnProperty("abc\tdef"));
  14. }
  15. [Theory]
  16. [InlineData("{\"a\":\"\\\"\"}", "\"")] // " quotation mark
  17. [InlineData("{\"a\":\"\\\\\"}", "\\")] // \ reverse solidus
  18. [InlineData("{\"a\":\"\\/\"}", "/")] // / solidus
  19. [InlineData("{\"a\":\"\\b\"}", "\b")] // backspace
  20. [InlineData("{\"a\":\"\\f\"}", "\f")] // formfeed
  21. [InlineData("{\"a\":\"\\n\"}", "\n")] // linefeed
  22. [InlineData("{\"a\":\"\\r\"}", "\r")] // carriage return
  23. [InlineData("{\"a\":\"\\t\"}", "\t")] // horizontal tab
  24. [InlineData("{\"a\":\"\\u0000\"}", "\0")]
  25. [InlineData("{\"a\":\"\\u0001\"}", "\x01")]
  26. [InlineData("{\"a\":\"\\u0061\"}", "a")]
  27. public void ShouldParseEscapedCharactersCorrectly(string json, string expectedCharacter)
  28. {
  29. var engine = new Engine();
  30. var parser = new JsonParser(engine);
  31. var parsedCharacter = parser.Parse(json).AsObject().Get("a").AsString();
  32. Assert.Equal(expectedCharacter, parsedCharacter);
  33. }
  34. [Theory]
  35. [InlineData("{\"a\":1", "Unexpected end of JSON input at position 6")]
  36. [InlineData("{\"a\":1},", "Unexpected token ',' in JSON at position 7")]
  37. [InlineData("{1}", "Unexpected number in JSON at position 1")]
  38. [InlineData("{\"a\" \"a\"}", "Unexpected string in JSON at position 5")]
  39. [InlineData("{true}", "Unexpected token 'true' in JSON at position 1")]
  40. [InlineData("{null}", "Unexpected token 'null' in JSON at position 1")]
  41. [InlineData("{:}", "Unexpected token ':' in JSON at position 1")]
  42. [InlineData("\"\\uah\"", "Expected hexadecimal digit in JSON at position 4")]
  43. [InlineData("0123", "Unexpected token '1' in JSON at position 1")] // leading 0 (octal number) not allowed
  44. [InlineData("1e+A", "Unexpected token 'A' in JSON at position 3")]
  45. [InlineData("truE", "Unexpected token 'tru' in JSON at position 0")]
  46. [InlineData("nul", "Unexpected token 'nul' in JSON at position 0")]
  47. [InlineData("\"ab\t\"", "Invalid character in JSON at position 3")] // invalid char in string literal
  48. [InlineData("\"ab", "Unexpected end of JSON input at position 3")] // unterminated string literal
  49. [InlineData("alpha", "Unexpected token 'a' in JSON at position 0")]
  50. [InlineData("[1,\na]", "Unexpected token 'a' in JSON at position 4")] // multiline
  51. [InlineData("\x06", "Unexpected token '\x06' in JSON at position 0")] // control char
  52. [InlineData("{\"\\v\":1}", "Unexpected token 'v' in JSON at position 3")] // invalid escape sequence
  53. public void ShouldReportHelpfulSyntaxErrorForInvalidJson(string json, string expectedMessage)
  54. {
  55. var engine = new Engine();
  56. var parser = new JsonParser(engine);
  57. var ex = Assert.ThrowsAny<JavaScriptException>(() =>
  58. {
  59. parser.Parse(json);
  60. });
  61. Assert.Equal(expectedMessage, ex.Message);
  62. var error = ex.Error as Native.Error.ErrorInstance;
  63. Assert.NotNull(error);
  64. Assert.Equal("SyntaxError", error.Get("name"));
  65. }
  66. [Theory]
  67. [InlineData("[[]]", "[\n []\n]")]
  68. [InlineData("[ { a: [{ x: 0 }], b:[]} ]",
  69. "[\n {\n \"a\": [\n {\n \"x\": 0\n }\n ],\n \"b\": []\n }\n]")]
  70. public void ShouldSerializeWithCorrectIndentation(string script, string expectedJson)
  71. {
  72. var engine = new Engine();
  73. engine.SetValue("x", engine.Evaluate(script));
  74. var result = engine.Evaluate("JSON.stringify(x, null, 2);").AsString();
  75. Assert.Equal(expectedJson, result);
  76. }
  77. }
  78. }