Skip to content

A .NET version of test262 ECMAScript test harness

License

Notifications You must be signed in to change notification settings

Tunealb/test262-harness-dotnet

 
 

Repository files navigation

Build NuGet NuGet MyGet

Test262-Harness-dotnet

This is a .NET test runner for Test262: ECMAScript Test Suite. It includes parsing and downloading logic for the test suite in package Test262Harness and test suite generator functionality via CLI too, Test262Harness.Console

Usage

Following projects are utilizing the test suite generation and show how to create NUnit based test suite that is being generated by downloaded snapshot from test262 GitHub repository.

  • Jint
    • Generates NUnit test suite to ensure compliance, suite can be run in parallel for faster feedback loop
  • esprima-dotnet
    • Generates NUnit test suite for parsing tests, also has custom console logic to compare allow-list.txt for problematic files and progress getting the to parse properly

First you need need to install the required package to your test project, it should look similar to this:

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
    <PackageReference Include="NUnit" Version="3.13.3" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
    <PackageReference Include="Test262Harness" Version="0.0.13" />
  </ItemGroup>

Next you will need a configuration, similar to this (also check the configuration format section):

Test262Harness.settings.json example

{
  "SuiteGitSha": "28b31c0bf1960878abb36ab8597a0cae224a684d",
  "TargetPath": "./Generated",
  "Namespace": "My.Tests.Test262",
  "ExcludedFeatures": [
    "Atomics",
    "Temporal"
  ],
  "ExcludedFlags": [
    "async" 
  ],
  "ExcludedDirectories": [
    "annexB",
    "intl402"
  ],
  "ExcludedFiles": [
    "language/expressions/object/dstr-async-gen-meth-*",
    "language/expressions/assignment/fn-name-lhs-cover.js"
  ]
}

You need to create minimal test file stub to initialize your testing target, example from Jint project.

using System;
using System.IO;
using Esprima;
using Jint.Native;
using Jint.Native.ArrayBuffer;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
using Test262Harness;

namespace Jint.Tests.Test262;

public static partial class State
{
    /// <summary>
    /// Pre-compiled scripts for faster execution.
    /// </summary>
    public static readonly Dictionary<string, Script> Sources = new(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Handles initializing testing state.
/// </summary>
public partial class TestHarness
{
    private static partial Task InitializeCustomState()
    {
        foreach (var file in State.HarnessFiles)
        {
            var source = file.Program;
            State.Sources[Path.GetFileName(file.FileName)] = new JavaScriptParser(source, new ParserOptions(file.FileName)).ParseScript();
        }

        return Task.CompletedTask;
    }
}

public abstract partial class Test262Test
{
    private Engine BuildTestExecutor(Test262File file)
    {
        var engine = new Engine(cfg =>
        {
            var relativePath = Path.GetDirectoryName(file.FileName);
            cfg.EnableModules(new Test262ModuleLoader(State.Test262Stream.Options.FileSystem, relativePath));
        });

        if (file.Flags.Contains("raw"))
        {
            // nothing should be loaded
            return engine;
        }

        engine.Execute(State.Sources["assert.js"]);
        engine.Execute(State.Sources["sta.js"]);

        // initialize engine with Test262 expected host defined functions here
        // https://github.com/tc39/test262/blob/main/INTERPRETING.md#host-defined-functions

        engine.SetValue("print", new ClrFunctionInstance(engine, "print", (_, args) => TypeConverter.ToString(args.At(0))));
        // and more...

        // the cinded files that that are expected
        foreach (var include in file.Includes)
        {
            engine.Execute(State.Sources[include]);
        }

        return engine;
    }

    private static void ExecuteTest(Engine engine, Test262File file)
    {
        if (file.Type == ProgramType.Module)
        {
            engine.AddModule(file.FileName, builder => builder.AddSource(file.Program));
            engine.ImportModule(file.FileName);
        }
        else
        {
            engine.Execute(new JavaScriptParser(file.Program, new ParserOptions(file.FileName)).ParseScript());
        }
    }

    private partial bool ShouldThrow(Test262File testCase, bool strict)
    {
        return testCase.Negative;
    }
}

And also the CLI tool for generating the test suite, run this in you test project directory.

dotnet tool add Test262Harness.Console

When everything is installed, you should be able to run:

dotnet tool restore
dotnet test262 generate

Test262Harness.settings.json configuration file

List of most important things you can tweak in configuration file:

Key Default Description
SuiteGitSha none The GitHub commit to use when downloading the test suite
SuiteDirectory none Alternatively, you can point to local repository root
TargetPath none Where to generate the file to
Namespace Test262Harness.TestSuite Namespace for the generated source files
ExcludedFeatures [] Any feature you want to ignore
ExcludedFlags [] Any flag you want to ignore
ExcludedDirectories [] Any sub-directory you would like to ignore, for example annexB
ExcludedFiles [] List of specific files you would like to ignore

Exclusion maps to setting [Ignore] attribute in test suite.

Branches and releases

About

A .NET version of test262 ECMAScript test harness

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 87.8%
  • Liquid 5.5%
  • PowerShell 3.6%
  • Shell 2.8%
  • Batchfile 0.3%