Test262Test.cs 12 KB

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