From ab28af7c53a8acc9b0e5aa3cdb6af6bc21ab89b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Wed, 23 Nov 2022 14:45:25 +0200 Subject: [PATCH] Add unit tests. Simplify interface. --- src/smartcontracts/argSerializer.ts | 2 +- src/smartcontracts/resultsParser.spec.ts | 56 ++++++++++++++++++++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/smartcontracts/argSerializer.ts b/src/smartcontracts/argSerializer.ts index a3b35a75..98b382e5 100644 --- a/src/smartcontracts/argSerializer.ts +++ b/src/smartcontracts/argSerializer.ts @@ -11,7 +11,7 @@ interface IArgSerializerOptions { } interface ICodec { - decodeTopLevel(buffer: Buffer, type: Type): TResult; + decodeTopLevel(buffer: Buffer, type: Type): TypedValue; encodeTopLevel(typedValue: TypedValue): Buffer; } diff --git a/src/smartcontracts/resultsParser.spec.ts b/src/smartcontracts/resultsParser.spec.ts index a862a123..19700286 100644 --- a/src/smartcontracts/resultsParser.spec.ts +++ b/src/smartcontracts/resultsParser.spec.ts @@ -1,14 +1,15 @@ +import { ContractQueryResponse, ContractResultItem, ContractResults, TransactionEvent, TransactionEventTopic, TransactionLogs, TransactionOnNetwork } from "@elrondnetwork/erdjs-network-providers"; +import { assert } from "chai"; import * as fs from "fs"; import path from "path"; -import { assert } from "chai"; -import { BigUIntType, BigUIntValue, EndpointDefinition, EndpointModifiers, EndpointParameterDefinition } from "./typesystem"; -import { BytesType, BytesValue } from "./typesystem/bytes"; -import { ReturnCode } from "./returnCode"; -import { ResultsParser } from "./resultsParser"; -import { Logger, LogLevel } from "../logger"; -import { ITransactionOnNetwork } from "../interfaceOfNetwork"; -import { ContractQueryResponse, ContractResultItem, ContractResults, TransactionEvent, TransactionEventTopic, TransactionLogs, TransactionOnNetwork } from "@elrondnetwork/erdjs-network-providers"; import { Address } from "../address"; +import { ITransactionOnNetwork } from "../interfaceOfNetwork"; +import { Logger, LogLevel } from "../logger"; +import { ArgSerializer } from "./argSerializer"; +import { ResultsParser } from "./resultsParser"; +import { ReturnCode } from "./returnCode"; +import { BigUIntType, BigUIntValue, EndpointDefinition, EndpointModifiers, EndpointParameterDefinition, TypedValue, U64Type, U64Value } from "./typesystem"; +import { BytesType, BytesValue } from "./typesystem/bytes"; const KnownReturnCodes: string[] = [ ReturnCode.None.valueOf(), @@ -32,6 +33,45 @@ const KnownReturnCodes: string[] = [ describe("test smart contract results parser", () => { let parser = new ResultsParser(); + it("should create parser with custom dependencies (1)", async () => { + const customParser = new ResultsParser({ + argsSerializer: { + buffersToValues(_buffers, _parameters) { + return [new U64Value(42)]; + }, + stringToBuffers(_joinedString) { + return [] + } + } + }); + + const endpoint = new EndpointDefinition("", [], [], new EndpointModifiers("", [])); + const queryResponse = new ContractQueryResponse({}); + const bundle = customParser.parseQueryResponse(queryResponse, endpoint); + assert.deepEqual(bundle.firstValue, new U64Value(42)); + }); + + it("should create parser with custom dependencies (2)", async () => { + const customParser = new ResultsParser({ + argsSerializer: new ArgSerializer({ + codec: { + decodeTopLevel(_buffer, _type): TypedValue { + return new U64Value(42); + }, + encodeTopLevel(_typedValue): Buffer { + return Buffer.from([]) + }, + } + }) + }); + + const outputParameters = [new EndpointParameterDefinition("", "", new U64Type())]; + const endpoint = new EndpointDefinition("", [], outputParameters, new EndpointModifiers("", [])); + const queryResponse = new ContractQueryResponse({ returnData: [""] }); + const bundle = customParser.parseQueryResponse(queryResponse, endpoint); + assert.deepEqual(bundle.firstValue, new U64Value(42)); + }); + it("should parse query response", async () => { let endpointModifiers = new EndpointModifiers("", []); let outputParameters = [