Skip to content

Commit

Permalink
fix(scxml): makes scxml based sources render correctly again (#224)
Browse files Browse the repository at this point in the history
## Description

- fix(parse/scxml): adds emitting id's for transitions (these id's are
now necessary for proper rendition)
- fix(test): makes tests properly fail when schema validation fails
- fix(schema): makes the 'id' transition attribute mandatory

## How Has This Been Tested?

- [x] green ci
- [x] updated automated non-regression tests

## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [ ] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij authored Dec 25, 2024
1 parent f3a2a0a commit 966a128
Show file tree
Hide file tree
Showing 80 changed files with 227 additions and 48 deletions.
3 changes: 3 additions & 0 deletions dist/parse/scxml/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/parse/smcat-ast.schema.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<meta property="og:image:type" content="image/png" />
<link rel="canonical" href="https://state-machine-cat.js.org">
<script nonce="known-inline-script">let LOG = false;</script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-3vcVtQMJN2N6fqB10BdsPfqDoL3KjNhCdE5Zn/h10PEJWTArwLYVgkjhf4kDgRABgC5iwHVgDR1g3D99cJscqQ=="></script>
<script src="smcat-online-interpreter.min.js" type="module" defer integrity="sha512-7I54aAq8ylg112ufDfO4SxBLIliKPx4uHvLB14NKZTVxBRSL8Jqlzd4o69um9MWR6p3sNxPe7gz45YYKfTuUDg=="></script>
<script defer src="https://code.getmdl.io/1.3.0/material.min.js" async></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="purple">
Expand Down
2 changes: 1 addition & 1 deletion docs/inpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
src="state-machine-cat-inpage.min.js"
type="module"
defer
integrity="sha512-XCJjJ6rK34lTDEMcYyblrtntUgwJ+QaODtTF+EdtHwz6InxYQR8KLixaOkXJGiTRBBUVfnVq6JtjJGdE+uFzew=="
integrity="sha512-9CN180iJ3HTh9OG66+9sLwGvlbPCqSa+CMKzViJ3nm1FV/KvFEOfbHQQvwePPFsGWoqRnXTdZbvv2djiTq389Q=="
></script>
<style>
body { font-family: sans-serif; margin: 0 auto; max-width: 799px;
Expand Down
20 changes: 10 additions & 10 deletions docs/smcat-online-interpreter.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/smcat-online-interpreter.min.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions docs/state-machine-cat-inpage.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/state-machine-cat-inpage.min.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/parse/scxml/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ function reduceTransition(pState) {

return pAllTransitions.concat(
lTargets.map((pTarget) => ({
id: parserHelpers.nextTransitionId(),
from: pState.id,
// a 'target-less transition' is typically
// a self-transition
Expand Down Expand Up @@ -200,6 +201,7 @@ function mapMachine(pSCXMLStateMachine) {
.concat(lNormalizedMachine.history.map(mapState("history")))
.concat(lNormalizedMachine.final.map(mapState("final"))),
};
parserHelpers.resetTransitionId();

const lTransitions = extractTransitions(lNormalizedMachine.initial)
.concat(extractTransitions(lNormalizedMachine.state))
Expand All @@ -208,6 +210,8 @@ function mapMachine(pSCXMLStateMachine) {
if (lTransitions.length > 0) {
lReturnValue.transitions = lTransitions;
}
parserHelpers.resetTransitionId();

return lReturnValue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/parse/smcat-ast.schema.mts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default {
type: "array",
items: {
type: "object",
required: ["from", "to"],
required: ["id", "from", "to"],
additionalProperties: false,
properties: {
id: {
Expand Down
2 changes: 2 additions & 0 deletions test/index.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ describe("integration - regular esm", () => {
],
transitions: [
{
id: 1,
from: "off",
to: "on",
event: "switch_flipped",
label: "switch_flipped",
},
{
id: 2,
from: "on",
to: "off",
event: "switch_flipped",
Expand Down
36 changes: 24 additions & 12 deletions test/parse/scxml.spec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import path from "node:path";
import { deepEqual, doesNotThrow, throws } from "node:assert/strict";
import { deepEqual, doesNotThrow, equal, throws } from "node:assert/strict";
import Ajv from "ajv";

import { parse } from "#parse/scxml/index.mjs";
Expand Down Expand Up @@ -31,7 +31,7 @@ describe("parse/scxml", () => {
),
),
);
ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
});
});

Expand All @@ -44,7 +44,7 @@ describe("parse/scxml", () => {
</scxml>`;
const lAST = parse(lStateWithAnInvoke);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand Down Expand Up @@ -73,7 +73,7 @@ describe("parse/scxml", () => {
</scxml>`;
const lAST = parse(lStateWithAnInvoke);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand Down Expand Up @@ -113,7 +113,7 @@ describe("parse/scxml", () => {
</scxml>`;
const lAST = parse(lScxmlWithTargetlessTransition);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -123,6 +123,7 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "a",
to: "a",
action: "do something interesting",
Expand All @@ -142,7 +143,7 @@ describe("parse/scxml", () => {
</scxml>`;
const lAST = parse(lScxmlWithInitialNode);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -156,6 +157,7 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "closed",
},
Expand All @@ -175,7 +177,7 @@ describe("parse/scxml", () => {
</scxml>`;
const lAST = parse(lScxmlWithInitialNode);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -194,6 +196,7 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "door.initial",
to: "closed",
},
Expand All @@ -217,7 +220,7 @@ describe("parse/scxml", () => {
`;
const lAST = parse(lScxmlOnentryWithXml);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -237,6 +240,7 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "a",
},
Expand All @@ -257,7 +261,7 @@ describe("parse/scxml", () => {
`;
const lAST = parse(lScxmlOnexitWithXml);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -277,6 +281,7 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "a",
},
Expand All @@ -297,7 +302,7 @@ describe("parse/scxml", () => {
`;
const lAST = parse(lScxmlTransitionWithXml);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -311,10 +316,12 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "a",
},
{
id: 2,
from: "a",
to: "a",
action: '<assign location="Var1" expr="return"/>',
Expand All @@ -339,7 +346,7 @@ describe("parse/scxml", () => {
`;
const lAST = parse(lScxmTransitionToMultipleTargets);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, {
states: [
{
Expand All @@ -361,14 +368,17 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "a",
},
{
id: 2,
from: "a",
to: "b",
},
{
id: 3,
from: "a",
to: "c",
},
Expand Down Expand Up @@ -421,10 +431,12 @@ describe("parse/scxml", () => {
],
transitions: [
{
id: 1,
from: "initial",
to: "ParallelState",
},
{
id: 2,
from: "ParallelState",
to: "Done",
},
Expand All @@ -433,7 +445,7 @@ describe("parse/scxml", () => {

const lAST = parse(lScxmlTransitionFromCompoundParallelState);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, lExpectedAst);
});

Expand Down
4 changes: 2 additions & 2 deletions test/parse/smcat-parser.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("#parse() - happy day ASTs -", () => {
it(pPair.title, () => {
const lAST = parseSmCat(pPair.program);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, pPair.ast);
});
}
Expand All @@ -53,7 +53,7 @@ describe("#parse() - file based - ", () => {
);
const lAST = parseSmCat(lProgram);

ajv.validate($schema, lAST);
equal(ajv.validate($schema, lAST), true);
deepEqual(lAST, requireJSON(`./${pPair.astFixtureFile}`));
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "initial",
"to": "some_state"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "initial",
"to": "some_state"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "initial",
"to": "some_state"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "initial",
"to": "some_state",
"action": "here's some executable content",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "a",
"to": "b"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "a",
"to": "b",
"event": "some_event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@
],
"transitions": [
{
"id": 1,
"from": "a",
"to": "b",
"event": "some_event",
"label": "some_event"
},
{
"id": 2,
"from": "a",
"to": "b",
"event": "some_other_event",
"label": "some_other_event"
},
{
"id": 3,
"from": "b",
"to": "a",
"event": "and_back_again",
"label": "and_back_again"
},
{
"id": 4,
"from": "b",
"to": "final"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"transitions": [
{
"id": 1,
"from": "a",
"to": "b",
"cond": "some condition",
Expand Down
Loading

0 comments on commit 966a128

Please sign in to comment.