EcmaTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Jint.Runtime;
  5. using Xunit;
  6. namespace Jint.Tests.Ecma
  7. {
  8. public class EcmaTest
  9. {
  10. private static string _lastError;
  11. protected Action<string> Error = s => { _lastError = s; };
  12. protected string BasePath;
  13. public EcmaTest()
  14. {
  15. var assemblyPath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
  16. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  17. BasePath = assemblyDirectory.Parent.Parent.FullName;
  18. }
  19. protected void RunTestCode(string code, bool negative)
  20. {
  21. _lastError = null;
  22. //NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
  23. var pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
  24. var engine = new Engine(cfg => cfg.LocalTimeZone(pacificTimeZone));
  25. // loading driver
  26. var driverFilename = Path.Combine(BasePath, "TestCases\\sta.js");
  27. engine.Execute(File.ReadAllText(driverFilename));
  28. if (negative)
  29. {
  30. try
  31. {
  32. engine.Execute(code);
  33. Assert.True(_lastError != null);
  34. Assert.False(true);
  35. }
  36. catch
  37. {
  38. // exception is expected
  39. }
  40. }
  41. else
  42. {
  43. try
  44. {
  45. engine.Execute(code);
  46. }
  47. catch (JavaScriptException j)
  48. {
  49. _lastError = TypeConverter.ToString(j.Error);
  50. }
  51. catch (Exception e)
  52. {
  53. _lastError = e.ToString();
  54. }
  55. Assert.Null(_lastError);
  56. }
  57. }
  58. protected void RunTest(string sourceFilename, bool negative)
  59. {
  60. var fullName = Path.Combine(BasePath, sourceFilename);
  61. if (!File.Exists(fullName))
  62. {
  63. throw new ArgumentException("Could not find source file: " + fullName);
  64. }
  65. string code = File.ReadAllText(fullName);
  66. RunTestCode(code, negative);
  67. }
  68. }
  69. public class EcmaTestTests : EcmaTest
  70. {
  71. [Fact]
  72. public void EcmaTestPassSucceededTestCase()
  73. {
  74. RunTestCode(@"
  75. function testcase() {
  76. return true;
  77. }
  78. runTestCase(testcase);
  79. ", false);
  80. }
  81. [Fact]
  82. public void EcmaTestPassNegativeTestCase()
  83. {
  84. RunTestCode(@"
  85. function testcase() {
  86. return false;
  87. }
  88. runTestCase(testcase);
  89. ", true);
  90. }
  91. }
  92. }