ModuleTests.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Jint.Runtime;
  2. using Jint.Runtime.Modules;
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using Xunit;
  7. using Xunit.Sdk;
  8. namespace Jint.Tests.Test262.Language;
  9. public class ModuleTests : Test262Test
  10. {
  11. [Theory(DisplayName = "language\\module-code")]
  12. [MemberData(nameof(SourceFiles), "language\\module-code", false)]
  13. [MemberData(nameof(SourceFiles), "language\\module-code", true, Skip = "Skipped")]
  14. protected void ModuleCode(SourceFile sourceFile)
  15. {
  16. RunModuleTest(sourceFile);
  17. }
  18. [Theory(DisplayName = "language\\export")]
  19. [MemberData(nameof(SourceFiles), "language\\export", false)]
  20. [MemberData(nameof(SourceFiles), "language\\export", true, Skip = "Skipped")]
  21. protected void Export(SourceFile sourceFile)
  22. {
  23. RunModuleTest(sourceFile);
  24. }
  25. [Theory(DisplayName = "language\\import")]
  26. [MemberData(nameof(SourceFiles), "language\\import", false)]
  27. [MemberData(nameof(SourceFiles), "language\\import", true, Skip = "Skipped")]
  28. protected void Import(SourceFile sourceFile)
  29. {
  30. RunModuleTest(sourceFile);
  31. }
  32. private static void RunModuleTest(SourceFile sourceFile)
  33. {
  34. if (sourceFile.Skip)
  35. {
  36. return;
  37. }
  38. var code = sourceFile.Code;
  39. var options = new Options();
  40. options.Host.Factory = _ => new ModuleTestHost();
  41. options.EnableModules(Path.Combine(BasePath, "test"));
  42. var engine = new Engine(options);
  43. var negative = code.IndexOf("negative:", StringComparison.OrdinalIgnoreCase) != -1;
  44. string lastError = null;
  45. try
  46. {
  47. engine.LoadModule(sourceFile.FullPath);
  48. }
  49. catch (JavaScriptException ex)
  50. {
  51. lastError = ex.ToString();
  52. }
  53. catch (Exception ex)
  54. {
  55. lastError = ex.ToString();
  56. }
  57. if (!negative && !string.IsNullOrWhiteSpace(lastError))
  58. {
  59. throw new XunitException(lastError);
  60. }
  61. }
  62. }