Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lahma/test262-harness-dotnet

A .NET version of test262 ECMAScript test harness
https://github.com/lahma/test262-harness-dotnet

ecmascript javascript test-automation test-framework

Last synced: 3 months ago
JSON representation

A .NET version of test262 ECMAScript test harness

Awesome Lists containing this project

README

        

[![Build](https://github.com/lahma/test262-harness-dotnet/actions/workflows/build.yml/badge.svg)](https://github.com/sebastienros/jint/actions/workflows/build.yml)
[![NuGet](https://img.shields.io/nuget/v/Test262Harness.svg)](https://www.nuget.org/packages/Test262Harness)
[![NuGet](https://img.shields.io/nuget/vpre/Test262Harness.svg)](https://www.nuget.org/packages/Test262Harness)
[![MyGet](https://img.shields.io/myget/test262harness/vpre/Test262Harness.svg?label=MyGet)](https://www.myget.org/feed/test262harness/package/nuget/Test262Harness)

# Test262-Harness-dotnet

This is a .NET test runner for Test262: [ECMAScript Test Suite](https://github.com/tc39/test262).
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](https://github.com/sebastienros/jint/tree/main/Jint.Tests.Test262)
* Generates NUnit test suite to ensure compliance, suite can be run in parallel for faster feedback loop
* [esprima-dotnet](https://github.com/sebastienros/esprima-dotnet/tree/main/test/Esprima.Tests.Test262)
* Generates NUnit test suite for parsing tests, also has [custom console logic](https://github.com/sebastienros/esprima-dotnet/blob/main/test/Esprima.Tests.Test262/Program.cs) to compare allow-list.txt for problematic files and progress getting the to parse properly
* [acornima](https://github.com/adams85/acornima/tree/master/test/Acornima.Tests.Test262)
* Generates NUnit test suite for parsing tests

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

````xml






````

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

__Test262Harness.settings.json example__
```json5
{
"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.

```c#
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
{
///
/// Pre-compiled scripts for faster execution.
///
public static readonly Dictionary Sources = new(StringComparer.OrdinalIgnoreCase);
}

///
/// Handles initializing testing state.
///
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 ClrFunction(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](https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#features) you want to ignore |
| ExcludedFlags | [] | Any [flag](https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#flags) 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

- The recommended branch is __main__, any PR should target this branch
- The __main__ branch is automatically built and published on [MyGet](https://www.myget.org/feed/Packages/Test262Harness). Add this feed to your NuGet sources to use it: https://www.myget.org/F/test262harness/api/v3/index.json
- The __main__ branch is occasionally published on [NuGet](https://www.nuget.org/packages/Test262Harness)