From d1a21493de806b57d5bf51bed14c493a30b6b9c6 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Thu, 4 Jan 2024 15:01:27 +0000 Subject: [PATCH] Add base visitor tests for each node (#137) * Create AccountNode.test.ts * Add test macros * Create DefinedTypeNode.test.ts * Add ErrorNode tests * Prepare all tests by copy pasting the account node tests * Update InstructionAccountNode.test.ts * Update instructionArgumentNode test * Add InstructionByteDeltaNode tests * Add instructionRemainingAccountsNode tests * Update PdaNode.test.ts * Add InstructionNode tests * Update ProgramNode.test.ts * Update RootNode.test.ts * Add tests for contextualValueNodes * Add discriminatorNodes tests * Add linkNodes tests * Add pdaSeedNodes tests * Add sizeNodes tests * Add typeNodes tests * Add valueNodes tests * Add changeset --- .changeset/few-meals-press.md | 5 + .../contextualValueNodes/ResolverValueNode.ts | 7 +- src/nodes/valueNodes/index.ts | 1 + src/visitors/getDebugStringVisitor.ts | 47 +++++- src/visitors/identityVisitor.ts | 34 +++-- src/visitors/mergeVisitor.ts | 8 + test/visitors/nodes/AccountNode.test.ts | 51 +++++++ test/visitors/nodes/DefinedTypeNode.test.ts | 43 ++++++ test/visitors/nodes/ErrorNode.test.ts | 20 +++ .../nodes/InstructionAccountNode.test.ts | 31 ++++ .../nodes/InstructionArgumentNode.test.ts | 35 +++++ .../nodes/InstructionByteDeltaNode.test.ts | 24 +++ test/visitors/nodes/InstructionNode.test.ts | 144 ++++++++++++++++++ .../InstructionRemainingAccountsNode.test.ts | 28 ++++ test/visitors/nodes/PdaNode.test.ts | 44 ++++++ test/visitors/nodes/ProgramNode.test.ts | 72 +++++++++ test/visitors/nodes/RootNode.test.ts | 32 ++++ test/visitors/nodes/_setup.ts | 60 ++++++++ .../AccountBumpValueNode.test.ts | 15 ++ .../AccountValueNode.test.ts | 15 ++ .../ArgumentValueNode.test.ts | 15 ++ .../ConditionalValueNode.test.ts | 54 +++++++ .../IdentityValueNode.test.ts | 15 ++ .../PayerValueNode.test.ts | 15 ++ .../PdaSeedValueNode.test.ts | 22 +++ .../contextualValueNodes/PdaValueNode.test.ts | 42 +++++ .../ProgramIdValueNode.test.ts | 15 ++ .../ResolverValueNode.test.ts | 35 +++++ .../ByteDiscriminatorNode.test.ts | 19 +++ .../FieldDiscriminatorNode.test.ts | 19 +++ .../SizeDiscriminatorNode.test.ts | 15 ++ .../nodes/linkNodes/AccountLinkNode.test.ts | 15 ++ .../linkNodes/DefinedTypeLinkNode.test.ts | 19 +++ .../nodes/linkNodes/PdaLinkNode.test.ts | 19 +++ .../nodes/linkNodes/ProgramLinkNode.test.ts | 19 +++ .../pdaSeedNodes/ConstantPdaSeedNode.test.ts | 28 ++++ .../pdaSeedNodes/ProgramIdPdaSeedNode.test.ts | 15 ++ .../pdaSeedNodes/VariablePdaSeedNode.test.ts | 22 +++ .../nodes/sizeNodes/FixedSizeNode.test.ts | 15 ++ .../nodes/sizeNodes/PrefixedSizeNode.test.ts | 22 +++ .../nodes/sizeNodes/RemainderSizeNode.test.ts | 15 ++ .../nodes/typeNodes/AmountTypeNode.test.ts | 22 +++ .../nodes/typeNodes/ArrayTypeNode.test.ts | 34 +++++ .../nodes/typeNodes/BooleanTypeNode.test.ts | 22 +++ .../nodes/typeNodes/BytesTypeNode.test.ts | 22 +++ .../nodes/typeNodes/DateTimeTypeNode.test.ts | 22 +++ .../EnumEmptyVariantTypeNode.test.ts | 19 +++ .../EnumStructVariantTypeNode.test.ts | 49 ++++++ .../EnumTupleVariantTypeNode.test.ts | 43 ++++++ .../nodes/typeNodes/EnumTypeNode.test.ts | 82 ++++++++++ .../nodes/typeNodes/MapTypeNode.test.ts | 39 +++++ .../nodes/typeNodes/NumberTypeNode.test.ts | 21 +++ .../nodes/typeNodes/OptionTypeNode.test.ts | 31 ++++ .../nodes/typeNodes/PublicKeyTypeNode.test.ts | 15 ++ .../nodes/typeNodes/SetTypeNode.test.ts | 28 ++++ .../nodes/typeNodes/SolAmountTypeNode.test.ts | 22 +++ .../nodes/typeNodes/StringTypeNode.test.ts | 41 +++++ .../typeNodes/StructFieldTypeNode.test.ts | 37 +++++ .../nodes/typeNodes/StructTypeNode.test.ts | 42 +++++ .../nodes/typeNodes/TupleTypeNode.test.ts | 32 ++++ .../nodes/valueNodes/ArrayValueNode.test.ts | 31 ++++ .../nodes/valueNodes/BooleanValueNode.test.ts | 15 ++ .../nodes/valueNodes/EnumValueNode.test.ts | 45 ++++++ .../valueNodes/MapEntryValueNode.test.ts | 31 ++++ .../nodes/valueNodes/MapValueNode.test.ts | 42 +++++ .../nodes/valueNodes/NoneValueNode.test.ts | 15 ++ .../nodes/valueNodes/NumberValueNode.test.ts | 15 ++ .../valueNodes/PublicKeyValueNode.test.ts | 19 +++ .../nodes/valueNodes/SetValueNode.test.ts | 31 ++++ .../nodes/valueNodes/SomeValueNode.test.ts | 24 +++ .../nodes/valueNodes/StringValueNode.test.ts | 15 ++ .../valueNodes/StructFieldValueNode.test.ts | 22 +++ .../nodes/valueNodes/StructValueNode.test.ts | 36 +++++ .../nodes/valueNodes/TupleValueNode.test.ts | 38 +++++ 74 files changed, 2158 insertions(+), 15 deletions(-) create mode 100644 .changeset/few-meals-press.md create mode 100644 test/visitors/nodes/AccountNode.test.ts create mode 100644 test/visitors/nodes/DefinedTypeNode.test.ts create mode 100644 test/visitors/nodes/ErrorNode.test.ts create mode 100644 test/visitors/nodes/InstructionAccountNode.test.ts create mode 100644 test/visitors/nodes/InstructionArgumentNode.test.ts create mode 100644 test/visitors/nodes/InstructionByteDeltaNode.test.ts create mode 100644 test/visitors/nodes/InstructionNode.test.ts create mode 100644 test/visitors/nodes/InstructionRemainingAccountsNode.test.ts create mode 100644 test/visitors/nodes/PdaNode.test.ts create mode 100644 test/visitors/nodes/ProgramNode.test.ts create mode 100644 test/visitors/nodes/RootNode.test.ts create mode 100644 test/visitors/nodes/_setup.ts create mode 100644 test/visitors/nodes/contextualValueNodes/AccountBumpValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/AccountValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/ArgumentValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/ConditionalValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/IdentityValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/PayerValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/PdaSeedValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/PdaValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/ProgramIdValueNode.test.ts create mode 100644 test/visitors/nodes/contextualValueNodes/ResolverValueNode.test.ts create mode 100644 test/visitors/nodes/discriminatorNodes/ByteDiscriminatorNode.test.ts create mode 100644 test/visitors/nodes/discriminatorNodes/FieldDiscriminatorNode.test.ts create mode 100644 test/visitors/nodes/discriminatorNodes/SizeDiscriminatorNode.test.ts create mode 100644 test/visitors/nodes/linkNodes/AccountLinkNode.test.ts create mode 100644 test/visitors/nodes/linkNodes/DefinedTypeLinkNode.test.ts create mode 100644 test/visitors/nodes/linkNodes/PdaLinkNode.test.ts create mode 100644 test/visitors/nodes/linkNodes/ProgramLinkNode.test.ts create mode 100644 test/visitors/nodes/pdaSeedNodes/ConstantPdaSeedNode.test.ts create mode 100644 test/visitors/nodes/pdaSeedNodes/ProgramIdPdaSeedNode.test.ts create mode 100644 test/visitors/nodes/pdaSeedNodes/VariablePdaSeedNode.test.ts create mode 100644 test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts create mode 100644 test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts create mode 100644 test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/AmountTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/BooleanTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/BytesTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/DateTimeTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/EnumEmptyVariantTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/EnumStructVariantTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/EnumTupleVariantTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/EnumTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/MapTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/NumberTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/OptionTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/PublicKeyTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/SetTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/SolAmountTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/StringTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/StructFieldTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/StructTypeNode.test.ts create mode 100644 test/visitors/nodes/typeNodes/TupleTypeNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/ArrayValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/BooleanValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/EnumValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/MapEntryValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/MapValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/NoneValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/NumberValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/PublicKeyValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/SetValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/SomeValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/StringValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/StructFieldValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/StructValueNode.test.ts create mode 100644 test/visitors/nodes/valueNodes/TupleValueNode.test.ts diff --git a/.changeset/few-meals-press.md b/.changeset/few-meals-press.md new file mode 100644 index 000000000..2b6f92153 --- /dev/null +++ b/.changeset/few-meals-press.md @@ -0,0 +1,5 @@ +--- +'@metaplex-foundation/kinobi': patch +--- + +Add base visitor tests for each node diff --git a/src/nodes/contextualValueNodes/ResolverValueNode.ts b/src/nodes/contextualValueNodes/ResolverValueNode.ts index 374696f33..fe05e8ce0 100644 --- a/src/nodes/contextualValueNodes/ResolverValueNode.ts +++ b/src/nodes/contextualValueNodes/ResolverValueNode.ts @@ -20,5 +20,10 @@ export function resolverValueNode( dependsOn?: ResolverValueNode['dependsOn']; } = {} ): ResolverValueNode { - return { kind: 'resolverValueNode', name: mainCase(name), ...options }; + return { + kind: 'resolverValueNode', + name: mainCase(name), + importFrom: options.importFrom, + dependsOn: options.dependsOn, + }; } diff --git a/src/nodes/valueNodes/index.ts b/src/nodes/valueNodes/index.ts index 40f3a71cf..80b3eb86d 100644 --- a/src/nodes/valueNodes/index.ts +++ b/src/nodes/valueNodes/index.ts @@ -3,6 +3,7 @@ export * from './BooleanValueNode'; export * from './EnumValueNode'; export * from './MapEntryValueNode'; export * from './MapValueNode'; +export * from './NoneValueNode'; export * from './NumberValueNode'; export * from './PublicKeyValueNode'; export * from './SetValueNode'; diff --git a/src/visitors/getDebugStringVisitor.ts b/src/visitors/getDebugStringVisitor.ts index 6b891f006..11bb71846 100644 --- a/src/visitors/getDebugStringVisitor.ts +++ b/src/visitors/getDebugStringVisitor.ts @@ -1,5 +1,5 @@ -import { pipe } from '../shared'; import { Node } from '../nodes'; +import { pipe } from '../shared'; import { interceptVisitor } from './interceptVisitor'; import { mergeVisitor } from './mergeVisitor'; import { Visitor } from './visitor'; @@ -59,6 +59,17 @@ function getNodeDetails(node: Node): string[] { ...(node.isSigner === 'either' ? ['optionalSigner'] : []), ...(node.isOptional ? ['optional'] : []), ]; + case 'instructionRemainingAccountsNode': + return [ + ...(node.isWritable ? ['writable'] : []), + ...(node.isSigner === true ? ['signer'] : []), + ...(node.isSigner === 'either' ? ['optionalSigner'] : []), + ]; + case 'instructionByteDeltaNode': + return [ + ...(node.subtract ? ['subtract'] : []), + ...(node.withHeader ? ['withHeader'] : []), + ]; case 'errorNode': return [node.code.toString(), node.name]; case 'programLinkNode': @@ -70,13 +81,45 @@ function getNodeDetails(node: Node): string[] { ...(node.importFrom ? [`from:${node.importFrom}`] : []), ]; case 'numberTypeNode': - return [node.format, ...(node.endian === 'be' ? ['be'] : [])]; + return [node.format, ...(node.endian === 'be' ? ['bigEndian'] : [])]; case 'amountTypeNode': return [node.decimals.toString(), ...(node.unit ? [node.unit] : [])]; case 'stringTypeNode': return [node.encoding]; + case 'optionTypeNode': + return node.fixed ? ['fixed'] : []; case 'fixedSizeNode': return [node.size.toString()]; + case 'numberValueNode': + return [node.number.toString()]; + case 'stringValueNode': + return [node.string]; + case 'booleanValueNode': + return [node.boolean ? 'true' : 'false']; + case 'publicKeyValueNode': + return [node.publicKey]; + case 'enumValueNode': + return [node.variant]; + case 'resolverValueNode': + return [ + node.name, + ...(node.importFrom ? [`from:${node.importFrom}`] : []), + ]; + case 'byteDiscriminatorNode': + return [ + ...(node.bytes.length > 0 + ? [ + `0x${node.bytes + .map((byte) => byte.toString(16).padStart(2, '0')) + .join('')}`, + ] + : []), + ...(node.offset > 0 ? [`offset:${node.offset}`] : []), + ]; + case 'fieldDiscriminatorNode': + return [node.name, ...(node.offset > 0 ? [`offset:${node.offset}`] : [])]; + case 'sizeDiscriminatorNode': + return [node.size.toString()]; default: return 'name' in node ? [node.name] : []; } diff --git a/src/visitors/identityVisitor.ts b/src/visitors/identityVisitor.ts index be95698b2..fbf5a5940 100644 --- a/src/visitors/identityVisitor.ts +++ b/src/visitors/identityVisitor.ts @@ -21,6 +21,7 @@ import { constantPdaSeedNode, dateTimeTypeNode, definedTypeNode, + enumEmptyVariantTypeNode, enumStructVariantTypeNode, enumTupleVariantTypeNode, enumTypeNode, @@ -261,8 +262,13 @@ export function identityVisitor( node ) { const newStruct = visit(this)(node.struct); - if (!newStruct) return null; + if (!newStruct) { + return enumEmptyVariantTypeNode(node.name); + } assertIsNode(newStruct, 'structTypeNode'); + if (newStruct.fields.length === 0) { + return enumEmptyVariantTypeNode(node.name); + } return enumStructVariantTypeNode(node.name, newStruct); }; } @@ -272,8 +278,13 @@ export function identityVisitor( node ) { const newTuple = visit(this)(node.tuple); - if (!newTuple) return null; + if (!newTuple) { + return enumEmptyVariantTypeNode(node.name); + } assertIsNode(newTuple, 'tupleTypeNode'); + if (newTuple.items.length === 0) { + return enumEmptyVariantTypeNode(node.name); + } return enumTupleVariantTypeNode(node.name, newTuple); }; } @@ -328,11 +339,10 @@ export function identityVisitor( if (castedNodeKeys.includes('structTypeNode')) { visitor.visitStructType = function visitStructType(node) { - return structTypeNode( - node.fields - .map(visit(this)) - .filter(removeNullAndAssertIsNodeFilter('structFieldTypeNode')) - ); + const fields = node.fields + .map(visit(this)) + .filter(removeNullAndAssertIsNodeFilter('structFieldTypeNode')); + return structTypeNode(fields); }; } @@ -351,11 +361,10 @@ export function identityVisitor( if (castedNodeKeys.includes('tupleTypeNode')) { visitor.visitTupleType = function visitTupleType(node) { - return tupleTypeNode( - node.items - .map(visit(this)) - .filter(removeNullAndAssertIsNodeFilter(TYPE_NODES)) - ); + const items = node.items + .map(visit(this)) + .filter(removeNullAndAssertIsNodeFilter(TYPE_NODES)); + return tupleTypeNode(items); }; } @@ -566,6 +575,7 @@ export function identityVisitor( ? visit(this)(node.ifFalse) ?? undefined : undefined; if (ifFalse) assertIsNode(ifFalse, CONDITIONAL_VALUE_BRANCH_NODES); + if (!ifTrue && !ifFalse) return null; return conditionalValueNode({ condition, value, ifTrue, ifFalse }); }; } diff --git a/src/visitors/mergeVisitor.ts b/src/visitors/mergeVisitor.ts index d46830ed0..810e386e8 100644 --- a/src/visitors/mergeVisitor.ts +++ b/src/visitors/mergeVisitor.ts @@ -86,6 +86,14 @@ export function mergeVisitor( }; } + if (castedNodeKeys.includes('instructionByteDeltaNode')) { + visitor.visitInstructionByteDelta = function visitInstructionByteDelta( + node + ) { + return merge(node, visit(this)(node.value)); + }; + } + if (castedNodeKeys.includes('definedTypeNode')) { visitor.visitDefinedType = function visitDefinedType(node) { return merge(node, visit(this)(node.type)); diff --git a/test/visitors/nodes/AccountNode.test.ts b/test/visitors/nodes/AccountNode.test.ts new file mode 100644 index 000000000..8f3980e3a --- /dev/null +++ b/test/visitors/nodes/AccountNode.test.ts @@ -0,0 +1,51 @@ +import test from 'ava'; +import { + accountNode, + numberTypeNode, + pdaLinkNode, + publicKeyTypeNode, + sizeDiscriminatorNode, + structFieldTypeNode, + structTypeNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = accountNode({ + name: 'token', + data: structTypeNode([ + structFieldTypeNode({ name: 'mint', type: publicKeyTypeNode() }), + structFieldTypeNode({ name: 'owner', type: publicKeyTypeNode() }), + structFieldTypeNode({ name: 'amount', type: numberTypeNode('u64') }), + ]), + pda: pdaLinkNode('associatedToken'), + discriminators: [sizeDiscriminatorNode(72)], + size: 72, +}); + +test(mergeVisitorMacro, node, 10); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[accountNode]', null); +test(deleteNodesVisitorMacro, node, '[pdaLinkNode]', { + ...node, + pda: undefined, +}); +test( + getDebugStringVisitorMacro, + node, + ` +accountNode [token] +| structTypeNode +| | structFieldTypeNode [mint] +| | | publicKeyTypeNode +| | structFieldTypeNode [owner] +| | | publicKeyTypeNode +| | structFieldTypeNode [amount] +| | | numberTypeNode [u64] +| pdaLinkNode [associatedToken] +| sizeDiscriminatorNode [72]` +); diff --git a/test/visitors/nodes/DefinedTypeNode.test.ts b/test/visitors/nodes/DefinedTypeNode.test.ts new file mode 100644 index 000000000..b07c7617c --- /dev/null +++ b/test/visitors/nodes/DefinedTypeNode.test.ts @@ -0,0 +1,43 @@ +import test from 'ava'; +import { + definedTypeNode, + fixedSizeNode, + numberTypeNode, + stringTypeNode, + structFieldTypeNode, + structTypeNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = definedTypeNode({ + name: 'person', + type: structTypeNode([ + structFieldTypeNode({ + name: 'name', + type: stringTypeNode({ size: fixedSizeNode(32) }), + }), + structFieldTypeNode({ name: 'age', type: numberTypeNode('u64') }), + ]), +}); + +test(mergeVisitorMacro, node, 7); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[definedTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[structTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +definedTypeNode [person] +| structTypeNode +| | structFieldTypeNode [name] +| | | stringTypeNode [utf8] +| | | | fixedSizeNode [32] +| | structFieldTypeNode [age] +| | | numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/ErrorNode.test.ts b/test/visitors/nodes/ErrorNode.test.ts new file mode 100644 index 000000000..def87a2b2 --- /dev/null +++ b/test/visitors/nodes/ErrorNode.test.ts @@ -0,0 +1,20 @@ +import test from 'ava'; +import { errorNode } from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = errorNode({ + name: 'InvalidTokenOwner', + code: 42, + message: + 'The provided account does not match the owner of the token account.', +}); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[errorNode]', null); +test(getDebugStringVisitorMacro, node, `errorNode [42.invalidTokenOwner]`); diff --git a/test/visitors/nodes/InstructionAccountNode.test.ts b/test/visitors/nodes/InstructionAccountNode.test.ts new file mode 100644 index 000000000..dbcb1ef84 --- /dev/null +++ b/test/visitors/nodes/InstructionAccountNode.test.ts @@ -0,0 +1,31 @@ +import test from 'ava'; +import { accountValueNode, instructionAccountNode } from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = instructionAccountNode({ + name: 'owner', + isWritable: true, + isSigner: 'either', + isOptional: false, + defaultValue: accountValueNode('authority'), +}); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[instructionAccountNode]', null); +test(deleteNodesVisitorMacro, node, '[accountValueNode]', { + ...node, + defaultValue: undefined, +}); +test( + getDebugStringVisitorMacro, + node, + ` +instructionAccountNode [owner.writable.optionalSigner] +| accountValueNode [authority]` +); diff --git a/test/visitors/nodes/InstructionArgumentNode.test.ts b/test/visitors/nodes/InstructionArgumentNode.test.ts new file mode 100644 index 000000000..ef03ff1c0 --- /dev/null +++ b/test/visitors/nodes/InstructionArgumentNode.test.ts @@ -0,0 +1,35 @@ +import test from 'ava'; +import { + instructionArgumentNode, + numberTypeNode, + numberValueNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = instructionArgumentNode({ + name: 'amount', + type: numberTypeNode('u64'), + defaultValue: numberValueNode(1), +}); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[instructionArgumentNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberValueNode]', { + ...node, + defaultValue: undefined, +}); +test( + getDebugStringVisitorMacro, + node, + ` +instructionArgumentNode [amount] +| numberTypeNode [u64] +| numberValueNode [1]` +); diff --git a/test/visitors/nodes/InstructionByteDeltaNode.test.ts b/test/visitors/nodes/InstructionByteDeltaNode.test.ts new file mode 100644 index 000000000..9c43ebc10 --- /dev/null +++ b/test/visitors/nodes/InstructionByteDeltaNode.test.ts @@ -0,0 +1,24 @@ +import test from 'ava'; +import { instructionByteDeltaNode, numberValueNode } from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = instructionByteDeltaNode(numberValueNode(42), { + subtract: true, +}); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[instructionByteDeltaNode]', null); +test(deleteNodesVisitorMacro, node, '[numberValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +instructionByteDeltaNode [subtract.withHeader] +| numberValueNode [42]` +); diff --git a/test/visitors/nodes/InstructionNode.test.ts b/test/visitors/nodes/InstructionNode.test.ts new file mode 100644 index 000000000..d4e5a6635 --- /dev/null +++ b/test/visitors/nodes/InstructionNode.test.ts @@ -0,0 +1,144 @@ +import test from 'ava'; +import { + fieldDiscriminatorNode, + instructionAccountNode, + instructionArgumentNode, + instructionByteDeltaNode, + instructionNode, + instructionRemainingAccountsNode, + numberTypeNode, + numberValueNode, + publicKeyTypeNode, + resolverValueNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = instructionNode({ + name: 'transferSol', + accounts: [ + instructionAccountNode({ + name: 'source', + isWritable: true, + isSigner: true, + }), + instructionAccountNode({ + name: 'destination', + isWritable: true, + isSigner: false, + }), + ], + arguments: [ + instructionArgumentNode({ + name: 'discriminator', + type: numberTypeNode('u32'), + }), + instructionArgumentNode({ + name: 'amount', + type: numberTypeNode('u64'), + }), + ], + discriminators: [fieldDiscriminatorNode('discriminator')], +}); + +test(mergeVisitorMacro, node, 8); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[instructionNode]', null); +test(deleteNodesVisitorMacro, node, '[instructionAccountNode]', { + ...node, + accounts: [], +}); +test(deleteNodesVisitorMacro, node, '[instructionArgumentNode]', { + ...node, + arguments: [], +}); +test(deleteNodesVisitorMacro, node, '[fieldDiscriminatorNode]', { + ...node, + discriminators: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +instructionNode [transferSol] +| instructionAccountNode [source.writable.signer] +| instructionAccountNode [destination.writable] +| instructionArgumentNode [discriminator] +| | numberTypeNode [u32] +| instructionArgumentNode [amount] +| | numberTypeNode [u64] +| fieldDiscriminatorNode [discriminator]` +); + +// Extra arguments. +const nodeWithExtraArguments = instructionNode({ + name: 'myInstruction', + extraArguments: [ + instructionArgumentNode({ + name: 'myExtraArgument', + type: publicKeyTypeNode(), + }), + ], +}); +test( + 'mergeVisitor: extraArguments', + mergeVisitorMacro, + nodeWithExtraArguments, + 3 +); +test( + 'identityVisitor: extraArguments', + identityVisitorMacro, + nodeWithExtraArguments +); + +// Remaining accounts. +const nodeWithRemainingAccounts = instructionNode({ + name: 'myInstruction', + remainingAccounts: [ + instructionRemainingAccountsNode(resolverValueNode('myResolver')), + ], +}); +test( + 'mergeVisitor: remainingAccounts', + mergeVisitorMacro, + nodeWithRemainingAccounts, + 3 +); +test( + 'identityVisitor: remainingAccounts', + identityVisitorMacro, + nodeWithRemainingAccounts +); + +// Byte deltas. +const nodeWithByteDeltas = instructionNode({ + name: 'myInstruction', + byteDeltas: [instructionByteDeltaNode(numberValueNode(42))], +}); +test('mergeVisitor: byteDeltas', mergeVisitorMacro, nodeWithByteDeltas, 3); +test('identityVisitor: byteDeltas', identityVisitorMacro, nodeWithByteDeltas); + +// Sub-instructions. +const nodeWithSubInstructions = instructionNode({ + name: 'myInstruction', + subInstructions: [ + instructionNode({ name: 'mySubInstruction1' }), + instructionNode({ name: 'mySubInstruction2' }), + ], +}); +test( + 'mergeVisitor: subInstructions', + mergeVisitorMacro, + nodeWithSubInstructions, + 3 +); +test( + 'identityVisitor: subInstructions', + identityVisitorMacro, + nodeWithSubInstructions +); diff --git a/test/visitors/nodes/InstructionRemainingAccountsNode.test.ts b/test/visitors/nodes/InstructionRemainingAccountsNode.test.ts new file mode 100644 index 000000000..6ab3b0697 --- /dev/null +++ b/test/visitors/nodes/InstructionRemainingAccountsNode.test.ts @@ -0,0 +1,28 @@ +import test from 'ava'; +import { + argumentValueNode, + instructionRemainingAccountsNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = instructionRemainingAccountsNode( + argumentValueNode('remainingAccounts'), + { isWritable: true, isSigner: 'either' } +); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[instructionRemainingAccountsNode]', null); +test(deleteNodesVisitorMacro, node, '[argumentValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +instructionRemainingAccountsNode [writable.optionalSigner] +| argumentValueNode [remainingAccounts]` +); diff --git a/test/visitors/nodes/PdaNode.test.ts b/test/visitors/nodes/PdaNode.test.ts new file mode 100644 index 000000000..bd98be02a --- /dev/null +++ b/test/visitors/nodes/PdaNode.test.ts @@ -0,0 +1,44 @@ +import test from 'ava'; +import { + pdaNode, + programIdPdaSeedNode, + publicKeyTypeNode, + variablePdaSeedNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = pdaNode('associatedToken', [ + variablePdaSeedNode('owner', publicKeyTypeNode()), + programIdPdaSeedNode(), + variablePdaSeedNode('mint', publicKeyTypeNode()), +]); + +test(mergeVisitorMacro, node, 6); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[pdaNode]', null); +test( + deleteNodesVisitorMacro, + node, + ['[variablePdaSeedNode]', '[programIdPdaSeedNode]'], + { ...node, seeds: [] } +); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', { + ...node, + seeds: [programIdPdaSeedNode()], +}); +test( + getDebugStringVisitorMacro, + node, + ` +pdaNode [associatedToken] +| variablePdaSeedNode [owner] +| | publicKeyTypeNode +| programIdPdaSeedNode +| variablePdaSeedNode [mint] +| | publicKeyTypeNode` +); diff --git a/test/visitors/nodes/ProgramNode.test.ts b/test/visitors/nodes/ProgramNode.test.ts new file mode 100644 index 000000000..07bf59587 --- /dev/null +++ b/test/visitors/nodes/ProgramNode.test.ts @@ -0,0 +1,72 @@ +import test from 'ava'; +import { + accountNode, + definedTypeNode, + enumTypeNode, + errorNode, + instructionNode, + pdaNode, + programNode, + structTypeNode, +} from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = programNode({ + name: 'splToken', + publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', + version: '1.2.3', + pdas: [pdaNode('associatedToken', [])], + accounts: [ + accountNode({ name: 'mint', data: structTypeNode([]) }), + accountNode({ name: 'token', data: structTypeNode([]) }), + ], + instructions: [ + instructionNode({ name: 'mintTokens' }), + instructionNode({ name: 'transferTokens' }), + ], + definedTypes: [ + definedTypeNode({ name: 'tokenState', type: enumTypeNode([]) }), + ], + errors: [ + errorNode({ name: 'invalidMint', code: 1, message: 'Invalid mint' }), + errorNode({ name: 'invalidToken', code: 2, message: 'Invalid token' }), + ], +}); + +test(mergeVisitorMacro, node, 13); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[programNode]', null); +test(deleteNodesVisitorMacro, node, '[pdaNode]', { ...node, pdas: [] }); +test(deleteNodesVisitorMacro, node, '[accountNode]', { ...node, accounts: [] }); +test(deleteNodesVisitorMacro, node, '[instructionNode]', { + ...node, + instructions: [], +}); +test(deleteNodesVisitorMacro, node, '[definedTypeNode]', { + ...node, + definedTypes: [], +}); +test(deleteNodesVisitorMacro, node, '[errorNode]', { ...node, errors: [] }); +test( + getDebugStringVisitorMacro, + node, + ` +programNode [splToken.TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA] +| pdaNode [associatedToken] +| accountNode [mint] +| | structTypeNode +| accountNode [token] +| | structTypeNode +| instructionNode [mintTokens] +| instructionNode [transferTokens] +| definedTypeNode [tokenState] +| | enumTypeNode +| | | numberTypeNode [u8] +| errorNode [1.invalidMint] +| errorNode [2.invalidToken]` +); diff --git a/test/visitors/nodes/RootNode.test.ts b/test/visitors/nodes/RootNode.test.ts new file mode 100644 index 000000000..8b9816173 --- /dev/null +++ b/test/visitors/nodes/RootNode.test.ts @@ -0,0 +1,32 @@ +import test from 'ava'; +import { programNode, rootNode } from '../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from './_setup'; + +const node = rootNode([ + programNode({ + name: 'splToken', + publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', + }), + programNode({ + name: 'splAddressLookupTable', + publicKey: 'AddressLookupTab1e1111111111111111111111111', + }), +]); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[rootNode]', null); +test(deleteNodesVisitorMacro, node, '[programNode]', { ...node, programs: [] }); +test( + getDebugStringVisitorMacro, + node, + ` +rootNode +| programNode [splToken.TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA] +| programNode [splAddressLookupTable.AddressLookupTab1e1111111111111111111111111]` +); diff --git a/test/visitors/nodes/_setup.ts b/test/visitors/nodes/_setup.ts new file mode 100644 index 000000000..8be9643be --- /dev/null +++ b/test/visitors/nodes/_setup.ts @@ -0,0 +1,60 @@ +import test from 'ava'; +import { + Node, + NodeSelector, + deleteNodesVisitor, + getDebugStringVisitor, + identityVisitor, + mergeVisitor, + visit, +} from '../../../src'; + +export const mergeVisitorMacro = test.macro({ + title: (title) => title ?? 'mergeVisitor', + exec(t, node: Node, expectedNodeCount: number) { + const visitor = mergeVisitor( + () => 1, + (_, values) => values.reduce((a, b) => a + b, 1) + ); + const result = visit(node, visitor); + t.is(result, expectedNodeCount); + }, +}); + +export const identityVisitorMacro = test.macro({ + title: (title) => title ?? 'identityVisitor', + exec(t, node: Node) { + const visitor = identityVisitor(); + const result = visit(node, visitor); + t.deepEqual(result, node); + t.not(result, node); + }, +}); + +export const deleteNodesVisitorMacro = test.macro({ + title(title, _node, selector: NodeSelector | NodeSelector[]) { + const selectors = Array.isArray(selector) ? selector : [selector]; + return title ?? `deleteNodesVisitor: ${selectors.join(', ')}`; + }, + exec( + t, + node: Node, + selector: NodeSelector | NodeSelector[], + expectedResult: Node | null + ) { + const selectors = Array.isArray(selector) ? selector : [selector]; + const visitor = deleteNodesVisitor(selectors); + const result = visit(node, visitor); + t.deepEqual(result, expectedResult); + t.not(result, node); + }, +}); + +export const getDebugStringVisitorMacro = test.macro({ + title: (title) => title ?? 'getDebugStringVisitor', + exec(t, node: Node, expectedIndentedString: string) { + const visitor = getDebugStringVisitor({ indent: true }); + const result = visit(node, visitor); + t.is(result, expectedIndentedString.trim()); + }, +}); diff --git a/test/visitors/nodes/contextualValueNodes/AccountBumpValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/AccountBumpValueNode.test.ts new file mode 100644 index 000000000..3dc9ec920 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/AccountBumpValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { accountBumpValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = accountBumpValueNode('metadata'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[accountBumpValueNode]', null); +test(getDebugStringVisitorMacro, node, `accountBumpValueNode [metadata]`); diff --git a/test/visitors/nodes/contextualValueNodes/AccountValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/AccountValueNode.test.ts new file mode 100644 index 000000000..e050bde8b --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/AccountValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { accountValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = accountValueNode('mint'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[accountValueNode]', null); +test(getDebugStringVisitorMacro, node, `accountValueNode [mint]`); diff --git a/test/visitors/nodes/contextualValueNodes/ArgumentValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/ArgumentValueNode.test.ts new file mode 100644 index 000000000..612e3eae8 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/ArgumentValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { argumentValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = argumentValueNode('space'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[argumentValueNode]', null); +test(getDebugStringVisitorMacro, node, `argumentValueNode [space]`); diff --git a/test/visitors/nodes/contextualValueNodes/ConditionalValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/ConditionalValueNode.test.ts new file mode 100644 index 000000000..5c1e02685 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/ConditionalValueNode.test.ts @@ -0,0 +1,54 @@ +import test from 'ava'; +import { + accountValueNode, + argumentValueNode, + conditionalValueNode, + enumValueNode, + programIdValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = conditionalValueNode({ + condition: argumentValueNode('tokenStandard'), + value: enumValueNode('tokenStandard', 'ProgrammableNonFungible'), + ifTrue: accountValueNode('mint'), + ifFalse: programIdValueNode(), +}); + +test(mergeVisitorMacro, node, 6); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[conditionalValueNode]', null); +test(deleteNodesVisitorMacro, node, '[enumValueNode]', { + ...node, + value: undefined, +}); +test(deleteNodesVisitorMacro, node, '[accountValueNode]', { + ...node, + ifTrue: undefined, +}); +test(deleteNodesVisitorMacro, node, '[programIdValueNode]', { + ...node, + ifFalse: undefined, +}); +test( + deleteNodesVisitorMacro, + node, + ['[accountValueNode]', '[programIdValueNode]'], + null +); +test( + getDebugStringVisitorMacro, + node, + ` +conditionalValueNode +| argumentValueNode [tokenStandard] +| enumValueNode [programmableNonFungible] +| | definedTypeLinkNode [tokenStandard] +| accountValueNode [mint] +| programIdValueNode` +); diff --git a/test/visitors/nodes/contextualValueNodes/IdentityValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/IdentityValueNode.test.ts new file mode 100644 index 000000000..e5c442289 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/IdentityValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { identityValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = identityValueNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[identityValueNode]', null); +test(getDebugStringVisitorMacro, node, `identityValueNode`); diff --git a/test/visitors/nodes/contextualValueNodes/PayerValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/PayerValueNode.test.ts new file mode 100644 index 000000000..9e61fde2c --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/PayerValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { payerValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = payerValueNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[payerValueNode]', null); +test(getDebugStringVisitorMacro, node, `payerValueNode`); diff --git a/test/visitors/nodes/contextualValueNodes/PdaSeedValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/PdaSeedValueNode.test.ts new file mode 100644 index 000000000..52cbd33ec --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/PdaSeedValueNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { accountValueNode, pdaSeedValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = pdaSeedValueNode('mint', accountValueNode('mint')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[pdaSeedValueNode]', null); +test(deleteNodesVisitorMacro, node, '[accountValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +pdaSeedValueNode [mint] +| accountValueNode [mint]` +); diff --git a/test/visitors/nodes/contextualValueNodes/PdaValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/PdaValueNode.test.ts new file mode 100644 index 000000000..7bc2804f7 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/PdaValueNode.test.ts @@ -0,0 +1,42 @@ +import test from 'ava'; +import { + accountValueNode, + pdaLinkNode, + pdaSeedValueNode, + pdaValueNode, + publicKeyValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = pdaValueNode(pdaLinkNode('associatedToken'), [ + pdaSeedValueNode('mint', accountValueNode('mint')), + pdaSeedValueNode( + 'owner', + publicKeyValueNode('8sphVBHQxufE4Jc1HMuWwWdKgoDjncQyPHwxYhfATRtF') + ), +]); + +test(mergeVisitorMacro, node, 6); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[pdaValueNode]', null); +test(deleteNodesVisitorMacro, node, '[pdaLinkNode]', null); +test(deleteNodesVisitorMacro, node, '[pdaSeedValueNode]', { + ...node, + seeds: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +pdaValueNode +| pdaLinkNode [associatedToken] +| pdaSeedValueNode [mint] +| | accountValueNode [mint] +| pdaSeedValueNode [owner] +| | publicKeyValueNode [8sphVBHQxufE4Jc1HMuWwWdKgoDjncQyPHwxYhfATRtF]` +); diff --git a/test/visitors/nodes/contextualValueNodes/ProgramIdValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/ProgramIdValueNode.test.ts new file mode 100644 index 000000000..3d32ce1e0 --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/ProgramIdValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { programIdValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = programIdValueNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[programIdValueNode]', null); +test(getDebugStringVisitorMacro, node, `programIdValueNode`); diff --git a/test/visitors/nodes/contextualValueNodes/ResolverValueNode.test.ts b/test/visitors/nodes/contextualValueNodes/ResolverValueNode.test.ts new file mode 100644 index 000000000..8f7f4b9ca --- /dev/null +++ b/test/visitors/nodes/contextualValueNodes/ResolverValueNode.test.ts @@ -0,0 +1,35 @@ +import test from 'ava'; +import { + accountValueNode, + argumentValueNode, + resolverValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = resolverValueNode('myCustomResolver', { + importFrom: 'hooked', + dependsOn: [accountValueNode('mint'), argumentValueNode('tokenStandard')], +}); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[resolverValueNode]', null); +test( + deleteNodesVisitorMacro, + node, + ['[accountValueNode]', '[argumentValueNode]'], + { ...node, dependsOn: undefined } +); +test( + getDebugStringVisitorMacro, + node, + ` +resolverValueNode [myCustomResolver.from:hooked] +| accountValueNode [mint] +| argumentValueNode [tokenStandard]` +); diff --git a/test/visitors/nodes/discriminatorNodes/ByteDiscriminatorNode.test.ts b/test/visitors/nodes/discriminatorNodes/ByteDiscriminatorNode.test.ts new file mode 100644 index 000000000..00d5f67f5 --- /dev/null +++ b/test/visitors/nodes/discriminatorNodes/ByteDiscriminatorNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { byteDiscriminatorNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = byteDiscriminatorNode([1, 2, 3, 4], 42); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[byteDiscriminatorNode]', null); +test( + getDebugStringVisitorMacro, + node, + `byteDiscriminatorNode [0x01020304.offset:42]` +); diff --git a/test/visitors/nodes/discriminatorNodes/FieldDiscriminatorNode.test.ts b/test/visitors/nodes/discriminatorNodes/FieldDiscriminatorNode.test.ts new file mode 100644 index 000000000..711405795 --- /dev/null +++ b/test/visitors/nodes/discriminatorNodes/FieldDiscriminatorNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { fieldDiscriminatorNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = fieldDiscriminatorNode('discriminator', 42); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[fieldDiscriminatorNode]', null); +test( + getDebugStringVisitorMacro, + node, + `fieldDiscriminatorNode [discriminator.offset:42]` +); diff --git a/test/visitors/nodes/discriminatorNodes/SizeDiscriminatorNode.test.ts b/test/visitors/nodes/discriminatorNodes/SizeDiscriminatorNode.test.ts new file mode 100644 index 000000000..1cde7ec23 --- /dev/null +++ b/test/visitors/nodes/discriminatorNodes/SizeDiscriminatorNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { sizeDiscriminatorNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = sizeDiscriminatorNode(42); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[sizeDiscriminatorNode]', null); +test(getDebugStringVisitorMacro, node, `sizeDiscriminatorNode [42]`); diff --git a/test/visitors/nodes/linkNodes/AccountLinkNode.test.ts b/test/visitors/nodes/linkNodes/AccountLinkNode.test.ts new file mode 100644 index 000000000..a711b13c2 --- /dev/null +++ b/test/visitors/nodes/linkNodes/AccountLinkNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { accountLinkNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = accountLinkNode('token', 'splToken'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[accountLinkNode]', null); +test(getDebugStringVisitorMacro, node, `accountLinkNode [token.from:splToken]`); diff --git a/test/visitors/nodes/linkNodes/DefinedTypeLinkNode.test.ts b/test/visitors/nodes/linkNodes/DefinedTypeLinkNode.test.ts new file mode 100644 index 000000000..934ece88c --- /dev/null +++ b/test/visitors/nodes/linkNodes/DefinedTypeLinkNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { definedTypeLinkNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = definedTypeLinkNode('tokenState', 'splToken'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[definedTypeLinkNode]', null); +test( + getDebugStringVisitorMacro, + node, + `definedTypeLinkNode [tokenState.from:splToken]` +); diff --git a/test/visitors/nodes/linkNodes/PdaLinkNode.test.ts b/test/visitors/nodes/linkNodes/PdaLinkNode.test.ts new file mode 100644 index 000000000..8aaf32d31 --- /dev/null +++ b/test/visitors/nodes/linkNodes/PdaLinkNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { pdaLinkNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = pdaLinkNode('associatedToken', 'splToken'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[pdaLinkNode]', null); +test( + getDebugStringVisitorMacro, + node, + `pdaLinkNode [associatedToken.from:splToken]` +); diff --git a/test/visitors/nodes/linkNodes/ProgramLinkNode.test.ts b/test/visitors/nodes/linkNodes/ProgramLinkNode.test.ts new file mode 100644 index 000000000..183ab7dd4 --- /dev/null +++ b/test/visitors/nodes/linkNodes/ProgramLinkNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { programLinkNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = programLinkNode('mplCandyGuard', 'mplCandyMachine'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[programLinkNode]', null); +test( + getDebugStringVisitorMacro, + node, + `programLinkNode [mplCandyGuard.from:mplCandyMachine]` +); diff --git a/test/visitors/nodes/pdaSeedNodes/ConstantPdaSeedNode.test.ts b/test/visitors/nodes/pdaSeedNodes/ConstantPdaSeedNode.test.ts new file mode 100644 index 000000000..35dc49a3e --- /dev/null +++ b/test/visitors/nodes/pdaSeedNodes/ConstantPdaSeedNode.test.ts @@ -0,0 +1,28 @@ +import test from 'ava'; +import { + constantPdaSeedNode, + numberTypeNode, + numberValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = constantPdaSeedNode(numberTypeNode('u8'), numberValueNode(42)); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[constantPdaSeedNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +constantPdaSeedNode +| numberTypeNode [u8] +| numberValueNode [42]` +); diff --git a/test/visitors/nodes/pdaSeedNodes/ProgramIdPdaSeedNode.test.ts b/test/visitors/nodes/pdaSeedNodes/ProgramIdPdaSeedNode.test.ts new file mode 100644 index 000000000..63ab597a2 --- /dev/null +++ b/test/visitors/nodes/pdaSeedNodes/ProgramIdPdaSeedNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { programIdPdaSeedNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = programIdPdaSeedNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[programIdPdaSeedNode]', null); +test(getDebugStringVisitorMacro, node, `programIdPdaSeedNode`); diff --git a/test/visitors/nodes/pdaSeedNodes/VariablePdaSeedNode.test.ts b/test/visitors/nodes/pdaSeedNodes/VariablePdaSeedNode.test.ts new file mode 100644 index 000000000..4019d9932 --- /dev/null +++ b/test/visitors/nodes/pdaSeedNodes/VariablePdaSeedNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { publicKeyTypeNode, variablePdaSeedNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = variablePdaSeedNode('mint', publicKeyTypeNode()); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[variablePdaSeedNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +variablePdaSeedNode [mint] +| publicKeyTypeNode` +); diff --git a/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts b/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts new file mode 100644 index 000000000..7b58c3cde --- /dev/null +++ b/test/visitors/nodes/sizeNodes/FixedSizeNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { fixedSizeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = fixedSizeNode(42); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[fixedSizeNode]', null); +test(getDebugStringVisitorMacro, node, `fixedSizeNode [42]`); diff --git a/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts b/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts new file mode 100644 index 000000000..367602a36 --- /dev/null +++ b/test/visitors/nodes/sizeNodes/PrefixedSizeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { numberTypeNode, prefixedSizeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = prefixedSizeNode(numberTypeNode('u64')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +prefixedSizeNode +| numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts b/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts new file mode 100644 index 000000000..9007d3a3b --- /dev/null +++ b/test/visitors/nodes/sizeNodes/RemainderSizeNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { remainderSizeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = remainderSizeNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[remainderSizeNode]', null); +test(getDebugStringVisitorMacro, node, `remainderSizeNode`); diff --git a/test/visitors/nodes/typeNodes/AmountTypeNode.test.ts b/test/visitors/nodes/typeNodes/AmountTypeNode.test.ts new file mode 100644 index 000000000..339c0b403 --- /dev/null +++ b/test/visitors/nodes/typeNodes/AmountTypeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { amountTypeNode, numberTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = amountTypeNode(numberTypeNode('u64'), 9, 'SOL'); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[amountTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +amountTypeNode [9.SOL] +| numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts b/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts new file mode 100644 index 000000000..4d1f84ecf --- /dev/null +++ b/test/visitors/nodes/typeNodes/ArrayTypeNode.test.ts @@ -0,0 +1,34 @@ +import test from 'ava'; +import { + arrayTypeNode, + numberTypeNode, + prefixedSizeNode, + publicKeyTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = arrayTypeNode( + publicKeyTypeNode(), + prefixedSizeNode(numberTypeNode('u64')) +); + +test(mergeVisitorMacro, node, 4); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[arrayTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +arrayTypeNode +| prefixedSizeNode +| | numberTypeNode [u64] +| publicKeyTypeNode` +); diff --git a/test/visitors/nodes/typeNodes/BooleanTypeNode.test.ts b/test/visitors/nodes/typeNodes/BooleanTypeNode.test.ts new file mode 100644 index 000000000..3e571340f --- /dev/null +++ b/test/visitors/nodes/typeNodes/BooleanTypeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { booleanTypeNode, numberTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = booleanTypeNode(numberTypeNode('u32')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[booleanTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +booleanTypeNode +| numberTypeNode [u32]` +); diff --git a/test/visitors/nodes/typeNodes/BytesTypeNode.test.ts b/test/visitors/nodes/typeNodes/BytesTypeNode.test.ts new file mode 100644 index 000000000..17b280797 --- /dev/null +++ b/test/visitors/nodes/typeNodes/BytesTypeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { bytesTypeNode, remainderSizeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = bytesTypeNode(remainderSizeNode()); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[bytesTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[remainderSizeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +bytesTypeNode +| remainderSizeNode` +); diff --git a/test/visitors/nodes/typeNodes/DateTimeTypeNode.test.ts b/test/visitors/nodes/typeNodes/DateTimeTypeNode.test.ts new file mode 100644 index 000000000..88dd27b9c --- /dev/null +++ b/test/visitors/nodes/typeNodes/DateTimeTypeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { dateTimeTypeNode, numberTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = dateTimeTypeNode(numberTypeNode('u64')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[dateTimeTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +dateTimeTypeNode +| numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/typeNodes/EnumEmptyVariantTypeNode.test.ts b/test/visitors/nodes/typeNodes/EnumEmptyVariantTypeNode.test.ts new file mode 100644 index 000000000..afe6cc976 --- /dev/null +++ b/test/visitors/nodes/typeNodes/EnumEmptyVariantTypeNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { enumEmptyVariantTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = enumEmptyVariantTypeNode('initialized'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[enumEmptyVariantTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + `enumEmptyVariantTypeNode [initialized]` +); diff --git a/test/visitors/nodes/typeNodes/EnumStructVariantTypeNode.test.ts b/test/visitors/nodes/typeNodes/EnumStructVariantTypeNode.test.ts new file mode 100644 index 000000000..fcd415c4a --- /dev/null +++ b/test/visitors/nodes/typeNodes/EnumStructVariantTypeNode.test.ts @@ -0,0 +1,49 @@ +import test from 'ava'; +import { + enumEmptyVariantTypeNode, + enumStructVariantTypeNode, + numberTypeNode, + structFieldTypeNode, + structTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = enumStructVariantTypeNode( + 'mouseClick', + structTypeNode([ + structFieldTypeNode({ name: 'x', type: numberTypeNode('u32') }), + structFieldTypeNode({ name: 'y', type: numberTypeNode('u32') }), + ]) +); + +test(mergeVisitorMacro, node, 6); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[enumStructVariantTypeNode]', null); +test( + deleteNodesVisitorMacro, + node, + '[structTypeNode]', + enumEmptyVariantTypeNode('mouseClick') +); +test( + deleteNodesVisitorMacro, + node, + '[structFieldTypeNode]', + enumEmptyVariantTypeNode('mouseClick') +); +test( + getDebugStringVisitorMacro, + node, + ` +enumStructVariantTypeNode [mouseClick] +| structTypeNode +| | structFieldTypeNode [x] +| | | numberTypeNode [u32] +| | structFieldTypeNode [y] +| | | numberTypeNode [u32]` +); diff --git a/test/visitors/nodes/typeNodes/EnumTupleVariantTypeNode.test.ts b/test/visitors/nodes/typeNodes/EnumTupleVariantTypeNode.test.ts new file mode 100644 index 000000000..897e2de46 --- /dev/null +++ b/test/visitors/nodes/typeNodes/EnumTupleVariantTypeNode.test.ts @@ -0,0 +1,43 @@ +import test from 'ava'; +import { + enumEmptyVariantTypeNode, + enumTupleVariantTypeNode, + numberTypeNode, + tupleTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = enumTupleVariantTypeNode( + 'coordinates', + tupleTypeNode([numberTypeNode('u32'), numberTypeNode('u32')]) +); + +test(mergeVisitorMacro, node, 4); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[enumTupleVariantTypeNode]', null); +test( + deleteNodesVisitorMacro, + node, + '[tupleTypeNode]', + enumEmptyVariantTypeNode('coordinates') +); +test( + deleteNodesVisitorMacro, + node, + '[numberTypeNode]', + enumEmptyVariantTypeNode('coordinates') +); +test( + getDebugStringVisitorMacro, + node, + ` +enumTupleVariantTypeNode [coordinates] +| tupleTypeNode +| | numberTypeNode [u32] +| | numberTypeNode [u32]` +); diff --git a/test/visitors/nodes/typeNodes/EnumTypeNode.test.ts b/test/visitors/nodes/typeNodes/EnumTypeNode.test.ts new file mode 100644 index 000000000..669dea73b --- /dev/null +++ b/test/visitors/nodes/typeNodes/EnumTypeNode.test.ts @@ -0,0 +1,82 @@ +import test from 'ava'; +import { + enumEmptyVariantTypeNode, + enumStructVariantTypeNode, + enumTupleVariantTypeNode, + enumTypeNode, + fixedSizeNode, + numberTypeNode, + stringTypeNode, + structFieldTypeNode, + structTypeNode, + tupleTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = enumTypeNode( + [ + enumEmptyVariantTypeNode('quit'), + enumTupleVariantTypeNode( + 'write', + tupleTypeNode([stringTypeNode({ size: fixedSizeNode(32) })]) + ), + enumStructVariantTypeNode( + 'move', + structTypeNode([ + structFieldTypeNode({ name: 'x', type: numberTypeNode('u32') }), + structFieldTypeNode({ name: 'y', type: numberTypeNode('u32') }), + ]) + ), + ], + { size: numberTypeNode('u64') } +); + +test(mergeVisitorMacro, node, 13); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[enumTypeNode]', null); +test( + deleteNodesVisitorMacro, + node, + [ + '[enumEmptyVariantTypeNode]', + '[enumTupleVariantTypeNode]', + '[enumStructVariantTypeNode]', + ], + { ...node, variants: [] } +); +test( + deleteNodesVisitorMacro, + node, + ['[tupleTypeNode]', '[structFieldTypeNode]'], + { + ...node, + variants: [ + enumEmptyVariantTypeNode('quit'), + enumEmptyVariantTypeNode('write'), + enumEmptyVariantTypeNode('move'), + ], + } +); +test( + getDebugStringVisitorMacro, + node, + ` +enumTypeNode +| numberTypeNode [u64] +| enumEmptyVariantTypeNode [quit] +| enumTupleVariantTypeNode [write] +| | tupleTypeNode +| | | stringTypeNode [utf8] +| | | | fixedSizeNode [32] +| enumStructVariantTypeNode [move] +| | structTypeNode +| | | structFieldTypeNode [x] +| | | | numberTypeNode [u32] +| | | structFieldTypeNode [y] +| | | | numberTypeNode [u32]` +); diff --git a/test/visitors/nodes/typeNodes/MapTypeNode.test.ts b/test/visitors/nodes/typeNodes/MapTypeNode.test.ts new file mode 100644 index 000000000..7b2ec635f --- /dev/null +++ b/test/visitors/nodes/typeNodes/MapTypeNode.test.ts @@ -0,0 +1,39 @@ +import test from 'ava'; +import { + fixedSizeNode, + mapTypeNode, + numberTypeNode, + prefixedSizeNode, + publicKeyTypeNode, + stringTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = mapTypeNode( + stringTypeNode({ size: fixedSizeNode(32) }), + publicKeyTypeNode(), + prefixedSizeNode(numberTypeNode('u8')) +); + +test(mergeVisitorMacro, node, 6); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[mapTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` + mapTypeNode +| prefixedSizeNode +| | numberTypeNode [u8] +| stringTypeNode [utf8] +| | fixedSizeNode [32] +| publicKeyTypeNode` +); diff --git a/test/visitors/nodes/typeNodes/NumberTypeNode.test.ts b/test/visitors/nodes/typeNodes/NumberTypeNode.test.ts new file mode 100644 index 000000000..03cd995fc --- /dev/null +++ b/test/visitors/nodes/typeNodes/NumberTypeNode.test.ts @@ -0,0 +1,21 @@ +import test from 'ava'; +import { numberTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = numberTypeNode('f64'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test(getDebugStringVisitorMacro, node, `numberTypeNode [f64]`); +test( + 'getDebugStringVisitor: bigEndian', + getDebugStringVisitorMacro, + numberTypeNode('f64', 'be'), + `numberTypeNode [f64.bigEndian]` +); diff --git a/test/visitors/nodes/typeNodes/OptionTypeNode.test.ts b/test/visitors/nodes/typeNodes/OptionTypeNode.test.ts new file mode 100644 index 000000000..7c3a6f276 --- /dev/null +++ b/test/visitors/nodes/typeNodes/OptionTypeNode.test.ts @@ -0,0 +1,31 @@ +import test from 'ava'; +import { + numberTypeNode, + optionTypeNode, + publicKeyTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = optionTypeNode(publicKeyTypeNode(), { + prefix: numberTypeNode('u64'), + fixed: true, +}); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[optionTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +optionTypeNode [fixed] +| numberTypeNode [u64] +| publicKeyTypeNode` +); diff --git a/test/visitors/nodes/typeNodes/PublicKeyTypeNode.test.ts b/test/visitors/nodes/typeNodes/PublicKeyTypeNode.test.ts new file mode 100644 index 000000000..a0af4828c --- /dev/null +++ b/test/visitors/nodes/typeNodes/PublicKeyTypeNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { publicKeyTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = publicKeyTypeNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(getDebugStringVisitorMacro, node, `publicKeyTypeNode`); diff --git a/test/visitors/nodes/typeNodes/SetTypeNode.test.ts b/test/visitors/nodes/typeNodes/SetTypeNode.test.ts new file mode 100644 index 000000000..c811ce6e2 --- /dev/null +++ b/test/visitors/nodes/typeNodes/SetTypeNode.test.ts @@ -0,0 +1,28 @@ +import test from 'ava'; +import { + publicKeyTypeNode, + remainderSizeNode, + setTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = setTypeNode(publicKeyTypeNode(), remainderSizeNode()); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[setTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[remainderSizeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +setTypeNode +| remainderSizeNode +| publicKeyTypeNode` +); diff --git a/test/visitors/nodes/typeNodes/SolAmountTypeNode.test.ts b/test/visitors/nodes/typeNodes/SolAmountTypeNode.test.ts new file mode 100644 index 000000000..db8444ead --- /dev/null +++ b/test/visitors/nodes/typeNodes/SolAmountTypeNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { numberTypeNode, solAmountTypeNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = solAmountTypeNode(numberTypeNode('u64')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[solAmountTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +solAmountTypeNode +| numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/typeNodes/StringTypeNode.test.ts b/test/visitors/nodes/typeNodes/StringTypeNode.test.ts new file mode 100644 index 000000000..f7032bab3 --- /dev/null +++ b/test/visitors/nodes/typeNodes/StringTypeNode.test.ts @@ -0,0 +1,41 @@ +import test from 'ava'; +import { + numberTypeNode, + prefixedSizeNode, + stringTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = stringTypeNode({ + size: prefixedSizeNode(numberTypeNode('u32')), +}); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[prefixedSizeNode]', null); +test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +stringTypeNode [utf8] +| prefixedSizeNode +| | numberTypeNode [u32]` +); + +// Different encoding. +test( + 'getDebugStringVisitor: different encoding', + getDebugStringVisitorMacro, + stringTypeNode({ encoding: 'base58' }), + ` +stringTypeNode [base58] +| prefixedSizeNode +| | numberTypeNode [u32]` +); diff --git a/test/visitors/nodes/typeNodes/StructFieldTypeNode.test.ts b/test/visitors/nodes/typeNodes/StructFieldTypeNode.test.ts new file mode 100644 index 000000000..160d05f63 --- /dev/null +++ b/test/visitors/nodes/typeNodes/StructFieldTypeNode.test.ts @@ -0,0 +1,37 @@ +import test from 'ava'; +import { + publicKeyTypeNode, + publicKeyValueNode, + structFieldTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = structFieldTypeNode({ + name: 'owner', + type: publicKeyTypeNode(), + defaultValue: publicKeyValueNode( + 'CzC5HidG6kR5J4haV7pKZmenYYVS7rw3SoBkqeStxZ9U' + ), +}); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[structFieldTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', { + ...node, + defaultValue: undefined, +}); +test( + getDebugStringVisitorMacro, + node, + ` +structFieldTypeNode [owner] +| publicKeyTypeNode +| publicKeyValueNode [CzC5HidG6kR5J4haV7pKZmenYYVS7rw3SoBkqeStxZ9U]` +); diff --git a/test/visitors/nodes/typeNodes/StructTypeNode.test.ts b/test/visitors/nodes/typeNodes/StructTypeNode.test.ts new file mode 100644 index 000000000..865716205 --- /dev/null +++ b/test/visitors/nodes/typeNodes/StructTypeNode.test.ts @@ -0,0 +1,42 @@ +import test from 'ava'; +import { + numberTypeNode, + publicKeyTypeNode, + structFieldTypeNode, + structTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = structTypeNode([ + structFieldTypeNode({ name: 'owner', type: publicKeyTypeNode() }), + structFieldTypeNode({ name: 'amount', type: numberTypeNode('u64') }), +]); + +test(mergeVisitorMacro, node, 5); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[structTypeNode]', null); +test(deleteNodesVisitorMacro, node, '[structFieldTypeNode]', { + ...node, + fields: [], +}); +test( + deleteNodesVisitorMacro, + node, + ['[publicKeyTypeNode]', '[numberTypeNode]'], + { ...node, fields: [] } +); +test( + getDebugStringVisitorMacro, + node, + ` +structTypeNode +| structFieldTypeNode [owner] +| | publicKeyTypeNode +| structFieldTypeNode [amount] +| | numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/typeNodes/TupleTypeNode.test.ts b/test/visitors/nodes/typeNodes/TupleTypeNode.test.ts new file mode 100644 index 000000000..4191b4991 --- /dev/null +++ b/test/visitors/nodes/typeNodes/TupleTypeNode.test.ts @@ -0,0 +1,32 @@ +import test from 'ava'; +import { + numberTypeNode, + publicKeyTypeNode, + tupleTypeNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = tupleTypeNode([publicKeyTypeNode(), numberTypeNode('u64')]); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[tupleTypeNode]', null); +test( + deleteNodesVisitorMacro, + node, + ['[publicKeyTypeNode]', '[numberTypeNode]'], + { ...node, items: [] } +); +test( + getDebugStringVisitorMacro, + node, + ` +tupleTypeNode +| publicKeyTypeNode +| numberTypeNode [u64]` +); diff --git a/test/visitors/nodes/valueNodes/ArrayValueNode.test.ts b/test/visitors/nodes/valueNodes/ArrayValueNode.test.ts new file mode 100644 index 000000000..91a2835cb --- /dev/null +++ b/test/visitors/nodes/valueNodes/ArrayValueNode.test.ts @@ -0,0 +1,31 @@ +import test from 'ava'; +import { arrayValueNode, publicKeyValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = arrayValueNode([ + publicKeyValueNode('8jeCDm1zPb68kfzv5MaUo9BK7beGzFrGHA9hhMGL1ay1'), + publicKeyValueNode('8PEBfDem8yb5aUkrhKgNKMraB9gR8WtJDprEW3QwLFz'), + publicKeyValueNode('97hChLjFswwqBrE82Q4wqctpFfB2jZ91svxCv68iCrqG'), +]); + +test(mergeVisitorMacro, node, 4); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[arrayValueNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', { + ...node, + items: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +arrayValueNode +| publicKeyValueNode [8jeCDm1zPb68kfzv5MaUo9BK7beGzFrGHA9hhMGL1ay1] +| publicKeyValueNode [8PEBfDem8yb5aUkrhKgNKMraB9gR8WtJDprEW3QwLFz] +| publicKeyValueNode [97hChLjFswwqBrE82Q4wqctpFfB2jZ91svxCv68iCrqG]` +); diff --git a/test/visitors/nodes/valueNodes/BooleanValueNode.test.ts b/test/visitors/nodes/valueNodes/BooleanValueNode.test.ts new file mode 100644 index 000000000..697c8bf48 --- /dev/null +++ b/test/visitors/nodes/valueNodes/BooleanValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { booleanValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = booleanValueNode(true); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[booleanValueNode]', null); +test(getDebugStringVisitorMacro, node, `booleanValueNode [true]`); diff --git a/test/visitors/nodes/valueNodes/EnumValueNode.test.ts b/test/visitors/nodes/valueNodes/EnumValueNode.test.ts new file mode 100644 index 000000000..88d795636 --- /dev/null +++ b/test/visitors/nodes/valueNodes/EnumValueNode.test.ts @@ -0,0 +1,45 @@ +import test from 'ava'; +import { + definedTypeLinkNode, + enumValueNode, + numberValueNode, + stringValueNode, + structFieldValueNode, + structValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = enumValueNode( + definedTypeLinkNode('entity'), + 'person', + structValueNode([ + structFieldValueNode('name', stringValueNode('Alice')), + structFieldValueNode('age', numberValueNode(42)), + ]) +); + +test(mergeVisitorMacro, node, 7); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[enumValueNode]', null); +test(deleteNodesVisitorMacro, node, '[definedTypeLinkNode]', null); +test(deleteNodesVisitorMacro, node, '[structValueNode]', { + ...node, + value: undefined, +}); +test( + getDebugStringVisitorMacro, + node, + ` +enumValueNode [person] +| definedTypeLinkNode [entity] +| structValueNode +| | structFieldValueNode [name] +| | | stringValueNode [Alice] +| | structFieldValueNode [age] +| | | numberValueNode [42]` +); diff --git a/test/visitors/nodes/valueNodes/MapEntryValueNode.test.ts b/test/visitors/nodes/valueNodes/MapEntryValueNode.test.ts new file mode 100644 index 000000000..459fa4df1 --- /dev/null +++ b/test/visitors/nodes/valueNodes/MapEntryValueNode.test.ts @@ -0,0 +1,31 @@ +import test from 'ava'; +import { + mapEntryValueNode, + publicKeyValueNode, + stringValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = mapEntryValueNode( + stringValueNode('Alice'), + publicKeyValueNode('GaxDPeNfXXgtwJXwHiDYVSDiM53RchFSRFTn2z2Jztuw') +); + +test(mergeVisitorMacro, node, 3); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[mapEntryValueNode]', null); +test(deleteNodesVisitorMacro, node, '[stringValueNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +mapEntryValueNode +| stringValueNode [Alice] +| publicKeyValueNode [GaxDPeNfXXgtwJXwHiDYVSDiM53RchFSRFTn2z2Jztuw]` +); diff --git a/test/visitors/nodes/valueNodes/MapValueNode.test.ts b/test/visitors/nodes/valueNodes/MapValueNode.test.ts new file mode 100644 index 000000000..0344a7f58 --- /dev/null +++ b/test/visitors/nodes/valueNodes/MapValueNode.test.ts @@ -0,0 +1,42 @@ +import test from 'ava'; +import { + mapEntryValueNode, + mapValueNode, + numberValueNode, + stringValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = mapValueNode([ + mapEntryValueNode(stringValueNode('Alice'), numberValueNode(42)), + mapEntryValueNode(stringValueNode('Bob'), numberValueNode(37)), + mapEntryValueNode(stringValueNode('Carla'), numberValueNode(29)), +]); + +test(mergeVisitorMacro, node, 10); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[mapValueNode]', null); +test(deleteNodesVisitorMacro, node, '[mapEntryValueNode]', { + ...node, + entries: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +mapValueNode +| mapEntryValueNode +| | stringValueNode [Alice] +| | numberValueNode [42] +| mapEntryValueNode +| | stringValueNode [Bob] +| | numberValueNode [37] +| mapEntryValueNode +| | stringValueNode [Carla] +| | numberValueNode [29]` +); diff --git a/test/visitors/nodes/valueNodes/NoneValueNode.test.ts b/test/visitors/nodes/valueNodes/NoneValueNode.test.ts new file mode 100644 index 000000000..a93465ddd --- /dev/null +++ b/test/visitors/nodes/valueNodes/NoneValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { noneValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = noneValueNode(); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[noneValueNode]', null); +test(getDebugStringVisitorMacro, node, `noneValueNode`); diff --git a/test/visitors/nodes/valueNodes/NumberValueNode.test.ts b/test/visitors/nodes/valueNodes/NumberValueNode.test.ts new file mode 100644 index 000000000..21b61d45c --- /dev/null +++ b/test/visitors/nodes/valueNodes/NumberValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { numberValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = numberValueNode(42); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[numberValueNode]', null); +test(getDebugStringVisitorMacro, node, `numberValueNode [42]`); diff --git a/test/visitors/nodes/valueNodes/PublicKeyValueNode.test.ts b/test/visitors/nodes/valueNodes/PublicKeyValueNode.test.ts new file mode 100644 index 000000000..b4e521b2c --- /dev/null +++ b/test/visitors/nodes/valueNodes/PublicKeyValueNode.test.ts @@ -0,0 +1,19 @@ +import test from 'ava'; +import { publicKeyValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = publicKeyValueNode('HqJgWgvkn5wMGU8LpzkRw8389N5Suvu2nZcmpya9JyJB'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + `publicKeyValueNode [HqJgWgvkn5wMGU8LpzkRw8389N5Suvu2nZcmpya9JyJB]` +); diff --git a/test/visitors/nodes/valueNodes/SetValueNode.test.ts b/test/visitors/nodes/valueNodes/SetValueNode.test.ts new file mode 100644 index 000000000..13984bec9 --- /dev/null +++ b/test/visitors/nodes/valueNodes/SetValueNode.test.ts @@ -0,0 +1,31 @@ +import test from 'ava'; +import { publicKeyValueNode, setValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = setValueNode([ + publicKeyValueNode('8jeCDm1zPb68kfzv5MaUo9BK7beGzFrGHA9hhMGL1ay1'), + publicKeyValueNode('8PEBfDem8yb5aUkrhKgNKMraB9gR8WtJDprEW3QwLFz'), + publicKeyValueNode('97hChLjFswwqBrE82Q4wqctpFfB2jZ91svxCv68iCrqG'), +]); + +test(mergeVisitorMacro, node, 4); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[setValueNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', { + ...node, + items: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +setValueNode +| publicKeyValueNode [8jeCDm1zPb68kfzv5MaUo9BK7beGzFrGHA9hhMGL1ay1] +| publicKeyValueNode [8PEBfDem8yb5aUkrhKgNKMraB9gR8WtJDprEW3QwLFz] +| publicKeyValueNode [97hChLjFswwqBrE82Q4wqctpFfB2jZ91svxCv68iCrqG]` +); diff --git a/test/visitors/nodes/valueNodes/SomeValueNode.test.ts b/test/visitors/nodes/valueNodes/SomeValueNode.test.ts new file mode 100644 index 000000000..cc6ad1896 --- /dev/null +++ b/test/visitors/nodes/valueNodes/SomeValueNode.test.ts @@ -0,0 +1,24 @@ +import test from 'ava'; +import { publicKeyValueNode, someValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = someValueNode( + publicKeyValueNode('73na6yX22Xw3w7q3z39MAwtZyQehEMnUQceszCt94GJ3') +); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[someValueNode]', null); +test(deleteNodesVisitorMacro, node, '[publicKeyValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +someValueNode +| publicKeyValueNode [73na6yX22Xw3w7q3z39MAwtZyQehEMnUQceszCt94GJ3]` +); diff --git a/test/visitors/nodes/valueNodes/StringValueNode.test.ts b/test/visitors/nodes/valueNodes/StringValueNode.test.ts new file mode 100644 index 000000000..f086f6604 --- /dev/null +++ b/test/visitors/nodes/valueNodes/StringValueNode.test.ts @@ -0,0 +1,15 @@ +import test from 'ava'; +import { stringValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = stringValueNode('Hello world!'); + +test(mergeVisitorMacro, node, 1); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[stringValueNode]', null); +test(getDebugStringVisitorMacro, node, `stringValueNode [Hello world!]`); diff --git a/test/visitors/nodes/valueNodes/StructFieldValueNode.test.ts b/test/visitors/nodes/valueNodes/StructFieldValueNode.test.ts new file mode 100644 index 000000000..abba195d8 --- /dev/null +++ b/test/visitors/nodes/valueNodes/StructFieldValueNode.test.ts @@ -0,0 +1,22 @@ +import test from 'ava'; +import { stringValueNode, structFieldValueNode } from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = structFieldValueNode('name', stringValueNode('Alice')); + +test(mergeVisitorMacro, node, 2); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[structFieldValueNode]', null); +test(deleteNodesVisitorMacro, node, '[stringValueNode]', null); +test( + getDebugStringVisitorMacro, + node, + ` +structFieldValueNode [name] +| stringValueNode [Alice]` +); diff --git a/test/visitors/nodes/valueNodes/StructValueNode.test.ts b/test/visitors/nodes/valueNodes/StructValueNode.test.ts new file mode 100644 index 000000000..24ad83f27 --- /dev/null +++ b/test/visitors/nodes/valueNodes/StructValueNode.test.ts @@ -0,0 +1,36 @@ +import test from 'ava'; +import { + numberValueNode, + stringValueNode, + structFieldValueNode, + structValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = structValueNode([ + structFieldValueNode('name', stringValueNode('Alice')), + structFieldValueNode('age', numberValueNode(42)), +]); + +test(mergeVisitorMacro, node, 5); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[structValueNode]', null); +test(deleteNodesVisitorMacro, node, '[structFieldValueNode]', { + ...node, + fields: [], +}); +test( + getDebugStringVisitorMacro, + node, + ` +structValueNode +| structFieldValueNode [name] +| | stringValueNode [Alice] +| structFieldValueNode [age] +| | numberValueNode [42]` +); diff --git a/test/visitors/nodes/valueNodes/TupleValueNode.test.ts b/test/visitors/nodes/valueNodes/TupleValueNode.test.ts new file mode 100644 index 000000000..c29ebe967 --- /dev/null +++ b/test/visitors/nodes/valueNodes/TupleValueNode.test.ts @@ -0,0 +1,38 @@ +import test from 'ava'; +import { + numberValueNode, + publicKeyValueNode, + stringValueNode, + tupleValueNode, +} from '../../../../src'; +import { + deleteNodesVisitorMacro, + getDebugStringVisitorMacro, + identityVisitorMacro, + mergeVisitorMacro, +} from '../_setup'; + +const node = tupleValueNode([ + stringValueNode('Hello'), + numberValueNode(42), + publicKeyValueNode('9sL9D2kshFgZSHz98pUQxGphwVUbCNBGqhYGaWWNJags'), +]); + +test(mergeVisitorMacro, node, 4); +test(identityVisitorMacro, node); +test(deleteNodesVisitorMacro, node, '[tupleValueNode]', null); +test( + deleteNodesVisitorMacro, + node, + ['[stringValueNode]', '[numberValueNode]', '[publicKeyValueNode]'], + { ...node, items: [] } +); +test( + getDebugStringVisitorMacro, + node, + ` +tupleValueNode +| stringValueNode [Hello] +| numberValueNode [42] +| publicKeyValueNode [9sL9D2kshFgZSHz98pUQxGphwVUbCNBGqhYGaWWNJags]` +);