EcmaTest.cs 3.0 KB

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