Test262Test.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using Jint.Runtime;
  6. using Newtonsoft.Json.Linq;
  7. using Xunit;
  8. namespace Jint.Tests.Test262
  9. {
  10. public abstract class Test262Test
  11. {
  12. private static readonly string[] Sources;
  13. private static readonly string BasePath;
  14. private static readonly TimeZoneInfo _pacificTimeZone;
  15. private static readonly Dictionary<string, string> _skipReasons =
  16. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  17. static Test262Test()
  18. {
  19. //NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
  20. _pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
  21. var assemblyPath = new Uri(typeof(Test262Test).GetTypeInfo().Assembly.CodeBase).LocalPath;
  22. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  23. BasePath = assemblyDirectory.Parent.Parent.Parent.FullName;
  24. string[] files =
  25. {
  26. @"harness\sta.js",
  27. @"harness\assert.js",
  28. @"harness\propertyHelper.js",
  29. @"harness\compareArray.js",
  30. @"harness\decimalToHexString.js",
  31. };
  32. Sources = new string[files.Length];
  33. for (var i = 0; i < files.Length; i++)
  34. {
  35. Sources[i] = File.ReadAllText(Path.Combine(BasePath, files[i]));
  36. }
  37. var content = File.ReadAllText(Path.Combine(BasePath, "test/skipped.json"));
  38. var doc = JArray.Parse(content);
  39. foreach (var entry in doc.Values<JObject>())
  40. {
  41. _skipReasons[entry["source"].Value<string>()] = entry["reason"].Value<string>();
  42. }
  43. }
  44. protected void RunTestCode(string code, bool strict)
  45. {
  46. var engine = new Engine(cfg => cfg
  47. .LocalTimeZone(_pacificTimeZone)
  48. .Strict(strict));
  49. for (int i = 0; i < Sources.Length; ++i)
  50. {
  51. engine.Execute(Sources[i]);
  52. }
  53. string lastError = null;
  54. try
  55. {
  56. engine.Execute(code);
  57. }
  58. catch (JavaScriptException j)
  59. {
  60. lastError = TypeConverter.ToString(j.Error);
  61. }
  62. catch (Exception e)
  63. {
  64. lastError = e.ToString();
  65. }
  66. Assert.Null(lastError);
  67. }
  68. protected void RunTestInternal(SourceFile sourceFile)
  69. {
  70. var fullName = sourceFile.FullPath;
  71. if (!File.Exists(fullName))
  72. {
  73. throw new ArgumentException("Could not find source file: " + fullName);
  74. }
  75. string code = File.ReadAllText(fullName);
  76. RunTestCode(code);
  77. }
  78. private void RunTestCode(string code)
  79. {
  80. if (code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
  81. {
  82. RunTestCode(code, strict: false);
  83. }
  84. if (code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
  85. {
  86. RunTestCode(code, strict: true);
  87. }
  88. }
  89. public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
  90. {
  91. var results = new List<object[]>();
  92. var fixturesPath = Path.Combine(BasePath, "test");
  93. var searchPath = Path.Combine(fixturesPath, pathPrefix);
  94. var files = Directory.GetFiles(searchPath, "*", SearchOption.AllDirectories);
  95. foreach (var file in files)
  96. {
  97. var name = file.Substring(fixturesPath.Length + 1).Replace("\\", "/");
  98. bool skip = _skipReasons.TryGetValue(name, out var reason);
  99. var sourceFile = new SourceFile(
  100. name,
  101. file,
  102. skip,
  103. reason);
  104. if (skipped == sourceFile.Skip)
  105. {
  106. results.Add(new object[]
  107. {
  108. sourceFile
  109. });
  110. }
  111. }
  112. return results;
  113. }
  114. }
  115. public class SourceFile
  116. {
  117. public SourceFile(
  118. string source,
  119. string fullPath,
  120. bool skip,
  121. string reason)
  122. {
  123. Skip = skip;
  124. Source = source;
  125. Reason = reason;
  126. FullPath = fullPath;
  127. }
  128. public string Source { get; }
  129. public bool Skip { get; }
  130. public string Reason { get; }
  131. public string FullPath { get; }
  132. public override string ToString()
  133. {
  134. return Source;
  135. }
  136. }
  137. }