From fd7ad016f455d6f7c72d4e4523563100858a57bd Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 5 Jul 2024 11:11:16 -0600 Subject: [PATCH 1/6] update testing framework --- package.json | 2 +- test/index.ts | 139 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 113 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 20b22bd859..3ce4876c48 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "crypto-browserify": "^3.12.0", + "deep-is": "^0.1.4", "esbuild": "^0.19.3", "esbuild-plugin-tsc": "^0.4.0", "ethers": "^6.11.1", @@ -59,7 +60,6 @@ "@typescript-eslint/eslint-plugin": "^6.13.0", "@typescript-eslint/parser": "^6.13.0", "deep-equal": "^2.2.3", - "deep-is": "^0.1.4", "eslint": "^8.54.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-simple-import-sort": "^10.0.0", diff --git a/test/index.ts b/test/index.ts index a35dbf1761..113260bfe2 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,26 +1,35 @@ import { execSync } from 'child_process'; +// TODO import deepEqual from 'deep-is' works for some +// TODO import { deepEqual } from 'deep-is' works for others +// TODO require seems to work for all of them +// eslint-disable-next-line @typescript-eslint/no-var-requires +const deepEqual = require('deep-is'); -export type Test = { +import { jsonStringify } from '../src/lib/json'; + +export type Test = { name: string; skip?: boolean; wait?: number; - prep?: () => Promise; - test?: () => Promise>; + prep?: (context: Context) => Promise; + test?: (context: Context) => Promise>; }; // export type Variant = Partial; -export type AzleResult = Partial<{ - Ok: T; +export type AzleResult = Partial<{ + Ok: { isSuccessful: boolean; message?: string; context?: Context }; Err: E; }>; -export type Ok = { - Ok: T; +export type Ok = { + Ok: { isSuccessful: boolean; message?: string; context?: Context }; }; // TODO let's get rid of this function in all tests and use match instead -export function ok(azle_result: AzleResult): azle_result is Ok { +export function ok( + azle_result: AzleResult +): azle_result is Ok { if (azle_result.Err === undefined) { return true; } else { @@ -34,45 +43,46 @@ export async function runTests( tests: Test[], exitProcess: boolean = true ): Promise { + let context = undefined; for (const test of tests) { try { if (test.skip === true) { - console.log(`Skipping: ${test.name}`); + console.info(`Skipping: ${test.name}`); continue; } - console.log(); + console.info(); if (test.prep !== undefined || test.wait !== undefined) { - console.log(`\n${test.name}\n`); + console.info(`\n${test.name}\n`); } else { - console.log(`\nRunning test: ${test.name}\n`); + console.info(`\nRunning test: ${test.name}\n`); } if (test.wait !== undefined) { - console.log(`waiting ${test.wait} milliseconds`); + console.info(`waiting ${test.wait} milliseconds`); await new Promise((resolve) => setTimeout(resolve, test.wait)); - console.log('done waiting'); + console.info('done waiting'); continue; } if (test.prep !== undefined) { - await test.prep(); + context = (await test.prep(context)) ?? context; continue; } - const result = + const result: AzleResult = test.test !== undefined - ? await test.test() + ? await test.test(context) : { Err: 'test is not defined' }; if (!ok(result)) { - console.log('\x1b[31m', `test: ${test.name} failed`); - console.log('\x1b[31m', `${result.Err}`); - console.log('\x1b[0m'); + console.info('\x1b[31m', `test: ${test.name} failed`); + console.info('\x1b[31m', `${result.Err}`); + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -81,9 +91,16 @@ export async function runTests( } } - if (result.Ok !== true) { - console.log('\x1b[31m', `test: ${test.name} failed`); - console.log('\x1b[0m'); + if (result.Ok.context !== undefined) { + context = result.Ok.context; + } + + if (result.Ok.isSuccessful !== true) { + console.info('\x1b[31m', `test: ${test.name} failed`); + if (result.Ok.message !== undefined) { + console.info('\x1b[31m', `${result.Ok.message}`); + } + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -92,15 +109,15 @@ export async function runTests( } } - console.log('\x1b[32m', `test: ${test.name} passed`); - console.log('\x1b[0m'); + console.info('\x1b[32m', `test: ${test.name} passed`); + console.info('\x1b[0m'); } catch (error) { - console.log( + console.info( '\x1b[31m', `test ${test.name} failed`, (error as any).toString() ); - console.log('\x1b[0m'); + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -155,6 +172,74 @@ export function deploy(canisterName: string, argument?: string): Test[] { ]; } +type EqualsOptions = { + failMessage?: string; + equals?: (actual: any, expected: any) => boolean; + toString?: (value: any) => string; + context?: Context; +}; + +// TODO is is better test framework conformity to call this assertEqual? I'll hold off for now, it should be easy to search for all testEquality and change it, easier than assertEqual I think +// TODO so based on this I think I've actually seen this in other testing frameworks, assertEquals will take two and make sure they are equals, and assert will take one boolean. Right now we have test instead of assert but it would be easy to change +export function testEquality( + actual: T, + expected: T, + options?: EqualsOptions +): AzleResult { + const equals = options?.equals ?? deepEqual; + const valueToString = options?.toString ?? jsonStringify; + + if (equals(actual, expected)) { + return succeed(options?.context); + } else { + const message = + options?.failMessage ?? + `Expected: ${valueToString(expected)}, Received: ${valueToString( + actual + )}`; + return fail(message); + } +} + +export function succeed(context?: Context): AzleResult { + return { Ok: { isSuccessful: true, context } }; +} + +export function fail(message?: string): AzleResult { + return { Ok: { isSuccessful: false, message } }; +} + +export function error(message: string): AzleResult { + return { Err: message }; +} + +// TODO when Jordan asks why we have this show call_raw, it's a great example for why we should have this guy +// But this is quickly boiling down to sugar and we are looking at the difference between +// return test( +// result.Ok.includes('blob'), +// `Expected result to be a candid blob. Received ${result.Ok}` +// ); +// and +// return { +// Ok: { +// isSuccessful: result.Ok.includes('blob'), +// message: `Expected result to be a candid blob. Received ${result.Ok}` +// } +// }; +// TODO date has a good example of when we would want to have an error message I think +// TODO ethers_base also +export function test( + succeeds: boolean, + message?: string, + context?: Context +): AzleResult { + if (succeeds) { + return succeed(context); + } else { + return fail(message); + } +} + export function createSnakeCaseProxy( target: T, async: boolean = true From d02063f593c75056d3c7374c1d8a1e6da1657c81 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Fri, 5 Jul 2024 11:14:01 -0600 Subject: [PATCH 2/6] fix imports --- src/lib/server.ts | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/lib/server.ts b/src/lib/server.ts index e7df029783..2960ede3ff 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -6,29 +6,25 @@ import { IncomingMessageForServer } from 'http'; // @ts-ignore import httpMessageParser from 'http-message-parser'; -import { - blob, - bool, - CandidType, - Canister, - Func, - ic, - init, - Manual, - nat16, - None, - Opt, - postUpgrade, - query, - Record, - Some, - text, - Tuple, - update, - Variant, - Vec -} from '.'; +import { CandidType } from './candid/candid_type'; +import { Manual } from './candid/manual'; +import { blob } from './candid/types/constructed/blob'; +import { None, Opt, Some } from './candid/types/constructed/opt'; +import { Record } from './candid/types/constructed/record'; +import { Tuple } from './candid/types/constructed/tuple'; +import { Variant } from './candid/types/constructed/variant'; +import { Vec } from './candid/types/constructed/vec'; +import { bool } from './candid/types/primitive/bool'; +import { nat16 } from './candid/types/primitive/nats/nat16'; +import { text } from './candid/types/primitive/text'; +import { Func } from './candid/types/reference/func'; +import { Canister } from './candid/types/reference/service/'; import { CanisterOptions } from './candid/types/reference/service/canister_function'; +import { init } from './canister_methods/methods/init'; +import { postUpgrade } from './canister_methods/methods/post_upgrade'; +import { query } from './canister_methods/methods/query'; +import { update } from './canister_methods/methods/update'; +import { ic } from './ic'; export type HeaderField = [text, text]; export const HeaderField = Tuple(text, text); From 86663b772ac3a84cfa74b8c93d71732953f5081c Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 27 May 2024 16:12:23 -0600 Subject: [PATCH 3/6] update prop tests with imporved testing framework --- .../tests/blob/test/generate_tests.ts | 8 +++---- .../tests/bool/test/generate_tests.ts | 8 +++---- .../http_request/test/generate_tests.ts | 9 ++++---- .../test/generate_tests.ts | 17 ++++++--------- .../init/test/generate_tests.ts | 12 +++-------- .../inspect_message/test/generate_tests.ts | 20 ++++++++++-------- .../post_upgrade/test/generate_init_tests.ts | 21 ++++++------------- .../test/generate_post_upgrade_tests.ts | 21 ++++++------------- .../canister_methods/pre_upgrade/test/test.ts | 7 ++++--- .../query/test/generate_tests.ts | 12 +++-------- .../update/test/generate_tests.ts | 11 +++------- .../tests/float32/test/generate_tests.ts | 8 +++---- .../tests/float64/test/generate_tests.ts | 8 +++---- .../tests/func/test/generate_tests.ts | 14 ++++++------- .../tests/int/test/generate_tests.ts | 8 +++---- .../tests/int16/test/generate_tests.ts | 8 +++---- .../tests/int32/test/generate_tests.ts | 8 +++---- .../tests/int64/test/generate_tests.ts | 8 +++---- .../tests/int8/test/generate_tests.ts | 8 +++---- .../tests/nat/test/generate_tests.ts | 8 +++---- .../tests/nat16/test/generate_tests.ts | 8 +++---- .../tests/nat32/test/generate_tests.ts | 8 +++---- .../tests/nat64/test/generate_tests.ts | 8 +++---- .../tests/nat8/test/generate_tests.ts | 8 +++---- .../tests/null/test/generate_tests.ts | 8 +++---- .../tests/opt/test/generate_tests.ts | 8 +++---- .../tests/principal/test/generate_tests.ts | 6 +++--- .../tests/record/test/generate_tests.ts | 14 ++++++------- .../tests/recursive/test/generate_tests.ts | 14 ++++++------- .../tests/service/test/generate_tests.ts | 11 +++++----- .../tests/text/test/generate_tests.ts | 8 +++---- .../tests/tuple/test/generate_tests.ts | 6 +++--- .../tests/variant/test/generate_tests.ts | 8 +++---- .../tests/vec/test/generate_tests.ts | 8 +++---- 34 files changed, 132 insertions(+), 215 deletions(-) diff --git a/property_tests/tests/blob/test/generate_tests.ts b/property_tests/tests/blob/test/generate_tests.ts index 807b248a0f..68d64967c1 100644 --- a/property_tests/tests/blob/test/generate_tests.ts +++ b/property_tests/tests/blob/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -29,9 +29,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/bool/test/generate_tests.ts b/property_tests/tests/bool/test/generate_tests.ts index 5e433736d1..3c94860698 100644 --- a/property_tests/tests/bool/test/generate_tests.ts +++ b/property_tests/tests/bool/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/http_request/test/generate_tests.ts b/property_tests/tests/canister_methods/http_request/test/generate_tests.ts index 3a25432547..2caeaa058d 100644 --- a/property_tests/tests/canister_methods/http_request/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/http_request/test/generate_tests.ts @@ -1,8 +1,8 @@ import { HttpRequest, HttpResponse } from 'azle/experimental'; -import { deepEqual, Named } from 'azle/property_tests'; +import { Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { HttpResponseAgentResponseValue } from 'azle/property_tests/arbitraries/http/response_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { fletch } from './fletch'; @@ -42,12 +42,11 @@ export function generateTests( ...expectedResponse, headers: sortedExpectedHeaders }; - const valuesAreEqual = deepEqual( + + return testEquality( processedResponse, processedExpectedResponse ); - - return { Ok: valuesAreEqual }; } } ] diff --git a/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts b/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts index fae0d69a50..3fa58cbd20 100644 --- a/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts @@ -1,8 +1,8 @@ import { HttpRequest, HttpResponse } from 'azle/experimental'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { HttpResponseAgentResponseValue } from 'azle/property_tests/arbitraries/http/response_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { fletch } from './fletch'; @@ -26,9 +26,7 @@ export function generateTests( const result = await actor['get_state'](); - return { - Ok: deepEqual(result, 0) - }; + return testEquality(result, 0); } }, { @@ -54,12 +52,11 @@ export function generateTests( ...expectedResponse, headers: sortedExpectedHeaders }; - const valuesAreEqual = deepEqual( + + return testEquality( processedResponse, processedExpectedResponse ); - - return { Ok: valuesAreEqual }; } }, { @@ -69,9 +66,7 @@ export function generateTests( const result = await actor['get_state'](); - return { - Ok: deepEqual(result, 1) - }; + return testEquality(result, 1); } } ] diff --git a/property_tests/tests/canister_methods/init/test/generate_tests.ts b/property_tests/tests/canister_methods/init/test/generate_tests.ts index 24c808b365..284743bfb8 100644 --- a/property_tests/tests/canister_methods/init/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/init/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -20,13 +20,7 @@ export function generateTests( const actor = getActor(__dirname); const result = await actor.getInitValues(); - const valuesAreEqual = deepEqual(result, expectedResult); - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts b/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts index 84f80b08e4..b97656bc1a 100644 --- a/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts @@ -1,9 +1,9 @@ import { Agent } from '@dfinity/agent'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { InspectMessageBehavior } from './test'; @@ -48,7 +48,7 @@ function generateTest( const result = await actor[functionName](...paramValues); if (behavior === 'ACCEPT') { - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } return { @@ -56,16 +56,18 @@ function generateTest( }; } catch (error: any) { if (behavior === 'RETURN') { - return { - Ok: error.message.includes('rejected the message') - }; + return testEquality( + error.message.includes('rejected the message'), + true + ); } if (behavior === 'THROW') { const expectedError = `Method \\"${functionName}\\" not allowed`; - return { - Ok: error.message.includes(expectedError) - }; + return testEquality( + error.message.includes(expectedError), + true + ); } throw error; diff --git a/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts b/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts index 956cc11e1c..ba37fbcf4c 100644 --- a/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts +++ b/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -23,19 +23,10 @@ export function generateTests( const isPostUpgradeCalled = await actor.isPostUpgradeCalled(); - const valuesAreEqual = - deepEqual(initValues, expectedResult) && - isPostUpgradeCalled === false; - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n -Incorrect return value - expected: ${expectedResult} - received: ${initValues} - isPostUpgradeCalled: ${isPostUpgradeCalled}` - }; + return testEquality( + [initValues, isPostUpgradeCalled], + [expectedResult, false] + ); } } ] diff --git a/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts b/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts index ac4b0feeb2..8a355563fc 100644 --- a/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts +++ b/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -24,19 +24,10 @@ export function generateTests( await actor.getPostUpgradeValues(); const isInitCalled = await actor.isInitCalled(); - const valuesAreEqual = - deepEqual(postUpgradeValues, expectedResult) && - isInitCalled === false; - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n -Incorrect return value -expected: ${expectedResult} -received: ${postUpgradeValues} -isInitCalled: ${isInitCalled}` - }; + return testEquality( + [postUpgradeValues, isInitCalled], + [expectedResult, false] + ); } } ] diff --git a/property_tests/tests/canister_methods/pre_upgrade/test/test.ts b/property_tests/tests/canister_methods/pre_upgrade/test/test.ts index 9e517424ce..2d35ed19f6 100644 --- a/property_tests/tests/canister_methods/pre_upgrade/test/test.ts +++ b/property_tests/tests/canister_methods/pre_upgrade/test/test.ts @@ -1,4 +1,4 @@ -import { deepEqual, getActor, runPropTests } from 'azle/property_tests'; +import { getActor, runPropTests } from 'azle/property_tests'; import { CandidReturnTypeArb } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMetaArb } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; @@ -12,6 +12,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb'; import { UpdateMethodArb } from 'azle/property_tests/arbitraries/canister_methods/update_method_arb'; +import { testEquality } from 'azle/test'; import fc from 'fast-check'; const SimplePreUpgradeArb = PreUpgradeMethodArb({ @@ -95,7 +96,7 @@ function generateGetPreUpgradeExecutedCanisterMethod(): QueryMethod { const actor = getActor(__dirname); const result = await actor.getPreUpgradeExecuted(); - return { Ok: deepEqual(result, []) }; + return testEquality(result, []); } } ], @@ -106,7 +107,7 @@ function generateGetPreUpgradeExecutedCanisterMethod(): QueryMethod { const actor = getActor(__dirname); const result = await actor.getPreUpgradeExecuted(); - return { Ok: deepEqual(result, [true]) }; + return testEquality(result, [true]); } } ] diff --git a/property_tests/tests/canister_methods/query/test/generate_tests.ts b/property_tests/tests/canister_methods/query/test/generate_tests.ts index d4359e5115..9b6f86be00 100644 --- a/property_tests/tests/canister_methods/query/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/query/test/generate_tests.ts @@ -1,8 +1,8 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,13 +21,7 @@ export function generateTests( test: async () => { const actor = getActor(__dirname); const result = await actor[functionName](...paramValues); - const valuesAreEqual = deepEqual(result, expectedResult); - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/update/test/generate_tests.ts b/property_tests/tests/canister_methods/update/test/generate_tests.ts index 29bff5bae6..d2acc41815 100644 --- a/property_tests/tests/canister_methods/update/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/update/test/generate_tests.ts @@ -1,8 +1,8 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,13 +21,8 @@ export function generateTests( test: async () => { const actor = getActor(__dirname); const result = await actor[functionName](...paramValues); - const valuesAreEqual = deepEqual(result, expectedResult); - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/float32/test/generate_tests.ts b/property_tests/tests/float32/test/generate_tests.ts index c24b4f746f..727cc21ec4 100644 --- a/property_tests/tests/float32/test/generate_tests.ts +++ b/property_tests/tests/float32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/float64/test/generate_tests.ts b/property_tests/tests/float64/test/generate_tests.ts index 921fb0cba3..7c0c4befc4 100644 --- a/property_tests/tests/float64/test/generate_tests.ts +++ b/property_tests/tests/float64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/func/test/generate_tests.ts b/property_tests/tests/func/test/generate_tests.ts index ad13f8691e..bd1f7504ba 100644 --- a/property_tests/tests/func/test/generate_tests.ts +++ b/property_tests/tests/func/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Func } from 'azle/property_tests/arbitraries/candid/reference/func_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( ) ); - return { - Ok: deepEqual( - result, - returnFunc.value.agentResponseValue - ) - }; + return testEquality( + result, + returnFunc.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/int/test/generate_tests.ts b/property_tests/tests/int/test/generate_tests.ts index f5ee4c664c..0d356e15bf 100644 --- a/property_tests/tests/int/test/generate_tests.ts +++ b/property_tests/tests/int/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int16/test/generate_tests.ts b/property_tests/tests/int16/test/generate_tests.ts index c71b2e8368..62725742e7 100644 --- a/property_tests/tests/int16/test/generate_tests.ts +++ b/property_tests/tests/int16/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int32/test/generate_tests.ts b/property_tests/tests/int32/test/generate_tests.ts index a26a235294..f95ad9b64e 100644 --- a/property_tests/tests/int32/test/generate_tests.ts +++ b/property_tests/tests/int32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int64/test/generate_tests.ts b/property_tests/tests/int64/test/generate_tests.ts index 01edd1730e..2be510faaf 100644 --- a/property_tests/tests/int64/test/generate_tests.ts +++ b/property_tests/tests/int64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int8/test/generate_tests.ts b/property_tests/tests/int8/test/generate_tests.ts index b63b263e52..6ff0af87c6 100644 --- a/property_tests/tests/int8/test/generate_tests.ts +++ b/property_tests/tests/int8/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat/test/generate_tests.ts b/property_tests/tests/nat/test/generate_tests.ts index 98c25e8312..a24868b63e 100644 --- a/property_tests/tests/nat/test/generate_tests.ts +++ b/property_tests/tests/nat/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat16/test/generate_tests.ts b/property_tests/tests/nat16/test/generate_tests.ts index a2919564d7..75bd87c367 100644 --- a/property_tests/tests/nat16/test/generate_tests.ts +++ b/property_tests/tests/nat16/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat32/test/generate_tests.ts b/property_tests/tests/nat32/test/generate_tests.ts index 7ff0d4244d..709ba84432 100644 --- a/property_tests/tests/nat32/test/generate_tests.ts +++ b/property_tests/tests/nat32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat64/test/generate_tests.ts b/property_tests/tests/nat64/test/generate_tests.ts index 56bc57c16c..e608ab1f8b 100644 --- a/property_tests/tests/nat64/test/generate_tests.ts +++ b/property_tests/tests/nat64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat8/test/generate_tests.ts b/property_tests/tests/nat8/test/generate_tests.ts index fbbf4e7a98..a31a97d302 100644 --- a/property_tests/tests/nat8/test/generate_tests.ts +++ b/property_tests/tests/nat8/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/null/test/generate_tests.ts b/property_tests/tests/null/test/generate_tests.ts index da9350041c..8d5d0ad651 100644 --- a/property_tests/tests/null/test/generate_tests.ts +++ b/property_tests/tests/null/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -20,9 +20,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, null) - }; + return testEquality(result, null); } } ] diff --git a/property_tests/tests/opt/test/generate_tests.ts b/property_tests/tests/opt/test/generate_tests.ts index 2574e48dd7..bae894a4a4 100644 --- a/property_tests/tests/opt/test/generate_tests.ts +++ b/property_tests/tests/opt/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Opt } from 'azle/property_tests/arbitraries/candid/constructed/opt_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( const result = await actor[functionName](...params); - return { - Ok: deepEqual(expectedResult, result) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/principal/test/generate_tests.ts b/property_tests/tests/principal/test/generate_tests.ts index adccf33806..a0c95cdf65 100644 --- a/property_tests/tests/principal/test/generate_tests.ts +++ b/property_tests/tests/principal/test/generate_tests.ts @@ -1,7 +1,7 @@ import { Principal } from '@dfinity/principal'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -25,7 +25,7 @@ export function generateTests( ) ); - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/record/test/generate_tests.ts b/property_tests/tests/record/test/generate_tests.ts index 3b5bcd1c84..2a0dc710cb 100644 --- a/property_tests/tests/record/test/generate_tests.ts +++ b/property_tests/tests/record/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Record } from 'azle/property_tests/arbitraries/candid/constructed/record_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( ) ); - return { - Ok: deepEqual( - result, - returnRecord.value.agentResponseValue - ) - }; + return testEquality( + result, + returnRecord.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/recursive/test/generate_tests.ts b/property_tests/tests/recursive/test/generate_tests.ts index 234c5ec79a..9c27802fc5 100644 --- a/property_tests/tests/recursive/test/generate_tests.ts +++ b/property_tests/tests/recursive/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Recursive } from 'azle/property_tests/arbitraries/candid/recursive'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( const result = await actor[functionName](...params); - return { - Ok: deepEqual( - result, - returnRecursive.value.agentResponseValue - ) - }; + return testEquality( + result, + returnRecursive.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/service/test/generate_tests.ts b/property_tests/tests/service/test/generate_tests.ts index 4abe1e70ee..7356346c22 100644 --- a/property_tests/tests/service/test/generate_tests.ts +++ b/property_tests/tests/service/test/generate_tests.ts @@ -1,7 +1,7 @@ import { Principal } from '@dfinity/principal'; import { Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { execSync } from 'child_process'; export function generateTests( @@ -31,11 +31,10 @@ export function generateTests( .toString() .trim(); - return { - Ok: - result === - `(service "${returnService.value.agentArgumentValue.toText()}")` - }; + return testEquality( + result, + `(service "${returnService.value.agentArgumentValue.toText()}")` + ); } } ] diff --git a/property_tests/tests/text/test/generate_tests.ts b/property_tests/tests/text/test/generate_tests.ts index 18090ce7cd..1876c12bfd 100644 --- a/property_tests/tests/text/test/generate_tests.ts +++ b/property_tests/tests/text/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/tuple/test/generate_tests.ts b/property_tests/tests/tuple/test/generate_tests.ts index 5740c9a8fc..1d8d7747b1 100644 --- a/property_tests/tests/tuple/test/generate_tests.ts +++ b/property_tests/tests/tuple/test/generate_tests.ts @@ -1,10 +1,10 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { ReturnTuple, Tuple } from 'azle/property_tests/arbitraries/candid/constructed/tuple_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,7 +26,7 @@ export function generateTests( ) ); - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/variant/test/generate_tests.ts b/property_tests/tests/variant/test/generate_tests.ts index 700fe5cf93..78834fa68f 100644 --- a/property_tests/tests/variant/test/generate_tests.ts +++ b/property_tests/tests/variant/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Variant } from 'azle/property_tests/arbitraries/candid/constructed/variant_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/vec/test/generate_tests.ts b/property_tests/tests/vec/test/generate_tests.ts index 162e75c72c..0d80090cc0 100644 --- a/property_tests/tests/vec/test/generate_tests.ts +++ b/property_tests/tests/vec/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,9 +21,7 @@ export function generateTests( ); const result = await actor[functionName](...params); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] From 00307449fb65ef324588c704448df6952da298e2 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 8 Jul 2024 10:55:18 -0600 Subject: [PATCH 4/6] simplify testing framework --- test/index.ts | 151 ++++++-------------------------------------------- 1 file changed, 18 insertions(+), 133 deletions(-) diff --git a/test/index.ts b/test/index.ts index 113260bfe2..a8115986df 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,4 +1,3 @@ -import { execSync } from 'child_process'; // TODO import deepEqual from 'deep-is' works for some // TODO import { deepEqual } from 'deep-is' works for others // TODO require seems to work for all of them @@ -7,43 +6,25 @@ const deepEqual = require('deep-is'); import { jsonStringify } from '../src/lib/json'; -export type Test = { +export type Test<> = { name: string; skip?: boolean; wait?: number; - prep?: (context: Context) => Promise; - test?: (context: Context) => Promise>; + prep?: () => Promise; + test?: () => Promise>; }; -// export type Variant = Partial; - -export type AzleResult = Partial<{ - Ok: { isSuccessful: boolean; message?: string; context?: Context }; +type AzleResult = Partial<{ + Ok: { isSuccessful: boolean; message?: string }; Err: E; }>; -export type Ok = { - Ok: { isSuccessful: boolean; message?: string; context?: Context }; -}; - -// TODO let's get rid of this function in all tests and use match instead -export function ok( - azle_result: AzleResult -): azle_result is Ok { - if (azle_result.Err === undefined) { - return true; - } else { - return false; - } -} - // TODO should this just return a boolean? // TODO then the function calling can decide to throw or not export async function runTests( tests: Test[], exitProcess: boolean = true ): Promise { - let context = undefined; for (const test of tests) { try { if (test.skip === true) { @@ -68,18 +49,17 @@ export async function runTests( } if (test.prep !== undefined) { - context = (await test.prep(context)) ?? context; continue; } - const result: AzleResult = + const result: AzleResult = test.test !== undefined - ? await test.test(context) + ? await test.test() : { Err: 'test is not defined' }; - if (!ok(result)) { + if (result.Err !== undefined || result.Ok === undefined) { console.info('\x1b[31m', `test: ${test.name} failed`); console.info('\x1b[31m', `${result.Err}`); console.info('\x1b[0m'); @@ -91,10 +71,6 @@ export async function runTests( } } - if (result.Ok.context !== undefined) { - context = result.Ok.context; - } - if (result.Ok.isSuccessful !== true) { console.info('\x1b[31m', `test: ${test.name} failed`); if (result.Ok.message !== undefined) { @@ -130,113 +106,22 @@ export async function runTests( return true; } -export function deploy(canisterName: string, argument?: string): Test[] { - return [ - { - // TODO hopefully we can get rid of this: https://forum.dfinity.org/t/generated-declarations-in-node-js-environment-break/12686/16?u=lastmjs - name: 'waiting for createActor fetchRootKey', - wait: 5000 - }, - { - name: `create canister ${canisterName}`, - prep: async () => { - execSync(`dfx canister create ${canisterName}`, { - stdio: 'inherit' - }); - } - }, - { - name: 'clear canister memory', - prep: async () => { - execSync( - `dfx canister uninstall-code ${canisterName} || true`, - { - stdio: 'inherit' - } - ); - } - }, - { - name: `deploy canister ${canisterName}`, - prep: async () => { - execSync( - `dfx deploy${ - argument === undefined ? '' : ` --argument ${argument}` - } ${canisterName}`, - { - stdio: 'inherit' - } - ); - } - } - ]; -} - -type EqualsOptions = { - failMessage?: string; - equals?: (actual: any, expected: any) => boolean; - toString?: (value: any) => string; - context?: Context; -}; - // TODO is is better test framework conformity to call this assertEqual? I'll hold off for now, it should be easy to search for all testEquality and change it, easier than assertEqual I think // TODO so based on this I think I've actually seen this in other testing frameworks, assertEquals will take two and make sure they are equals, and assert will take one boolean. Right now we have test instead of assert but it would be easy to change -export function testEquality( +export function testEquality( actual: T, - expected: T, - options?: EqualsOptions -): AzleResult { - const equals = options?.equals ?? deepEqual; - const valueToString = options?.toString ?? jsonStringify; + expected: T +): AzleResult { + const equals = deepEqual; + const valueToString = jsonStringify; if (equals(actual, expected)) { - return succeed(options?.context); - } else { - const message = - options?.failMessage ?? - `Expected: ${valueToString(expected)}, Received: ${valueToString( - actual - )}`; - return fail(message); - } -} - -export function succeed(context?: Context): AzleResult { - return { Ok: { isSuccessful: true, context } }; -} - -export function fail(message?: string): AzleResult { - return { Ok: { isSuccessful: false, message } }; -} - -export function error(message: string): AzleResult { - return { Err: message }; -} - -// TODO when Jordan asks why we have this show call_raw, it's a great example for why we should have this guy -// But this is quickly boiling down to sugar and we are looking at the difference between -// return test( -// result.Ok.includes('blob'), -// `Expected result to be a candid blob. Received ${result.Ok}` -// ); -// and -// return { -// Ok: { -// isSuccessful: result.Ok.includes('blob'), -// message: `Expected result to be a candid blob. Received ${result.Ok}` -// } -// }; -// TODO date has a good example of when we would want to have an error message I think -// TODO ethers_base also -export function test( - succeeds: boolean, - message?: string, - context?: Context -): AzleResult { - if (succeeds) { - return succeed(context); + return { Ok: { isSuccessful: true } }; } else { - return fail(message); + const message = `Expected: ${valueToString( + expected + )}, Received: ${valueToString(actual)}`; + return { Ok: { isSuccessful: false, message } }; } } From d795bd9b294035195fc5428d5139cf94d76ad873 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Mon, 8 Jul 2024 10:02:02 -0600 Subject: [PATCH 5/6] add backwards compatibility with old tests --- test/index.ts | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/test/index.ts b/test/index.ts index a8115986df..3052d3b231 100644 --- a/test/index.ts +++ b/test/index.ts @@ -14,10 +14,13 @@ export type Test<> = { test?: () => Promise>; }; -type AzleResult = Partial<{ - Ok: { isSuccessful: boolean; message?: string }; - Err: E; -}>; +// TODO get rid of this union once the jest migration is complete +type AzleResult = + | Partial<{ + Ok: { isSuccessful: boolean; message?: string }; + Err: E; + }> + | Partial<{ Ok: boolean; Err: E }>; // TODO should this just return a boolean? // TODO then the function calling can decide to throw or not @@ -71,10 +74,20 @@ export async function runTests( } } - if (result.Ok.isSuccessful !== true) { + // TODO replace this with the below commented out code once jest migration is complete + const message = + typeof result.Ok === 'object' && result.Ok !== null + ? result.Ok.message + : undefined; + const successful = + typeof result.Ok === 'boolean' + ? result.Ok + : result.Ok.isSuccessful; + + if (successful !== true) { console.info('\x1b[31m', `test: ${test.name} failed`); - if (result.Ok.message !== undefined) { - console.info('\x1b[31m', `${result.Ok.message}`); + if (message !== undefined) { + console.info('\x1b[31m', `${message}`); } console.info('\x1b[0m'); @@ -85,6 +98,21 @@ export async function runTests( } } + // TODO bring this back once jest migration is complete + // if (result.Ok.isSuccessful !== true) { + // console.info('\x1b[31m', `test: ${test.name} failed`); + // if (result.Ok.message !== undefined) { + // console.info('\x1b[31m', `${result.Ok.message}`); + // } + // console.info('\x1b[0m'); + + // if (exitProcess) { + // process.exit(1); + // } else { + // return false; + // } + // } + console.info('\x1b[32m', `test: ${test.name} passed`); console.info('\x1b[0m'); } catch (error) { From b4ba2c95ba177e334732ccb35b98ec8b774ca931 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Tue, 9 Jul 2024 09:21:44 -0600 Subject: [PATCH 6/6] pr_fix --- test/index.ts | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/test/index.ts b/test/index.ts index 3052d3b231..72b76fdcf7 100644 --- a/test/index.ts +++ b/test/index.ts @@ -11,16 +11,16 @@ export type Test<> = { skip?: boolean; wait?: number; prep?: () => Promise; - test?: () => Promise>; + test?: () => Promise; }; // TODO get rid of this union once the jest migration is complete -type AzleResult = +type AzleResult = | Partial<{ Ok: { isSuccessful: boolean; message?: string }; - Err: E; + Err: string; }> - | Partial<{ Ok: boolean; Err: E }>; + | Partial<{ Ok: boolean; Err: string }>; // TODO should this just return a boolean? // TODO then the function calling can decide to throw or not @@ -55,7 +55,7 @@ export async function runTests( continue; } - const result: AzleResult = + const result: AzleResult = test.test !== undefined ? await test.test() : { @@ -136,19 +136,13 @@ export async function runTests( // TODO is is better test framework conformity to call this assertEqual? I'll hold off for now, it should be easy to search for all testEquality and change it, easier than assertEqual I think // TODO so based on this I think I've actually seen this in other testing frameworks, assertEquals will take two and make sure they are equals, and assert will take one boolean. Right now we have test instead of assert but it would be easy to change -export function testEquality( - actual: T, - expected: T -): AzleResult { - const equals = deepEqual; - const valueToString = jsonStringify; - - if (equals(actual, expected)) { +export function testEquality(actual: T, expected: T): AzleResult { + if (deepEqual(actual, expected)) { return { Ok: { isSuccessful: true } }; } else { - const message = `Expected: ${valueToString( + const message = `Expected: ${jsonStringify( expected - )}, Received: ${valueToString(actual)}`; + )}, Received: ${jsonStringify(actual)}`; return { Ok: { isSuccessful: false, message } }; } }