Test262Test.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text.RegularExpressions;
  7. using Jint.Runtime;
  8. using Newtonsoft.Json.Linq;
  9. using Xunit;
  10. namespace Jint.Tests.Test262
  11. {
  12. public abstract class Test262Test
  13. {
  14. private static readonly string[] Sources;
  15. private static readonly string BasePath;
  16. private static readonly TimeZoneInfo _pacificTimeZone;
  17. private static readonly Dictionary<string, string> _skipReasons =
  18. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  19. static Test262Test()
  20. {
  21. //NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
  22. _pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
  23. var assemblyPath = new Uri(typeof(Test262Test).GetTypeInfo().Assembly.CodeBase).LocalPath;
  24. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  25. BasePath = assemblyDirectory.Parent.Parent.Parent.FullName;
  26. string[] files =
  27. {
  28. @"harness\sta.js",
  29. @"harness\assert.js",
  30. @"harness\propertyHelper.js",
  31. @"harness\compareArray.js",
  32. @"harness\decimalToHexString.js",
  33. };
  34. Sources = new string[files.Length];
  35. for (var i = 0; i < files.Length; i++)
  36. {
  37. Sources[i] = File.ReadAllText(Path.Combine(BasePath, files[i]));
  38. }
  39. var content = File.ReadAllText(Path.Combine(BasePath, "test/skipped.json"));
  40. var doc = JArray.Parse(content);
  41. foreach (var entry in doc.Values<JObject>())
  42. {
  43. _skipReasons[entry["source"].Value<string>()] = entry["reason"].Value<string>();
  44. }
  45. }
  46. protected void RunTestCode(string code, bool strict)
  47. {
  48. var engine = new Engine(cfg => cfg
  49. .LocalTimeZone(_pacificTimeZone)
  50. .Strict(strict));
  51. for (int i = 0; i < Sources.Length; ++i)
  52. {
  53. engine.Execute(Sources[i]);
  54. }
  55. string lastError = null;
  56. bool negative = code.IndexOf("negative:", StringComparison.Ordinal) > -1;
  57. try
  58. {
  59. engine.Execute(code);
  60. }
  61. catch (JavaScriptException j)
  62. {
  63. lastError = TypeConverter.ToString(j.Error);
  64. }
  65. catch (Exception e)
  66. {
  67. lastError = e.ToString();
  68. }
  69. if (negative)
  70. {
  71. Assert.NotNull(lastError);
  72. }
  73. else
  74. {
  75. Assert.Null(lastError);
  76. }
  77. }
  78. protected void RunTestInternal(SourceFile sourceFile)
  79. {
  80. RunTestCode(sourceFile.Code);
  81. }
  82. private void RunTestCode(string code)
  83. {
  84. if (code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
  85. {
  86. RunTestCode(code, strict: false);
  87. }
  88. if (code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
  89. {
  90. RunTestCode(code, strict: true);
  91. }
  92. }
  93. public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
  94. {
  95. var results = new List<object[]>();
  96. var fixturesPath = Path.Combine(BasePath, "test");
  97. var searchPath = Path.Combine(fixturesPath, pathPrefix);
  98. var files = Directory.GetFiles(searchPath, "*", SearchOption.AllDirectories);
  99. foreach (var file in files)
  100. {
  101. var name = file.Substring(fixturesPath.Length + 1).Replace("\\", "/");
  102. bool skip = _skipReasons.TryGetValue(name, out var reason);
  103. var code = skip ? "" : File.ReadAllText(file);
  104. var features = Regex.Match(code, "features: \\[(.+?)\\]");
  105. if (features.Success)
  106. {
  107. var items = features.Groups[1].Captures[0].Value.Split(",");
  108. foreach (var item in items.Select(x => x.Trim()))
  109. {
  110. switch (item)
  111. {
  112. // TODO implement
  113. case "cross-realm":
  114. skip = true;
  115. reason = "realms not implemented";
  116. break;
  117. case "tail-call-optimization":
  118. skip = true;
  119. reason = "tail-calls not implemented";
  120. break;
  121. case "class":
  122. skip = true;
  123. reason = "class keyword not implemented";
  124. break;
  125. case "Symbol.species":
  126. skip = true;
  127. reason = "Symbol.species not implemented";
  128. break;
  129. case "Proxy":
  130. skip = true;
  131. reason = "Proxies not implemented";
  132. break;
  133. case "object-spread":
  134. skip = true;
  135. reason = "Object spread not implemented";
  136. break;
  137. case "Symbol.unscopables":
  138. skip = true;
  139. reason = "Symbol.unscopables not implemented";
  140. break;
  141. case "Symbol.match":
  142. skip = true;
  143. reason = "Symbol.match not implemented";
  144. break;
  145. case "Symbol.matchAll":
  146. skip = true;
  147. reason = "Symbol.matchAll not implemented";
  148. break;
  149. case "Symbol.split":
  150. skip = true;
  151. reason = "Symbol.split not implemented";
  152. break;
  153. case "String.prototype.matchAll":
  154. skip = true;
  155. reason = "proposal stage";
  156. break;
  157. case "Symbol.search":
  158. skip = true;
  159. reason = "Symbol.search not implemented";
  160. break;
  161. case "Symbol.replace":
  162. skip = true;
  163. reason = "Symbol.replace not implemented";
  164. break;
  165. case "Symbol.toStringTag":
  166. skip = true;
  167. reason = "Symbol.toStringTag not implemented";
  168. break;
  169. case "BigInt":
  170. skip = true;
  171. reason = "BigInt not implemented";
  172. break;
  173. case "generators":
  174. skip = true;
  175. reason = "generators not implemented";
  176. break;
  177. case "destructuring-binding":
  178. skip = true;
  179. reason = "destructuring-binding not implemented";
  180. break;
  181. case "let":
  182. skip = true;
  183. reason = "let not implemented";
  184. break;
  185. case "async-functions":
  186. skip = true;
  187. reason = "async-functions not implemented";
  188. break;
  189. }
  190. }
  191. }
  192. if (name.StartsWith("built-ins/String/raw/"))
  193. {
  194. skip = true;
  195. reason = "requires template string";
  196. }
  197. if (code.IndexOf("SpecialCasing.txt") > -1)
  198. {
  199. skip = true;
  200. reason = "SpecialCasing.txt not implemented";
  201. }
  202. var sourceFile = new SourceFile(
  203. name,
  204. file,
  205. skip,
  206. reason,
  207. code);
  208. if (skipped == sourceFile.Skip)
  209. {
  210. results.Add(new object[]
  211. {
  212. sourceFile
  213. });
  214. }
  215. }
  216. return results;
  217. }
  218. }
  219. public class SourceFile
  220. {
  221. public SourceFile(
  222. string source,
  223. string fullPath,
  224. bool skip,
  225. string reason,
  226. string code)
  227. {
  228. Skip = skip;
  229. Source = source;
  230. Reason = reason;
  231. FullPath = fullPath;
  232. Code = code;
  233. }
  234. public string Source { get; }
  235. public bool Skip { get; }
  236. public string Reason { get; }
  237. public string FullPath { get; }
  238. public string Code { get; }
  239. public override string ToString()
  240. {
  241. return Source;
  242. }
  243. }
  244. }