From bafab63473f759e985e2a98ea494e32056f8d11d Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Mon, 17 Aug 2020 16:22:21 +0200 Subject: [PATCH] Tests: Added an option to accept the actual token stream (#2515) --- test-suite.html | 24 +++++++- tests/helper/test-case.js | 124 +++++++++++++++++++++++--------------- tests/run.js | 4 +- 3 files changed, 99 insertions(+), 53 deletions(-) diff --git a/test-suite.html b/test-suite.html index 2e2e171b3b..e1079901ab 100644 --- a/test-suite.html +++ b/test-suite.html @@ -93,8 +93,11 @@

Writing your test

Your file is built up of two or three sections, separated by ten or more dashes -, starting at the begin of the line:

    -
  1. Your language snippet. The code you want to compile using Prism. (required)
  2. -
  3. The simplified token stream you expect. Needs to be valid JSON. (required)
  4. +
  5. Your language snippet. The code you want to tokenize using Prism. (required)
  6. +
  7. + The simplified token stream you expect. Needs to be valid JSON. (optional)
    + If there no token stream defined, the test case will fail unless the --accept flag is present when running the test command (e.g. npm run test:languages -- --accept). If the flag is present and there is no expected token stream, the runner will insert the actual token stream into the test case file, changing it. +
  8. A comment explaining the test case. (optional)

The easiest way would be to look at an existing test file:

@@ -114,10 +117,25 @@

Writing your test

This is a comment explaining this test case. +

The easy way

+

The easy way to create one or multiple new test case(s) is this:

+ +
    +
  1. Create a new file for a new test case in tests/languages/${language}.
  2. +
  3. Insert the code you want to test (and nothing more).
  4. +
  5. Repeat the first two steps for as many test cases as you want.
  6. +
  7. Run npm run test:languages -- --accept.
  8. +
  9. Done.
  10. +
+ +

This works by making the test runner insert the actual token stream of you test code as the expected token stream. Carefully check that the inserted token stream is actually what you expect or else the test is meaningless!

+ +

Optionally, you can then also add comments to test cases.

+

Explaining the simplified token stream

-

While compiling, Prism transforms your source code into a token stream. This is basically a tree of nested tokens (or arrays, or strings).

+

While highlighting, Prism transforms your source code into a token stream. This is basically a tree of nested tokens (or arrays, or strings).

As these trees are hard to write by hand, the test runner uses a simplified version of it.

It uses the following rules: