ErrorTests.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using Jint.Parser;
  3. using Jint.Runtime;
  4. using Xunit;
  5. namespace Jint.Tests.Runtime
  6. {
  7. public class ErrorTests
  8. {
  9. [Fact]
  10. public void CanReturnCorrectErrorMessageAndLocation1()
  11. {
  12. var script = @"
  13. var a = {};
  14. var b = a.user.name;
  15. ";
  16. var engine = new Engine();
  17. var e = Assert.Throws<JavaScriptException>(() => engine.Execute(script));
  18. Assert.Equal("user is undefined", e.Message);
  19. Assert.Equal(4, e.Location.Start.Line);
  20. Assert.Equal(8, e.Location.Start.Column);
  21. }
  22. [Fact]
  23. public void CanReturnCorrectErrorMessageAndLocation2()
  24. {
  25. var script = @"
  26. test();
  27. ";
  28. var engine = new Engine();
  29. var e = Assert.Throws<JavaScriptException>(() => engine.Execute(script));
  30. Assert.Equal("test is not defined", e.Message);
  31. Assert.Equal(2, e.Location.Start.Line);
  32. Assert.Equal(1, e.Location.Start.Column);
  33. }
  34. [Fact]
  35. public void CanProduceCorrectStackTrace()
  36. {
  37. var engine = new Engine(options => options.LimitRecursion(1000));
  38. engine.Execute(@"var a = function(v) {
  39. return v.xxx.yyy;
  40. }
  41. var b = function(v) {
  42. return a(v);
  43. }", new ParserOptions
  44. {
  45. Source = "custom.js"
  46. });
  47. var e = Assert.Throws<JavaScriptException>(() => engine.Execute("var x = b(7);", new ParserOptions { Source = "main.js"}));
  48. Assert.Equal("xxx is undefined", e.Message);
  49. Assert.Equal(2, e.Location.Start.Line);
  50. Assert.Equal(8, e.Location.Start.Column);
  51. Assert.Equal("custom.js", e.Location.Source);
  52. var stack = e.CallStack;
  53. Assert.Equal(@" at a(v) @ custom.js 8:6
  54. at b(7) @ main.js 8:1
  55. ".Replace("\r\n", "\n"), stack.Replace("\r\n", "\n"));
  56. }
  57. }
  58. }