import {CompilationTargetId} from "../../types/CompilationTargetId"; import {ChunkType} from "../chunks/types/ChunkType"; import {NodeChunk} from "../chunks/types/NodeChunk"; import {nodeChunkToNodeDef} from "./nodeChunkToNodeDef"; describe("nodeChunkToNodeDef()", () => { it("should convert node chunk into node definition", () => { const nodeChunk: NodeChunk = { "offset": { "line": 7, "column": 0 }, "type": ChunkType.Node, "children": [ { "offset": { "line": 8, "column": 2 }, "type": ChunkType.About, "children": [], "content": "Prints \"Hello, World!\" on the console.", "captures": [ "about" ] }, { "offset": { "line": 10, "column": 2 }, "type": ChunkType.NodeImpl, "children": [ { "offset": { "line": 11, "column": 4 }, "type": ChunkType.ChildNodes, "children": [ { "offset": { "line": 12, "column": 6 }, "type": ChunkType.ChildNodesItem, "captures": [ "(start program):(Start)", { "type": ChunkType.NodeDecl, "captures": [ "(start program):(Start)", { "type": ChunkType.Identifier, "captures": [ "(start program)" ], "offset": { "line": 12, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(Start)" ], "offset": { "line": 12, "column": 6 } } ], "offset": { "line": 12, "column": 6 } } ] }, { "offset": { "line": 13, "column": 6 }, "type": ChunkType.ChildNodesItem, "children": [ { "offset": { "line": 14, "column": 8 }, "type": ChunkType.NodeParam, "captures": [ "param (value) = \"Hello, World!\"", { "type": ChunkType.VarAssignment, "captures": [ "(value) = \"Hello, World!\"", { "type": ChunkType.Identifier, "captures": [ "(value)" ], "offset": { "line": 14, "column": 8 } }, { "type": ChunkType.Any, "captures": [ "\"Hello, World!\"" ], "offset": { "line": 14, "column": 8 } } ], "offset": { "line": 14, "column": 8 } } ] } ], "content": "param (value) = \"Hello, World!\"", "captures": [ "(send \"Hello, World!\"):(Value)", { "type": ChunkType.NodeDecl, "captures": [ "(send \"Hello, World!\"):(Value)", { "type": ChunkType.Identifier, "captures": [ "(send \"Hello, World!\")" ], "offset": { "line": 13, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(Value)" ], "offset": { "line": 13, "column": 6 } } ], "offset": { "line": 13, "column": 6 } } ] }, { "offset": { "line": 15, "column": 6 }, "type": ChunkType.ChildNodesItem, "captures": [ "(log to console):(Console Log)", { "type": ChunkType.NodeDecl, "captures": [ "(log to console):(Console Log)", { "type": ChunkType.Identifier, "captures": [ "(log to console)" ], "offset": { "line": 15, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(Console Log)" ], "offset": { "line": 15, "column": 6 } } ], "offset": { "line": 15, "column": 6 } } ] } ], "content": "(start program):(Start)\n(send \"Hello, World!\"):(Value)\n param (value) = \"Hello, World!\"\n(log to console):(Console Log)", "captures": [ "children" ] }, { "offset": { "line": 16, "column": 4 }, "type": ChunkType.Connections, "children": [ { "offset": { "line": 17, "column": 6 }, "type": ChunkType.ConnectionsItem, "captures": [ "out.(start) and in.(start)", { "type": ChunkType.ConnectionBody, "captures": [ "out.(start) and in.(start)", { "type": ChunkType.ChildPort, "captures": [ "out.(start)", { "type": ChunkType.Identifier, "captures": [ "(start program)" ], "offset": { "line": 17, "column": 6 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "out" ], "offset": { "line": 17, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(start)" ], "offset": { "line": 17, "column": 6 } } ], "offset": { "line": 17, "column": 6 } }, { "type": ChunkType.ChildPort, "captures": [ "in.(start)", { "type": ChunkType.Identifier, "captures": [ "(send \"Hello, World!\")" ], "offset": { "line": 17, "column": 6 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "in" ], "offset": { "line": 17, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(start)" ], "offset": { "line": 17, "column": 6 } } ], "offset": { "line": 17, "column": 6 } } ], "offset": { "line": 17, "column": 6 } } ] }, { "offset": { "line": 18, "column": 6 }, "type": ChunkType.ConnectionsItem, "captures": [ "out.(value) and in.(value)", { "type": ChunkType.ConnectionBody, "captures": [ "out.(value) and in.(value)", { "type": ChunkType.ChildPort, "captures": [ "out.(value)", { "type": ChunkType.Identifier, "captures": [ "(send \"Hello, World!\")" ], "offset": { "line": 18, "column": 6 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "out" ], "offset": { "line": 18, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(value)" ], "offset": { "line": 18, "column": 6 } } ], "offset": { "line": 18, "column": 6 } }, { "type": ChunkType.ChildPort, "captures": [ "in.(value)", { "type": ChunkType.Identifier, "captures": [ "(log to console)" ], "offset": { "line": 18, "column": 6 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "in" ], "offset": { "line": 18, "column": 6 } }, { "type": ChunkType.Identifier, "captures": [ "(value)" ], "offset": { "line": 18, "column": 6 } } ], "offset": { "line": 18, "column": 6 } } ], "offset": { "line": 18, "column": 6 } } ] } ], "content": "out.(start) and in.(start)\nout.(value) and in.(value)", "captures": [ "connect" ] } ], "content": "children\n (start program):(Start)\n (send \"Hello, World!\"):(Value)\n param (value) = \"Hello, World!\"\n (log to console):(Console Log)\nconnect\n out.(start) and in.(start)\n out.(value) and in.(value)", "captures": [ "in all", { "type": ChunkType.BuiltinTarget, "captures": [ "all" ], "offset": { "line": 10, "column": 2 } }, null ] } ], "content": "about\n Prints \"Hello, World!\" on the console.\nin all\n children\n (start program):(Start)\n (send \"Hello, World!\"):(Value)\n param (value) = \"Hello, World!\"\n (log to console):(Console Log)\n connect\n out.(start) and in.(start)\n out.(value) and in.(value)", "captures": [ "main", null, null ] }; const result = nodeChunkToNodeDef(nodeChunk, CompilationTargetId.es6); expect(result).toMatchSnapshot(); }); describe("when node has ports defined", () => { it("should add port names to definition", () => { const nodeChunk: NodeChunk = { "offset": { "line": 20, "column": 0 }, "type": ChunkType.Node, "children": [ { "offset": { "line": 21, "column": 2 }, "type": ChunkType.About, "children": [], "content": "Emits true when both inputs are true, false otherwise.", "captures": [ "about" ] }, { "offset": { "line": 23, "column": 2 }, "type": ChunkType.Ports, "children": [ { "offset": { "line": 24, "column": 4 }, "type": ChunkType.PortsItem, "captures": [ "(a):boolean", { "type": ChunkType.PortDecl, "captures": [ "(a):boolean", null, { "type": ChunkType.VarDecl, "captures": [ "(a):boolean", { "type": ChunkType.Identifier, "captures": [ "(a)" ], "offset": { "line": 24, "column": 4 } }, { "type": ChunkType.Any, "captures": [ "boolean" ], "offset": { "line": 24, "column": 4 } } ], "offset": { "line": 24, "column": 4 } } ], "offset": { "line": 24, "column": 4 } } ] }, { "offset": { "line": 25, "column": 4 }, "type": ChunkType.PortsItem, "captures": [ "(b):boolean", { "type": ChunkType.PortDecl, "captures": [ "(b):boolean", null, { "type": ChunkType.VarDecl, "captures": [ "(b):boolean", { "type": ChunkType.Identifier, "captures": [ "(b)" ], "offset": { "line": 25, "column": 4 } }, { "type": ChunkType.Any, "captures": [ "boolean" ], "offset": { "line": 25, "column": 4 } } ], "offset": { "line": 25, "column": 4 } } ], "offset": { "line": 25, "column": 4 } } ] } ], "content": "(a):boolean\n(b):boolean", "captures": [ "inputs", { "type": ChunkType.PortsDirection, "captures": [ "inputs" ], "offset": { "line": 23, "column": 2 } } ] }, { "offset": { "line": 26, "column": 2 }, "type": ChunkType.Ports, "children": [ { "offset": { "line": 27, "column": 4 }, "type": ChunkType.PortsItem, "captures": [ "(result):boolean", { "type": ChunkType.PortDecl, "captures": [ "(result):boolean", null, { "type": ChunkType.VarDecl, "captures": [ "(result):boolean", { "type": ChunkType.Identifier, "captures": [ "(result)" ], "offset": { "line": 27, "column": 4 } }, { "type": ChunkType.Any, "captures": [ "boolean" ], "offset": { "line": 27, "column": 4 } } ], "offset": { "line": 27, "column": 4 } } ], "offset": { "line": 27, "column": 4 } } ] } ], "content": "(result):boolean", "captures": [ "outputs", { "type": ChunkType.PortsDirection, "captures": [ "outputs" ], "offset": { "line": 26, "column": 2 } } ] }, { "offset": { "line": 28, "column": 2 }, "type": ChunkType.NodeImpl, "children": [ { "offset": { "line": 29, "column": 4 }, "type": ChunkType.ChildNode, "children": [ { "offset": { "line": 30, "column": 6 }, "type": ChunkType.NodeParam, "captures": [ "param (fields) = [\"a\", \"b\"]", { "type": ChunkType.VarAssignment, "captures": [ "(fields) = [\"a\", \"b\"]", { "type": ChunkType.Identifier, "captures": [ "(fields)" ], "offset": { "line": 30, "column": 6 } }, { "type": ChunkType.Any, "captures": [ "[\"a\", \"b\"]" ], "offset": { "line": 30, "column": 6 } } ], "offset": { "line": 30, "column": 6 } } ] } ], "content": "param (fields) = [\"a\", \"b\"]", "captures": [ "child (sync a & b):(Syncer)", { "type": ChunkType.NodeDecl, "captures": [ "(sync a & b):(Syncer)", { "type": ChunkType.Identifier, "captures": [ "(sync a & b)" ], "offset": { "line": 29, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(Syncer)" ], "offset": { "line": 29, "column": 4 } } ], "offset": { "line": 29, "column": 4 } } ] }, { "offset": { "line": 31, "column": 4 }, "type": ChunkType.ChildNode, "captures": [ "child (and synced):(And [internal])", { "type": ChunkType.NodeDecl, "captures": [ "(and synced):(And [internal])", { "type": ChunkType.Identifier, "captures": [ "(and synced)" ], "offset": { "line": 31, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(And [internal])" ], "offset": { "line": 31, "column": 4 } } ], "offset": { "line": 31, "column": 4 } } ] }, { "offset": { "line": 32, "column": 4 }, "type": ChunkType.Connection, "captures": [ "connect out.(synced) and in.(a & b)", { "type": ChunkType.ConnectionBody, "captures": [ "out.(synced) and in.(a & b)", { "type": ChunkType.ChildPort, "captures": [ "out.(synced)", { "type": ChunkType.Identifier, "captures": [ "(sync a & b)" ], "offset": { "line": 32, "column": 4 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "out" ], "offset": { "line": 32, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(synced)" ], "offset": { "line": 32, "column": 4 } } ], "offset": { "line": 32, "column": 4 } }, { "type": ChunkType.ChildPort, "captures": [ "in.(a & b)", { "type": ChunkType.Identifier, "captures": [ "(and synced)" ], "offset": { "line": 32, "column": 4 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "in" ], "offset": { "line": 32, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(a & b)" ], "offset": { "line": 32, "column": 4 } } ], "offset": { "line": 32, "column": 4 } } ], "offset": { "line": 32, "column": 4 } } ] }, { "offset": { "line": 33, "column": 4 }, "type": ChunkType.PortExposure, "captures": [ "expose in.(a) as (a)", { "type": ChunkType.PortExposureBody, "captures": [ "in.(a) as (a)", { "type": ChunkType.ChildPort, "captures": [ "in.(a)", { "type": ChunkType.Identifier, "captures": [ "(sync a & b)" ], "offset": { "line": 33, "column": 4 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "in" ], "offset": { "line": 33, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(a)" ], "offset": { "line": 33, "column": 4 } } ], "offset": { "line": 33, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(a)" ], "offset": { "line": 33, "column": 4 } } ], "offset": { "line": 33, "column": 4 } } ] }, { "offset": { "line": 34, "column": 4 }, "type": ChunkType.PortExposure, "captures": [ "expose in.(b) as (b)", { "type": ChunkType.PortExposureBody, "captures": [ "in.(b) as (b)", { "type": ChunkType.ChildPort, "captures": [ "in.(b)", { "type": ChunkType.Identifier, "captures": [ "(sync a & b)" ], "offset": { "line": 34, "column": 4 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "in" ], "offset": { "line": 34, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(b)" ], "offset": { "line": 34, "column": 4 } } ], "offset": { "line": 34, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(b)" ], "offset": { "line": 34, "column": 4 } } ], "offset": { "line": 34, "column": 4 } } ] }, { "offset": { "line": 35, "column": 4 }, "type": ChunkType.PortExposure, "captures": [ "expose out.(a & b) as (result)", { "type": ChunkType.PortExposureBody, "captures": [ "out.(a & b) as (result)", { "type": ChunkType.ChildPort, "captures": [ "out.(a & b)", { "type": ChunkType.Identifier, "captures": [ "(and synced)" ], "offset": { "line": 35, "column": 4 } }, { "type": ChunkType.ChildPortDirection, "captures": [ "out" ], "offset": { "line": 35, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(a & b)" ], "offset": { "line": 35, "column": 4 } } ], "offset": { "line": 35, "column": 4 } }, { "type": ChunkType.Identifier, "captures": [ "(result)" ], "offset": { "line": 35, "column": 4 } } ], "offset": { "line": 35, "column": 4 } } ] } ], "content": "child (sync a & b):(Syncer)\n param (fields) = [\"a\", \"b\"]\nchild (and synced):(And [internal])\nconnect out.(synced) and in.(a & b)\nexpose in.(a) as (a)\nexpose in.(b) as (b)\nexpose out.(a & b) as (result)", "captures": [ "in all", { "type": ChunkType.BuiltinTarget, "captures": [ "all" ], "offset": { "line": 28, "column": 2 } }, null ] } ], "content": "about\n Emits true when both inputs are true, false otherwise.\ninputs\n (a):boolean\n (b):boolean\noutputs\n (result):boolean\nin all\n child (sync a & b):(Syncer)\n param (fields) = [\"a\", \"b\"]\n child (and synced):(And [internal])\n connect out.(synced) and in.(a & b)\n expose in.(a) as (a)\n expose in.(b) as (b)\n expose out.(a & b) as (result)", "captures": [ "export node (And)", { "type": ChunkType.Export, "captures": [ "export" ], "offset": { "line": 20, "column": 0 } }, { "type": ChunkType.Identifier, "captures": [ "(And)" ], "offset": { "line": 20, "column": 0 } } ] }; const result = nodeChunkToNodeDef(nodeChunk, CompilationTargetId.es6); expect(result).toMatchSnapshot(); }); }); });