Test262Test.cs 13 KB

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