Test262Test.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using Esprima;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Interop;
  12. using Newtonsoft.Json.Linq;
  13. using Xunit;
  14. using Xunit.Abstractions;
  15. namespace Jint.Tests.Test262
  16. {
  17. public abstract class Test262Test
  18. {
  19. private static readonly Dictionary<string, string> Sources;
  20. private static readonly string BasePath;
  21. private static readonly TimeZoneInfo _pacificTimeZone;
  22. private static readonly Dictionary<string, string> _skipReasons =
  23. new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  24. private static readonly HashSet<string> _strictSkips =
  25. new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  26. static Test262Test()
  27. {
  28. //NOTE: The Date tests in test262 assume the local timezone is Pacific Standard Time
  29. _pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
  30. var assemblyPath = new Uri(typeof(Test262Test).GetTypeInfo().Assembly.CodeBase).LocalPath;
  31. var assemblyDirectory = new FileInfo(assemblyPath).Directory;
  32. BasePath = assemblyDirectory.Parent.Parent.Parent.FullName;
  33. string[] files =
  34. {
  35. "sta.js",
  36. "assert.js",
  37. "arrayContains.js",
  38. "propertyHelper.js",
  39. "compareArray.js",
  40. "decimalToHexString.js",
  41. "proxyTrapsHelper.js",
  42. "dateConstants.js",
  43. "assertRelativeDateMs.js",
  44. "regExpUtils.js",
  45. "nans.js",
  46. "compareIterator.js"
  47. };
  48. Sources = new Dictionary<string, string>(files.Length);
  49. for (var i = 0; i < files.Length; i++)
  50. {
  51. Sources[files[i]] = File.ReadAllText(Path.Combine(BasePath, "harness", files[i]));
  52. }
  53. var content = File.ReadAllText(Path.Combine(BasePath, "test/skipped.json"));
  54. var doc = JArray.Parse(content);
  55. foreach (var entry in doc.Values<JObject>())
  56. {
  57. var source = entry["source"].Value<string>();
  58. _skipReasons[source] = entry["reason"].Value<string>();
  59. if (entry.TryGetValue("mode", out var mode) && mode.Value<string>() == "strict")
  60. {
  61. _strictSkips.Add(source);
  62. }
  63. }
  64. }
  65. protected void RunTestCode(string code, bool strict)
  66. {
  67. var engine = new Engine(cfg => cfg
  68. .LocalTimeZone(_pacificTimeZone)
  69. .Strict(strict)
  70. );
  71. engine.Execute(Sources["sta.js"]);
  72. engine.Execute(Sources["assert.js"]);
  73. engine.SetValue("print", new ClrFunctionInstance(engine, "print", (thisObj, args) => TypeConverter.ToString(args.At(0))));
  74. var o = engine.Object.Construct(Arguments.Empty);
  75. o.FastSetProperty("evalScript", new PropertyDescriptor(new ClrFunctionInstance(engine, "evalScript", (thisObj, args) =>
  76. {
  77. if (args.Length > 1)
  78. {
  79. throw new Exception("only script parsing supported");
  80. }
  81. var options = new ParserOptions { AdaptRegexp = true, Tolerant = false };
  82. var parser = new JavaScriptParser(args.At(0).AsString(), options);
  83. var script = parser.ParseScript(strict);
  84. var value = engine.Execute(script).GetCompletionValue();
  85. return value;
  86. }), true, true, true));
  87. engine.SetValue("$262", o);
  88. var includes = Regex.Match(code, @"includes: \[(.+?)\]");
  89. if (includes.Success)
  90. {
  91. var files = includes.Groups[1].Captures[0].Value.Split(',');
  92. foreach (var file in files)
  93. {
  94. engine.Execute(Sources[file.Trim()]);
  95. }
  96. }
  97. if (code.IndexOf("propertyHelper.js", StringComparison.OrdinalIgnoreCase) != -1)
  98. {
  99. engine.Execute(Sources["propertyHelper.js"]);
  100. }
  101. string lastError = null;
  102. bool negative = code.IndexOf("negative:", StringComparison.Ordinal) > -1;
  103. try
  104. {
  105. engine.Execute(code);
  106. }
  107. catch (JavaScriptException j)
  108. {
  109. lastError = TypeConverter.ToString(j.Error);
  110. }
  111. catch (Exception e)
  112. {
  113. lastError = e.ToString();
  114. }
  115. if (negative)
  116. {
  117. Assert.NotNull(lastError);
  118. }
  119. else
  120. {
  121. Assert.Null(lastError);
  122. }
  123. }
  124. protected void RunTestInternal(SourceFile sourceFile)
  125. {
  126. if (sourceFile.Skip)
  127. {
  128. return;
  129. }
  130. if (sourceFile.Code.IndexOf("onlyStrict", StringComparison.Ordinal) < 0)
  131. {
  132. RunTestCode(sourceFile.Code, strict: false);
  133. }
  134. if (!_strictSkips.Contains(sourceFile.Source)
  135. && sourceFile.Code.IndexOf("noStrict", StringComparison.Ordinal) < 0)
  136. {
  137. RunTestCode(sourceFile.Code, strict: true);
  138. }
  139. }
  140. public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
  141. {
  142. var results = new ConcurrentBag<object[]>();
  143. var fixturesPath = Path.Combine(BasePath, "test");
  144. var searchPath = Path.Combine(fixturesPath, pathPrefix);
  145. var files = Directory.GetFiles(searchPath, "*", SearchOption.AllDirectories);
  146. foreach (var file in files)
  147. {
  148. var name = file.Substring(fixturesPath.Length + 1).Replace("\\", "/");
  149. bool skip = _skipReasons.TryGetValue(name, out var reason);
  150. var code = skip ? "" : File.ReadAllText(file);
  151. var flags = Regex.Match(code, "flags: \\[(.+?)\\]");
  152. if (flags.Success)
  153. {
  154. var items = flags.Groups[1].Captures[0].Value.Split(',');
  155. foreach (var item in items.Select(x => x.Trim()))
  156. {
  157. switch (item)
  158. {
  159. // TODO implement
  160. case "async":
  161. skip = true;
  162. reason = "async not implemented";
  163. break;
  164. }
  165. }
  166. }
  167. var features = Regex.Match(code, "features: \\[(.+?)\\]");
  168. if (features.Success)
  169. {
  170. var items = features.Groups[1].Captures[0].Value.Split(',');
  171. foreach (var item in items.Select(x => x.Trim()))
  172. {
  173. switch (item)
  174. {
  175. // TODO implement
  176. case "cross-realm":
  177. skip = true;
  178. reason = "realms not implemented";
  179. break;
  180. case "tail-call-optimization":
  181. skip = true;
  182. reason = "tail-calls not implemented";
  183. break;
  184. case "class":
  185. skip = true;
  186. reason = "class keyword not implemented";
  187. break;
  188. case "BigInt":
  189. skip = true;
  190. reason = "BigInt not implemented";
  191. break;
  192. case "generators":
  193. skip = true;
  194. reason = "generators not implemented";
  195. break;
  196. case "async-functions":
  197. skip = true;
  198. reason = "async-functions not implemented";
  199. break;
  200. case "async-iteration":
  201. skip = true;
  202. reason = "async not implemented";
  203. break;
  204. case "new.target":
  205. skip = true;
  206. reason = "MetaProperty not implemented";
  207. break;
  208. case "super":
  209. skip = true;
  210. reason = "super not implemented";
  211. break;
  212. case "String.prototype.replaceAll":
  213. skip = true;
  214. reason = "not in spec yet";
  215. break;
  216. case "u180e":
  217. skip = true;
  218. reason = "unicode/regexp not implemented";
  219. break;
  220. case "regexp-match-indices":
  221. skip = true;
  222. reason = "regexp-match-indices not implemented";
  223. break;
  224. case "regexp-named-groups":
  225. skip = true;
  226. reason = "regexp-named-groups not implemented";
  227. break;
  228. case "regexp-lookbehind":
  229. skip = true;
  230. reason = "regexp-lookbehind not implemented";
  231. break;
  232. case "TypedArray":
  233. skip = true;
  234. reason = "TypedArray not implemented";
  235. break;
  236. }
  237. }
  238. }
  239. if (code.IndexOf("SpecialCasing.txt") > -1)
  240. {
  241. skip = true;
  242. reason = "SpecialCasing.txt not implemented";
  243. }
  244. if (name.StartsWith("language/expressions/object/dstr-async-gen-meth-"))
  245. {
  246. skip = true;
  247. reason = "Esprima problem, Unexpected token *";
  248. }
  249. if (name.StartsWith("built-ins/RegExp/property-escapes/generated/"))
  250. {
  251. skip = true;
  252. reason = "Esprima problem, Invalid regular expression";
  253. }
  254. if (name.StartsWith("built-ins/RegExp/unicode_"))
  255. {
  256. skip = true;
  257. reason = "Unicode support and its special cases need more work";
  258. }
  259. if (name.StartsWith("built-ins/RegExp/CharacterClassEscapes/"))
  260. {
  261. skip = true;
  262. reason = "for-of not implemented";
  263. }
  264. if (file.EndsWith("tv-line-continuation.js")
  265. || file.EndsWith("tv-line-terminator-sequence.js")
  266. || file.EndsWith("special-characters.js"))
  267. {
  268. // LF endings required
  269. code = code.Replace("\r\n", "\n");
  270. }
  271. var sourceFile = new SourceFile(
  272. name,
  273. file,
  274. skip,
  275. reason,
  276. code);
  277. if (skipped == sourceFile.Skip)
  278. {
  279. results.Add(new object[]
  280. {
  281. sourceFile
  282. });
  283. }
  284. }
  285. return results;
  286. }
  287. }
  288. public class SourceFile : IXunitSerializable
  289. {
  290. public SourceFile()
  291. {
  292. }
  293. public SourceFile(
  294. string source,
  295. string fullPath,
  296. bool skip,
  297. string reason,
  298. string code)
  299. {
  300. Skip = skip;
  301. Source = source;
  302. Reason = reason;
  303. FullPath = fullPath;
  304. Code = code;
  305. }
  306. public string Source { get; set; }
  307. public bool Skip { get; set; }
  308. public string Reason { get; set; }
  309. public string FullPath { get; set; }
  310. public string Code { get; set; }
  311. public void Deserialize(IXunitSerializationInfo info)
  312. {
  313. Skip = info.GetValue<bool>(nameof(Skip));
  314. Source = info.GetValue<string>(nameof(Source));
  315. Reason = info.GetValue<string>(nameof(Reason));
  316. FullPath = info.GetValue<string>(nameof(FullPath));
  317. Code = info.GetValue<string>(nameof(Code));
  318. }
  319. public void Serialize(IXunitSerializationInfo info)
  320. {
  321. info.AddValue(nameof(Skip), Skip);
  322. info.AddValue(nameof(Source), Source);
  323. info.AddValue(nameof(Reason), Reason);
  324. info.AddValue(nameof(FullPath), FullPath);
  325. info.AddValue(nameof(Code), Code);
  326. }
  327. public override string ToString()
  328. {
  329. return Source;
  330. }
  331. }
  332. }